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 <math.h>
17#include <stdio.h>
18#include "fp_lib.h"
19
20int test__compiler_rt_logbf(fp_t x) {
21  fp_t crt_value = __compiler_rt_logbf(x);
22  fp_t libm_value = logbf(x);
23  // Compare actual rep, e.g. to avoid NaN != the same NaN
24  if (toRep(crt_value) != toRep(libm_value)) {
25    printf("error: in __compiler_rt_logb(%a [%X]) = %a [%X] !=  %a [%X]\n", x,
26           toRep(x), crt_value, toRep(crt_value), libm_value,
27           toRep(libm_value));
28    return 1;
29  }
30  return 0;
31}
32
33double cases[] = {
34    1.e-6, -1.e-6, NAN, -NAN, INFINITY, -INFINITY, -1,
35    -0.0,  0.0,    1,   -2,   2,        -0.5,      0.5,
36};
37
38int main() {
39  const unsigned N = sizeof(cases) / sizeof(cases[0]);
40  unsigned i;
41  for (i = 0; i < N; ++i) {
42    if (test__compiler_rt_logbf(cases[i])) return 1;
43  }
44
45  // Test a moving 1 bit, especially to handle denormal values.
46  // Test the negation as well.
47  rep_t x = signBit;
48  while (x) {
49    if (test__compiler_rt_logbf(fromRep(x))) return 1;
50    if (test__compiler_rt_logbf(fromRep(signBit ^ x))) return 1;
51    x >>= 1;
52  }
53  // Also try a couple moving ones
54  x = signBit | (signBit >> 1) | (signBit >> 2);
55  while (x) {
56    if (test__compiler_rt_logbf(fromRep(x))) return 1;
57    if (test__compiler_rt_logbf(fromRep(signBit ^ x))) return 1;
58    x >>= 1;
59  }
60
61  return 0;
62}
63