File: ieee-from-fp32-value.cc

package info (click to toggle)
fp16 0.0~git20200514.4dfe081-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 828 kB
  • sloc: cpp: 6,690; ansic: 675; python: 140; makefile: 11
file content (524 lines) | stat: -rw-r--r-- 24,301 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
#include <gtest/gtest.h>

#include <cstdint>
#include <iomanip>

#include <fp16.h>
#include <tables.h>

#if (defined(__i386__) || defined(__x86_64__)) && defined(__F16C__)
	#include <x86intrin.h>
#endif


TEST(FP16_IEEE_FROM_FP32_VALUE, normalized_powers_of_2) {
	const uint16_t min_po2_f16   = UINT16_C(0x0400);
	const uint16_t eighths_f16   = UINT16_C(0x3000);
	const uint16_t quarter_f16   = UINT16_C(0x3400);
	const uint16_t half_f16      = UINT16_C(0x3800);
	const uint16_t one_f16       = UINT16_C(0x3C00);
	const uint16_t two_f16       = UINT16_C(0x4000);
	const uint16_t four_f16      = UINT16_C(0x4400);
	const uint16_t eight_f16     = UINT16_C(0x4800);
	const uint16_t sixteen_f16   = UINT16_C(0x4C00);
	const uint16_t thirtytwo_f16 = UINT16_C(0x5000);
	const uint16_t sixtyfour_f16 = UINT16_C(0x5400);
	const uint16_t max_po2_f16   = UINT16_C(0x7800);

	const uint32_t min_po2_f32   = UINT32_C(0x38800000);
	const uint32_t eighths_f32   = UINT32_C(0x3E000000);
	const uint32_t quarter_f32   = UINT32_C(0x3E800000);
	const uint32_t half_f32      = UINT32_C(0x3F000000);
	const uint32_t one_f32       = UINT32_C(0x3F800000);
	const uint32_t two_f32       = UINT32_C(0x40000000);
	const uint32_t four_f32      = UINT32_C(0x40800000);
	const uint32_t eight_f32     = UINT32_C(0x41000000);
	const uint32_t sixteen_f32   = UINT32_C(0x41800000);
	const uint32_t thirtytwo_f32 = UINT32_C(0x42000000);
	const uint32_t sixtyfour_f32 = UINT32_C(0x42800000);
	const uint32_t max_po2_f32   = UINT32_C(0x47000000);

	float min_po2_value;
	memcpy(&min_po2_value, &min_po2_f32, sizeof(min_po2_value));
	EXPECT_EQ(min_po2_f16, fp16_ieee_from_fp32_value(min_po2_value)) <<
		std::hex << std::uppercase << std::setfill('0') <<
		"F32 = 0x" << std::setw(8) << min_po2_f32 << ", " <<
		"F16(F32) = 0x" << std::setw(4) << fp16_ieee_from_fp32_value(min_po2_value) << ", " <<
		"F16 = 0x" << std::setw(4) << min_po2_f16;

	float eighths_value;
	memcpy(&eighths_value, &eighths_f32, sizeof(eighths_value));
	EXPECT_EQ(eighths_f16, fp16_ieee_from_fp32_value(eighths_value)) <<
		std::hex << std::uppercase << std::setfill('0') <<
		"F32 = 0x" << std::setw(8) << eighths_f32 << ", " <<
		"F16(F32) = 0x" << std::setw(4) << fp16_ieee_from_fp32_value(eighths_value) << ", " <<
		"F16 = 0x" << std::setw(4) << eighths_f16;

	float quarter_value;
	memcpy(&quarter_value, &quarter_f32, sizeof(quarter_value));
	EXPECT_EQ(quarter_f16, fp16_ieee_from_fp32_value(quarter_value)) <<
		std::hex << std::uppercase << std::setfill('0') <<
		"F32 = 0x" << std::setw(8) << quarter_f32 << ", " <<
		"F16(F32) = 0x" << std::setw(4) << fp16_ieee_from_fp32_value(quarter_value) << ", " <<
		"F16 = 0x" << std::setw(4) << quarter_f16;

	float half_value;
	memcpy(&half_value, &half_f32, sizeof(half_value));
	EXPECT_EQ(half_f16, fp16_ieee_from_fp32_value(half_value)) <<
		std::hex << std::uppercase << std::setfill('0') <<
		"F32 = 0x" << std::setw(8) << half_f32 << ", " <<
		"F16(F32) = 0x" << std::setw(4) << fp16_ieee_from_fp32_value(half_value) << ", " <<
		"F16 = 0x" << std::setw(4) << half_f16;

	float one_value;
	memcpy(&one_value, &one_f32, sizeof(one_value));
	EXPECT_EQ(one_f16, fp16_ieee_from_fp32_value(one_value)) <<
		std::hex << std::uppercase << std::setfill('0') <<
		"F32 = 0x" << std::setw(8) << one_f32 << ", " <<
		"F16(F32) = 0x" << std::setw(4) << fp16_ieee_from_fp32_value(one_value) << ", " <<
		"F16 = 0x" << std::setw(4) << one_f16;

	float two_value;
	memcpy(&two_value, &two_f32, sizeof(two_value));
	EXPECT_EQ(two_f16, fp16_ieee_from_fp32_value(two_value)) <<
		std::hex << std::uppercase << std::setfill('0') <<
		"F32 = 0x" << std::setw(8) << two_f32 << ", " <<
		"F16(F32) = 0x" << std::setw(4) << fp16_ieee_from_fp32_value(two_value) << ", " <<
		"F16 = 0x" << std::setw(4) << two_f16;

	float four_value;
	memcpy(&four_value, &four_f32, sizeof(four_value));
	EXPECT_EQ(four_f16, fp16_ieee_from_fp32_value(four_value)) <<
		std::hex << std::uppercase << std::setfill('0') <<
		"F32 = 0x" << std::setw(8) << four_f32 << ", " <<
		"F16(F32) = 0x" << std::setw(4) << fp16_ieee_from_fp32_value(four_value) << ", " <<
		"F16 = 0x" << std::setw(4) << four_f16;

	float eight_value;
	memcpy(&eight_value, &eight_f32, sizeof(eight_value));
	EXPECT_EQ(eight_f16, fp16_ieee_from_fp32_value(eight_value)) <<
		std::hex << std::uppercase << std::setfill('0') <<
		"F32 = 0x" << std::setw(8) << eight_f32 << ", " <<
		"F16(F32) = 0x" << std::setw(4) << fp16_ieee_from_fp32_value(eight_value) << ", " <<
		"F16 = 0x" << std::setw(4) << eight_f16;

	float sixteen_value;
	memcpy(&sixteen_value, &sixteen_f32, sizeof(sixteen_value));
	EXPECT_EQ(sixteen_f16, fp16_ieee_from_fp32_value(sixteen_value)) <<
		std::hex << std::uppercase << std::setfill('0') <<
		"F32 = 0x" << std::setw(8) << sixteen_f32 << ", " <<
		"F16(F32) = 0x" << std::setw(4) << fp16_ieee_from_fp32_value(sixteen_value) << ", " <<
		"F16 = 0x" << std::setw(4) << sixteen_f16;

	float thirtytwo_value;
	memcpy(&thirtytwo_value, &thirtytwo_f32, sizeof(thirtytwo_value));
	EXPECT_EQ(thirtytwo_f16, fp16_ieee_from_fp32_value(thirtytwo_value)) <<
		std::hex << std::uppercase << std::setfill('0') <<
		"F32 = 0x" << std::setw(8) << thirtytwo_f32 << ", " <<
		"F16(F32) = 0x" << std::setw(4) << fp16_ieee_from_fp32_value(thirtytwo_value) << ", " <<
		"F16 = 0x" << std::setw(4) << thirtytwo_f16;

	float sixtyfour_value;
	memcpy(&sixtyfour_value, &sixtyfour_f32, sizeof(sixtyfour_value));
	EXPECT_EQ(sixtyfour_f16, fp16_ieee_from_fp32_value(sixtyfour_value)) <<
		std::hex << std::uppercase << std::setfill('0') <<
		"F32 = 0x" << std::setw(8) << sixtyfour_f32 << ", " <<
		"F16(F32) = 0x" << std::setw(4) << fp16_ieee_from_fp32_value(sixtyfour_value) << ", " <<
		"F16 = 0x" << std::setw(4) << sixtyfour_f16;

	float max_po2_value;
	memcpy(&max_po2_value, &max_po2_f32, sizeof(max_po2_value));
	EXPECT_EQ(max_po2_f16, fp16_ieee_from_fp32_value(max_po2_value)) <<
		std::hex << std::uppercase << std::setfill('0') <<
		"F32 = 0x" << std::setw(8) << max_po2_f32 << ", " <<
		"F16(F32) = 0x" << std::setw(4) << fp16_ieee_from_fp32_value(max_po2_value) << ", " <<
		"F16 = 0x" << std::setw(4) << max_po2_f16;
}

