1// RUN: %clang_builtins %s %librt -o %t && %run %t
2//
3// Bug 42496
4// XFAIL: sparcv9-target-arch
5//
6//===-- compiler_rt_logbl_test.c - Test __compiler_rt_logbl ---------------===//
7//
8// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
9// See https://llvm.org/LICENSE.txt for license information.
10// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
11//
12//===----------------------------------------------------------------------===//
13//
14// This file checks __compiler_rt_logbl from the compiler_rt library for
15// conformance against libm.
16//
17//===----------------------------------------------------------------------===//
18
19#define QUAD_PRECISION
20#include <math.h>
21#include <stdio.h>
22#include "fp_lib.h"
23#include "int_lib.h"
24
25#if defined(CRT_HAS_128BIT) && defined(CRT_LDBL_128BIT)
26
27int test__compiler_rt_logbl(fp_t x) {
28  fp_t crt_value = __compiler_rt_logbl(x);
29  fp_t libm_value = logbl(x);
30  // Compare actual rep, e.g. to avoid NaN != the same NaN
31  if (toRep(crt_value) != toRep(libm_value)) {
32    // Split expected values into two for printf
33    twords x_t, crt_value_t, libm_value_t;
34    x_t.all = toRep(x);
35    crt_value_t.all = toRep(crt_value);
36    libm_value_t.all = toRep(libm_value);
37    printf(
38        "error: in __compiler_rt_logb(%a [%llX %llX]) = %a [%llX %llX] !=  %a "
39        "[%llX %llX]\n",
40        x, x_t.s.high, x_t.s.low, crt_value, crt_value_t.s.high,
41        crt_value_t.s.low, libm_value, libm_value_t.s.high, libm_value_t.s.low);
42    return 1;
43  }
44  return 0;
45}
46
47double cases[] = {
48    1.e-6, -1.e-6, NAN, -NAN, INFINITY, -INFINITY, -1,
49    -0.0,  0.0,    1,   -2,   2,        -0.5,      0.5,
50};
51
52#endif
53
54int main() {
55#if defined(CRT_HAS_128BIT) && defined(CRT_LDBL_128BIT)
56  const unsigned N = sizeof(cases) / sizeof(cases[0]);
57  unsigned i;
58  for (i = 0; i < N; ++i) {
59    if (test__compiler_rt_logbl(cases[i])) return 1;
60  }
61
62  // Test a moving 1 bit, especially to handle denormal values.
63  // Test the negation as well.
64  rep_t x = signBit;
65  while (x) {
66    if (test__compiler_rt_logbl(fromRep(x))) return 1;
67    if (test__compiler_rt_logbl(fromRep(signBit ^ x))) return 1;
68    x >>= 1;
69  }
70  // Also try a couple moving ones
71  x = signBit | (signBit >> 1) | (signBit >> 2);
72  while (x) {
73    if (test__compiler_rt_logbl(fromRep(x))) return 1;
74    if (test__compiler_rt_logbl(fromRep(signBit ^ x))) return 1;
75    x >>= 1;
76  }
77#else
78  printf("skipped\n");
79#endif
80
81  return 0;
82}
83