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

source code of libc/test/src/math/LdExpTest.h