TEST(FP16_IEEE_FROM_FP32_VALUE, denormalized_powers_of_2) {
	const uint16_t exp2_minus_15_f16 = UINT16_C(0x0200);
	const uint16_t exp2_minus_16_f16 = UINT16_C(0x0100);
	const uint16_t exp2_minus_17_f16 = UINT16_C(0x0080);
	const uint16_t exp2_minus_18_f16 = UINT16_C(0x0040);
	const uint16_t exp2_minus_19_f16 = UINT16_C(0x0020);
	const uint16_t exp2_minus_20_f16 = UINT16_C(0x0010);
	const uint16_t exp2_minus_21_f16 = UINT16_C(0x0008);
	const uint16_t exp2_minus_22_f16 = UINT16_C(0x0004);
	const uint16_t exp2_minus_23_f16 = UINT16_C(0x0002);
	const uint16_t exp2_minus_24_f16 = UINT16_C(0x0001);
	const uint16_t exp2_minus_25_f16 = UINT16_C(0x0000);

	const uint32_t exp2_minus_15_f32 = UINT32_C(0x38000000);
	const uint32_t exp2_minus_16_f32 = UINT32_C(0x37800000);
	const uint32_t exp2_minus_17_f32 = UINT32_C(0x37000000);
	const uint32_t exp2_minus_18_f32 = UINT32_C(0x36800000);
	const uint32_t exp2_minus_19_f32 = UINT32_C(0x36000000);
	const uint32_t exp2_minus_20_f32 = UINT32_C(0x35800000);
	const uint32_t exp2_minus_21_f32 = UINT32_C(0x35000000);
	const uint32_t exp2_minus_22_f32 = UINT32_C(0x34800000);
	const uint32_t exp2_minus_23_f32 = UINT32_C(0x34000000);
	const uint32_t exp2_minus_24_f32 = UINT32_C(0x33800000);
	const uint32_t exp2_minus_25_f32 = UINT32_C(0x33000000);

	float exp2_minus_15_value;
	memcpy(&exp2_minus_15_value, &exp2_minus_15_f32, sizeof(exp2_minus_15_value));
	EXPECT_EQ(exp2_minus_15_f16, fp16_ieee_from_fp32_value(exp2_minus_15_value)) <<
		std::hex << std::uppercase << std::setfill('0') <<
		"F32 = 0x" << std::setw(8) << exp2_minus_15_f32 << ", " <<
		"F16(F32) = 0x" << std::setw(4) << fp16_ieee_from_fp32_value(exp2_minus_15_value) << ", " <<
		"F16 = 0x" << std::setw(4) << exp2_minus_15_f16;

	float exp2_minus_16_value;
	memcpy(&exp2_minus_16_value, &exp2_minus_16_f32, sizeof(exp2_minus_16_value));
	EXPECT_EQ(exp2_minus_16_f16, fp16_ieee_from_fp32_value(exp2_minus_16_value)) <<
		std::hex << std::uppercase << std::setfill('0') <<
		"F32 = 0x" << std::setw(8) << exp2_minus_16_f32 << ", " <<
		"F16(F32) = 0x" << std::setw(4) << fp16_ieee_from_fp32_value(exp2_minus_16_value) << ", " <<
		"F16 = 0x" << std::setw(4) << exp2_minus_16_f16;

	float exp2_minus_17_value;
	memcpy(&exp2_minus_17_value, &exp2_minus_17_f32, sizeof(exp2_minus_17_value));
	EXPECT_EQ(exp2_minus_17_f16, fp16_ieee_from_fp32_value(exp2_minus_17_value)) <<
		std::hex << std::uppercase << std::setfill('0') <<
		"F32 = 0x" << std::setw(8) << exp2_minus_17_f32 << ", " <<
		"F16(F32) = 0x" << std::setw(4) << fp16_ieee_from_fp32_value(exp2_minus_17_value) << ", " <<
		"F16 = 0x" << std::setw(4) << exp2_minus_17_f16;

	float exp2_minus_18_value;
	memcpy(&exp2_minus_18_value, &exp2_minus_18_f32, sizeof(exp2_minus_18_value));
	EXPECT_EQ(exp2_minus_18_f16, fp16_ieee_from_fp32_value(exp2_minus_18_value)) <<
		std::hex << std::uppercase << std::setfill('0') <<
		"F32 = 0x" << std::setw(8) << exp2_minus_18_f32 << ", " <<
		"F16(F32) = 0x" << std::setw(4) << fp16_ieee_from_fp32_value(exp2_minus_18_value) << ", " <<
		"F16 = 0x" << std::setw(4) << exp2_minus_18_f16;

	float exp2_minus_19_value;
	memcpy(&exp2_minus_19_value, &exp2_minus_19_f32, sizeof(exp2_minus_19_value));
	EXPECT_EQ(exp2_minus_19_f16, fp16_ieee_from_fp32_value(exp2_minus_19_value)) <<
		std::hex << std::uppercase << std::setfill('0') <<
		"F32 = 0x" << std::setw(8) << exp2_minus_19_f32 << ", " <<
		"F16(F32) = 0x" << std::setw(4) << fp16_ieee_from_fp32_value(exp2_minus_19_value) << ", " <<
		"F16 = 0x" << std::setw(4) << exp2_minus_19_f16;

	float exp2_minus_20_value;
	memcpy(&exp2_minus_20_value, &exp2_minus_20_f32, sizeof(exp2_minus_20_value));
	EXPECT_EQ(exp2_minus_20_f16, fp16_ieee_from_fp32_value(exp2_minus_20_value)) <<
		std::hex << std::uppercase << std::setfill('0') <<
		"F32 = 0x" << std::setw(8) << exp2_minus_20_f32 << ", " <<
		"F16(F32) = 0x" << std::setw(4) << fp16_ieee_from_fp32_value(exp2_minus_20_value) << ", " <<
		"F16 = 0x" << std::setw(4) << exp2_minus_20_f16;

	float exp2_minus_21_value;
	memcpy(&exp2_minus_21_value, &exp2_minus_21_f32, sizeof(exp2_minus_21_value));
	EXPECT_EQ(exp2_minus_21_f16, fp16_ieee_from_fp32_value(exp2_minus_21_value)) <<
		std::hex << std::uppercase << std::setfill('0') <<
		"F32 = 0x" << std::setw(8) << exp2_minus_21_f32 << ", " <<
		"F16(F32) = 0x" << std::setw(4) << fp16_ieee_from_fp32_value(exp2_minus_21_value) << ", " <<
		"F16 = 0x" << std::setw(4) << exp2_minus_21_f16;

	float exp2_minus_22_value;
	memcpy(&exp2_minus_22_value, &exp2_minus_22_f32, sizeof(exp2_minus_22_value));
	EXPECT_EQ(exp2_minus_22_f16, fp16_ieee_from_fp32_value(exp2_minus_22_value)) <<
		std::hex << std::uppercase << std::setfill('0') <<
		"F32 = 0x" << std::setw(8) << exp2_minus_22_f32 << ", " <<
		"F16(F32) = 0x" << std::setw(4) << fp16_ieee_from_fp32_value(exp2_minus_22_value) << ", " <<
		"F16 = 0x" << std::setw(4) << exp2_minus_22_f16;

	float exp2_minus_23_value;
	memcpy(&exp2_minus_23_value, &exp2_minus_23_f32, sizeof(exp2_minus_23_value));
	EXPECT_EQ(exp2_minus_23_f16, fp16_ieee_from_fp32_value(exp2_minus_23_value)) <<
		std::hex << std::uppercase << std::setfill('0') <<
		"F32 = 0x" << std::setw(8) << exp2_minus_23_f32 << ", " <<
		"F16(F32) = 0x" << std::setw(4) << fp16_ieee_from_fp32_value(exp2_minus_23_value) << ", " <<
		"F16 = 0x" << std::setw(4) << exp2_minus_23_f16;

	float exp2_minus_24_value;
	memcpy(&exp2_minus_24_value, &exp2_minus_24_f32, sizeof(exp2_minus_24_value));
	EXPECT_EQ(exp2_minus_24_f16, fp16_ieee_from_fp32_value(exp2_minus_24_value)) <<
		std::hex << std::uppercase << std::setfill('0') <<
		"F32 = 0x" << std::setw(8) << exp2_minus_24_f32 << ", " <<
		"F16(F32) = 0x" << std::setw(4) << fp16_ieee_from_fp32_value(exp2_minus_24_value) << ", " <<
		"F16 = 0x" << std::setw(4) << exp2_minus_24_f16;

	float exp2_minus_25_value;
	memcpy(&exp2_minus_25_value, &exp2_minus_25_f32, sizeof(exp2_minus_25_value));
	EXPECT_EQ(exp2_minus_25_f16, fp16_ieee_from_fp32_value(exp2_minus_25_value)) <<
		std::hex << std::uppercase << std::setfill('0') <<
		"F32 = 0x" << std::setw(8) << exp2_minus_25_f32 << ", " <<
		"F16(F32) = 0x" << std::setw(4) << fp16_ieee_from_fp32_value(exp2_minus_25_value) << ", " <<
		"F16 = 0x" << std::setw(4) << exp2_minus_25_f16;
}

