Blender V4.3
half.h
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5#ifndef __UTIL_HALF_H__
6#define __UTIL_HALF_H__
7
8#include "util/math.h"
9#include "util/types.h"
10
11#if !defined(__KERNEL_GPU__) && defined(__KERNEL_SSE2__)
12# include "util/simd.h"
13#endif
14
16
17/* Half Floats */
18
19#if defined(__KERNEL_METAL__)
20
22{
23 float f;
24 union {
25 half h;
26 uint16_t s;
27 } val;
28 val.h = h_in;
29
30 *((ccl_private int *)&f) = ((val.s & 0x8000) << 16) | (((val.s & 0x7c00) + 0x1C000) << 13) |
31 ((val.s & 0x03FF) << 13);
32
33 return f;
34}
35
36#else
37
38/* CUDA has its own half data type, no need to define then */
39# if !defined(__KERNEL_CUDA__) && !defined(__KERNEL_HIP__) && !defined(__KERNEL_ONEAPI__)
40/* Implementing this as a class rather than a typedef so that the compiler can tell it apart from
41 * unsigned shorts. */
42class half {
43 public:
44 half() = default;
45 half(const unsigned short &i) : v(i) {}
46 operator unsigned short()
47 {
48 return v;
49 }
50 half &operator=(const unsigned short &i)
51 {
52 v = i;
53 return *this;
54 }
55
56 private:
57 unsigned short v;
58};
59# endif
60
61struct half4 {
62 half x, y, z, w;
63};
64#endif
65
66/* Conversion to/from half float for image textures
67 *
68 * Simplified float to half for fast sampling on processor without a native
69 * instruction, and eliminating any NaN and inf values. */
70
72{
73#if defined(__KERNEL_METAL__) || defined(__KERNEL_ONEAPI__)
74 return half(min(f, 65504.0f));
75#elif defined(__KERNEL_CUDA__) || defined(__KERNEL_HIP__)
76 return __float2half(min(f, 65504.0f));
77#else
78 const uint u = __float_as_uint(f);
79 /* Sign bit, shifted to its position. */
80 uint sign_bit = u & 0x80000000;
81 sign_bit >>= 16;
82 /* Exponent. */
83 uint exponent_bits = u & 0x7f800000;
84 /* Non-sign bits. */
85 uint value_bits = u & 0x7fffffff;
86 value_bits >>= 13; /* Align mantissa on MSB. */
87 value_bits -= 0x1c000; /* Adjust bias. */
88 /* Flush-to-zero. */
89 value_bits = (exponent_bits < 0x38800000) ? 0 : value_bits;
90 /* Clamp-to-max. */
91 value_bits = (exponent_bits > 0x47000000) ? 0x7bff : value_bits;
92 /* Denormals-as-zero. */
93 value_bits = (exponent_bits == 0 ? 0 : value_bits);
94 /* Re-insert sign bit and return. */
95 return (value_bits | sign_bit);
96#endif
97}
98
100{
101#if defined(__KERNEL_METAL__)
102 return half_to_float(h);
103#elif defined(__KERNEL_ONEAPI__)
104 return float(h);
105#elif defined(__KERNEL_CUDA__) || defined(__KERNEL_HIP__)
106 return __half2float(h);
107#else
108 const int x = ((h & 0x8000) << 16) | (((h & 0x7c00) + 0x1C000) << 13) | ((h & 0x03FF) << 13);
109 return __int_as_float(x);
110#endif
111}
112
114{
115 /* Unable to use because it gives different results half_to_float_image, can we
116 * modify float_to_half_image so the conversion results are identical? */
117#if 0 /* defined(__KERNEL_AVX2__) */
118 /* CPU: AVX. */
119 __m128i x = _mm_castpd_si128(_mm_load_sd((const double *)&h));
120 return float4(_mm_cvtph_ps(x));
121#endif
122
123 const float4 f = make_float4(half_to_float_image(h.x),
127 return f;
128}
129
130/* Conversion to half float texture for display.
131 *
132 * Simplified float to half for fast display texture conversion on processors
133 * without a native instruction. Assumes no negative, no NaN, no inf, and sets
134 * denormal to 0. */
135
137{
138#if defined(__KERNEL_METAL__) || defined(__KERNEL_ONEAPI__)
139 return half(min(f, 65504.0f));
140#elif defined(__KERNEL_CUDA__) || defined(__KERNEL_HIP__)
141 return __float2half(min(f, 65504.0f));
142#else
143 const int x = __float_as_int((f > 0.0f) ? ((f < 65504.0f) ? f : 65504.0f) : 0.0f);
144 const int absolute = x & 0x7FFFFFFF;
145 const int Z = absolute + 0xC8000000;
146 const int result = (absolute < 0x38800000) ? 0 : Z;
147 const int rshift = (result >> 13);
148 return (rshift & 0x7FFF);
149#endif
150}
151
153{
154#ifdef __KERNEL_SSE__
155 /* CPU: SSE and AVX. */
156 float4 x = min(max(f, make_float4(0.0f)), make_float4(65504.0f));
157# ifdef __KERNEL_AVX2__
158 int4 rpack = int4(_mm_cvtps_ph(x, 0));
159# else
160 int4 absolute = cast(x) & make_int4(0x7FFFFFFF);
161 int4 Z = absolute + make_int4(0xC8000000);
162 int4 result = andnot(absolute < make_int4(0x38800000), Z);
163 int4 rshift = (result >> 13) & make_int4(0x7FFF);
164 int4 rpack = int4(_mm_packs_epi32(rshift, rshift));
165# endif
166 half4 h;
167 _mm_storel_pi((__m64 *)&h, _mm_castsi128_ps(rpack));
168 return h;
169#else
170 /* GPU and scalar fallback. */
171 const half4 h = {float_to_half_display(f.x),
175 return h;
176#endif
177}
178
180
181#endif /* __UTIL_HALF_H__ */
unsigned int uint
#define Z
btMatrix3x3 absolute() const
Return the matrix with all values non negative.
Definition half.h:42
half & operator=(const unsigned short &i)
Definition half.h:50
half(const unsigned short &i)
Definition half.h:45
half()=default
#define ccl_private
unsigned short half
#define ccl_device_inline
ccl_device_forceinline float __half2float(const half h)
#define CCL_NAMESPACE_END
ccl_device_forceinline float4 make_float4(const float x, const float y, const float z, const float w)
#define __float2half(x)
#define __int_as_float(x)
#define __float_as_int(x)
#define __float_as_uint(x)
ccl_device_forceinline int4 make_int4(const int x, const int y, const int z, const int w)
draw_view in_light_buf[] float
ccl_device_inline half float_to_half_display(const float f)
Definition half.h:136
ccl_device_inline half float_to_half_image(float f)
Definition half.h:71
ccl_device_inline float4 half4_to_float4_image(const half4 h)
Definition half.h:113
ccl_device_inline float half_to_float_image(half h)
Definition half.h:99
ccl_device_inline half4 float4_to_half4_display(const float4 f)
Definition half.h:152
ccl_device_inline int4 cast(const float4 a)
Definition math_float4.h:29
float half_to_float(uint16_t v)
Definition math_half.cc:91
#define min(a, b)
Definition sort.c:32
unsigned short uint16_t
Definition stdint.h:79
Definition half.h:61
half x
Definition half.h:62
half w
Definition half.h:62
half z
Definition half.h:62
half y
Definition half.h:62
float max