Blender  V2.93
util_half.h
Go to the documentation of this file.
1 /*
2  * Copyright 2011-2013 Blender Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef __UTIL_HALF_H__
18 #define __UTIL_HALF_H__
19 
20 #include "util/util_math.h"
21 #include "util/util_types.h"
22 
23 #if !defined(__KERNEL_GPU__) && defined(__KERNEL_SSE2__)
24 # include "util/util_simd.h"
25 #endif
26 
28 
29 /* Half Floats */
30 
31 #ifdef __KERNEL_OPENCL__
32 
33 # define float4_store_half(h, f, scale) vstore_half4(f *(scale), 0, h);
34 
35 #else
36 
37 /* CUDA has its own half data type, no need to define then */
38 # ifndef __KERNEL_CUDA__
39 /* Implementing this as a class rather than a typedef so that the compiler can tell it apart from
40  * unsigned shorts. */
41 class half {
42  public:
43  half() : v(0)
44  {
45  }
46  half(const unsigned short &i) : v(i)
47  {
48  }
49  operator unsigned short()
50  {
51  return v;
52  }
53  half &operator=(const unsigned short &i)
54  {
55  v = i;
56  return *this;
57  }
58 
59  private:
60  unsigned short v;
61 };
62 # endif
63 
64 struct half4 {
65  half x, y, z, w;
66 };
67 
68 # ifdef __KERNEL_CUDA__
69 
70 ccl_device_inline void float4_store_half(half *h, float4 f, float scale)
71 {
72  h[0] = __float2half(f.x * scale);
73  h[1] = __float2half(f.y * scale);
74  h[2] = __float2half(f.z * scale);
75  h[3] = __float2half(f.w * scale);
76 }
77 
78 # else
79 
80 ccl_device_inline void float4_store_half(half *h, float4 f, float scale)
81 {
82 # ifndef __KERNEL_SSE2__
83  for (int i = 0; i < 4; i++) {
84  /* optimized float to half for pixels:
85  * assumes no negative, no nan, no inf, and sets denormal to 0 */
86  union {
87  uint i;
88  float f;
89  } in;
90  float fscale = f[i] * scale;
91  in.f = (fscale > 0.0f) ? ((fscale < 65504.0f) ? fscale : 65504.0f) : 0.0f;
92  int x = in.i;
93 
94  int absolute = x & 0x7FFFFFFF;
95  int Z = absolute + 0xC8000000;
96  int result = (absolute < 0x38800000) ? 0 : Z;
97  int rshift = (result >> 13);
98 
99  h[i] = (rshift & 0x7FFF);
100  }
101 # else
102  /* same as above with SSE */
103  ssef fscale = load4f(f) * scale;
104  ssef x = min(max(fscale, 0.0f), 65504.0f);
105 
106 # ifdef __KERNEL_AVX2__
107  ssei rpack = _mm_cvtps_ph(x, 0);
108 # else
109  ssei absolute = cast(x) & 0x7FFFFFFF;
110  ssei Z = absolute + 0xC8000000;
111  ssei result = andnot(absolute < 0x38800000, Z);
112  ssei rshift = (result >> 13) & 0x7FFF;
113  ssei rpack = _mm_packs_epi32(rshift, rshift);
114 # endif
115 
116  _mm_storel_pi((__m64 *)h, _mm_castsi128_ps(rpack));
117 # endif
118 }
119 
121 {
122  float f;
123 
124  *((int *)&f) = ((h & 0x8000) << 16) | (((h & 0x7c00) + 0x1C000) << 13) | ((h & 0x03FF) << 13);
125 
126  return f;
127 }
128 
130 {
131  float4 f;
132 
133  f.x = half_to_float(h.x);
134  f.y = half_to_float(h.y);
135  f.z = half_to_float(h.z);
136  f.w = half_to_float(h.w);
137 
138  return f;
139 }
140 
142 {
143  const uint u = __float_as_uint(f);
144  /* Sign bit, shifted to its position. */
145  uint sign_bit = u & 0x80000000;
146  sign_bit >>= 16;
147  /* Exponent. */
148  uint exponent_bits = u & 0x7f800000;
149  /* Non-sign bits. */
150  uint value_bits = u & 0x7fffffff;
151  value_bits >>= 13; /* Align mantissa on MSB. */
152  value_bits -= 0x1c000; /* Adjust bias. */
153  /* Flush-to-zero. */
154  value_bits = (exponent_bits < 0x38800000) ? 0 : value_bits;
155  /* Clamp-to-max. */
156  value_bits = (exponent_bits > 0x47000000) ? 0x7bff : value_bits;
157  /* Denormals-as-zero. */
158  value_bits = (exponent_bits == 0 ? 0 : value_bits);
159  /* Re-insert sign bit and return. */
160  return (value_bits | sign_bit);
161 }
162 
163 # endif
164 
165 #endif
166 
168 
169 #endif /* __UTIL_HALF_H__ */
unsigned int uint
Definition: BLI_sys_types.h:83
#define Z
Definition: GeomUtils.cpp:215
btMatrix3x3 absolute() const
Return the matrix with all values non negative.
Definition: btMatrix3x3.h:1028
Definition: util_half.h:41
half()
Definition: util_half.h:43
half(const unsigned short &i)
Definition: util_half.h:46
half & operator=(const unsigned short &i)
Definition: util_half.h:53
__device__ half __float2half(const float f)
#define ccl_device_inline
#define CCL_NAMESPACE_END
U * cast(T *in)
Definition: Cast.h:27
#define min(a, b)
Definition: sort.c:51
half x
Definition: util_half.h:65
half w
Definition: util_half.h:65
half z
Definition: util_half.h:65
half y
Definition: util_half.h:65
float max
ccl_device_inline float4 half4_to_float4(half4 h)
Definition: util_half.h:129
ccl_device_inline void float4_store_half(half *h, float4 f, float scale)
Definition: util_half.h:80
ccl_device_inline half float_to_half(float f)
Definition: util_half.h:141
ccl_device_inline float half_to_float(half h)
Definition: util_half.h:120
ccl_device_inline uint __float_as_uint(float f)
Definition: util_math.h:222