TEST(FP16_IEEE_FROM_FP32_VALUE, zero) {
	const uint16_t positive_zero_f16 = UINT16_C(0x0000);
	const uint16_t negative_zero_f16 = UINT16_C(0x8000);

	const uint32_t positive_zero_f32 = UINT32_C(0x00000000);
	const uint32_t negative_zero_f32 = UINT32_C(0x80000000);

	float positive_zero_value;
	memcpy(&positive_zero_value, &positive_zero_f32, sizeof(positive_zero_value));
	EXPECT_EQ(positive_zero_f16, fp16_ieee_from_fp32_value(positive_zero_value)) <<
		std::hex << std::uppercase << std::setfill('0') <<
		"F32 = 0x" << std::setw(8) << positive_zero_f32 << ", " <<
		"F16(F32) = 0x" << std::setw(4) << fp16_ieee_from_fp32_value(positive_zero_value) << ", " <<
		"F16 = 0x" << std::setw(4) << positive_zero_f16;

	float negative_zero_value;
	memcpy(&negative_zero_value, &negative_zero_f32, sizeof(negative_zero_value));
	EXPECT_EQ(negative_zero_f16, fp16_ieee_from_fp32_value(negative_zero_value)) <<
		std::hex << std::uppercase << std::setfill('0') <<
		"F32 = 0x" << std::setw(8) << negative_zero_f32 << ", " <<
		"F16(F32) = 0x" << std::setw(4) << fp16_ieee_from_fp32_value(negative_zero_value) << ", " <<
		"F16 = 0x" << std::setw(4) << negative_zero_f16;
}

