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   fp_t crt_value = __compiler_rt_logbf(x);
11   fp_t libm_value = logbf(x);
12   // `!=` operator on fp_t returns false for NaNs so also check if operands are
13   // both NaN. We don't do `toRepr(crt_value) != toRepr(libm_value)` because
14   // that treats different representations of NaN as not equivalent.
15   if (crt_value != libm_value &&
16       !(crt_isnan(crt_value) && crt_isnan(libm_value))) {
17     printf("error: in __compiler_rt_logb(%a [%X]) = %a [%X] !=  %a [%X]\n", x,
18            toRep(x), crt_value, toRep(crt_value), libm_value,
19            toRep(libm_value));
20     return 1;
21   }
22   return 0;
23 }
24 
25 double cases[] = {
26     1.e-6, -1.e-6, NAN, -NAN, INFINITY, -INFINITY, -1,
27     -0.0,  0.0,    1,   -2,   2,        -0.5,      0.5,
28 };
29 
main()30 int main() {
31   const unsigned N = sizeof(cases) / sizeof(cases[0]);
32   unsigned i;
33   for (i = 0; i < N; ++i) {
34     if (test__compiler_rt_logbf(cases[i])) return 1;
35   }
36 
37   // Test a moving 1 bit, especially to handle denormal values.
38   // Test the negation as well.
39   rep_t x = signBit;
40   while (x) {
41     if (test__compiler_rt_logbf(fromRep(x))) return 1;
42     if (test__compiler_rt_logbf(fromRep(signBit ^ x))) return 1;
43     x >>= 1;
44   }
45   // Also try a couple moving ones
46   x = signBit | (signBit >> 1) | (signBit >> 2);
47   while (x) {
48     if (test__compiler_rt_logbf(fromRep(x))) return 1;
49     if (test__compiler_rt_logbf(fromRep(signBit ^ x))) return 1;
50     x >>= 1;
51   }
52 
53   return 0;
54 }
55