Blender V5.3
BLI_math_half_test.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2024 Blender Authors
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5#include "testing/testing.h"
6
7#include "BLI_math_half.hh"
8#include "BLI_simd.hh"
9#include "BLI_time.hh"
10
11#include <cmath>
12
13// #define DO_PERF_TESTS 1
14
15namespace blender::tests {
16
17TEST(math_half, half_to_float_scalar)
18{
20 EXPECT_EQ(math::half_to_float(1), 5.960464478e-08f);
21 EXPECT_EQ(math::half_to_float(32), 1.907348633e-06f);
22 EXPECT_EQ(math::half_to_float(37), 2.205371857e-06f);
23 EXPECT_EQ(math::half_to_float(511), 3.045797348e-05f);
24 EXPECT_EQ(math::half_to_float(999), 5.954504013e-05f);
25 EXPECT_EQ(math::half_to_float(1024), 6.103515625e-05f);
26 EXPECT_EQ(math::half_to_float(1357), 8.088350296e-05f);
27 EXPECT_EQ(math::half_to_float(6789), 0.003183364868f);
28 EXPECT_EQ(math::half_to_float(16383), 1.999023438f);
29 EXPECT_EQ(math::half_to_float(16384), 2.0f);
30 EXPECT_EQ(math::half_to_float(31743), 65504.0f);
31 EXPECT_EQ(math::half_to_float(31744), std::numeric_limits<float>::infinity());
32 EXPECT_TRUE(std::isnan(math::half_to_float(31746)));
33 EXPECT_TRUE(std::isnan(math::half_to_float(32767)));
34 EXPECT_EQ(math::half_to_float(32768), -0.0f);
35 EXPECT_EQ(math::half_to_float(32769), -5.960464478e-08f);
36 EXPECT_EQ(math::half_to_float(46765), -0.4172363281f);
37 EXPECT_EQ(math::half_to_float(54501), -78.3125f);
38 EXPECT_EQ(math::half_to_float(64511), -65504.0f);
39 EXPECT_EQ(math::half_to_float(64512), -std::numeric_limits<float>::infinity());
40 EXPECT_TRUE(std::isnan(math::half_to_float(64513)));
41 EXPECT_TRUE(std::isnan(math::half_to_float(65535)));
42}
43
44TEST(math_half, float_to_half_scalar)
45{
46#define HFUN(v) math::float_to_half(v)
47 EXPECT_EQ(HFUN(0.0f), 0);
48 EXPECT_EQ(HFUN(std::numeric_limits<float>::min()), 0);
49 EXPECT_EQ(HFUN(5.960464478e-08f), 1);
50 EXPECT_EQ(HFUN(1.907348633e-06f), 32);
51 EXPECT_EQ(HFUN(2.205371857e-06f), 37);
52 EXPECT_EQ(HFUN(3.045797348e-05f), 511);
53 EXPECT_EQ(HFUN(5.954504013e-05f), 999);
54 EXPECT_EQ(HFUN(6.103515625e-05f), 1024);
55 EXPECT_EQ(HFUN(8.088350296e-05f), 1357);
56 EXPECT_EQ(HFUN(0.003183364868f), 6789);
57 EXPECT_EQ(HFUN(0.1f), 11878);
58 EXPECT_EQ(HFUN(1.0f), 15360);
59 EXPECT_EQ(HFUN(1.999023438f), 16383);
60 EXPECT_EQ(HFUN(1.999523438f), 16384);
61 EXPECT_EQ(HFUN(2.0f), 16384);
62 EXPECT_EQ(HFUN(11.0f), 18816);
63 EXPECT_EQ(HFUN(65504.0f), 31743);
64 EXPECT_EQ(HFUN(65535.0f), 31744); /* FP16 inf */
65 EXPECT_EQ(HFUN(1.0e6f), 31744); /* FP16 inf */
66 EXPECT_EQ(HFUN(std::numeric_limits<float>::infinity()), 31744);
67 EXPECT_EQ(HFUN(std::numeric_limits<float>::max()), 31744);
68 EXPECT_EQ(HFUN(std::numeric_limits<float>::quiet_NaN()), 32256);
69 EXPECT_EQ(HFUN(-0.0f), 32768);
70 EXPECT_EQ(HFUN(-5.960464478e-08f), 32769);
71 EXPECT_EQ(HFUN(-0.4172363281f), 46765);
72 EXPECT_EQ(HFUN(-1.0f), 48128);
73 EXPECT_EQ(HFUN(-78.3125f), 54501);
74 EXPECT_EQ(HFUN(-123.5f), 55224);
75 EXPECT_EQ(HFUN(-65504.0f), 64511);
76 EXPECT_EQ(HFUN(-65536.0f), 64512); /* FP16 -inf */
77 EXPECT_EQ(HFUN(-1.0e6f), 64512); /* FP16 -inf */
78 EXPECT_EQ(HFUN(-std::numeric_limits<float>::infinity()), 64512);
79#undef HFUN
80}
81
82TEST(math_half, float_to_half_make_finite_scalar)
83{
84#define HFUN(v) math::float_to_half_make_finite(v)
85 EXPECT_EQ(HFUN(0.0f), 0);
86 EXPECT_EQ(HFUN(std::numeric_limits<float>::min()), 0);
87 EXPECT_EQ(HFUN(5.960464478e-08f), 1);
88 EXPECT_EQ(HFUN(1.907348633e-06f), 32);
89 EXPECT_EQ(HFUN(2.205371857e-06f), 37);
90 EXPECT_EQ(HFUN(3.045797348e-05f), 511);
91 EXPECT_EQ(HFUN(5.954504013e-05f), 999);
92 EXPECT_EQ(HFUN(6.103515625e-05f), 1024);
93 EXPECT_EQ(HFUN(8.088350296e-05f), 1357);
94 EXPECT_EQ(HFUN(0.003183364868f), 6789);
95 EXPECT_EQ(HFUN(0.1f), 11878);
96 EXPECT_EQ(HFUN(1.0f), 15360);
97 EXPECT_EQ(HFUN(1.999023438f), 16383);
98 EXPECT_EQ(HFUN(1.999523438f), 16384);
99 EXPECT_EQ(HFUN(2.0f), 16384);
100 EXPECT_EQ(HFUN(11.0f), 18816);
101 EXPECT_EQ(HFUN(65504.0f), 31743);
102 /* Too large: result is FP16 65504. */
103 EXPECT_EQ(HFUN(65535.0f), 31743);
104 EXPECT_EQ(HFUN(1.0e6f), 31743);
105 EXPECT_EQ(HFUN(std::numeric_limits<float>::infinity()), 31743);
106 EXPECT_EQ(HFUN(std::numeric_limits<float>::max()), 31743);
107 /* NaN: result is zero. */
108 EXPECT_EQ(HFUN(std::numeric_limits<float>::quiet_NaN()), 0);
109 EXPECT_EQ(HFUN(std::numeric_limits<float>::signaling_NaN()), 0);
110 EXPECT_EQ(HFUN(-0.0f), 32768);
111 EXPECT_EQ(HFUN(-5.960464478e-08f), 32769);
112 EXPECT_EQ(HFUN(-0.4172363281f), 46765);
113 EXPECT_EQ(HFUN(-1.0f), 48128);
114 EXPECT_EQ(HFUN(-78.3125f), 54501);
115 EXPECT_EQ(HFUN(-123.5f), 55224);
116 EXPECT_EQ(HFUN(-65504.0f), 64511);
117 /* Too large negative: result is FP16 -65504. */
118 EXPECT_EQ(HFUN(-65536.0f), 64511);
119 EXPECT_EQ(HFUN(-1.0e6f), 64511);
120 EXPECT_EQ(HFUN(-std::numeric_limits<float>::infinity()), 64511);
121 /* -NaN: result is negative zero. */
122 EXPECT_EQ(HFUN(-std::numeric_limits<float>::quiet_NaN()), 32768);
123 EXPECT_EQ(HFUN(-std::numeric_limits<float>::signaling_NaN()), 32768);
124#undef HFUN
125}
126
127TEST(math_half, half_to_float_array)
128{
129 const uint16_t src[13] = {
130 0, 1, 6789, 16383, 16384, 31743, 31744, 32768, 32769, 46765, 54501, 64511, 64512};
131 /* One extra entry in destination, to check that function leaves it intact. */
132 const float exp[14] = {
133 0.0f,
134 5.960464478e-08f,
135 0.003183364868f,
136 1.999023438f,
137 2.0f,
138 65504.0f,
139 std::numeric_limits<float>::infinity(),
140 -0.0f,
141 -5.960464478e-08f,
142 -0.4172363281f,
143 -78.3125f,
144 -65504.0f,
145 -std::numeric_limits<float>::infinity(),
146 1.2345f,
147 };
148 float dst[14] = {};
149 dst[13] = 1.2345f;
150
151 math::half_to_float_array(src, dst, 13);
152 EXPECT_EQ_ARRAY(exp, dst, 14);
153}
154
155TEST(math_half, half_to_float_array_flush_to_zero)
156{
157 /* Subnormal halves are within the normal float range, so they must convert exactly even when
158 * the CPU is set to flush-to-zero and denormals-are-zero (as it is for threads using Embree). */
159 const uint16_t src[8] = {0, 1, 32, 511, 1023, 1024, 32769, 33791};
160 const float exp[8] = {
161 0.0f,
162 5.960464478e-08f,
163 1.907348633e-06f,
164 3.045797348e-05f,
165 6.097555160e-05f,
166 6.103515625e-05f,
167 -5.960464478e-08f,
168 -6.097555160e-05f,
169 };
170 float dst[8] = {};
171
172 {
173 const ScopedFlushToZero flush_to_zero;
174 math::half_to_float_array(src, dst, 8);
175 }
176
177 EXPECT_EQ_ARRAY(exp, dst, 8);
178}
179
180TEST(math_half, float_to_half_array)
181{
182 const float src[13] = {0.0f,
183 5.960464478e-08f,
184 0.003183364868f,
185 1.999023438f,
186 2.0f,
187 65504.0f,
188 std::numeric_limits<float>::infinity(),
189 -0.0f,
190 -5.960464478e-08f,
191 -0.4172363281f,
192 -78.3125f,
193 -65504.0f,
194 -std::numeric_limits<float>::infinity()};
195 /* One extra entry in destination, to check that function leaves it intact. */
196 const uint16_t exp[14] = {
197 0, 1, 6789, 16383, 16384, 31743, 31744, 32768, 32769, 46765, 54501, 64511, 64512, 12345};
198 uint16_t dst[14] = {};
199 dst[13] = 12345;
200
201 math::float_to_half_array(src, dst, 13);
202 EXPECT_EQ_ARRAY(exp, dst, 14);
203}
204
205TEST(math_half, float_to_half_clamp_array)
206{
207 const float src[17] = {
208 0.0f,
209 5.960464478e-08f,
210 0.003183364868f,
211 1.999023438f,
212 2.0f,
213 65504.0f,
214 std::numeric_limits<float>::infinity(),
215 -0.0f,
216 -5.960464478e-08f,
217 -0.4172363281f,
218 -78.3125f,
219 -65504.0f,
220 -std::numeric_limits<float>::infinity(),
221 100000.0f,
222 -100000.0f,
223 std::numeric_limits<float>::quiet_NaN(),
224 -std::numeric_limits<float>::quiet_NaN(),
225 };
226 /* One extra entry in destination, to check that function leaves it intact. */
227 const uint16_t exp[18] = {0,
228 1,
229 6789,
230 16383,
231 16384,
232 25552,
233 25552,
234 32768,
235 32769,
236 46765,
237 51456,
238 51456,
239 51456,
240 25552,
241 51456,
242 0,
243 0,
244 12345};
245 uint16_t dst[18] = {};
246 dst[17] = 12345;
247 math::float_to_half_clamp_array(src, dst, 17, -10.0f, 1000.0f);
248 EXPECT_EQ_ARRAY(exp, dst, 18);
249}
250
251TEST(math_half, float_to_half_make_finite_array)
252{
253 const float src[17] = {
254 0.0f,
255 5.960464478e-08f,
256 0.003183364868f,
257 1.999023438f,
258 2.0f,
259 65504.0f,
260 std::numeric_limits<float>::infinity(),
261 -0.0f,
262 -5.960464478e-08f,
263 -0.4172363281f,
264 -78.3125f,
265 -65504.0f,
266 -std::numeric_limits<float>::infinity(),
267 100000.0f,
268 -100000.0f,
269 std::numeric_limits<float>::quiet_NaN(),
270 -std::numeric_limits<float>::quiet_NaN(),
271 };
272 /* One extra entry in destination, to check that function leaves it intact. */
273 const uint16_t exp[18] = {0,
274 1,
275 6789,
276 16383,
277 16384,
278 31743,
279 31743,
280 32768,
281 32769,
282 46765,
283 54501,
284 64511,
285 64511,
286 31743,
287 64511,
288 0,
289 32768,
290 12345};
291 uint16_t dst[18] = {};
292 dst[17] = 12345;
294 EXPECT_EQ_ARRAY(exp, dst, 18);
295}
296
297#ifdef DO_PERF_TESTS
298
299/*
300 * Time to convert 100 million numbers FP16 -> FP32.
301 *
302 * Ryzen 5950X (VS2022):
303 * - `half_to_float`: 164ms
304 * - `half_to_float_array`: 132ms (scalar)
305 * - `half_to_float_array`: 84ms (SSE2 4x wide path)
306 * - `half_to_float_array`: 86ms (w/ AVX2 F16C - however Blender is not compiled for AVX2 yet)
307 *
308 * Mac M1 Max (Clang 15), using NEON VCVT:
309 * - `half_to_float`: 97ms
310 * - `half_to_float_array`: 53ms
311 */
312TEST(math_half_perf, half_to_float_scalar)
313{
314 double t0 = BLI_time_now_seconds();
315 size_t sum = 0;
316 for (int i = 0; i < 100'000'000; i++) {
317 float f = math::half_to_float(uint16_t(i & 0xFFFF));
318 uint32_t fu;
319 memcpy(&fu, &f, sizeof(f));
320 sum += fu;
321 }
322 double t1 = BLI_time_now_seconds();
323 printf("- FP16->FP32 scalar: %.3fs sum %zu\n", t1 - t0, sum);
324}
325
326TEST(math_half_perf, half_to_float_array)
327{
328 const int test_size = 100'000'000;
329 uint16_t *src = new uint16_t[test_size];
330 float *dst = new float[test_size];
331 for (int i = 0; i < test_size; i++) {
332 src[i] = i & 0xFFFF;
333 }
334 double t0 = BLI_time_now_seconds();
335 size_t sum = 0;
336 math::half_to_float_array(src, dst, test_size);
337 for (int i = 0; i < test_size; i++) {
338 uint32_t fu;
339 memcpy(&fu, &dst[i], sizeof(fu));
340 sum += fu;
341 }
342 double t1 = BLI_time_now_seconds();
343 printf("- FP16->FP32 array : %.3fs sum %zu\n", t1 - t0, sum);
344 delete[] src;
345 delete[] dst;
346}
347
348/*
349 * Time to convert 100 million numbers FP32 -> FP16.
350 *
351 * Ryzen 5950X (VS2022):
352 * - `float_to_half`: 242ms
353 * - `float_to_half_array`: 184ms (scalar)
354 * - `float_to_half_array`: 68ms (SSE2 4x wide path)
355 * - `float_to_half_array`: 50ms (w/ AVX2 F16C - however Blender is not compiled for AVX2 yet)
356 *
357 * Mac M1 Max (Clang 15), using NEON VCVT:
358 * - `float_to_half`: 93ms
359 * - `float_to_half_array`: 21ms
360 */
361TEST(math_half_perf, float_to_half_scalar)
362{
363 double t0 = BLI_time_now_seconds();
364 uint32_t sum = 0;
365 for (int i = 0; i < 100'000'000; i++) {
366 float f = ((i & 0xFFFF) - 0x8000) + 0.1f;
368 sum += h;
369 }
370 double t1 = BLI_time_now_seconds();
371 printf("- FP32->FP16 scalar: %.3fs sum %u\n", t1 - t0, sum);
372}
373
374TEST(math_half_perf, float_to_half_array)
375{
376 const int test_size = 100'000'000;
377 float *src = new float[test_size];
378 uint16_t *dst = new uint16_t[test_size];
379 for (int i = 0; i < test_size; i++) {
380 src[i] = ((i & 0xFFFF) - 0x8000) + 0.1f;
381 }
382
383 double t0 = BLI_time_now_seconds();
384 uint32_t sum = 0;
385 math::float_to_half_array(src, dst, test_size);
386 for (int i = 0; i < test_size; i++) {
387 sum += dst[i];
388 }
389 double t1 = BLI_time_now_seconds();
390 printf("- FP32->FP16 array : %.3fs sum %u\n", t1 - t0, sum);
391 delete[] src;
392 delete[] dst;
393}
394
395TEST(math_half_perf, float_to_half_make_finite_array)
396{
397 const int test_size = 100'000'000;
398 float *src = new float[test_size];
399 uint16_t *dst = new uint16_t[test_size];
400 for (int i = 0; i < test_size; i++) {
401 src[i] = ((i & 0xFFFF) - 0x8000) + 0.1f;
402 }
403 double t0 = BLI_time_now_seconds();
404 uint32_t sum = 0;
405 math::float_to_half_make_finite_array(src, dst, test_size);
406 for (int i = 0; i < test_size; i++) {
407 sum += dst[i];
408 }
409 double t1 = BLI_time_now_seconds();
410 printf("- FP32->FP16 finite array : %.3fs sum %u\n", t1 - t0, sum);
411 delete[] src;
412 delete[] dst;
413}
414
415#endif // #ifdef DO_PERF_TESTS
416
417} // namespace blender::tests
#define HFUN(v)
Platform independent time functions.
#define printf
#define exp
#define EXPECT_TRUE(result)
void float_to_half_array(const float *src, uint16_t *dst, size_t length)
Definition math_half.cc:256
uint16_t float_to_half(float v)
Definition math_half.cc:32
void float_to_half_clamp_array(const float *src, uint16_t *dst, size_t length, float min_value, float max_value)
Definition math_half.cc:292
void half_to_float_array(const uint16_t *src, float *dst, size_t length)
Definition math_half.cc:449
void float_to_half_make_finite_array(const float *src, uint16_t *dst, size_t length)
Definition math_half.cc:355
float half_to_float(uint16_t v)
Definition math_half.cc:113
TEST(any_virtual, DefaultConstructor)
TEST(array_store, Nop)
unsigned short uint16_t
Definition BKE_text.h:80
unsigned int uint32_t
Definition BKE_text.h:81
EXPECT_EQ(BLI_expr_pylike_eval(expr, nullptr, 0, &result), EXPR_PYLIKE_INVALID)
double BLI_time_now_seconds()
Definition time.cc:30