TEST(FP16_IEEE_FROM_FP32_VALUE, infinity) {
	const uint16_t positive_infinity_f16 = UINT16_C(0x7C00);
	const uint16_t negative_infinity_f16 = UINT16_C(0xFC00);

	const uint32_t positive_infinity_f32 = UINT32_C(0x7F800000);
	const uint32_t negative_infinity_f32 = UINT32_C(0xFF800000);

	float positive_infinity_value;
	memcpy(&positive_infinity_value, &positive_infinity_f32, sizeof(positive_infinity_value));
	EXPECT_EQ(positive_infinity_f16, fp16_ieee_from_fp32_value(positive_infinity_value)) <<
		std::hex << std::uppercase << std::setfill('0') <<
		"F32 = 0x" << std::setw(8) << positive_infinity_f32 << ", " <<
		"F16(F32) = 0x" << std::setw(4) << fp16_ieee_from_fp32_value(positive_infinity_value) << ", " <<
		"F16 = 0x" << std::setw(4) << positive_infinity_f16;

	float negative_infinity_value;
	memcpy(&negative_infinity_value, &negative_infinity_f32, sizeof(negative_infinity_value));
	EXPECT_EQ(negative_infinity_f16, fp16_ieee_from_fp32_value(negative_infinity_value)) <<
		std::hex << std::uppercase << std::setfill('0') <<
		"F32 = 0x" << std::setw(8) << negative_infinity_f32 << ", " <<
		"F16(F32) = 0x" << std::setw(4) << fp16_ieee_from_fp32_value(negative_infinity_value) << ", " <<
		"F16 = 0x" << std::setw(4) << negative_infinity_f16;
}

