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