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 9static void 10test(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 36int 37main(int argc, char **argv) 38{ 39 test(); 40 41 /* Test non-f16c. */ 42 if (util_get_cpu_caps()->has_f16c) { 43 ((struct util_cpu_caps_t *)util_get_cpu_caps())->has_f16c = false; 44 test(); 45 } 46 47 printf("Success!\n"); 48 return 0; 49} 50