TEST(FP16_IEEE_FROM_FP32_VALUE, positive_nan) {
	for (uint32_t nan_f32 = UINT32_C(0x7FFFFFFF); nan_f32 > UINT32_C(0x7F800000); nan_f32--) {
		float nan_value;
		memcpy(&nan_value, &nan_f32, sizeof(nan_value));
		const uint16_t nan_f16 = fp16_ieee_from_fp32_value(nan_value);

		/* Check sign */
		ASSERT_EQ(nan_f16 & UINT16_C(0x8000), 0) <<
			std::hex << std::uppercase << std::setfill('0') <<
			"F32 = 0x" << std::setw(8) << nan_f32 << ", " <<
			"F16(F32) = 0x" << std::setw(4) << nan_f16;

		/* Check exponent */
		ASSERT_EQ(nan_f16 & UINT16_C(0x7C00), UINT16_C(0x7C00)) <<
			std::hex << std::uppercase << std::setfill('0') <<
			"F32 = 0x" << std::setw(8) << nan_f32 << ", " <<
			"F16(F32) = 0x" << std::setw(4) << nan_f16;

		/* Check mantissa */
		ASSERT_NE(nan_f16 & UINT16_C(0x03FF), 0) <<
			std::hex << std::uppercase << std::setfill('0') <<
			"F32 = 0x" << std::setw(8) << nan_f32 << ", " <<
			"F16(F32) = 0x" << std::setw(4) << nan_f16;
	}
}

