1 // RUN: %clang_builtins %s %librt -o %t && %run %t
2 
3 #define DOUBLE_PRECISION
4 #include <math.h>
5 #include <stdio.h>
6 #include "fp_lib.h"
7 
test__compiler_rt_logb(fp_t x)8 int test__compiler_rt_logb(fp_t x) {
9   fp_t crt_value = __compiler_rt_logb(x);
10   fp_t libm_value = logb(x);
11   // Compare the values, considering all NaNs equivalent, as the spec doesn't
12   // specify the NaN signedness/payload.
13   if (crt_value != libm_value &&
14       !(crt_isnan(crt_value) && crt_isnan(libm_value))) {
15     printf("error: in __compiler_rt_logb(%a [%lX]) = %a [%lX] !=  %a [%lX]\n",
16            x, toRep(x), crt_value, toRep(crt_value), libm_value,
17            toRep(libm_value));
18     return 1;
19   }
20   return 0;
21 }
22 
23 double cases[] = {
24     1.e-6, -1.e-6, NAN, -NAN, INFINITY, -INFINITY, -1,
25     -0.0,  0.0,    1,   -2,   2,        -0.5,      0.5,
26 };
27 
28 #ifndef __GLIBC_PREREQ
29 #define __GLIBC_PREREQ(x, y) 0
30 #endif
31 
main()32 int main() {
33   // Do not the run the compiler-rt logb test case if using GLIBC version
34   // < 2.23. Older versions might not compute to the same value as the
35   // compiler-rt value.
36 #if __GLIBC_PREREQ(2, 23)
37   const unsigned N = sizeof(cases) / sizeof(cases[0]);
38   unsigned i;
39   for (i = 0; i < N; ++i) {
40     if (test__compiler_rt_logb(cases[i])) return 1;
41   }
42 
43   // Test a moving 1 bit, especially to handle denormal values.
44   // Test the negation as well.
45   rep_t x = signBit;
46   while (x) {
47     if (test__compiler_rt_logb(fromRep(x))) return 1;
48     if (test__compiler_rt_logb(fromRep(signBit ^ x))) return 1;
49     x >>= 1;
50   }
51   // Also try a couple moving ones
52   x = signBit | (signBit >> 1) | (signBit >> 2);
53   while (x) {
54     if (test__compiler_rt_logb(fromRep(x))) return 1;
55     if (test__compiler_rt_logb(fromRep(signBit ^ x))) return 1;
56     x >>= 1;
57   }
58 #else
59   printf("skipped\n");
60 #endif
61 
62   return 0;
63 }
64