xref: /llvm-project/compiler-rt/test/builtins/Unit/compiler_rt_logbf_test.c (revision 496e7f330c43094ab508e07cf75237e52394fa66)
1 // RUN: %clang_builtins %s %librt -o %t && %run %t
2 
3 #define SINGLE_PRECISION
4 #include "fp_lib.h"
5 #include "int_math.h"
6 #include <math.h>
7 #include <stdio.h>
8 
test__compiler_rt_logbf(fp_t x)9 int test__compiler_rt_logbf(fp_t x) {
10 #if defined(__ve__)
11   if (fpclassify(x) == FP_SUBNORMAL)
12     return 0;
13 #endif
14   fp_t crt_value = __compiler_rt_logbf(x);
15   fp_t libm_value = logbf(x);
16   // `!=` operator on fp_t returns false for NaNs so also check if operands are
17   // both NaN. We don't do `toRepr(crt_value) != toRepr(libm_value)` because
18   // that treats different representations of NaN as not equivalent.
19   if (crt_value != libm_value &&
20       !(crt_isnan(crt_value) && crt_isnan(libm_value))) {
21     printf("error: in __compiler_rt_logb(%a [%X]) = %a [%X] !=  %a [%X]\n", x,
22            toRep(x), crt_value, toRep(crt_value), libm_value,
23            toRep(libm_value));
24     return 1;
25   }
26   return 0;
27 }
28 
29 double cases[] = {
30     1.e-6, -1.e-6, NAN, -NAN, INFINITY, -INFINITY, -1,
31     -0.0,  0.0,    1,   -2,   2,        -0.5,      0.5,
32 };
33 
main()34 int main() {
35   const unsigned N = sizeof(cases) / sizeof(cases[0]);
36   unsigned i;
37   for (i = 0; i < N; ++i) {
38     if (test__compiler_rt_logbf(cases[i])) return 1;
39   }
40 
41   // Test a moving 1 bit, especially to handle denormal values.
42   // Test the negation as well.
43   rep_t x = signBit;
44   while (x) {
45     if (test__compiler_rt_logbf(fromRep(x))) return 1;
46     if (test__compiler_rt_logbf(fromRep(signBit ^ x))) return 1;
47     x >>= 1;
48   }
49   // Also try a couple moving ones
50   x = signBit | (signBit >> 1) | (signBit >> 2);
51   while (x) {
52     if (test__compiler_rt_logbf(fromRep(x))) return 1;
53     if (test__compiler_rt_logbf(fromRep(signBit ^ x))) return 1;
54     x >>= 1;
55   }
56 
57   return 0;
58 }
59