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