Skip to content

Commit 2d03f2e

Browse files
committed
bug fix
Bug fixes Ran formatter Updated tests Ran formatter bug fix Bug fixes Minor bug fixes Bug Fixes Add namespace Bug Fix Changed namespace Minor changes Experimenting revert changed type Update tests Added types Update BasicOperations.h Bug Fix Formatter minor changes pushing all tests Formatting revert Bug fix Minor changes changes Formatter Testing changes Added a test Ran formatter Minor push formatter fix Typo Removed bit 61 revamp testing TEst 4 changes added type Formatter Test Bug fix Bug fix Fixed bug Formatter nits nits info RE s s s s TEst TEst Added test 5 F minor changes nits nits bit62 fix testing revert nots fix nits Format fix F test test Buf Fix
1 parent c92d43a commit 2d03f2e

File tree

3 files changed

+144
-107
lines changed

3 files changed

+144
-107
lines changed

libc/src/__support/FPUtil/BasicOperations.h

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -94,13 +94,12 @@ LIBC_INLINE int canonicalize(T &cx, const T &x) {
9494
// Values | | | (−1)**s × m × 2**−16382
9595
bool bit63 = sx.get_implicit_bit();
9696
UInt128 mantissa = sx.get_explicit_mantissa();
97-
bool bit62 = mantissa & (1ULL << 62);
98-
bool bit61 = mantissa & (1ULL << 61);
97+
bool bit62 = ((mantissa & (1ULL << 62)) >> 62);
9998
int exponent = sx.get_biased_exponent();
10099
if (exponent == 0x7FFF) {
101100
if (!bit63 && !bit62) {
102-
if (!bit61)
103-
cx = FPBits<T>::inf().get_val();
101+
if (mantissa == 0)
102+
cx = FPBits<T>::inf(sx.sign()).get_val();
104103
else {
105104
cx = FPBits<T>::quiet_nan(sx.sign(), mantissa).get_val();
106105
raise_except_if_required(FE_INVALID);
@@ -110,12 +109,23 @@ LIBC_INLINE int canonicalize(T &cx, const T &x) {
110109
cx = FPBits<T>::quiet_nan(sx.sign(), mantissa).get_val();
111110
raise_except_if_required(FE_INVALID);
112111
return 1;
113-
}
112+
} else if (LIBC_UNLIKELY(sx.is_signaling_nan())) {
113+
cx = FPBits<T>::quiet_nan(sx.sign(), sx.get_explicit_mantissa())
114+
.get_val();
115+
raise_except_if_required(FE_INVALID);
116+
return 1;
117+
} else
118+
cx = x;
114119
} else if (exponent == 0 && bit63)
115-
cx = FPBits<T>::make_value(mantissa, 1).get_val();
116-
else if (!bit63)
117-
cx = FPBits<T>::make_value(mantissa, 1).get_val();
118-
else
120+
cx = FPBits<T>::make_value(mantissa, 0).get_val();
121+
else if (exponent != 0 && !bit63)
122+
cx = FPBits<T>::make_value(mantissa, 0).get_val();
123+
else if (LIBC_UNLIKELY(sx.is_signaling_nan())) {
124+
cx =
125+
FPBits<T>::quiet_nan(sx.sign(), sx.get_explicit_mantissa()).get_val();
126+
raise_except_if_required(FE_INVALID);
127+
return 1;
128+
} else
119129
cx = x;
120130
} else if (LIBC_UNLIKELY(sx.is_signaling_nan())) {
121131
cx = FPBits<T>::quiet_nan(sx.sign(), sx.get_explicit_mantissa()).get_val();

libc/src/__support/FPUtil/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ add_header_library(
8686
libc.src.__support.CPP.type_traits
8787
libc.src.__support.uint128
8888
libc.src.__support.common
89-
libc.src.__support.macros.optimization.h
89+
libc.src.__support.macros.optimization
9090
)
9191

9292
add_header_library(

libc/test/src/math/smoke/CanonicalizeTest.h

Lines changed: 124 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -16,162 +16,189 @@
1616
#include "include/llvm-libc-macros/math-macros.h"
1717

1818
#define TEST_SPECIAL(x, y, expected, expected_exception) \
19-
EXPECT_FP_EQ(expected, f(&x, &y)); \
19+
EXPECT_EQ(expected, f(&x, &y)); \
2020
EXPECT_FP_EXCEPTION(expected_exception); \
2121
LIBC_NAMESPACE::fputil::clear_except(FE_ALL_EXCEPT)
2222

2323
#define TEST_REGULAR(x, y, expected) TEST_SPECIAL(x, y, expected, 0)
2424

25+
#define LIBC_NAMESPACE __llvm_libc_19_0_0_git
26+
2527
template <typename T>
2628
class CanonicalizeTest : public LIBC_NAMESPACE::testing::Test {
2729

2830
DECLARE_SPECIAL_CONSTANTS(T)
2931

3032
public:
31-
typedef T (*CanonicalizeFunc)(T *, T *);
32-
using FPBits = LIBC_NAMESPACE::fputil::FPBits<T>;
33-
using StorageType = typename FPBits::StorageType;
33+
typedef int (*CanonicalizeFunc)(T *, const T *);
3434

3535
void testSpecialNumbers(CanonicalizeFunc f) {
3636
T cx;
37+
3738
TEST_SPECIAL(cx, zero, 0, 0);
38-
EXPECT_EQ(cx, T(0.0));
39+
EXPECT_FP_EQ(cx, zero);
40+
3941
TEST_SPECIAL(cx, neg_zero, 0, 0);
40-
EXPECT_EQ(cx, T(-0.0));
42+
EXPECT_FP_EQ(cx, neg_zero);
43+
4144
TEST_SPECIAL(cx, inf, 0, 0);
42-
EXPECT_EQ(cx, inf);
45+
EXPECT_FP_EQ(cx, inf);
46+
4347
TEST_SPECIAL(cx, neg_inf, 0, 0);
44-
EXPECT_EQ(cx, neg_inf);
48+
EXPECT_FP_EQ(cx, neg_inf);
49+
4550
TEST_SPECIAL(cx, sNaN, 1, FE_INVALID);
46-
EXPECT_EQ(cx, aNaN);
47-
TEST_SPECIAL(cx, -sNaN, 1, FE_INVALID);
48-
EXPECT_EQ(cx, -aNaN);
51+
EXPECT_FP_EQ(cx, aNaN);
4952
}
5053

5154
void testX64_80SpecialNumbers(CanonicalizeFunc f) {
52-
if constexpr (LIBC_NAMESPACE::fputil::get_fp_type() ==
53-
FPType::X86_Binary80) {
55+
if constexpr (LIBC_NAMESPACE::fputil::get_fp_type<T>() ==
56+
LIBC_NAMESPACE::fputil::FPType::X86_Binary80) {
5457
T cx;
5558
// Exponent | Significand | Meaning
5659
// | Bits 63-62 | Bits 61-0 |
5760
// All Ones | 00 | Zero | Pseudo Infinity, Value = Infinty
5861

59-
FPBits test1(UInt128(0x7FFF) << 64 + UInt128(0x0000000000000000));
60-
TEST_SPECIAL(cx, test1.get_val(), 0, 0);
61-
EXPECT_EQ(cx, inf);
62+
FPBits test1((UInt128(0x7FFF) << 64) + UInt128(0x0000000000000000));
63+
const T test1_val = test1.get_val();
64+
TEST_SPECIAL(cx, test1_val, 0, 0);
65+
EXPECT_FP_EQ(cx, inf);
6266

6367
// Exponent | Significand | Meaning
6468
// | Bits 63-62 | Bits 61-0 |
6569
// All Ones | 00 | Non-Zero | Pseudo NaN, Value = SNaN
6670

67-
FPBits test2_1(UInt128(0x7FFF) << 64 + UInt128(0x0000000000000001));
68-
TEST_SPECIAL(cx, test2_1.get_val(), 1, FE_INVALID);
69-
EXPECT_EQ(cx, aNaN);
71+
FPBits test2_1((UInt128(0x7FFF) << 64) + UInt128(0x0000000000000001));
72+
const T test2_1_val = test2_1.get_val();
73+
TEST_SPECIAL(cx, test2_1_val, 1, FE_INVALID);
74+
EXPECT_FP_EQ(cx, aNaN);
7075

71-
FPBits test2_2(UInt128(0x7FFF) << 64 + UInt128(0x0000004270000001));
72-
TEST_SPECIAL(cx, test2_2.get_val(), 1, FE_INVALID);
73-
EXPECT_EQ(cx, aNaN);
76+
FPBits test2_2((UInt128(0x7FFF) << 64) + UInt128(0x0000004270000001));
77+
const T test2_2_val = test2_2.get_val();
78+
TEST_SPECIAL(cx, test2_2_val, 1, FE_INVALID);
79+
EXPECT_FP_EQ(cx, aNaN);
7480

75-
FPBits test2_3(UInt128(0x7FFF) << 64 + UInt128(0x0000000008261001));
76-
TEST_SPECIAL(cx, test2_3.get_val(), 1, FE_INVALID);
77-
EXPECT_EQ(cx, aNaN);
81+
FPBits test2_3((UInt128(0x7FFF) << 64) + UInt128(0x0000000008261001));
82+
const T test2_3_val = test2_3.get_val();
83+
TEST_SPECIAL(cx, test2_3_val, 1, FE_INVALID);
84+
EXPECT_FP_EQ(cx, aNaN);
85+
86+
FPBits test2_4((UInt128(0x7FFF) << 64) + UInt128(0x0000780008261001));
87+
const T test2_4_val = test2_4.get_val();
88+
TEST_SPECIAL(cx, test2_4_val, 1, FE_INVALID);
89+
EXPECT_FP_EQ(cx, aNaN);
7890

7991
// Exponent | Significand | Meaning
8092
// | Bits 63-62 | Bits 61-0 |
8193
// All Ones | 01 | Anything | Pseudo NaN, Value = SNaN
8294

83-
FPBits test3_1(UInt128(0x7FFF) << 64 + UInt128(0x4000000000000000));
84-
TEST_SPECIAL(cx, test3_1.get_val(), 1, FE_INVALID);
85-
EXPECT_EQ(cx, aNaN);
95+
FPBits test3_1((UInt128(0x7FFF) << 64) + UInt128(0x4000000000000000));
96+
const T test3_1_val = test3_1.get_val();
97+
TEST_SPECIAL(cx, test3_1_val, 1, FE_INVALID);
98+
EXPECT_FP_EQ(cx, aNaN);
99+
100+
FPBits test3_2((UInt128(0x7FFF) << 64) + UInt128(0x4000004270000001));
101+
const T test3_2_val = test3_2.get_val();
102+
TEST_SPECIAL(cx, test3_2_val, 1, FE_INVALID);
103+
EXPECT_FP_EQ(cx, aNaN);
86104

87-
FPBits test3_2(UInt128(0x7FFF) << 64 + UInt128(0x4000004270000001));
88-
TEST_SPECIAL(cx, test3_2.get_val(), 1, FE_INVALID);
89-
EXPECT_EQ(cx, aNaN);
105+
FPBits test3_3((UInt128(0x7FFF) << 64) + UInt128(0x4000000008261001));
106+
const T test3_3_val = test3_3.get_val();
107+
TEST_SPECIAL(cx, test3_3_val, 1, FE_INVALID);
108+
EXPECT_FP_EQ(cx, aNaN);
90109

91-
FPBits test3_3(UInt128(0x7FFF) << 64 + UInt128(0x4000000008261001));
92-
TEST_SPECIAL(cx, test3_3.get_val(), 1, FE_INVALID);
93-
EXPECT_EQ(cx, aNaN);
110+
FPBits test3_4((UInt128(0x7FFF) << 64) + UInt128(0x4007800008261001));
111+
const T test3_4_val = test3_4.get_val();
112+
TEST_SPECIAL(cx, test3_4_val, 1, FE_INVALID);
113+
EXPECT_FP_EQ(cx, aNaN);
94114

95115
// Exponent | Significand | Meaning
96116
// | Bit 63 | Bits 62-0 |
97117
// All zeroes | One | Anything | Pseudo Denormal, Value =
98118
// | | | (−1)**s × m × 2**−16382
99119

100-
FPBits test4_1(UInt128(0x0000) << 64 + UInt128(0x8000000000000000));
101-
TEST_SPECIAL(cx, test4_1.get_val(), 0, 0);
102-
EXPECT_EQ(
103-
cx,
104-
FPBits::make_value(test4_1.get_explicit_mantissa(), 1).get_val(););
120+
FPBits test4_1((UInt128(0x0000) << 64) + UInt128(0x8000000000000000));
121+
const T test4_1_val = test4_1.get_val();
122+
TEST_SPECIAL(cx, test4_1_val, 0, 0);
123+
EXPECT_FP_EQ(
124+
cx, FPBits::make_value(test4_1.get_explicit_mantissa(), 0).get_val());
105125

106-
FPBits test4_2(UInt128(0x0000) << 64 + UInt128(0x8000004270000001));
107-
TEST_SPECIAL(cx, test4_2.get_val(), 0, 0);
108-
EXPECT_EQ(
109-
cx,
110-
FPBits::make_value(test4_2.get_explicit_mantissa(), 1).get_val(););
126+
FPBits test4_2((UInt128(0x0000) << 64) + UInt128(0x8000004270000001));
127+
const T test4_2_val = test4_2.get_val();
128+
TEST_SPECIAL(cx, test4_2_val, 0, 0);
129+
EXPECT_FP_EQ(
130+
cx, FPBits::make_value(test4_2.get_explicit_mantissa(), 0).get_val());
111131

112-
FPBits test4_3(UInt128(0x0000) << 64 + UInt128(0x8000000008261001));
113-
TEST_SPECIAL(cx, test4_3.get_val(), 0, 0);
114-
EXPECT_EQ(
115-
cx,
116-
FPBits::make_value(test4_3.get_explicit_mantissa(), 1).get_val(););
132+
FPBits test4_3((UInt128(0x0000) << 64) + UInt128(0x8000000008261001));
133+
const T test4_3_val = test4_3.get_val();
134+
TEST_SPECIAL(cx, test4_3_val, 0, 0);
135+
EXPECT_FP_EQ(
136+
cx, FPBits::make_value(test4_3.get_explicit_mantissa(), 0).get_val());
117137

118138
// Exponent | Significand | Meaning
119139
// | Bit 63 | Bits 62-0 |
120140
// All Other | Zero | Anything | Unnormal, Value =
121141
// Values | | | (−1)**s × m × 2**−16382
122142

123-
FPBits test5_1(UInt128(0x0001) << 64 + UInt128(0x0000000000000000));
124-
TEST_SPECIAL(cx, test5_1.get_val(), 0, 0);
125-
EXPECT_EQ(
126-
cx,
127-
FPBits::make_value(test5_1.get_explicit_mantissa(), 1).get_val(););
128-
129-
FPBits test5_2(UInt128(0x0001) << 64 + UInt128(0x0000004270000001));
130-
TEST_SPECIAL(cx, test5_2.get_val(), 0, 0);
131-
EXPECT_EQ(
132-
cx,
133-
FPBits::make_value(test5_2.get_explicit_mantissa(), 1).get_val(););
134-
135-
FPBits test5_3(UInt128(0x0001) << 64 + UInt128(0x0000000008261001));
136-
TEST_SPECIAL(cx, test5_3.get_val(), 0, 0);
137-
EXPECT_EQ(
138-
cx,
139-
FPBits::make_value(test5_3.get_explicit_mantissa(), 1).get_val(););
140-
141-
FPBits test5_4(UInt128(0x0012) << 64 + UInt128(0x0000000000000000));
142-
TEST_SPECIAL(cx, test5_4.get_val(), 0, 0);
143-
EXPECT_EQ(
144-
cx,
145-
FPBits::make_value(test5_4.get_explicit_mantissa(), 1).get_val(););
146-
147-
FPBits test5_5(UInt128(0x0027) << 64 + UInt128(0x0000004270000001));
148-
TEST_SPECIAL(cx, test5_5.get_val(), 0, 0);
149-
EXPECT_EQ(
150-
cx,
151-
FPBits::make_value(test5_5.get_explicit_mantissa(), 1).get_val(););
152-
153-
FPBits test5_6(UInt128(0x0034) << 64 + UInt128(0x0000000008261001));
154-
TEST_SPECIAL(cx, test5_6.get_val(), 0, 0);
155-
EXPECT_EQ(
156-
cx,
157-
FPBits::make_value(test5_6.get_explicit_mantissa(), 1).get_val(););
143+
FPBits test5_1(UInt128(0x0000000000000001));
144+
int exponent = test5_1.get_biased_exponent();
145+
const T test5_1_val = test5_1.get_val();
146+
TEST_SPECIAL(cx, test5_1_val, 0, 0);
147+
EXPECT_FP_EQ(
148+
cx, FPBits::make_value(test5_1.get_explicit_mantissa(), 0).get_val());
149+
150+
FPBits test5_2(UInt128(0x0000004270000001));
151+
const T test5_2_val = test5_2.get_val();
152+
TEST_SPECIAL(cx, test5_2_val, 0, 0);
153+
EXPECT_FP_EQ(
154+
cx, FPBits::make_value(test5_2.get_explicit_mantissa(), 0).get_val());
155+
156+
FPBits test5_3(UInt128(0x0000000008261001));
157+
const T test5_3_val = test5_3.get_val();
158+
TEST_SPECIAL(cx, test5_3_val, 0, 0);
159+
EXPECT_FP_EQ(
160+
cx, FPBits::make_value(test5_3.get_explicit_mantissa(), 0).get_val());
161+
162+
FPBits test5_4(UInt128(0x0000002816000000));
163+
const T test5_4_val = test5_4.get_val();
164+
TEST_SPECIAL(cx, test5_4_val, 0, 0);
165+
EXPECT_FP_EQ(
166+
cx, FPBits::make_value(test5_4.get_explicit_mantissa(), 0).get_val());
167+
168+
FPBits test5_5(UInt128(0x0000004270000001));
169+
const T test5_5_val = test5_5.get_val();
170+
TEST_SPECIAL(cx, test5_5_val, 0, 0);
171+
EXPECT_FP_EQ(
172+
cx, FPBits::make_value(test5_5.get_explicit_mantissa(), 0).get_val());
173+
174+
FPBits test5_6(UInt128(0x0000000008261001));
175+
const T test5_6_val = test5_6.get_val();
176+
TEST_SPECIAL(cx, test5_6_val, 0, 0);
177+
EXPECT_FP_EQ(
178+
cx, FPBits::make_value(test5_6.get_explicit_mantissa(), 0).get_val());
158179
}
159180
}
160181

161-
void testRegularNumbers(CanonicalizeFunc func) {
182+
void testRegularNumbers(CanonicalizeFunc f) {
162183
T cx;
163-
TEST_REGULAR(cx, T(1.0), 0);
164-
EXPECT_EQ(cx, T(1.0));
165-
TEST_REGULAR(cx, T(-1.0), 0);
166-
EXPECT_EQ(cx, T(-1.0));
167-
TEST_REGULAR(cx, T(10.0), 0);
168-
EXPECT_EQ(cx, T(10.0));
169-
TEST_REGULAR(cx, T(-10.0), 0);
170-
EXPECT_EQ(cx, T(-10.0));
171-
TEST_REGULAR(cx, T(1234.0), 0);
172-
EXPECT_EQ(cx, T(1234.0));
173-
TEST_REGULAR(cx, T(-1234.0), 0);
174-
EXPECT_EQ(cx, T(-1234.0));
184+
const T test_var_1 = T(1.0);
185+
TEST_REGULAR(cx, test_var_1, 0);
186+
EXPECT_FP_EQ(cx, test_var_1);
187+
const T test_var_2 = T(-1.0);
188+
TEST_REGULAR(cx, test_var_2, 0);
189+
EXPECT_FP_EQ(cx, test_var_2);
190+
const T test_var_3 = T(10.0);
191+
TEST_REGULAR(cx, test_var_3, 0);
192+
EXPECT_FP_EQ(cx, test_var_3);
193+
const T test_var_4 = T(-10.0);
194+
TEST_REGULAR(cx, test_var_4, 0);
195+
EXPECT_FP_EQ(cx, test_var_4);
196+
const T test_var_5 = T(1234.0);
197+
TEST_REGULAR(cx, test_var_5, 0);
198+
EXPECT_FP_EQ(cx, test_var_5);
199+
const T test_var_6 = T(-1234.0);
200+
TEST_REGULAR(cx, test_var_6, 0);
201+
EXPECT_FP_EQ(cx, test_var_6);
175202
}
176203
};
177204

0 commit comments

Comments
 (0)