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