TEST(FP16_IEEE_FROM_FP32_VALUE, negative_nan) {
	for (uint32_t nan_f32 = UINT32_C(0xFFFFFFFF); nan_f32 > UINT32_C(0xFF800000); nan_f32--) {
		float nan_value;
		memcpy(&nan_value, &nan_f32, sizeof(nan_value));
		const uint16_t nan_f16 = fp16_ieee_from_fp32_value(nan_value);

		/* Check sign */
		ASSERT_EQ(nan_f16 & UINT16_C(0x8000), UINT16_C(0x8000)) <<
			std::hex << std::uppercase << std::setfill('0') <<
			"F32 = 0x" << std::setw(8) << nan_f32 << ", " <<
			"F16(F32) = 0x" << std::setw(4) << nan_f16;

		/* Check exponent */
		ASSERT_EQ(nan_f16 & UINT16_C(0x7C00), UINT16_C(0x7C00)) <<
			std::hex << std::uppercase << std::setfill('0') <<
			"F32 = 0x" << std::setw(8) << nan_f32 << ", " <<
			"F16(F32) = 0x" << std::setw(4) << nan_f16;

		/* Check mantissa */
		ASSERT_NE(nan_f16 & UINT16_C(0x03FF), 0) <<
			std::hex << std::uppercase << std::setfill('0') <<
			"F32 = 0x" << std::setw(8) << nan_f32 << ", " <<
			"F16(F32) = 0x" << std::setw(4) << nan_f16;
	}
}

TEST(FP16_IEEE_FROM_FP32_VALUE, revertible) {
	/* Positive values */
	for (uint16_t f16 = UINT16_C(0x0000); f16 < UINT16_C(0x7C00); f16++) {
		const float value_f32 = fp16_ieee_to_fp32_value(f16);
		uint32_t bits_f32;
		memcpy(&bits_f32, &value_f32, sizeof(bits_f32));

		ASSERT_EQ(f16, fp16_ieee_from_fp32_value(value_f32)) <<
			std::hex << std::uppercase << std::setfill('0') <<
			"F16 = 0x" << std::setw(4) << f16 << ", " <<
			"F32(F16) = 0x" << std::setw(8) << bits_f32 << ", " <<
			"F16(F32(F16)) = 0x" << std::setw(4) << fp16_ieee_from_fp32_value(value_f32);
	}

	/* Negative values */
	for (uint16_t f16 = UINT16_C(0x8000); f16 < UINT16_C(0xFC00); f16++) {
		const float value_f32 = fp16_ieee_to_fp32_value(f16);
		uint32_t bits_f32;
		memcpy(&bits_f32, &value_f32, sizeof(bits_f32));

		ASSERT_EQ(f16, fp16_ieee_from_fp32_value(value_f32)) <<
			std::hex << std::uppercase << std::setfill('0') <<
			"F16 = 0x" << std::setw(4) << f16 << ", " <<
			"F32(F16) = 0x" << std::setw(8) << bits_f32 << ", " <<
			"F16(F32(F16)) = 0x" << std::setw(4) << fp16_ieee_from_fp32_value(value_f32);
	}
}

