]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - test/builtins/Unit/compiler_rt_logb_test.c
Vendor import of compiler-rt trunk r351319 (just before the release_80
[FreeBSD/FreeBSD.git] / test / builtins / Unit / compiler_rt_logb_test.c
1 // RUN: %clang_builtins %s %librt -o %t && %run %t
2 //===-- compiler_rt_logb_test.c - Test __compiler_rt_logb -----------------===//
3 //
4 //                     The LLVM Compiler Infrastructure
5 //
6 // This file is dual licensed under the MIT and the University of Illinois Open
7 // Source Licenses. See LICENSE.TXT for details.
8 //
9 //===----------------------------------------------------------------------===//
10 //
11 // This file checks __compiler_rt_logb from the compiler_rt library for
12 // conformance against libm.
13 //
14 //===----------------------------------------------------------------------===//
15
16 #define DOUBLE_PRECISION
17 #include <math.h>
18 #include <stdio.h>
19 #include "fp_lib.h"
20
21 int test__compiler_rt_logb(fp_t x) {
22   fp_t crt_value = __compiler_rt_logb(x);
23   fp_t libm_value = logb(x);
24   // Compare actual rep, e.g. to avoid NaN != the same NaN
25   if (toRep(crt_value) != toRep(libm_value)) {
26     printf("error: in __compiler_rt_logb(%a [%lX]) = %a [%lX] !=  %a [%lX]\n",
27            x, toRep(x), crt_value, toRep(crt_value), libm_value,
28            toRep(libm_value));
29     return 1;
30   }
31   return 0;
32 }
33
34 double cases[] = {
35     1.e-6, -1.e-6, NAN, -NAN, INFINITY, -INFINITY, -1,
36     -0.0,  0.0,    1,   -2,   2,        -0.5,      0.5,
37 };
38
39 int main() {
40   const unsigned N = sizeof(cases) / sizeof(cases[0]);
41   unsigned i;
42   for (i = 0; i < N; ++i) {
43     if (test__compiler_rt_logb(cases[i])) return 1;
44   }
45
46   // Test a moving 1 bit, especially to handle denormal values.
47   // Test the negation as well.
48   rep_t x = signBit;
49   while (x) {
50     if (test__compiler_rt_logb(fromRep(x))) return 1;
51     if (test__compiler_rt_logb(fromRep(signBit ^ x))) return 1;
52     x >>= 1;
53   }
54   // Also try a couple moving ones
55   x = signBit | (signBit >> 1) | (signBit >> 2);
56   while (x) {
57     if (test__compiler_rt_logb(fromRep(x))) return 1;
58     if (test__compiler_rt_logb(fromRep(signBit ^ x))) return 1;
59     x >>= 1;
60   }
61
62   return 0;
63 }