2 * Test Floating Point Conversion
5 /* we want additional float type definitions */
6 #define __STDC_WANT_IEC_60559_BFP_EXT__
7 #define __STDC_WANT_IEC_60559_TYPES_EXT__
15 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
17 static char flag_str
[256];
19 static char *get_flag_state(int flags
)
22 snprintf(flag_str
, sizeof(flag_str
), "%s %s %s %s %s",
23 flags
& FE_OVERFLOW
? "OVERFLOW" : "",
24 flags
& FE_UNDERFLOW
? "UNDERFLOW" : "",
25 flags
& FE_DIVBYZERO
? "DIV0" : "",
26 flags
& FE_INEXACT
? "INEXACT" : "",
27 flags
& FE_INVALID
? "INVALID" : "");
29 snprintf(flag_str
, sizeof(flag_str
), "OK");
35 static void print_double_number(int i
, double num
)
37 uint64_t double_as_hex
= *(uint64_t *) &num
;
38 int flags
= fetestexcept(FE_ALL_EXCEPT
);
39 char *fstr
= get_flag_state(flags
);
41 printf("%02d DOUBLE: %02.20e / %#020" PRIx64
" (%#x => %s)\n",
42 i
, num
, double_as_hex
, flags
, fstr
);
45 static void print_single_number(int i
, float num
)
47 uint32_t single_as_hex
= *(uint32_t *) &num
;
48 int flags
= fetestexcept(FE_ALL_EXCEPT
);
49 char *fstr
= get_flag_state(flags
);
51 printf("%02d SINGLE: %02.20e / %#010x (%#x => %s)\n",
52 i
, num
, single_as_hex
, flags
, fstr
);
55 static void print_half_number(int i
, uint16_t num
)
57 int flags
= fetestexcept(FE_ALL_EXCEPT
);
58 char *fstr
= get_flag_state(flags
);
60 printf("%02d HALF: %#04x (%#x => %s)\n",
64 static void print_int64(int i
, int64_t num
)
66 uint64_t int64_as_hex
= *(uint64_t *) &num
;
67 int flags
= fetestexcept(FE_ALL_EXCEPT
);
68 char *fstr
= get_flag_state(flags
);
70 printf("%02d INT64: %20" PRId64
"/%#020" PRIx64
" (%#x => %s)\n",
71 i
, num
, int64_as_hex
, flags
, fstr
);
75 /* Signaling NaN macros, if supported. */
76 # define SNANF (__builtin_nansf (""))
77 # define SNAN (__builtin_nans (""))
78 # define SNANL (__builtin_nansl (""))
81 float single_numbers
[] = { -SNANF
,
93 5.96046E-8, /* min positive FP16 subnormal */
94 6.09756E-5, /* max subnormal FP16 */
95 6.10352E-5, /* min positive normal FP16 */
97 1.0009765625, /* smallest float after 1.0 FP16 */
101 65504.0, /* max FP16 */
104 131008.0, /* max AFP */
112 static void convert_single_to_half(void)
116 printf("Converting single-precision to half-precision\n");
118 for (i
= 0; i
< ARRAY_SIZE(single_numbers
); ++i
) {
119 float input
= single_numbers
[i
];
121 feclearexcept(FE_ALL_EXCEPT
);
123 print_single_number(i
, input
);
126 asm("vcvtb.f16.f32 %0, %1" : "=t" (output
) : "x" (input
));
129 asm("fcvt %h0, %s1" : "=w" (output
) : "w" (input
));
131 print_half_number(i
, output
);
135 static void convert_single_to_double(void)
139 printf("Converting single-precision to double-precision\n");
141 for (i
= 0; i
< ARRAY_SIZE(single_numbers
); ++i
) {
142 float input
= single_numbers
[i
];
143 /* uint64_t output; */
146 feclearexcept(FE_ALL_EXCEPT
);
148 print_single_number(i
, input
);
150 asm("vcvt.f64.f32 %P0, %1" : "=w" (output
) : "t" (input
));
152 asm("fcvt %d0, %s1" : "=w" (output
) : "w" (input
));
154 print_double_number(i
, output
);
158 static void convert_single_to_integer(void)
162 printf("Converting single-precision to integer\n");
164 for (i
= 0; i
< ARRAY_SIZE(single_numbers
); ++i
) {
165 float input
= single_numbers
[i
];
168 feclearexcept(FE_ALL_EXCEPT
);
170 print_single_number(i
, input
);
172 /* asm("vcvt.s32.f32 %s0, %s1" : "=t" (output) : "t" (input)); */
175 asm("fcvtzs %0, %s1" : "=r" (output
) : "w" (input
));
177 print_int64(i
, output
);
181 /* This allows us to initialise some doubles as pure hex */
187 test_doubles double_numbers
[] = {
195 {-1.111E+30}, /* half prec */
202 {5.96046E-8}, /* min positive FP16 subnormal */
203 {6.09756E-5}, /* max subnormal FP16 */
204 {6.10352E-5}, /* min positive normal FP16 */
206 {1.0009765625}, /* smallest float after 1.0 FP16 */
208 {1.3789972848607228e-308},
209 {1.4914738736681624e-308},
213 {65504.0}, /* max FP16 */
216 {131008.0}, /* max AFP */
218 {.h
= 0x41dfffffffc00000 }, /* to int = 0x7fffffff */
224 {.h
= 0x7ff0000000000001}, /* SNAN */
228 static void convert_double_to_half(void)
232 printf("Converting double-precision to half-precision\n");
234 for (i
= 0; i
< ARRAY_SIZE(double_numbers
); ++i
) {
235 double input
= double_numbers
[i
].d
;
238 feclearexcept(FE_ALL_EXCEPT
);
240 print_double_number(i
, input
);
242 /* as we don't have _Float16 support */
244 /* asm("vcvtb.f16.f64 %0, %P1" : "=t" (output) : "x" (input)); */
247 asm("fcvt %h0, %d1" : "=w" (output
) : "w" (input
));
249 print_half_number(i
, output
);
253 static void convert_double_to_single(void)
257 printf("Converting double-precision to single-precision\n");
259 for (i
= 0; i
< ARRAY_SIZE(double_numbers
); ++i
) {
260 double input
= double_numbers
[i
].d
;
263 feclearexcept(FE_ALL_EXCEPT
);
265 print_double_number(i
, input
);
268 asm("vcvt.f32.f64 %0, %P1" : "=w" (output
) : "x" (input
));
270 asm("fcvt %s0, %d1" : "=w" (output
) : "w" (input
));
273 print_single_number(i
, output
);
277 static void convert_double_to_integer(void)
281 printf("Converting double-precision to integer\n");
283 for (i
= 0; i
< ARRAY_SIZE(double_numbers
); ++i
) {
284 double input
= double_numbers
[i
].d
;
287 feclearexcept(FE_ALL_EXCEPT
);
289 print_double_number(i
, input
);
291 /* asm("vcvt.s32.f32 %s0, %s1" : "=t" (output) : "t" (input)); */
294 asm("fcvtzs %0, %d1" : "=r" (output
) : "w" (input
));
296 print_int64(i
, output
);
300 /* no handy defines for these numbers */
301 uint16_t half_numbers
[] = {
302 0xffff, /* -NaN / AHP -Max */
303 0xfcff, /* -NaN / AHP */
304 0xfc01, /* -NaN / AHP */
309 0x8001, /* -MIN subnormal */
312 0x0001, /* MIN subnormal */
316 0x7c01, /* NaN / AHP */
317 0x7cff, /* NaN / AHP */
318 0x7fff, /* NaN / AHP +Max*/
321 static void convert_half_to_double(void)
325 printf("Converting half-precision to double-precision\n");
327 for (i
= 0; i
< ARRAY_SIZE(half_numbers
); ++i
) {
328 uint16_t input
= half_numbers
[i
];
331 feclearexcept(FE_ALL_EXCEPT
);
333 print_half_number(i
, input
);
335 /* asm("vcvtb.f64.f16 %P0, %1" : "=w" (output) : "t" (input)); */
338 asm("fcvt %d0, %h1" : "=w" (output
) : "w" (input
));
340 print_double_number(i
, output
);
344 static void convert_half_to_single(void)
348 printf("Converting half-precision to single-precision\n");
350 for (i
= 0; i
< ARRAY_SIZE(half_numbers
); ++i
) {
351 uint16_t input
= half_numbers
[i
];
354 feclearexcept(FE_ALL_EXCEPT
);
356 print_half_number(i
, input
);
359 * Clang refuses to allocate an integer to a fp register.
360 * Perform the move from a general register by hand.
362 asm("vmov %0, %1\n\t"
363 "vcvtb.f32.f16 %0, %0" : "=w" (output
) : "r" (input
));
365 asm("fcvt %s0, %h1" : "=w" (output
) : "w" (input
));
367 print_single_number(i
, output
);
371 static void convert_half_to_integer(void)
375 printf("Converting half-precision to integer\n");
377 for (i
= 0; i
< ARRAY_SIZE(half_numbers
); ++i
) {
378 uint16_t input
= half_numbers
[i
];
381 feclearexcept(FE_ALL_EXCEPT
);
383 print_half_number(i
, input
);
385 /* asm("vcvt.s32.f16 %0, %1" : "=t" (output) : "t" (input)); v8.2*/
388 asm("fcvt %s0, %h1" : "=w" (output
) : "w" (input
));
390 print_int64(i
, output
);
399 float_mapping round_flags
[] = {
400 { FE_TONEAREST
, "to nearest" },
401 { FE_UPWARD
, "upwards" },
402 { FE_DOWNWARD
, "downwards" },
403 { FE_TOWARDZERO
, "to zero" }
406 int main(int argc
, char *argv
[argc
])
410 printf("#### Enabling IEEE Half Precision\n");
412 for (i
= 0; i
< ARRAY_SIZE(round_flags
); ++i
) {
413 fesetround(round_flags
[i
].flag
);
414 printf("### Rounding %s\n", round_flags
[i
].desc
);
415 convert_single_to_half();
416 convert_single_to_double();
417 convert_double_to_half();
418 convert_double_to_single();
419 convert_half_to_single();
420 convert_half_to_double();
423 /* convert to integer */
424 convert_single_to_integer();
425 convert_double_to_integer();
426 convert_half_to_integer();
428 /* And now with ARM alternative FP16 */
430 asm("vmrs r1, fpscr\n\t"
431 "orr r1, r1, %[flags]\n\t"
433 : /* no output */ : [flags
] "n" (1 << 26) : "r1" );
435 asm("mrs x1, fpcr\n\t"
436 "orr x1, x1, %[flags]\n\t"
438 : /* no output */ : [flags
] "n" (1 << 26) : "x1" );
441 printf("#### Enabling ARM Alternative Half Precision\n");
443 for (i
= 0; i
< ARRAY_SIZE(round_flags
); ++i
) {
444 fesetround(round_flags
[i
].flag
);
445 printf("### Rounding %s\n", round_flags
[i
].desc
);
446 convert_single_to_half();
447 convert_single_to_double();
448 convert_double_to_half();
449 convert_double_to_single();
450 convert_half_to_single();
451 convert_half_to_double();
454 /* convert to integer */
455 convert_single_to_integer();
456 convert_double_to_integer();
457 convert_half_to_integer();