mesa/src/gallium/tests/unit/u_half_test.c
Eric Anholt bb5801ad98 u_half_test: Turn it into an actual unit test.
You could break the test and meson test wouldn't complain, since we
returned success either way.

Reviewed-by: Thomas Helland <thomashelland90@gmail.com>
Reviewed-by: Kristian H. Kristensen <hoegsberg@google.com>
2019-07-16 12:51:13 -07:00

36 lines
773 B
C

#include <stdlib.h>
#include <stdio.h>
#include <float.h>
#include "util/u_math.h"
#include "util/u_half.h"
int
main(int argc, char **argv)
{
unsigned i;
unsigned roundtrip_fails = 0;
for(i = 0; i < 1 << 16; ++i)
{
uint16_t h = (uint16_t) i;
union fi f;
uint16_t rh;
f.f = util_half_to_float(h);
rh = util_float_to_half(f.f);
if (h != rh && !(util_is_half_nan(h) && util_is_half_nan(rh))) {
printf("Roundtrip failed: %x -> %x = %f -> %x\n", h, f.ui, f.f, rh);
++roundtrip_fails;
}
}
if(roundtrip_fails) {
printf("Failure! %u/65536 half floats failed a conversion to float and back.\n", roundtrip_fails);
return 1;
} else {
printf("Success!\n");
return 0;
}
}