TEST(FP16_IEEE_FROM_FP32_VALUE, underflow) {
	const uint32_t min_nonzero_f32 = UINT32_C(0x33000001);
	const uint16_t zero_f16 = UINT16_C(0x0000);
	const uint16_t min_f16 = UINT16_C(0x0001);
	for (uint32_t bits = UINT32_C(0x00000001); bits < min_nonzero_f32; bits++) {
		float value;
		memcpy(&value, &bits, sizeof(value));
		ASSERT_EQ(zero_f16, fp16_ieee_from_fp32_value(value)) <<
			std::hex << std::uppercase << std::setfill('0') <<
			"F32 = 0x" << std::setw(8) << bits << ", " <<
			"F16(F32) = 0x" << std::setw(4) << fp16_ieee_from_fp32_value(value) << ", " <<
			"F16 = 0x" << std::setw(4) << zero_f16;
	}
	float min_nonzero_value;
	memcpy(&min_nonzero_value, &min_nonzero_f32, sizeof(min_nonzero_value));
	ASSERT_EQ(min_f16, fp16_ieee_from_fp32_value(min_nonzero_value)) <<
		std::hex << std::uppercase << std::setfill('0') <<
		"F32 = 0x" << std::setw(8) << min_nonzero_f32 << ", " <<
		"F16(F32) = 0x" << std::setw(4) << fp16_ieee_from_fp32_value(min_nonzero_value) << ", " <<
		"F16 = 0x" << std::setw(4) << min_f16;
}

TEST(FP16_IEEE_FROM_FP32_VALUE, overflow) {
	const uint32_t max_finite_f32 = UINT32_C(0x477FEFFF);
	const uint16_t max_finite_f16 = UINT16_C(0x7BFF);
	const uint32_t positive_infinity_f32 = UINT32_C(0x7F800000);
	const uint16_t positive_infinity_f16 = UINT16_C(0x7C00);
	for (uint32_t bits = positive_infinity_f32; bits > max_finite_f32; bits--) {
		float value;
		memcpy(&value, &bits, sizeof(value));
		ASSERT_EQ(positive_infinity_f16, fp16_ieee_from_fp32_value(value)) <<
			std::hex << std::uppercase << std::setfill('0') <<
			"F32 = 0x" << std::setw(8) << bits << ", " <<
			"F16(F32) = 0x" << std::setw(4) << fp16_ieee_from_fp32_value(value) << ", " <<
			"F16 = 0x" << std::setw(4) << positive_infinity_f16;
	}
	float max_finite_value;
	memcpy(&max_finite_value, &max_finite_f32, sizeof(max_finite_value));
	ASSERT_EQ(max_finite_f16, fp16_ieee_from_fp32_value(max_finite_value)) <<
		std::hex << std::uppercase << std::setfill('0') <<
		"F32 = 0x" << std::setw(8) << max_finite_f32 << ", " <<
		"F16(F32) = 0x" << std::setw(4) << fp16_ieee_from_fp32_value(max_finite_value) << ", " <<
		"F16 = 0x" << std::setw(4) << max_finite_f16;
}

TEST(FP16_IEEE_FROM_FP32_VALUE, positive_denormalized_values) {
	const uint32_t min_nonzero_f32 = UINT32_C(0x33000001);

	uint32_t f32_begin = min_nonzero_f32;
	for (uint16_t f16 = 0; f16 < UINT16_C(0x0400); f16++) {
		const uint32_t f32_end = fp16::denormalizedRanges[f16];
		for (uint32_t f32 = f32_begin; f32 < f32_end; f32++) {
			float value;
			memcpy(&value, &f32, sizeof(value));
			ASSERT_EQ(f16, fp16_ieee_from_fp32_value(value)) <<
				std::hex << std::uppercase << std::setfill('0') <<
				"F32 = 0x" << std::setw(8) << f32 << ", " <<
				"F16(F32) = 0x" << std::setw(4) << fp16_ieee_from_fp32_value(value) << ", " <<
				"F16 = 0x" << std::setw(4) << f16;
		}
		f32_begin = f32_end;
	}
}

TEST(FP16_IEEE_FROM_FP32_VALUE, negative_denormalized_values) {
	const uint32_t min_nonzero_f32 = UINT32_C(0x33000001);

	uint32_t f32_begin = min_nonzero_f32 | UINT32_C(0x80000000);
	for (uint16_t f16 = UINT16_C(0x8000); f16 < UINT16_C(0x8400); f16++) {
		const uint32_t f32_end = fp16::denormalizedRanges[f16 & UINT16_C(0x7FFF)] | UINT32_C(0x80000000);
		for (uint32_t f32 = f32_begin; f32 < f32_end; f32++) {
			float value;
			memcpy(&value, &f32, sizeof(value));
			ASSERT_EQ(f16, fp16_ieee_from_fp32_value(value)) <<
				std::hex << std::uppercase << std::setfill('0') <<
				"F32 = 0x" << std::setw(8) << f32 << ", " <<
				"F16(F32) = 0x" << std::setw(4) << fp16_ieee_from_fp32_value(value) << ", " <<
				"F16 = 0x" << std::setw(4) << f16;
		}
		f32_begin = f32_end;
	}
}

