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