xref: /llvm-project/libc/test/src/math/LdExpTest.h (revision f6b2a222beed734df1d91bd5c165a5233c19fddb)
1bb8f2585SSiva Chandra Reddy //===-- Utility class to test different flavors of ldexp --------*- C++ -*-===//
2bb8f2585SSiva Chandra Reddy //
3bb8f2585SSiva Chandra Reddy // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4bb8f2585SSiva Chandra Reddy // See https://llvm.org/LICENSE.txt for license information.
5bb8f2585SSiva Chandra Reddy // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6bb8f2585SSiva Chandra Reddy //
7bb8f2585SSiva Chandra Reddy //===----------------------------------------------------------------------===//
8bb8f2585SSiva Chandra Reddy 
9bb8f2585SSiva Chandra Reddy #ifndef LLVM_LIBC_TEST_SRC_MATH_LDEXPTEST_H
10bb8f2585SSiva Chandra Reddy #define LLVM_LIBC_TEST_SRC_MATH_LDEXPTEST_H
11bb8f2585SSiva Chandra Reddy 
1272ce6294Slntue #include "src/__support/CPP/limits.h" // INT_MAX
13c120edc7SMichael Jones #include "src/__support/FPUtil/FPBits.h"
14c120edc7SMichael Jones #include "src/__support/FPUtil/NormalFloat.h"
15837dab96SRoland McGrath #include "test/UnitTest/FEnvSafeTest.h"
16af1315c2SSiva Chandra Reddy #include "test/UnitTest/FPMatcher.h"
17af1315c2SSiva Chandra Reddy #include "test/UnitTest/Test.h"
18bb8f2585SSiva Chandra Reddy 
195748ad84Slntue #include "hdr/math_macros.h"
20bb8f2585SSiva Chandra Reddy #include <stdint.h>
21bb8f2585SSiva Chandra Reddy 
22*f6b2a222SMichael Jones using LIBC_NAMESPACE::Sign;
23*f6b2a222SMichael Jones 
24bb8f2585SSiva Chandra Reddy template <typename T>
25837dab96SRoland McGrath class LdExpTestTemplate : public LIBC_NAMESPACE::testing::FEnvSafeTest {
26b6bc9d72SGuillaume Chatelet   using FPBits = LIBC_NAMESPACE::fputil::FPBits<T>;
27b6bc9d72SGuillaume Chatelet   using NormalFloat = LIBC_NAMESPACE::fputil::NormalFloat<T>;
283546f4daSGuillaume Chatelet   using StorageType = typename FPBits::StorageType;
2911ec512fSGuillaume Chatelet 
302856db0dSGuillaume Chatelet   const T inf = FPBits::inf(Sign::POS).get_val();
312856db0dSGuillaume Chatelet   const T neg_inf = FPBits::inf(Sign::NEG).get_val();
322856db0dSGuillaume Chatelet   const T zero = FPBits::zero(Sign::POS).get_val();
332856db0dSGuillaume Chatelet   const T neg_zero = FPBits::zero(Sign::NEG).get_val();
34ace383dfSGuillaume Chatelet   const T nan = FPBits::quiet_nan().get_val();
3511ec512fSGuillaume Chatelet 
36bb8f2585SSiva Chandra Reddy   // A normalized mantissa to be used with tests.
373546f4daSGuillaume Chatelet   static constexpr StorageType MANTISSA = NormalFloat::ONE + 0x1234;
38bb8f2585SSiva Chandra Reddy 
39bb8f2585SSiva Chandra Reddy public:
40bb8f2585SSiva Chandra Reddy   typedef T (*LdExpFunc)(T, int);
41bb8f2585SSiva Chandra Reddy 
42bb8f2585SSiva Chandra Reddy   void testSpecialNumbers(LdExpFunc func) {
4325226f3eSMichael Jones     int exp_array[5] = {-INT_MAX - 1, -10, 0, 10, INT_MAX};
4425226f3eSMichael Jones     for (int exp : exp_array) {
45bb8f2585SSiva Chandra Reddy       ASSERT_FP_EQ(zero, func(zero, exp));
461c92911eSMichael Jones       ASSERT_FP_EQ(neg_zero, func(neg_zero, exp));
47bb8f2585SSiva Chandra Reddy       ASSERT_FP_EQ(inf, func(inf, exp));
481c92911eSMichael Jones       ASSERT_FP_EQ(neg_inf, func(neg_inf, exp));
490524da67SSiva Chandra Reddy       ASSERT_FP_EQ(nan, func(nan, exp));
50bb8f2585SSiva Chandra Reddy     }
51bb8f2585SSiva Chandra Reddy   }
52bb8f2585SSiva Chandra Reddy 
53bb8f2585SSiva Chandra Reddy   void testPowersOfTwo(LdExpFunc func) {
5425226f3eSMichael Jones     int32_t exp_array[5] = {1, 2, 3, 4, 5};
5525226f3eSMichael Jones     int32_t val_array[6] = {1, 2, 4, 8, 16, 32};
5625226f3eSMichael Jones     for (int32_t exp : exp_array) {
5725226f3eSMichael Jones       for (int32_t val : val_array) {
58bb8f2585SSiva Chandra Reddy         ASSERT_FP_EQ(T(val << exp), func(T(val), exp));
59bb8f2585SSiva Chandra Reddy         ASSERT_FP_EQ(T(-1 * (val << exp)), func(T(-val), exp));
60bb8f2585SSiva Chandra Reddy       }
61bb8f2585SSiva Chandra Reddy     }
62bb8f2585SSiva Chandra Reddy   }
63bb8f2585SSiva Chandra Reddy 
64bb8f2585SSiva Chandra Reddy   void testOverflow(LdExpFunc func) {
65508c4efeSGuillaume Chatelet     NormalFloat x(Sign::POS, FPBits::MAX_BIASED_EXPONENT - 10,
66508c4efeSGuillaume Chatelet                   NormalFloat::ONE + 0xF00BA);
67bb8f2585SSiva Chandra Reddy     for (int32_t exp = 10; exp < 100; ++exp) {
68bb8f2585SSiva Chandra Reddy       ASSERT_FP_EQ(inf, func(T(x), exp));
691c92911eSMichael Jones       ASSERT_FP_EQ(neg_inf, func(-T(x), exp));
70bb8f2585SSiva Chandra Reddy     }
71bb8f2585SSiva Chandra Reddy   }
72bb8f2585SSiva Chandra Reddy 
73bb8f2585SSiva Chandra Reddy   void testUnderflowToZeroOnNormal(LdExpFunc func) {
74bb8f2585SSiva Chandra Reddy     // In this test, we pass a normal nubmer to func and expect zero
75bb8f2585SSiva Chandra Reddy     // to be returned due to underflow.
763546f4daSGuillaume Chatelet     int32_t base_exponent = FPBits::EXP_BIAS + FPBits::FRACTION_LEN;
7725226f3eSMichael Jones     int32_t exp_array[] = {base_exponent + 5, base_exponent + 4,
7825226f3eSMichael Jones                            base_exponent + 3, base_exponent + 2,
7925226f3eSMichael Jones                            base_exponent + 1};
80508c4efeSGuillaume Chatelet     T x = NormalFloat(Sign::POS, 0, MANTISSA);
8125226f3eSMichael Jones     for (int32_t exp : exp_array) {
821c92911eSMichael Jones       ASSERT_FP_EQ(func(x, -exp), x > 0 ? zero : neg_zero);
83bb8f2585SSiva Chandra Reddy     }
84bb8f2585SSiva Chandra Reddy   }
85bb8f2585SSiva Chandra Reddy 
86bb8f2585SSiva Chandra Reddy   void testUnderflowToZeroOnSubnormal(LdExpFunc func) {
87bb8f2585SSiva Chandra Reddy     // In this test, we pass a normal nubmer to func and expect zero
88bb8f2585SSiva Chandra Reddy     // to be returned due to underflow.
893546f4daSGuillaume Chatelet     int32_t base_exponent = FPBits::EXP_BIAS + FPBits::FRACTION_LEN;
9025226f3eSMichael Jones     int32_t exp_array[] = {base_exponent + 5, base_exponent + 4,
9125226f3eSMichael Jones                            base_exponent + 3, base_exponent + 2,
9225226f3eSMichael Jones                            base_exponent + 1};
93508c4efeSGuillaume Chatelet     T x = NormalFloat(Sign::POS, -FPBits::EXP_BIAS, MANTISSA);
9425226f3eSMichael Jones     for (int32_t exp : exp_array) {
951c92911eSMichael Jones       ASSERT_FP_EQ(func(x, -exp), x > 0 ? zero : neg_zero);
96bb8f2585SSiva Chandra Reddy     }
97bb8f2585SSiva Chandra Reddy   }
98bb8f2585SSiva Chandra Reddy 
99bb8f2585SSiva Chandra Reddy   void testNormalOperation(LdExpFunc func) {
100508c4efeSGuillaume Chatelet     T val_array[] = {// Normal numbers
101508c4efeSGuillaume Chatelet                      NormalFloat(Sign::POS, 100, MANTISSA),
102508c4efeSGuillaume Chatelet                      NormalFloat(Sign::POS, -100, MANTISSA),
103508c4efeSGuillaume Chatelet                      NormalFloat(Sign::NEG, 100, MANTISSA),
104508c4efeSGuillaume Chatelet                      NormalFloat(Sign::NEG, -100, MANTISSA),
105bb8f2585SSiva Chandra Reddy                      // Subnormal numbers
106508c4efeSGuillaume Chatelet                      NormalFloat(Sign::POS, -FPBits::EXP_BIAS, MANTISSA),
107508c4efeSGuillaume Chatelet                      NormalFloat(Sign::NEG, -FPBits::EXP_BIAS, MANTISSA)};
1083546f4daSGuillaume Chatelet     for (int32_t exp = 0; exp <= FPBits::FRACTION_LEN; ++exp) {
10925226f3eSMichael Jones       for (T x : val_array) {
110bb8f2585SSiva Chandra Reddy         // We compare the result of ldexp with the result
111bb8f2585SSiva Chandra Reddy         // of the native multiplication/division instruction.
112a2df87c2SMikhail R. Gadelha 
113a2df87c2SMikhail R. Gadelha         // We need to use a NormalFloat here (instead of 1 << exp), because
114a2df87c2SMikhail R. Gadelha         // there are 32 bit systems that don't support 128bit long ints but
115a2df87c2SMikhail R. Gadelha         // support long doubles. This test can do 1 << 64, which would fail
116a2df87c2SMikhail R. Gadelha         // in these systems.
117a2df87c2SMikhail R. Gadelha         NormalFloat two_to_exp = NormalFloat(static_cast<T>(1.L));
118a2df87c2SMikhail R. Gadelha         two_to_exp = two_to_exp.mul2(exp);
119a2df87c2SMikhail R. Gadelha 
120a2df87c2SMikhail R. Gadelha         ASSERT_FP_EQ(func(x, exp), x * two_to_exp);
121a2df87c2SMikhail R. Gadelha         ASSERT_FP_EQ(func(x, -exp), x / two_to_exp);
122bb8f2585SSiva Chandra Reddy       }
123bb8f2585SSiva Chandra Reddy     }
124bb8f2585SSiva Chandra Reddy 
125bb8f2585SSiva Chandra Reddy     // Normal which trigger mantissa overflow.
126508c4efeSGuillaume Chatelet     T x = NormalFloat(Sign::POS, -FPBits::EXP_BIAS + 1,
127508c4efeSGuillaume Chatelet                       StorageType(2) * NormalFloat::ONE - StorageType(1));
128bb8f2585SSiva Chandra Reddy     ASSERT_FP_EQ(func(x, -1), x / 2);
129bb8f2585SSiva Chandra Reddy     ASSERT_FP_EQ(func(-x, -1), -x / 2);
1304d8dede5SSiva Chandra Reddy 
1314d8dede5SSiva Chandra Reddy     // Start with a normal number high exponent but pass a very low number for
1324d8dede5SSiva Chandra Reddy     // exp. The result should be a subnormal number.
133508c4efeSGuillaume Chatelet     x = NormalFloat(Sign::POS, FPBits::EXP_BIAS, NormalFloat::ONE);
1343ae5a9b6SGuillaume Chatelet     int exp = -FPBits::MAX_BIASED_EXPONENT - 5;
1354d8dede5SSiva Chandra Reddy     T result = func(x, exp);
13625226f3eSMichael Jones     FPBits result_bits(result);
13725226f3eSMichael Jones     ASSERT_FALSE(result_bits.is_zero());
1384d8dede5SSiva Chandra Reddy     // Verify that the result is indeed subnormal.
1397b387d27SGuillaume Chatelet     ASSERT_EQ(result_bits.get_biased_exponent(), uint16_t(0));
1404d8dede5SSiva Chandra Reddy     // But if the exp is so less that normalization leads to zero, then
1414d8dede5SSiva Chandra Reddy     // the result should be zero.
1423ae5a9b6SGuillaume Chatelet     result = func(x, -FPBits::MAX_BIASED_EXPONENT - FPBits::FRACTION_LEN - 5);
1431c92911eSMichael Jones     ASSERT_TRUE(FPBits(result).is_zero());
1444d8dede5SSiva Chandra Reddy 
1454d8dede5SSiva Chandra Reddy     // Start with a subnormal number but pass a very high number for exponent.
1464d8dede5SSiva Chandra Reddy     // The result should not be infinity.
147508c4efeSGuillaume Chatelet     x = NormalFloat(Sign::POS, -FPBits::EXP_BIAS + 1, NormalFloat::ONE >> 10);
1483ae5a9b6SGuillaume Chatelet     exp = FPBits::MAX_BIASED_EXPONENT + 5;
1491c92911eSMichael Jones     ASSERT_FALSE(FPBits(func(x, exp)).is_inf());
1504d8dede5SSiva Chandra Reddy     // But if the exp is large enough to oversome than the normalization shift,
1514d8dede5SSiva Chandra Reddy     // then it should result in infinity.
1523ae5a9b6SGuillaume Chatelet     exp = FPBits::MAX_BIASED_EXPONENT + 15;
1530c8466c0SSiva Chandra Reddy     ASSERT_FP_EQ(func(x, exp), inf);
154bb8f2585SSiva Chandra Reddy   }
155bb8f2585SSiva Chandra Reddy };
156bb8f2585SSiva Chandra Reddy 
157bb8f2585SSiva Chandra Reddy #define LIST_LDEXP_TESTS(T, func)                                              \
1581df0dbfcSMichael Jones   using LlvmLibcLdExpTest = LdExpTestTemplate<T>;                              \
1591df0dbfcSMichael Jones   TEST_F(LlvmLibcLdExpTest, SpecialNumbers) { testSpecialNumbers(&func); }     \
1601df0dbfcSMichael Jones   TEST_F(LlvmLibcLdExpTest, PowersOfTwo) { testPowersOfTwo(&func); }           \
1611df0dbfcSMichael Jones   TEST_F(LlvmLibcLdExpTest, OverFlow) { testOverflow(&func); }                 \
1621df0dbfcSMichael Jones   TEST_F(LlvmLibcLdExpTest, UnderflowToZeroOnNormal) {                         \
163bb8f2585SSiva Chandra Reddy     testUnderflowToZeroOnNormal(&func);                                        \
164bb8f2585SSiva Chandra Reddy   }                                                                            \
1651df0dbfcSMichael Jones   TEST_F(LlvmLibcLdExpTest, UnderflowToZeroOnSubnormal) {                      \
166bb8f2585SSiva Chandra Reddy     testUnderflowToZeroOnSubnormal(&func);                                     \
167bb8f2585SSiva Chandra Reddy   }                                                                            \
1681df0dbfcSMichael Jones   TEST_F(LlvmLibcLdExpTest, NormalOperation) { testNormalOperation(&func); }
169bb8f2585SSiva Chandra Reddy 
170bb8f2585SSiva Chandra Reddy #endif // LLVM_LIBC_TEST_SRC_MATH_LDEXPTEST_H
171