1 // RUN: %clang_builtins %s %librt -o %t && %run %t
2 //===-- compiler_rt_logbf_test.c - Test __compiler_rt_logbf ---------------===//
3 //
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file checks __compiler_rt_logbf from the compiler_rt library for
11 // conformance against libm.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #define SINGLE_PRECISION
16 #include "fp_lib.h"
17 #include "int_math.h"
18 #include <math.h>
19 #include <stdio.h>
20 
21 int test__compiler_rt_logbf(fp_t x) {
22   fp_t crt_value = __compiler_rt_logbf(x);
23   fp_t libm_value = logbf(x);
24   // `!=` operator on fp_t returns false for NaNs so also check if operands are
25   // both NaN. We don't do `toRepr(crt_value) != toRepr(libm_value)` because
26   // that treats different representations of NaN as not equivalent.
27   if (crt_value != libm_value &&
28       !(crt_isnan(crt_value) && crt_isnan(libm_value))) {
29     printf("error: in __compiler_rt_logb(%a [%X]) = %a [%X] !=  %a [%X]\n", x,
30            toRep(x), crt_value, toRep(crt_value), libm_value,
31            toRep(libm_value));
32     return 1;
33   }
34   return 0;
35 }
36 
37 double cases[] = {
38     1.e-6, -1.e-6, NAN, -NAN, INFINITY, -INFINITY, -1,
39     -0.0,  0.0,    1,   -2,   2,        -0.5,      0.5,
40 };
41 
42 int main() {
43   const unsigned N = sizeof(cases) / sizeof(cases[0]);
44   unsigned i;
45   for (i = 0; i < N; ++i) {
46     if (test__compiler_rt_logbf(cases[i])) return 1;
47   }
48 
49   // Test a moving 1 bit, especially to handle denormal values.
50   // Test the negation as well.
51   rep_t x = signBit;
52   while (x) {
53     if (test__compiler_rt_logbf(fromRep(x))) return 1;
54     if (test__compiler_rt_logbf(fromRep(signBit ^ x))) return 1;
55     x >>= 1;
56   }
57   // Also try a couple moving ones
58   x = signBit | (signBit >> 1) | (signBit >> 2);
59   while (x) {
60     if (test__compiler_rt_logbf(fromRep(x))) return 1;
61     if (test__compiler_rt_logbf(fromRep(signBit ^ x))) return 1;
62     x >>= 1;
63   }
64 
65   return 0;
66 }
67