Blender V5.3
util_half_test.h
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2026 Blender Foundation
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5#include <gtest/gtest.h>
6
7#include "util/half.h"
8#include "util/simd.h"
9#include "util/system.h"
10#include <array>
11
13
15{
16#if defined(__KERNEL_AVX2__)
18#else
19 return true;
20#endif
21}
22
23#define EXPECT_HALF_EQ(a, b) \
24 do { \
25 const uint16_t a_ = (a); \
26 const uint16_t b_ = (b); \
27 if ((a_ & 0x7fff) == 0 && (b_ & 0x7fff) == 0) { \
28 /* Ignore sign of zero, -0.0 getting converted to 0.0 is fine. */ \
29 } \
30 else { \
31 EXPECT_EQ(a_, b_); \
32 } \
33 } while (0)
34
36 float f;
37 uint16_t h;
38};
39
40/* We don't support NaN and inf, so not tested here. */
41static const std::array<HalfTestVal, 13> test_values = {{{0.0f, 0x0000},
42 {-0.0f, 0x8000},
43 {1.0f, 0x3c00},
44 {2.0f, 0x4000},
45 {10.0f, 0x4900},
46 {100.0f, 0x5640},
47 {0.0999755859375f, 0x2e66},
48 {-0.0999755859375f, 0xae66},
49 {-0.5f, 0xb800},
50 {-1.0f, 0xbc00},
51 /* Smallest positive normal (2^-14) */
52 {6.103515625e-5f, 0x0400},
53 /* Largest denormal (2^-14 - 2^-24) */
54 {6.0975551605e-5f, 0x03ff},
55 /* Smallest positive denormal (2^-24) */
56 {5.9604644775e-8f, 0x0001}}};
57
59{
61 GTEST_SKIP();
62 return;
63 }
64 for (const HalfTestVal &val : test_values) {
65 EXPECT_HALF_EQ(float_to_half(val.f), val.h);
66 EXPECT_EQ(half_to_float(half(val.h)), val.f);
67 }
68}
69
71{
73 GTEST_SKIP();
74 return;
75 }
76 for (size_t i = 0; i < test_values.size(); i++) {
77 const size_t i0 = i;
78 const size_t i1 = (i + 1) % test_values.size();
79 const size_t i2 = (i + 2) % test_values.size();
80
81 const float3 in = make_float3(test_values[i0].f, test_values[i1].f, test_values[i2].f);
82
85
86 EXPECT_EQ(out.x, in.x);
87 EXPECT_EQ(out.y, in.y);
88 EXPECT_EQ(out.z, in.z);
89
90 EXPECT_HALF_EQ(uint16_t(h.x), test_values[i0].h);
91 EXPECT_HALF_EQ(uint16_t(h.y), test_values[i1].h);
92 EXPECT_HALF_EQ(uint16_t(h.z), test_values[i2].h);
93 }
94}
95
97{
99 GTEST_SKIP();
100 return;
101 }
102 for (size_t i = 0; i < test_values.size(); i += 4) {
103 const size_t i0 = i;
104 const size_t i1 = (i + 1) % test_values.size();
105 const size_t i2 = (i + 2) % test_values.size();
106 const size_t i3 = (i + 3) % test_values.size();
107
108 const float4 in = make_float4(
109 test_values[i0].f, test_values[i1].f, test_values[i2].f, test_values[i3].f);
110
111 const half4 h = float4_to_half4(in);
112 const float4 out = half4_to_float4(h);
113
114 EXPECT_EQ(out.x, in.x);
115 EXPECT_EQ(out.y, in.y);
116 EXPECT_EQ(out.z, in.z);
117 EXPECT_EQ(out.w, in.w);
118
119 EXPECT_HALF_EQ(uint16_t(h.x), test_values[i0].h);
120 EXPECT_HALF_EQ(uint16_t(h.y), test_values[i1].h);
121 EXPECT_HALF_EQ(uint16_t(h.z), test_values[i2].h);
122 EXPECT_HALF_EQ(uint16_t(h.w), test_values[i3].h);
123 }
124}
125
127{
129 GTEST_SKIP();
130 return;
131 }
132 for (const HalfTestVal &val : test_values) {
134 EXPECT_EQ(fallback_half_to_float(half(val.h)), val.f);
135 }
136}
137
138TEST(TEST_CATEGORY_NAME, half_to_float_flush_to_zero)
139{
141 GTEST_SKIP();
142 return;
143 }
144
145 const ScopedFlushToZero flush_to_zero;
146
147 for (size_t i = 0; i < test_values.size(); i += 4) {
148 const size_t i0 = i;
149 const size_t i1 = (i + 1) % test_values.size();
150 const size_t i2 = (i + 2) % test_values.size();
151 const size_t i3 = (i + 3) % test_values.size();
152
153 const half4 h = {half(test_values[i0].h),
154 half(test_values[i1].h),
155 half(test_values[i2].h),
156 half(test_values[i3].h)};
157
158 const float4 out = half4_to_float4(h);
159 EXPECT_EQ(out.x, test_values[i0].f);
160 EXPECT_EQ(out.y, test_values[i1].f);
161 EXPECT_EQ(out.z, test_values[i2].f);
162 EXPECT_EQ(out.w, test_values[i3].f);
163
164 const float4 fallback_out = fallback_half4_to_float4(h);
165 EXPECT_EQ(fallback_out.x, test_values[i0].f);
166 EXPECT_EQ(fallback_out.y, test_values[i1].f);
167 EXPECT_EQ(fallback_out.z, test_values[i2].f);
168 EXPECT_EQ(fallback_out.w, test_values[i3].f);
169 }
170
171 for (const HalfTestVal &val : test_values) {
172 EXPECT_EQ(half_to_float(half(val.h)), val.f);
173 EXPECT_EQ(fallback_half_to_float(half(val.h)), val.f);
174 }
175}
176
178{
180 GTEST_SKIP();
181 return;
182 }
183 for (size_t i = 0; i < test_values.size(); i++) {
184 const size_t i0 = i;
185 const size_t i1 = (i + 1) % test_values.size();
186 const size_t i2 = (i + 2) % test_values.size();
187
188 const float3 in = make_float3(test_values[i0].f, test_values[i1].f, test_values[i2].f);
189
192
193 EXPECT_EQ(out.x, in.x);
194 EXPECT_EQ(out.y, in.y);
195 EXPECT_EQ(out.z, in.z);
196
197 EXPECT_HALF_EQ(uint16_t(h.x), test_values[i0].h);
198 EXPECT_HALF_EQ(uint16_t(h.y), test_values[i1].h);
199 EXPECT_HALF_EQ(uint16_t(h.z), test_values[i2].h);
200 }
201}
202
204{
206 GTEST_SKIP();
207 return;
208 }
209 for (size_t i = 0; i < test_values.size(); i += 4) {
210 const size_t i0 = i;
211 const size_t i1 = (i + 1) % test_values.size();
212 const size_t i2 = (i + 2) % test_values.size();
213 const size_t i3 = (i + 3) % test_values.size();
214
215 const float4 in = make_float4(
216 test_values[i0].f, test_values[i1].f, test_values[i2].f, test_values[i3].f);
217
220
221 EXPECT_EQ(out.x, in.x);
222 EXPECT_EQ(out.y, in.y);
223 EXPECT_EQ(out.z, in.z);
224 EXPECT_EQ(out.w, in.w);
225
226 EXPECT_HALF_EQ(uint16_t(h.x), test_values[i0].h);
227 EXPECT_HALF_EQ(uint16_t(h.y), test_values[i1].h);
228 EXPECT_HALF_EQ(uint16_t(h.z), test_values[i2].h);
229 EXPECT_HALF_EQ(uint16_t(h.w), test_values[i3].h);
230 }
231}
232
Definition half.h:29
#define CCL_NAMESPACE_END
ccl_device_forceinline float3 make_float3(const float x, const float y, const float z)
#define TEST(a, b)
#define EXPECT_EQ(result, expect)
ccl_device_inline float4 fallback_half4_to_float4(const half4 h)
Definition half.h:98
ccl_device_inline float3 fallback_packed_half3_to_float3(const packed_half3 h)
Definition half.h:117
ccl_device_inline float3 packed_half3_to_float3(const packed_half3 h)
Definition half.h:298
ccl_device_inline half4 fallback_float4_to_half4(const float4 f)
Definition half.h:156
ccl_device_inline packed_half3 fallback_float3_to_packed_half3(const float3 f)
Definition half.h:191
ccl_device_inline half4 float4_to_half4(const float4 f)
Definition half.h:233
ccl_device_inline float half_to_float(half h)
Definition half.h:198
ccl_device_inline packed_half3 float3_to_packed_half3(const float3 f)
Definition half.h:279
ccl_device_inline float4 half4_to_float4(const half4 h)
Definition half.h:257
ccl_device_inline half fallback_float_to_half(const float f)
Definition half.h:122
ccl_device_inline float fallback_half_to_float(const half h)
Definition half.h:81
ccl_device_inline half float_to_half(const float f)
Definition half.h:218
#define make_float4
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:53
half x
Definition half.h:54
half w
Definition half.h:54
half z
Definition half.h:54
half y
Definition half.h:54
half z
Definition half.h:50
half y
Definition half.h:50
half x
Definition half.h:50
bool system_cpu_support_avx2()
Definition system.cpp:240
#define TEST_CATEGORY_NAME
static CCL_NAMESPACE_BEGIN bool validate_cpu_capabilities()
static const std::array< HalfTestVal, 13 > test_values
#define EXPECT_HALF_EQ(a, b)