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
8int 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: 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(format: "error: in __compiler_rt_logb(%a [%lX]) = %a [%lX] != %a [%lX]\n",
20 x, toRep(x), crt_value, toRep(x: crt_value), libm_value,
21 toRep(x: libm_value));
22 return 1;
23 }
24 return 0;
25}
26
27double 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,
30};
31
32#ifndef __GLIBC_PREREQ
33#define __GLIBC_PREREQ(x, y) 0
34#endif
35
36int 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(x: cases[i])) return 1;
45 }
46
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(x: fromRep(x))) return 1;
52 if (test__compiler_rt_logb(x: fromRep(signBit ^ x))) return 1;
53 x >>= 1;
54 }
55 // Also try a couple moving ones
56 x = signBit | (signBit >> 1) | (signBit >> 2);
57 while (x) {
58 if (test__compiler_rt_logb(x: fromRep(x))) return 1;
59 if (test__compiler_rt_logb(x: fromRep(signBit ^ x))) return 1;
60 x >>= 1;
61 }
62#else
63 printf("skipped\n");
64#endif
65
66 return 0;
67}
68

source code of compiler-rt/test/builtins/Unit/compiler_rt_logb_test.c