xref: /aosp_15_r20/external/llvm-libc/test/src/math/smoke/LdExpTest.h (revision 71db0c75aadcf003ffe3238005f61d7618a3fead)
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 "src/__support/CPP/limits.h" // INT_MAX
13 #include "src/__support/FPUtil/FPBits.h"
14 #include "src/__support/FPUtil/NormalFloat.h"
15 #include "test/UnitTest/FEnvSafeTest.h"
16 #include "test/UnitTest/FPMatcher.h"
17 #include "test/UnitTest/Test.h"
18 
19 #include <stdint.h>
20 
21 using LIBC_NAMESPACE::Sign;
22 
23 template <typename T, typename U = int>
24 class LdExpTestTemplate : public LIBC_NAMESPACE::testing::FEnvSafeTest {
25   using FPBits = LIBC_NAMESPACE::fputil::FPBits<T>;
26   using NormalFloat = LIBC_NAMESPACE::fputil::NormalFloat<T>;
27   using StorageType = typename FPBits::StorageType;
28 
29   const T inf = FPBits::inf(Sign::POS).get_val();
30   const T neg_inf = FPBits::inf(Sign::NEG).get_val();
31   const T zero = FPBits::zero(Sign::POS).get_val();
32   const T neg_zero = FPBits::zero(Sign::NEG).get_val();
33   const T nan = FPBits::quiet_nan().get_val();
34 
35   // A normalized mantissa to be used with tests.
36   static constexpr StorageType MANTISSA = NormalFloat::ONE + 0x123;
37 
38 public:
39   typedef T (*LdExpFunc)(T, U);
40 
testSpecialNumbers(LdExpFunc func)41   void testSpecialNumbers(LdExpFunc func) {
42     int exp_array[5] = {INT_MIN, -10, 0, 10, INT_MAX};
43     for (int exp : exp_array) {
44       ASSERT_FP_EQ(zero, func(zero, exp));
45       ASSERT_FP_EQ(neg_zero, func(neg_zero, exp));
46       ASSERT_FP_EQ(inf, func(inf, exp));
47       ASSERT_FP_EQ(neg_inf, func(neg_inf, exp));
48       ASSERT_FP_EQ(nan, func(nan, exp));
49     }
50 
51     if constexpr (sizeof(U) < sizeof(long) || sizeof(long) == sizeof(int))
52       return;
53     long long_exp_array[4] = {LONG_MIN, static_cast<long>(INT_MIN - 1LL),
54                               static_cast<long>(INT_MAX + 1LL), LONG_MAX};
55     for (long exp : long_exp_array) {
56       ASSERT_FP_EQ(zero, func(zero, exp));
57       ASSERT_FP_EQ(neg_zero, func(neg_zero, exp));
58       ASSERT_FP_EQ(inf, func(inf, exp));
59       ASSERT_FP_EQ(neg_inf, func(neg_inf, exp));
60       ASSERT_FP_EQ(nan, func(nan, exp));
61     }
62   }
63 
testPowersOfTwo(LdExpFunc func)64   void testPowersOfTwo(LdExpFunc func) {
65     int32_t exp_array[5] = {1, 2, 3, 4, 5};
66     int32_t val_array[6] = {1, 2, 4, 8, 16, 32};
67     for (int32_t exp : exp_array) {
68       for (int32_t val : val_array) {
69         ASSERT_FP_EQ(T(val << exp), func(T(val), exp));
70         ASSERT_FP_EQ(T(-1 * (val << exp)), func(T(-val), exp));
71       }
72     }
73   }
74 
testOverflow(LdExpFunc func)75   void testOverflow(LdExpFunc func) {
76     NormalFloat x(Sign::POS, FPBits::MAX_BIASED_EXPONENT - 10,
77                   NormalFloat::ONE + 0xFB);
78     for (int32_t exp = 10; exp < 100; ++exp) {
79       ASSERT_FP_EQ(inf, func(T(x), exp));
80       ASSERT_FP_EQ(neg_inf, func(-T(x), exp));
81     }
82   }
83 
testUnderflowToZeroOnNormal(LdExpFunc func)84   void testUnderflowToZeroOnNormal(LdExpFunc func) {
85     // In this test, we pass a normal nubmer to func and expect zero
86     // to be returned due to underflow.
87     int32_t base_exponent = FPBits::EXP_BIAS + FPBits::FRACTION_LEN;
88     int32_t exp_array[] = {base_exponent + 5, base_exponent + 4,
89                            base_exponent + 3, base_exponent + 2,
90                            base_exponent + 1};
91     T x = NormalFloat(Sign::POS, 0, MANTISSA);
92     for (int32_t exp : exp_array) {
93       ASSERT_FP_EQ(func(x, -exp), x > 0 ? zero : neg_zero);
94     }
95   }
96 
testUnderflowToZeroOnSubnormal(LdExpFunc func)97   void testUnderflowToZeroOnSubnormal(LdExpFunc func) {
98     // In this test, we pass a normal nubmer to func and expect zero
99     // to be returned due to underflow.
100     int32_t base_exponent = FPBits::EXP_BIAS + FPBits::FRACTION_LEN;
101     int32_t exp_array[] = {base_exponent + 5, base_exponent + 4,
102                            base_exponent + 3, base_exponent + 2,
103                            base_exponent + 1};
104     T x = NormalFloat(Sign::POS, -FPBits::EXP_BIAS, MANTISSA);
105     for (int32_t exp : exp_array) {
106       ASSERT_FP_EQ(func(x, -exp), x > 0 ? zero : neg_zero);
107     }
108   }
109 
testNormalOperation(LdExpFunc func)110   void testNormalOperation(LdExpFunc func) {
111     T val_array[] = {// Normal numbers
112                      NormalFloat(Sign::POS, 10, MANTISSA),
113                      NormalFloat(Sign::POS, -10, MANTISSA),
114                      NormalFloat(Sign::NEG, 10, MANTISSA),
115                      NormalFloat(Sign::NEG, -10, MANTISSA),
116                      // Subnormal numbers
117                      NormalFloat(Sign::POS, -FPBits::EXP_BIAS, MANTISSA),
118                      NormalFloat(Sign::NEG, -FPBits::EXP_BIAS, MANTISSA)};
119     for (int32_t exp = 0; exp <= FPBits::FRACTION_LEN; ++exp) {
120       for (T x : val_array) {
121         // We compare the result of ldexp with the result
122         // of the native multiplication/division instruction.
123 
124         // We need to use a NormalFloat here (instead of 1 << exp), because
125         // there are 32 bit systems that don't support 128bit long ints but
126         // support long doubles. This test can do 1 << 64, which would fail
127         // in these systems.
128         NormalFloat two_to_exp = NormalFloat(static_cast<T>(1.L));
129         two_to_exp = two_to_exp.mul2(exp);
130 
131         ASSERT_FP_EQ(func(x, exp), x * static_cast<T>(two_to_exp));
132         ASSERT_FP_EQ(func(x, -exp), x / static_cast<T>(two_to_exp));
133       }
134     }
135 
136     // Normal which trigger mantissa overflow.
137     T x = NormalFloat(Sign::POS, -FPBits::EXP_BIAS + 1,
138                       StorageType(2) * NormalFloat::ONE - StorageType(1));
139     ASSERT_FP_EQ(func(x, -1), x / 2);
140     ASSERT_FP_EQ(func(-x, -1), -x / 2);
141 
142     // Start with a normal number high exponent but pass a very low number for
143     // exp. The result should be a subnormal number.
144     x = NormalFloat(Sign::POS, FPBits::EXP_BIAS, NormalFloat::ONE);
145     int exp = -FPBits::MAX_BIASED_EXPONENT - 5;
146     T result = func(x, exp);
147     FPBits result_bits(result);
148     ASSERT_FALSE(result_bits.is_zero());
149     // Verify that the result is indeed subnormal.
150     ASSERT_EQ(result_bits.get_biased_exponent(), uint16_t(0));
151     // But if the exp is so less that normalization leads to zero, then
152     // the result should be zero.
153     result = func(x, -FPBits::MAX_BIASED_EXPONENT - FPBits::FRACTION_LEN - 5);
154     ASSERT_TRUE(FPBits(result).is_zero());
155 
156     // Start with a subnormal number but pass a very high number for exponent.
157     // The result should not be infinity.
158     x = NormalFloat(Sign::POS, -FPBits::EXP_BIAS + 1, NormalFloat::ONE >> 10);
159     exp = FPBits::MAX_BIASED_EXPONENT + 5;
160     ASSERT_FALSE(FPBits(func(x, exp)).is_inf());
161     // But if the exp is large enough to oversome than the normalization shift,
162     // then it should result in infinity.
163     exp = FPBits::MAX_BIASED_EXPONENT + 15;
164     ASSERT_FP_EQ(func(x, exp), inf);
165   }
166 };
167 
168 #define LIST_LDEXP_TESTS(T, func)                                              \
169   using LlvmLibcLdExpTest = LdExpTestTemplate<T>;                              \
170   TEST_F(LlvmLibcLdExpTest, SpecialNumbers) { testSpecialNumbers(&func); }     \
171   TEST_F(LlvmLibcLdExpTest, PowersOfTwo) { testPowersOfTwo(&func); }           \
172   TEST_F(LlvmLibcLdExpTest, OverFlow) { testOverflow(&func); }                 \
173   TEST_F(LlvmLibcLdExpTest, UnderflowToZeroOnNormal) {                         \
174     testUnderflowToZeroOnNormal(&func);                                        \
175   }                                                                            \
176   TEST_F(LlvmLibcLdExpTest, UnderflowToZeroOnSubnormal) {                      \
177     testUnderflowToZeroOnSubnormal(&func);                                     \
178   }                                                                            \
179   TEST_F(LlvmLibcLdExpTest, NormalOperation) { testNormalOperation(&func); }   \
180   static_assert(true)
181 
182 #endif // LLVM_LIBC_TEST_SRC_MATH_LDEXPTEST_H
183