1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <float.h>
4 
5 #include "util/u_math.h"
6 #include "util/half_float.h"
7 #include "util/u_cpu_detect.h"
8 
9 static void
test(void)10 test(void)
11 {
12    unsigned i;
13    unsigned roundtrip_fails = 0;
14 
15    for(i = 0; i < 1 << 16; ++i)
16    {
17       uint16_t h = (uint16_t) i;
18       union fi f;
19       uint16_t rh;
20 
21       f.f = _mesa_half_to_float(h);
22       rh = _mesa_float_to_half(f.f);
23 
24       if (h != rh && !(util_is_half_nan(h) && util_is_half_nan(rh))) {
25          printf("Roundtrip failed: %x -> %x = %f -> %x\n", h, f.ui, f.f, rh);
26          ++roundtrip_fails;
27       }
28    }
29 
30    if(roundtrip_fails) {
31       printf("Failure! %u/65536 half floats failed a conversion to float and back.\n", roundtrip_fails);
32       exit(1);
33    }
34 }
35 
36 int
main(int argc,char ** argv)37 main(int argc, char **argv)
38 {
39    assert(!util_cpu_caps.has_f16c);
40    test();
41 
42    /* Test f16c. */
43    util_cpu_detect();
44    if (util_cpu_caps.has_f16c)
45       test();
46 
47    printf("Success!\n");
48    return 0;
49 }
50