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/FPUtil/FPBits.h" 13 #include "src/__support/FPUtil/NormalFloat.h" 14 #include "utils/UnitTest/FPMatcher.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 MANTISSA_WIDTH = 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 = T(__llvm_libc::fputil::FPBits<T>::zero()); 32 const T neg_zero = T(__llvm_libc::fputil::FPBits<T>::neg_zero()); 33 const T inf = T(__llvm_libc::fputil::FPBits<T>::inf()); 34 const T neg_inf = T(__llvm_libc::fputil::FPBits<T>::neg_inf()); 35 const T nan = T(__llvm_libc::fputil::FPBits<T>::build_nan(1)); 36 37 public: 38 typedef T (*LdExpFunc)(T, int); 39 testSpecialNumbers(LdExpFunc func)40 void testSpecialNumbers(LdExpFunc func) { 41 int exp_array[5] = {-INT_MAX - 1, -10, 0, 10, INT_MAX}; 42 for (int exp : exp_array) { 43 ASSERT_FP_EQ(zero, func(zero, exp)); 44 ASSERT_FP_EQ(neg_zero, func(neg_zero, exp)); 45 ASSERT_FP_EQ(inf, func(inf, exp)); 46 ASSERT_FP_EQ(neg_inf, func(neg_inf, exp)); 47 ASSERT_FP_EQ(nan, func(nan, exp)); 48 } 49 } 50 testPowersOfTwo(LdExpFunc func)51 void testPowersOfTwo(LdExpFunc func) { 52 int32_t exp_array[5] = {1, 2, 3, 4, 5}; 53 int32_t val_array[6] = {1, 2, 4, 8, 16, 32}; 54 for (int32_t exp : exp_array) { 55 for (int32_t val : val_array) { 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::MAX_EXPONENT - 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(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::EXPONENT_BIAS + MANTISSA_WIDTH; 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(0, MANTISSA, 0); 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::EXPONENT_BIAS + MANTISSA_WIDTH; 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(-FPBits::EXPONENT_BIAS, MANTISSA, 0); 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[] = { 98 // Normal numbers 99 NormalFloat(100, MANTISSA, 0), NormalFloat(-100, MANTISSA, 0), 100 NormalFloat(100, MANTISSA, 1), NormalFloat(-100, MANTISSA, 1), 101 // Subnormal numbers 102 NormalFloat(-FPBits::EXPONENT_BIAS, MANTISSA, 0), 103 NormalFloat(-FPBits::EXPONENT_BIAS, MANTISSA, 1)}; 104 for (int32_t exp = 0; exp <= static_cast<int32_t>(MANTISSA_WIDTH); ++exp) { 105 for (T x : val_array) { 106 // We compare the result of ldexp with the result 107 // of the native multiplication/division instruction. 108 ASSERT_FP_EQ(func(x, exp), x * (UIntType(1) << exp)); 109 ASSERT_FP_EQ(func(x, -exp), x / (UIntType(1) << exp)); 110 } 111 } 112 113 // Normal which trigger mantissa overflow. 114 T x = NormalFloat(-FPBits::EXPONENT_BIAS + 1, 2 * NormalFloat::ONE - 1, 0); 115 ASSERT_FP_EQ(func(x, -1), x / 2); 116 ASSERT_FP_EQ(func(-x, -1), -x / 2); 117 118 // Start with a normal number high exponent but pass a very low number for 119 // exp. The result should be a subnormal number. 120 x = NormalFloat(FPBits::EXPONENT_BIAS, NormalFloat::ONE, 0); 121 int exp = -FPBits::MAX_EXPONENT - 5; 122 T result = func(x, exp); 123 FPBits result_bits(result); 124 ASSERT_FALSE(result_bits.is_zero()); 125 // Verify that the result is indeed subnormal. 126 ASSERT_EQ(result_bits.get_unbiased_exponent(), uint16_t(0)); 127 // But if the exp is so less that normalization leads to zero, then 128 // the result should be zero. 129 result = func(x, -FPBits::MAX_EXPONENT - int(MANTISSA_WIDTH) - 5); 130 ASSERT_TRUE(FPBits(result).is_zero()); 131 132 // Start with a subnormal number but pass a very high number for exponent. 133 // The result should not be infinity. 134 x = NormalFloat(-FPBits::EXPONENT_BIAS + 1, NormalFloat::ONE >> 10, 0); 135 exp = FPBits::MAX_EXPONENT + 5; 136 ASSERT_FALSE(FPBits(func(x, exp)).is_inf()); 137 // But if the exp is large enough to oversome than the normalization shift, 138 // then it should result in infinity. 139 exp = FPBits::MAX_EXPONENT + 15; 140 ASSERT_FP_EQ(func(x, exp), inf); 141 } 142 }; 143 144 #define LIST_LDEXP_TESTS(T, func) \ 145 using LlvmLibcLdExpTest = LdExpTestTemplate<T>; \ 146 TEST_F(LlvmLibcLdExpTest, SpecialNumbers) { testSpecialNumbers(&func); } \ 147 TEST_F(LlvmLibcLdExpTest, PowersOfTwo) { testPowersOfTwo(&func); } \ 148 TEST_F(LlvmLibcLdExpTest, OverFlow) { testOverflow(&func); } \ 149 TEST_F(LlvmLibcLdExpTest, UnderflowToZeroOnNormal) { \ 150 testUnderflowToZeroOnNormal(&func); \ 151 } \ 152 TEST_F(LlvmLibcLdExpTest, UnderflowToZeroOnSubnormal) { \ 153 testUnderflowToZeroOnSubnormal(&func); \ 154 } \ 155 TEST_F(LlvmLibcLdExpTest, NormalOperation) { testNormalOperation(&func); } 156 157 #endif // LLVM_LIBC_TEST_SRC_MATH_LDEXPTEST_H 158