TEST(FP16_IEEE_FROM_FP32_VALUE, positive_normalized_values) {
	/* Minimum number that rounds to 1.0h when converted to half-precision */
	const uint32_t min_one_f32 = UINT32_C(0x3F7FF000);
	const uint32_t e_bias = 15;

	for (int32_t e = -14; e <= 15; e++) {
		uint32_t f32_begin = min_one_f32 + (uint32_t(e) << 23);
		for (uint16_t f16 = uint16_t(e + e_bias) << 10; f16 < uint16_t(e + e_bias + 1) << 10; f16++) {
			const uint32_t f32_end = fp16::normalizedRanges[f16 & UINT16_C(0x3FF)] + (uint32_t(e) << 23);
			for (uint32_t f32 = f32_begin; f32 < f32_end; f32++) {
				float value;
				memcpy(&value, &f32, sizeof(value));
				ASSERT_EQ(f16, fp16_ieee_from_fp32_value(value)) <<
					std::hex << std::uppercase << std::setfill('0') <<
					"F32 = 0x" << std::setw(8) << f32 << ", " <<
					"F16(F32) = 0x" << std::setw(4) << fp16_ieee_from_fp32_value(value) << ", " <<
					"F16 = 0x" << std::setw(4) << f16;
			}
			f32_begin = f32_end;
		}
	}
}

TEST(FP16_IEEE_FROM_FP32_VALUE, negative_normalized_values) {
	/* Minimum number that rounds to 1.0h when converted to half-precision */
	const uint32_t min_one_f32 = UINT32_C(0x3F7FF000);
	const uint32_t e_bias = 15;

	for (int32_t e = -14; e <= 15; e++) {
		uint32_t f32_begin = (min_one_f32 | UINT32_C(0x80000000)) + (uint32_t(e) << 23);
		for (uint16_t f16 = (UINT16_C(0x8000) | (uint16_t(e + e_bias) << 10)); f16 < (UINT16_C(0x8000) | (uint16_t(e + e_bias + 1) << 10)); f16++) {
			const uint32_t f32_end = (fp16::normalizedRanges[f16 & UINT16_C(0x3FF)] | UINT32_C(0x80000000)) + (uint32_t(e) << 23);
			for (uint32_t f32 = f32_begin; f32 < f32_end; f32++) {
				float value;
				memcpy(&value, &f32, sizeof(value));
				ASSERT_EQ(f16, fp16_ieee_from_fp32_value(value)) <<
					std::hex << std::uppercase << std::setfill('0') <<
					"F32 = 0x" << std::setw(8) << f32 << ", " <<
					"F16(F32) = 0x" << std::setw(4) << fp16_ieee_from_fp32_value(value) << ", " <<
					"F16 = 0x" << std::setw(4) << f16;
			}
			f32_begin = f32_end;
		}
	}
}

#if (defined(__i386__) || defined(__x86_64__)) && defined(__F16C__)
	TEST(FP16_IEEE_FROM_FP32_VALUE, match_hardware) {
		const uint32_t min_nonzero = UINT32_C(0x00000001);
		const uint32_t max_finite = UINT32_C(0x7F800000);
		for (uint32_t bits = min_nonzero; bits < max_finite; bits++) {
			float value;
			memcpy(&value, &bits, sizeof(value));
			const uint16_t reference = uint16_t(_mm_cvtsi128_si32(_mm_cvtps_ph(_mm_set_ss(value), _MM_FROUND_CUR_DIRECTION)));
			ASSERT_EQ(reference, fp16_ieee_from_fp32_value(value)) <<
				std::hex << std::uppercase << std::setfill('0') <<
				"F32 = 0x" << std::setw(8) << bits << ", " <<
				"F16(F32) = 0x" << std::setw(4) << fp16_ieee_from_fp32_value(value) << ", " <<
				"F16 = 0x" << std::setw(4) << reference;
		}
	}
#endif