• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===-- Utility class to test different flavors of ldexp --------*- C++ -*-===//
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 #ifndef LLVM_LIBC_TEST_SRC_MATH_LDEXPTEST_H
10 #define LLVM_LIBC_TEST_SRC_MATH_LDEXPTEST_H
11 
12 #include "utils/FPUtil/FPBits.h"
13 #include "utils/FPUtil/NormalFloat.h"
14 #include "utils/FPUtil/TestHelpers.h"
15 #include "utils/UnitTest/Test.h"
16 
17 #include <limits.h>
18 #include <math.h>
19 #include <stdint.h>
20 
21 template <typename T>
22 class LdExpTestTemplate : public __llvm_libc::testing::Test {
23   using FPBits = __llvm_libc::fputil::FPBits<T>;
24   using NormalFloat = __llvm_libc::fputil::NormalFloat<T>;
25   using UIntType = typename FPBits::UIntType;
26   static constexpr UIntType mantissaWidth =
27       __llvm_libc::fputil::MantissaWidth<T>::value;
28   // A normalized mantissa to be used with tests.
29   static constexpr UIntType mantissa = NormalFloat::one + 0x1234;
30 
31   const T zero = __llvm_libc::fputil::FPBits<T>::zero();
32   const T negZero = __llvm_libc::fputil::FPBits<T>::negZero();
33   const T inf = __llvm_libc::fputil::FPBits<T>::inf();
34   const T negInf = __llvm_libc::fputil::FPBits<T>::negInf();
35   const T nan = __llvm_libc::fputil::FPBits<T>::buildNaN(1);
36 
37 public:
38   typedef T (*LdExpFunc)(T, int);
39 
testSpecialNumbers(LdExpFunc func)40   void testSpecialNumbers(LdExpFunc func) {
41     int expArray[5] = {-INT_MAX - 1, -10, 0, 10, INT_MAX};
42     for (int exp : expArray) {
43       ASSERT_FP_EQ(zero, func(zero, exp));
44       ASSERT_FP_EQ(negZero, func(negZero, exp));
45       ASSERT_FP_EQ(inf, func(inf, exp));
46       ASSERT_FP_EQ(negInf, func(negInf, exp));
47       ASSERT_NE(isnan(func(nan, exp)), 0);
48     }
49   }
50 
testPowersOfTwo(LdExpFunc func)51   void testPowersOfTwo(LdExpFunc func) {
52     int32_t expArray[5] = {1, 2, 3, 4, 5};
53     int32_t valArray[6] = {1, 2, 4, 8, 16, 32};
54     for (int32_t exp : expArray) {
55       for (int32_t val : valArray) {
56         ASSERT_FP_EQ(T(val << exp), func(T(val), exp));
57         ASSERT_FP_EQ(T(-1 * (val << exp)), func(T(-val), exp));
58       }
59     }
60   }
61 
testOverflow(LdExpFunc func)62   void testOverflow(LdExpFunc func) {
63     NormalFloat x(FPBits::maxExponent - 10, NormalFloat::one + 0xF00BA, 0);
64     for (int32_t exp = 10; exp < 100; ++exp) {
65       ASSERT_FP_EQ(inf, func(T(x), exp));
66       ASSERT_FP_EQ(negInf, func(-T(x), exp));
67     }
68   }
69 
testUnderflowToZeroOnNormal(LdExpFunc func)70   void testUnderflowToZeroOnNormal(LdExpFunc func) {
71     // In this test, we pass a normal nubmer to func and expect zero
72     // to be returned due to underflow.
73     int32_t baseExponent = FPBits::exponentBias + mantissaWidth;
74     int32_t expArray[] = {baseExponent + 5, baseExponent + 4, baseExponent + 3,
75                           baseExponent + 2, baseExponent + 1};
76     T x = NormalFloat(0, mantissa, 0);
77     for (int32_t exp : expArray) {
78       ASSERT_FP_EQ(func(x, -exp), x > 0 ? zero : negZero);
79     }
80   }
81 
testUnderflowToZeroOnSubnormal(LdExpFunc func)82   void testUnderflowToZeroOnSubnormal(LdExpFunc func) {
83     // In this test, we pass a normal nubmer to func and expect zero
84     // to be returned due to underflow.
85     int32_t baseExponent = FPBits::exponentBias + mantissaWidth;
86     int32_t expArray[] = {baseExponent + 5, baseExponent + 4, baseExponent + 3,
87                           baseExponent + 2, baseExponent + 1};
88     T x = NormalFloat(-FPBits::exponentBias, mantissa, 0);
89     for (int32_t exp : expArray) {
90       ASSERT_FP_EQ(func(x, -exp), x > 0 ? zero : negZero);
91     }
92   }
93 
testNormalOperation(LdExpFunc func)94   void testNormalOperation(LdExpFunc func) {
95     T valArray[] = {
96         // Normal numbers
97         NormalFloat(100, mantissa, 0), NormalFloat(-100, mantissa, 0),
98         NormalFloat(100, mantissa, 1), NormalFloat(-100, mantissa, 1),
99         // Subnormal numbers
100         NormalFloat(-FPBits::exponentBias, mantissa, 0),
101         NormalFloat(-FPBits::exponentBias, mantissa, 1)};
102     for (int32_t exp = 0; exp <= static_cast<int32_t>(mantissaWidth); ++exp) {
103       for (T x : valArray) {
104         // We compare the result of ldexp with the result
105         // of the native multiplication/division instruction.
106         ASSERT_FP_EQ(func(x, exp), x * (UIntType(1) << exp));
107         ASSERT_FP_EQ(func(x, -exp), x / (UIntType(1) << exp));
108       }
109     }
110 
111     // Normal which trigger mantissa overflow.
112     T x = NormalFloat(-FPBits::exponentBias + 1, 2 * NormalFloat::one - 1, 0);
113     ASSERT_FP_EQ(func(x, -1), x / 2);
114     ASSERT_FP_EQ(func(-x, -1), -x / 2);
115 
116     // Start with a normal number high exponent but pass a very low number for
117     // exp. The result should be a subnormal number.
118     x = NormalFloat(FPBits::exponentBias, NormalFloat::one, 0);
119     int exp = -FPBits::maxExponent - 5;
120     T result = func(x, exp);
121     FPBits resultBits(result);
122     ASSERT_FALSE(resultBits.isZero());
123     // Verify that the result is indeed subnormal.
124     ASSERT_EQ(resultBits.exponent, uint16_t(0));
125     // But if the exp is so less that normalization leads to zero, then
126     // the result should be zero.
127     result = func(x, -FPBits::maxExponent - int(mantissaWidth) - 5);
128     ASSERT_TRUE(FPBits(result).isZero());
129 
130     // Start with a subnormal number but pass a very high number for exponent.
131     // The result should not be infinity.
132     x = NormalFloat(-FPBits::exponentBias + 1, NormalFloat::one >> 10, 0);
133     exp = FPBits::maxExponent + 5;
134     ASSERT_EQ(isinf(func(x, exp)), 0);
135     // But if the exp is large enough to oversome than the normalization shift,
136     // then it should result in infinity.
137     exp = FPBits::maxExponent + 15;
138     ASSERT_NE(isinf(func(x, exp)), 0);
139   }
140 };
141 
142 #define LIST_LDEXP_TESTS(T, func)                                              \
143   using LdExpTest = LdExpTestTemplate<T>;                                      \
144   TEST_F(LdExpTest, SpecialNumbers) { testSpecialNumbers(&func); }             \
145   TEST_F(LdExpTest, PowersOfTwo) { testPowersOfTwo(&func); }                   \
146   TEST_F(LdExpTest, OverFlow) { testOverflow(&func); }                         \
147   TEST_F(LdExpTest, UnderflowToZeroOnNormal) {                                 \
148     testUnderflowToZeroOnNormal(&func);                                        \
149   }                                                                            \
150   TEST_F(LdExpTest, UnderflowToZeroOnSubnormal) {                              \
151     testUnderflowToZeroOnSubnormal(&func);                                     \
152   }                                                                            \
153   TEST_F(LdExpTest, NormalOperation) { testNormalOperation(&func); }
154 
155 #endif // LLVM_LIBC_TEST_SRC_MATH_LDEXPTEST_H
156