• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===-- Unittests for logb ------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "src/math/logb.h"
10 #include "utils/FPUtil/BitPatterns.h"
11 #include "utils/FPUtil/FloatOperations.h"
12 #include "utils/FPUtil/FloatProperties.h"
13 #include "utils/FPUtil/ManipulationFunctions.h"
14 #include "utils/UnitTest/Test.h"
15 #include <math.h>
16 
17 using __llvm_libc::fputil::valueAsBits;
18 using __llvm_libc::fputil::valueFromBits;
19 
20 using BitPatterns = __llvm_libc::fputil::BitPatterns<double>;
21 using Properties = __llvm_libc::fputil::FloatProperties<double>;
22 
TEST(LogbTest,SpecialNumbers)23 TEST(LogbTest, SpecialNumbers) {
24   EXPECT_EQ(
25       BitPatterns::aQuietNaN,
26       valueAsBits(__llvm_libc::logb(valueFromBits(BitPatterns::aQuietNaN))));
27   EXPECT_EQ(BitPatterns::aNegativeQuietNaN,
28             valueAsBits(__llvm_libc::logb(
29                 valueFromBits(BitPatterns::aNegativeQuietNaN))));
30 
31   EXPECT_EQ(BitPatterns::aSignallingNaN,
32             valueAsBits(
33                 __llvm_libc::logb(valueFromBits(BitPatterns::aSignallingNaN))));
34   EXPECT_EQ(BitPatterns::aNegativeSignallingNaN,
35             valueAsBits(__llvm_libc::logb(
36                 valueFromBits(BitPatterns::aNegativeSignallingNaN))));
37 
38   EXPECT_EQ(BitPatterns::inf,
39             valueAsBits(__llvm_libc::logb(valueFromBits(BitPatterns::inf))));
40   EXPECT_EQ(BitPatterns::inf,
41             valueAsBits(__llvm_libc::logb(valueFromBits(BitPatterns::negInf))));
42 
43   EXPECT_EQ(BitPatterns::negInf,
44             valueAsBits(__llvm_libc::logb(valueFromBits(BitPatterns::zero))));
45   EXPECT_EQ(BitPatterns::negInf, valueAsBits(__llvm_libc::logb(
46                                      valueFromBits(BitPatterns::negZero))));
47 }
48 
TEST(LogbTest,PowersOfTwo)49 TEST(LogbTest, PowersOfTwo) {
50   EXPECT_EQ(valueAsBits(0.0), valueAsBits(__llvm_libc::logb(1.0)));
51   EXPECT_EQ(valueAsBits(0.0), valueAsBits(__llvm_libc::logb(-1.0)));
52 
53   EXPECT_EQ(valueAsBits(1.0), valueAsBits(__llvm_libc::logb(2.0)));
54   EXPECT_EQ(valueAsBits(1.0), valueAsBits(__llvm_libc::logb(-2.0)));
55 
56   EXPECT_EQ(valueAsBits(2.0), valueAsBits(__llvm_libc::logb(4.0)));
57   EXPECT_EQ(valueAsBits(2.0), valueAsBits(__llvm_libc::logb(-4.0)));
58 
59   EXPECT_EQ(valueAsBits(3.0), valueAsBits(__llvm_libc::logb(8.0)));
60   EXPECT_EQ(valueAsBits(3.0), valueAsBits(__llvm_libc::logb(-8.0)));
61 
62   EXPECT_EQ(valueAsBits(4.0), valueAsBits(__llvm_libc::logb(16.0)));
63   EXPECT_EQ(valueAsBits(4.0), valueAsBits(__llvm_libc::logb(-16.0)));
64 
65   EXPECT_EQ(valueAsBits(5.0), valueAsBits(__llvm_libc::logb(32.0)));
66   EXPECT_EQ(valueAsBits(5.0), valueAsBits(__llvm_libc::logb(-32.0)));
67 }
68 
TEST(LogbTest,SomeIntegers)69 TEST(LogbTest, SomeIntegers) {
70   EXPECT_EQ(valueAsBits(1.0), valueAsBits(__llvm_libc::logb(3.0)));
71   EXPECT_EQ(valueAsBits(1.0), valueAsBits(__llvm_libc::logb(-3.0)));
72 
73   EXPECT_EQ(valueAsBits(2.0), valueAsBits(__llvm_libc::logb(7.0)));
74   EXPECT_EQ(valueAsBits(2.0), valueAsBits(__llvm_libc::logb(-7.0)));
75 
76   EXPECT_EQ(valueAsBits(3.0), valueAsBits(__llvm_libc::logb(10.0)));
77   EXPECT_EQ(valueAsBits(3.0), valueAsBits(__llvm_libc::logb(-10.0)));
78 
79   EXPECT_EQ(valueAsBits(4.0), valueAsBits(__llvm_libc::logb(31.0)));
80   EXPECT_EQ(valueAsBits(4.0), valueAsBits(__llvm_libc::logb(-31.0)));
81 
82   EXPECT_EQ(valueAsBits(5.0), valueAsBits(__llvm_libc::logb(55.0)));
83   EXPECT_EQ(valueAsBits(5.0), valueAsBits(__llvm_libc::logb(-55.0)));
84 }
85 
TEST(LogbTest,InDoubleRange)86 TEST(LogbTest, InDoubleRange) {
87   using BitsType = Properties::BitsType;
88   constexpr BitsType count = 10000000;
89   constexpr BitsType step = UINT64_MAX / count;
90   for (BitsType i = 0, v = 0; i <= count; ++i, v += step) {
91     double x = valueFromBits(v);
92     if (isnan(x) || isinf(x) || x == 0.0)
93       continue;
94 
95     int exponent;
96     __llvm_libc::fputil::frexp(x, exponent);
97     ASSERT_TRUE(double(exponent) == __llvm_libc::logb(x) + 1.0);
98   }
99 }
100