1//===-- Unittests for the FPBits class ------------------------------------===//
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#include "src/__support/FPUtil/FPBits.h"
10#include "src/__support/FPUtil/fpbits_str.h"
11#include "src/__support/integer_literals.h"
12#include "src/__support/sign.h" // Sign
13#include "test/UnitTest/Test.h"
14
15using LIBC_NAMESPACE::fputil::FPBits;
16using LIBC_NAMESPACE::fputil::FPType;
17using LIBC_NAMESPACE::fputil::internal::FPRep;
18
19using LIBC_NAMESPACE::operator""_u16;
20using LIBC_NAMESPACE::operator""_u32;
21using LIBC_NAMESPACE::operator""_u64;
22using LIBC_NAMESPACE::operator""_u128;
23
24TEST(LlvmLibcFPBitsTest, FPType_IEEE754_Binary16) {
25 using Rep = FPRep<FPType::IEEE754_Binary16>;
26 using u16 = typename Rep::StorageType;
27
28 EXPECT_EQ(0b0'00000'0000000000_u16, u16(Rep::zero()));
29 EXPECT_EQ(0b0'01111'0000000000_u16, u16(Rep::one()));
30 EXPECT_EQ(0b0'00000'0000000001_u16, u16(Rep::min_subnormal()));
31 EXPECT_EQ(0b0'00000'1111111111_u16, u16(Rep::max_subnormal()));
32 EXPECT_EQ(0b0'00001'0000000000_u16, u16(Rep::min_normal()));
33 EXPECT_EQ(0b0'11110'1111111111_u16, u16(Rep::max_normal()));
34 EXPECT_EQ(0b0'11111'0000000000_u16, u16(Rep::inf()));
35 EXPECT_EQ(0b0'11111'0100000000_u16, u16(Rep::signaling_nan()));
36 EXPECT_EQ(0b0'11111'1000000000_u16, u16(Rep::quiet_nan()));
37}
38
39TEST(LlvmLibcFPBitsTest, FPType_IEEE754_Binary32) {
40 using Rep = FPRep<FPType::IEEE754_Binary32>;
41 using u32 = typename Rep::StorageType;
42
43 EXPECT_EQ(0b0'00000000'00000000000000000000000_u32, u32(Rep::zero()));
44 EXPECT_EQ(0b0'01111111'00000000000000000000000_u32, u32(Rep::one()));
45 EXPECT_EQ(0b0'00000000'00000000000000000000001_u32,
46 u32(Rep::min_subnormal()));
47 EXPECT_EQ(0b0'00000000'11111111111111111111111_u32,
48 u32(Rep::max_subnormal()));
49 EXPECT_EQ(0b0'00000001'00000000000000000000000_u32, u32(Rep::min_normal()));
50 EXPECT_EQ(0b0'11111110'11111111111111111111111_u32, u32(Rep::max_normal()));
51 EXPECT_EQ(0b0'11111111'00000000000000000000000_u32, u32(Rep::inf()));
52 EXPECT_EQ(0b0'11111111'01000000000000000000000_u32,
53 u32(Rep::signaling_nan()));
54 EXPECT_EQ(0b0'11111111'10000000000000000000000_u32, u32(Rep::quiet_nan()));
55}
56
57TEST(LlvmLibcFPBitsTest, FPType_IEEE754_Binary64) {
58 using Rep = FPRep<FPType::IEEE754_Binary64>;
59 using u64 = typename Rep::StorageType;
60
61 EXPECT_EQ(
62 0b0'00000000000'0000000000000000000000000000000000000000000000000000_u64,
63 u64(Rep::zero()));
64 EXPECT_EQ(
65 0b0'01111111111'0000000000000000000000000000000000000000000000000000_u64,
66 u64(Rep::one()));
67 EXPECT_EQ(
68 0b0'00000000000'0000000000000000000000000000000000000000000000000001_u64,
69 u64(Rep::min_subnormal()));
70 EXPECT_EQ(
71 0b0'00000000000'1111111111111111111111111111111111111111111111111111_u64,
72 u64(Rep::max_subnormal()));
73 EXPECT_EQ(
74 0b0'00000000001'0000000000000000000000000000000000000000000000000000_u64,
75 u64(Rep::min_normal()));
76 EXPECT_EQ(
77 0b0'11111111110'1111111111111111111111111111111111111111111111111111_u64,
78 u64(Rep::max_normal()));
79 EXPECT_EQ(
80 0b0'11111111111'0000000000000000000000000000000000000000000000000000_u64,
81 u64(Rep::inf()));
82 EXPECT_EQ(
83 0b0'11111111111'0100000000000000000000000000000000000000000000000000_u64,
84 u64(Rep::signaling_nan()));
85 EXPECT_EQ(
86 0b0'11111111111'1000000000000000000000000000000000000000000000000000_u64,
87 u64(Rep::quiet_nan()));
88}
89
90TEST(LlvmLibcFPBitsTest, FPType_IEEE754_Binary128) {
91 using Rep = FPRep<FPType::IEEE754_Binary128>;
92
93 EXPECT_EQ(
94 0b0'000000000000000'0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000_u128,
95 UInt128(Rep::zero()));
96 EXPECT_EQ(
97 0b0'011111111111111'0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000_u128,
98 UInt128(Rep::one()));
99 EXPECT_EQ(
100 0b0'000000000000000'0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001_u128,
101 UInt128(Rep::min_subnormal()));
102 EXPECT_EQ(
103 0b0'000000000000000'1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111_u128,
104 UInt128(Rep::max_subnormal()));
105 EXPECT_EQ(
106 0b0'000000000000001'0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000_u128,
107 UInt128(Rep::min_normal()));
108 EXPECT_EQ(
109 0b0'111111111111110'1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111_u128,
110 UInt128(Rep::max_normal()));
111 EXPECT_EQ(
112 0b0'111111111111111'0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000_u128,
113 UInt128(Rep::inf()));
114 EXPECT_EQ(
115 0b0'111111111111111'0100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000_u128,
116 UInt128(Rep::signaling_nan()));
117 EXPECT_EQ(
118 0b0'111111111111111'1000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000_u128,
119 UInt128(Rep::quiet_nan()));
120}
121
122TEST(LlvmLibcFPBitsTest, FPType_X86_Binary80) {
123 using Rep = FPRep<FPType::X86_Binary80>;
124
125 EXPECT_EQ(
126 0b0'0000000000000000000000000000000000000000000000000000000000000000000000000000000_u128,
127 UInt128(Rep::zero()));
128 EXPECT_EQ(
129 0b0'0111111111111111000000000000000000000000000000000000000000000000000000000000000_u128,
130 UInt128(Rep::one()));
131 EXPECT_EQ(
132 0b0'0000000000000000000000000000000000000000000000000000000000000000000000000000001_u128,
133 UInt128(Rep::min_subnormal()));
134 EXPECT_EQ(
135 0b0'0000000000000000111111111111111111111111111111111111111111111111111111111111111_u128,
136 UInt128(Rep::max_subnormal()));
137 EXPECT_EQ(
138 0b0'0000000000000011000000000000000000000000000000000000000000000000000000000000000_u128,
139 UInt128(Rep::min_normal()));
140 EXPECT_EQ(
141 0b0'1111111111111101111111111111111111111111111111111111111111111111111111111111111_u128,
142 UInt128(Rep::max_normal()));
143 EXPECT_EQ(
144 0b0'1111111111111111000000000000000000000000000000000000000000000000000000000000000_u128,
145 UInt128(Rep::inf()));
146 EXPECT_EQ(
147 0b0'1111111111111111010000000000000000000000000000000000000000000000000000000000000_u128,
148 UInt128(Rep::signaling_nan()));
149 EXPECT_EQ(
150 0b0'1111111111111111100000000000000000000000000000000000000000000000000000000000000_u128,
151 UInt128(Rep::quiet_nan()));
152}
153
154TEST(LlvmLibcFPBitsTest, FPType_X86_Binary80_IsNan) {
155 using Rep = FPRep<FPType::X86_Binary80>;
156
157 EXPECT_TRUE( // NAN : Pseudo-Infinity
158 Rep(0b0'111111111111111'0000000000000000000000000000000000000000000000000000000000000000_u128)
159 .is_nan());
160 EXPECT_TRUE( // NAN : Pseudo Not a Number
161 Rep(0b0'111111111111111'0000000000000000000000000000000000000000000000000000000000000001_u128)
162 .is_nan());
163 EXPECT_TRUE( // NAN : Pseudo Not a Number
164 Rep(0b0'111111111111111'0100000000000000000000000000000000000000000000000000000000000000_u128)
165 .is_nan());
166 EXPECT_TRUE( // NAN : Signalling Not a Number
167 Rep(0b0'111111111111111'1000000000000000000000000000000000000000000000000000000000000001_u128)
168 .is_nan());
169 EXPECT_TRUE( // NAN : Floating-point Indefinite
170 Rep(0b0'111111111111111'1100000000000000000000000000000000000000000000000000000000000000_u128)
171 .is_nan());
172 EXPECT_TRUE( // NAN : Quiet Not a Number
173 Rep(0b0'111111111111111'1100000000000000000000000000000000000000000000000000000000000001_u128)
174 .is_nan());
175 EXPECT_TRUE( // NAN : Unnormal
176 Rep(0b0'111111111111110'0000000000000000000000000000000000000000000000000000000000000000_u128)
177 .is_nan());
178 EXPECT_FALSE( // Zero
179 Rep(0b0'000000000000000'0000000000000000000000000000000000000000000000000000000000000000_u128)
180 .is_nan());
181 EXPECT_FALSE( // Subnormal
182 Rep(0b0'000000000000000'0000000000000000000000000000000000000000000000000000000000000001_u128)
183 .is_nan());
184 EXPECT_FALSE( // Pseudo Denormal
185 Rep(0b0'000000000000000'1000000000000000000000000000000000000000000000000000000000000001_u128)
186 .is_nan());
187 EXPECT_FALSE( // Infinity
188 Rep(0b0'111111111111111'1000000000000000000000000000000000000000000000000000000000000000_u128)
189 .is_nan());
190 EXPECT_FALSE( // Normalized
191 Rep(0b0'111111111111110'1000000000000000000000000000000000000000000000000000000000000000_u128)
192 .is_nan());
193}
194
195enum class FP {
196 ZERO,
197 MIN_SUBNORMAL,
198 MAX_SUBNORMAL,
199 MIN_NORMAL,
200 ONE,
201 MAX_NORMAL,
202 INF,
203 SIGNALING_NAN,
204 QUIET_NAN
205};
206
207constexpr FP all_fp_values[] = {
208 FP::ZERO, FP::MIN_SUBNORMAL, FP::MAX_SUBNORMAL,
209 FP::MIN_NORMAL, FP::ONE, FP::MAX_NORMAL,
210 FP::INF, FP::SIGNALING_NAN, FP::QUIET_NAN,
211};
212
213constexpr Sign all_signs[] = {Sign::POS, Sign::NEG};
214
215using FPTypes = LIBC_NAMESPACE::testing::TypeList<
216 FPRep<FPType::IEEE754_Binary16>, FPRep<FPType::IEEE754_Binary32>,
217 FPRep<FPType::IEEE754_Binary64>, FPRep<FPType::IEEE754_Binary128>,
218 FPRep<FPType::X86_Binary80>>;
219
220template <typename T> constexpr auto make(Sign sign, FP fp) {
221 switch (fp) {
222 case FP::ZERO:
223 return T::zero(sign);
224 case FP::MIN_SUBNORMAL:
225 return T::min_subnormal(sign);
226 case FP::MAX_SUBNORMAL:
227 return T::max_subnormal(sign);
228 case FP::MIN_NORMAL:
229 return T::min_normal(sign);
230 case FP::ONE:
231 return T::one(sign);
232 case FP::MAX_NORMAL:
233 return T::max_normal(sign);
234 case FP::INF:
235 return T::inf(sign);
236 case FP::SIGNALING_NAN:
237 return T::signaling_nan(sign);
238 case FP::QUIET_NAN:
239 return T::quiet_nan(sign);
240 }
241}
242
243// Tests all properties for all types of float.
244TYPED_TEST(LlvmLibcFPBitsTest, Properties, FPTypes) {
245 for (Sign sign : all_signs) {
246 for (FP fp : all_fp_values) {
247 const T value = make<T>(sign, fp);
248 // is_zero
249 ASSERT_EQ(value.is_zero(), fp == FP::ZERO);
250 // is_inf_or_nan
251 ASSERT_EQ(value.is_inf_or_nan(), fp == FP::INF ||
252 fp == FP::SIGNALING_NAN ||
253 fp == FP::QUIET_NAN);
254 // is_finite
255 ASSERT_EQ(value.is_finite(), fp != FP::INF && fp != FP::SIGNALING_NAN &&
256 fp != FP::QUIET_NAN);
257 // is_inf
258 ASSERT_EQ(value.is_inf(), fp == FP::INF);
259 // is_nan
260 ASSERT_EQ(value.is_nan(), fp == FP::SIGNALING_NAN || fp == FP::QUIET_NAN);
261 // is_normal
262 ASSERT_EQ(value.is_normal(),
263 fp == FP::MIN_NORMAL || fp == FP::ONE || fp == FP::MAX_NORMAL);
264 // is_quiet_nan
265 ASSERT_EQ(value.is_quiet_nan(), fp == FP::QUIET_NAN);
266 // is_signaling_nan
267 ASSERT_EQ(value.is_signaling_nan(), fp == FP::SIGNALING_NAN);
268 // is_subnormal
269 ASSERT_EQ(value.is_subnormal(), fp == FP::ZERO ||
270 fp == FP::MIN_SUBNORMAL ||
271 fp == FP::MAX_SUBNORMAL);
272 // is_pos
273 ASSERT_EQ(value.is_pos(), sign == Sign::POS);
274 ASSERT_EQ(value.sign().is_pos(), sign == Sign::POS);
275 // is_neg
276 ASSERT_EQ(value.is_neg(), sign == Sign::NEG);
277 ASSERT_EQ(value.sign().is_neg(), sign == Sign::NEG);
278 }
279 }
280}
281
282#define ASSERT_SAME_REP(A, B) ASSERT_EQ(A.uintval(), B.uintval());
283
284TYPED_TEST(LlvmLibcFPBitsTest, NextTowardInf, FPTypes) {
285 struct {
286 FP before, after;
287 } TEST_CASES[] = {
288 {FP::ZERO, FP::MIN_SUBNORMAL}, //
289 {FP::MAX_SUBNORMAL, FP::MIN_NORMAL}, //
290 {FP::MAX_NORMAL, FP::INF}, //
291 {FP::INF, FP::INF}, //
292 {FP::QUIET_NAN, FP::QUIET_NAN}, //
293 {FP::SIGNALING_NAN, FP::SIGNALING_NAN}, //
294 };
295 for (Sign sign : all_signs) {
296 for (auto tc : TEST_CASES) {
297 T val = make<T>(sign, tc.before);
298 ASSERT_SAME_REP(val.next_toward_inf(), make<T>(sign, tc.after));
299 }
300 }
301}
302
303TEST(LlvmLibcFPBitsTest, FloatType) {
304 using FloatBits = FPBits<float>;
305
306 EXPECT_STREQ(LIBC_NAMESPACE::str(FloatBits::inf(Sign::POS)).c_str(),
307 "(+Infinity)");
308 EXPECT_STREQ(LIBC_NAMESPACE::str(FloatBits::inf(Sign::NEG)).c_str(),
309 "(-Infinity)");
310 EXPECT_STREQ(LIBC_NAMESPACE::str(FloatBits::signaling_nan()).c_str(),
311 "(NaN)");
312
313 FloatBits zero(0.0f);
314 EXPECT_TRUE(zero.is_pos());
315 EXPECT_EQ(zero.get_biased_exponent(), 0_u16);
316 EXPECT_EQ(zero.get_mantissa(), 0_u32);
317 EXPECT_EQ(zero.uintval(), 0_u32);
318 EXPECT_STREQ(LIBC_NAMESPACE::str(zero).c_str(),
319 "0x00000000 = (S: 0, E: 0x0000, M: 0x00000000)");
320
321 FloatBits negzero(-0.0f);
322 EXPECT_TRUE(negzero.is_neg());
323 EXPECT_EQ(negzero.get_biased_exponent(), 0_u16);
324 EXPECT_EQ(negzero.get_mantissa(), 0_u32);
325 EXPECT_EQ(negzero.uintval(), 0x80000000_u32);
326 EXPECT_STREQ(LIBC_NAMESPACE::str(negzero).c_str(),
327 "0x80000000 = (S: 1, E: 0x0000, M: 0x00000000)");
328
329 FloatBits one(1.0f);
330 EXPECT_TRUE(one.is_pos());
331 EXPECT_EQ(one.get_biased_exponent(), 0x7F_u16);
332 EXPECT_EQ(one.get_mantissa(), 0_u32);
333 EXPECT_EQ(one.uintval(), 0x3F800000_u32);
334 EXPECT_STREQ(LIBC_NAMESPACE::str(one).c_str(),
335 "0x3F800000 = (S: 0, E: 0x007F, M: 0x00000000)");
336
337 FloatBits negone(-1.0f);
338 EXPECT_TRUE(negone.is_neg());
339 EXPECT_EQ(negone.get_biased_exponent(), 0x7F_u16);
340 EXPECT_EQ(negone.get_mantissa(), 0_u32);
341 EXPECT_EQ(negone.uintval(), 0xBF800000_u32);
342 EXPECT_STREQ(LIBC_NAMESPACE::str(negone).c_str(),
343 "0xBF800000 = (S: 1, E: 0x007F, M: 0x00000000)");
344
345 FloatBits num(1.125f);
346 EXPECT_TRUE(num.is_pos());
347 EXPECT_EQ(num.get_biased_exponent(), 0x7F_u16);
348 EXPECT_EQ(num.get_mantissa(), 0x00100000_u32);
349 EXPECT_EQ(num.uintval(), 0x3F900000_u32);
350 EXPECT_STREQ(LIBC_NAMESPACE::str(num).c_str(),
351 "0x3F900000 = (S: 0, E: 0x007F, M: 0x00100000)");
352
353 FloatBits negnum(-1.125f);
354 EXPECT_TRUE(negnum.is_neg());
355 EXPECT_EQ(negnum.get_biased_exponent(), 0x7F_u16);
356 EXPECT_EQ(negnum.get_mantissa(), 0x00100000_u32);
357 EXPECT_EQ(negnum.uintval(), 0xBF900000_u32);
358 EXPECT_STREQ(LIBC_NAMESPACE::str(negnum).c_str(),
359 "0xBF900000 = (S: 1, E: 0x007F, M: 0x00100000)");
360
361 FloatBits quiet_nan = FloatBits::quiet_nan();
362 EXPECT_EQ(quiet_nan.is_quiet_nan(), true);
363}
364
365TEST(LlvmLibcFPBitsTest, DoubleType) {
366 using DoubleBits = FPBits<double>;
367
368 EXPECT_STREQ(LIBC_NAMESPACE::str(DoubleBits::inf(Sign::POS)).c_str(),
369 "(+Infinity)");
370 EXPECT_STREQ(LIBC_NAMESPACE::str(DoubleBits::inf(Sign::NEG)).c_str(),
371 "(-Infinity)");
372 EXPECT_STREQ(LIBC_NAMESPACE::str(DoubleBits::signaling_nan()).c_str(),
373 "(NaN)");
374
375 DoubleBits zero(0.0);
376 EXPECT_TRUE(zero.is_pos());
377 EXPECT_EQ(zero.get_biased_exponent(), 0_u16);
378 EXPECT_EQ(zero.get_mantissa(), 0_u64);
379 EXPECT_EQ(zero.uintval(), 0_u64);
380 EXPECT_STREQ(LIBC_NAMESPACE::str(zero).c_str(),
381 "0x0000000000000000 = (S: 0, E: 0x0000, M: 0x0000000000000000)");
382
383 DoubleBits negzero(-0.0);
384 EXPECT_TRUE(negzero.is_neg());
385 EXPECT_EQ(negzero.get_biased_exponent(), 0_u16);
386 EXPECT_EQ(negzero.get_mantissa(), 0_u64);
387 EXPECT_EQ(negzero.uintval(), 0x8000000000000000_u64);
388 EXPECT_STREQ(LIBC_NAMESPACE::str(negzero).c_str(),
389 "0x8000000000000000 = (S: 1, E: 0x0000, M: 0x0000000000000000)");
390
391 DoubleBits one(1.0);
392 EXPECT_TRUE(one.is_pos());
393 EXPECT_EQ(one.get_biased_exponent(), 0x03FF_u16);
394 EXPECT_EQ(one.get_mantissa(), 0_u64);
395 EXPECT_EQ(one.uintval(), 0x3FF0000000000000_u64);
396 EXPECT_STREQ(LIBC_NAMESPACE::str(one).c_str(),
397 "0x3FF0000000000000 = (S: 0, E: 0x03FF, M: 0x0000000000000000)");
398
399 DoubleBits negone(-1.0);
400 EXPECT_TRUE(negone.is_neg());
401 EXPECT_EQ(negone.get_biased_exponent(), 0x03FF_u16);
402 EXPECT_EQ(negone.get_mantissa(), 0_u64);
403 EXPECT_EQ(negone.uintval(), 0xBFF0000000000000_u64);
404 EXPECT_STREQ(LIBC_NAMESPACE::str(negone).c_str(),
405 "0xBFF0000000000000 = (S: 1, E: 0x03FF, M: 0x0000000000000000)");
406
407 DoubleBits num(1.125);
408 EXPECT_TRUE(num.is_pos());
409 EXPECT_EQ(num.get_biased_exponent(), 0x03FF_u16);
410 EXPECT_EQ(num.get_mantissa(), 0x0002000000000000_u64);
411 EXPECT_EQ(num.uintval(), 0x3FF2000000000000_u64);
412 EXPECT_STREQ(LIBC_NAMESPACE::str(num).c_str(),
413 "0x3FF2000000000000 = (S: 0, E: 0x03FF, M: 0x0002000000000000)");
414
415 DoubleBits negnum(-1.125);
416 EXPECT_TRUE(negnum.is_neg());
417 EXPECT_EQ(negnum.get_biased_exponent(), 0x03FF_u16);
418 EXPECT_EQ(negnum.get_mantissa(), 0x0002000000000000_u64);
419 EXPECT_EQ(negnum.uintval(), 0xBFF2000000000000_u64);
420 EXPECT_STREQ(LIBC_NAMESPACE::str(negnum).c_str(),
421 "0xBFF2000000000000 = (S: 1, E: 0x03FF, M: 0x0002000000000000)");
422
423 DoubleBits quiet_nan = DoubleBits::quiet_nan();
424 EXPECT_EQ(quiet_nan.is_quiet_nan(), true);
425}
426
427#ifdef LIBC_TARGET_ARCH_IS_X86
428TEST(LlvmLibcFPBitsTest, X86LongDoubleType) {
429 using LongDoubleBits = FPBits<long double>;
430
431 if constexpr (sizeof(long double) == sizeof(double))
432 return; // The tests for the "double" type cover for this case.
433
434 EXPECT_STREQ(LIBC_NAMESPACE::str(LongDoubleBits::inf(Sign::POS)).c_str(),
435 "(+Infinity)");
436 EXPECT_STREQ(LIBC_NAMESPACE::str(LongDoubleBits::inf(Sign::NEG)).c_str(),
437 "(-Infinity)");
438 EXPECT_STREQ(LIBC_NAMESPACE::str(LongDoubleBits::signaling_nan()).c_str(),
439 "(NaN)");
440
441 LongDoubleBits zero(0.0l);
442 EXPECT_TRUE(zero.is_pos());
443 EXPECT_EQ(zero.get_biased_exponent(), 0_u16);
444 EXPECT_EQ(zero.get_mantissa(), 0_u128);
445 EXPECT_EQ(zero.uintval(), 0_u128);
446 EXPECT_STREQ(
447 LIBC_NAMESPACE::str(zero).c_str(),
448 "0x00000000000000000000000000000000 = "
449 "(S: 0, E: 0x0000, I: 0, M: 0x00000000000000000000000000000000)");
450
451 LongDoubleBits negzero(-0.0l);
452 EXPECT_TRUE(negzero.is_neg());
453 EXPECT_EQ(negzero.get_biased_exponent(), 0_u16);
454 EXPECT_EQ(negzero.get_mantissa(), 0_u128);
455 EXPECT_EQ(negzero.uintval(), 0x8000'00000000'00000000_u128);
456 EXPECT_STREQ(
457 LIBC_NAMESPACE::str(negzero).c_str(),
458 "0x00000000000080000000000000000000 = "
459 "(S: 1, E: 0x0000, I: 0, M: 0x00000000000000000000000000000000)");
460
461 LongDoubleBits one(1.0l);
462 EXPECT_TRUE(one.is_pos());
463 EXPECT_EQ(one.get_biased_exponent(), 0x3FFF_u16);
464 EXPECT_EQ(one.get_mantissa(), 0_u128);
465 EXPECT_EQ(one.uintval(), 0x3FFF'80000000'00000000_u128);
466 EXPECT_STREQ(
467 LIBC_NAMESPACE::str(one).c_str(),
468 "0x0000000000003FFF8000000000000000 = "
469 "(S: 0, E: 0x3FFF, I: 1, M: 0x00000000000000000000000000000000)");
470
471 LongDoubleBits negone(-1.0l);
472 EXPECT_TRUE(negone.is_neg());
473 EXPECT_EQ(negone.get_biased_exponent(), 0x3FFF_u16);
474 EXPECT_EQ(negone.get_mantissa(), 0_u128);
475 EXPECT_EQ(negone.uintval(), 0xBFFF'80000000'00000000_u128);
476 EXPECT_STREQ(
477 LIBC_NAMESPACE::str(negone).c_str(),
478 "0x000000000000BFFF8000000000000000 = "
479 "(S: 1, E: 0x3FFF, I: 1, M: 0x00000000000000000000000000000000)");
480
481 LongDoubleBits num(1.125l);
482 EXPECT_TRUE(num.is_pos());
483 EXPECT_EQ(num.get_biased_exponent(), 0x3FFF_u16);
484 EXPECT_EQ(num.get_mantissa(), 0x10000000'00000000_u128);
485 EXPECT_EQ(num.uintval(), 0x3FFF'90000000'00000000_u128);
486 EXPECT_STREQ(
487 LIBC_NAMESPACE::str(num).c_str(),
488 "0x0000000000003FFF9000000000000000 = "
489 "(S: 0, E: 0x3FFF, I: 1, M: 0x00000000000000001000000000000000)");
490
491 LongDoubleBits negnum(-1.125l);
492 EXPECT_TRUE(negnum.is_neg());
493 EXPECT_EQ(negnum.get_biased_exponent(), 0x3FFF_u16);
494 EXPECT_EQ(negnum.get_mantissa(), 0x10000000'00000000_u128);
495 EXPECT_EQ(negnum.uintval(), 0xBFFF'90000000'00000000_u128);
496 EXPECT_STREQ(
497 LIBC_NAMESPACE::str(negnum).c_str(),
498 "0x000000000000BFFF9000000000000000 = "
499 "(S: 1, E: 0x3FFF, I: 1, M: 0x00000000000000001000000000000000)");
500
501 LongDoubleBits quiet_nan = LongDoubleBits::quiet_nan();
502 EXPECT_EQ(quiet_nan.is_quiet_nan(), true);
503}
504#else
505TEST(LlvmLibcFPBitsTest, LongDoubleType) {
506#if defined(LIBC_TYPES_LONG_DOUBLE_IS_FLOAT64)
507 return; // The tests for the "double" type cover for this case.
508#else
509 using LongDoubleBits = FPBits<long double>;
510
511 EXPECT_STREQ(LIBC_NAMESPACE::str(LongDoubleBits::inf(Sign::POS)).c_str(),
512 "(+Infinity)");
513 EXPECT_STREQ(LIBC_NAMESPACE::str(LongDoubleBits::inf(Sign::NEG)).c_str(),
514 "(-Infinity)");
515 EXPECT_STREQ(LIBC_NAMESPACE::str(LongDoubleBits::signaling_nan()).c_str(),
516 "(NaN)");
517
518 LongDoubleBits zero(0.0l);
519 EXPECT_TRUE(zero.is_pos());
520 EXPECT_EQ(zero.get_biased_exponent(), 0_u16);
521 EXPECT_EQ(zero.get_mantissa(), 0_u128);
522 EXPECT_EQ(zero.uintval(), 0_u128);
523 EXPECT_STREQ(LIBC_NAMESPACE::str(zero).c_str(),
524 "0x00000000000000000000000000000000 = "
525 "(S: 0, E: 0x0000, M: 0x00000000000000000000000000000000)");
526
527 LongDoubleBits negzero(-0.0l);
528 EXPECT_TRUE(negzero.is_neg());
529 EXPECT_EQ(negzero.get_biased_exponent(), 0_u16);
530 EXPECT_EQ(negzero.get_mantissa(), 0_u128);
531 EXPECT_EQ(negzero.uintval(), 0x80000000'00000000'00000000'00000000_u128);
532 EXPECT_STREQ(LIBC_NAMESPACE::str(negzero).c_str(),
533 "0x80000000000000000000000000000000 = "
534 "(S: 1, E: 0x0000, M: 0x00000000000000000000000000000000)");
535
536 LongDoubleBits one(1.0l);
537 EXPECT_TRUE(one.is_pos());
538 EXPECT_EQ(one.get_biased_exponent(), 0x3FFF_u16);
539 EXPECT_EQ(one.get_mantissa(), 0_u128);
540 EXPECT_EQ(one.uintval(), 0x3FFF0000'00000000'00000000'00000000_u128);
541 EXPECT_STREQ(LIBC_NAMESPACE::str(one).c_str(),
542 "0x3FFF0000000000000000000000000000 = "
543 "(S: 0, E: 0x3FFF, M: 0x00000000000000000000000000000000)");
544
545 LongDoubleBits negone(-1.0l);
546 EXPECT_TRUE(negone.is_neg());
547 EXPECT_EQ(negone.get_biased_exponent(), 0x3FFF_u16);
548 EXPECT_EQ(negone.get_mantissa(), 0_u128);
549 EXPECT_EQ(negone.uintval(), 0xBFFF0000'00000000'00000000'00000000_u128);
550 EXPECT_STREQ(LIBC_NAMESPACE::str(negone).c_str(),
551 "0xBFFF0000000000000000000000000000 = "
552 "(S: 1, E: 0x3FFF, M: 0x00000000000000000000000000000000)");
553
554 LongDoubleBits num(1.125l);
555 EXPECT_TRUE(num.is_pos());
556 EXPECT_EQ(num.get_biased_exponent(), 0x3FFF_u16);
557 EXPECT_EQ(num.get_mantissa(), 0x2000'00000000'00000000'00000000_u128);
558 EXPECT_EQ(num.uintval(), 0x3FFF2000'00000000'00000000'00000000_u128);
559 EXPECT_STREQ(LIBC_NAMESPACE::str(num).c_str(),
560 "0x3FFF2000000000000000000000000000 = "
561 "(S: 0, E: 0x3FFF, M: 0x00002000000000000000000000000000)");
562
563 LongDoubleBits negnum(-1.125l);
564 EXPECT_TRUE(negnum.is_neg());
565 EXPECT_EQ(negnum.get_biased_exponent(), 0x3FFF_u16);
566 EXPECT_EQ(negnum.get_mantissa(), 0x2000'00000000'00000000'00000000_u128);
567 EXPECT_EQ(negnum.uintval(), 0xBFFF2000'00000000'00000000'00000000_u128);
568 EXPECT_STREQ(LIBC_NAMESPACE::str(negnum).c_str(),
569 "0xBFFF2000000000000000000000000000 = "
570 "(S: 1, E: 0x3FFF, M: 0x00002000000000000000000000000000)");
571
572 LongDoubleBits quiet_nan = LongDoubleBits::quiet_nan();
573 EXPECT_EQ(quiet_nan.is_quiet_nan(), true);
574#endif
575}
576#endif
577
578#if defined(LIBC_TYPES_HAS_FLOAT128)
579TEST(LlvmLibcFPBitsTest, Float128Type) {
580 using Float128Bits = FPBits<float128>;
581
582 EXPECT_STREQ(LIBC_NAMESPACE::str(Float128Bits::inf(Sign::POS)).c_str(),
583 "(+Infinity)");
584 EXPECT_STREQ(LIBC_NAMESPACE::str(Float128Bits::inf(Sign::NEG)).c_str(),
585 "(-Infinity)");
586 EXPECT_STREQ(LIBC_NAMESPACE::str(Float128Bits::signaling_nan()).c_str(),
587 "(NaN)");
588
589 Float128Bits zero = Float128Bits::zero(sign: Sign::POS);
590 EXPECT_TRUE(zero.is_pos());
591 EXPECT_EQ(zero.get_biased_exponent(), 0_u16);
592 EXPECT_EQ(zero.get_mantissa(), 0_u128);
593 EXPECT_EQ(zero.uintval(), 0_u128);
594 EXPECT_STREQ(LIBC_NAMESPACE::str(zero).c_str(),
595 "0x00000000000000000000000000000000 = "
596 "(S: 0, E: 0x0000, M: 0x00000000000000000000000000000000)");
597
598 Float128Bits negzero = Float128Bits::zero(sign: Sign::NEG);
599 EXPECT_TRUE(negzero.is_neg());
600 EXPECT_EQ(negzero.get_biased_exponent(), 0_u16);
601 EXPECT_EQ(negzero.get_mantissa(), 0_u128);
602 EXPECT_EQ(negzero.uintval(), 0x80000000'00000000'00000000'00000000_u128);
603 EXPECT_STREQ(LIBC_NAMESPACE::str(negzero).c_str(),
604 "0x80000000000000000000000000000000 = "
605 "(S: 1, E: 0x0000, M: 0x00000000000000000000000000000000)");
606
607 Float128Bits one(float128(1.0));
608 EXPECT_TRUE(one.is_pos());
609 EXPECT_EQ(one.get_biased_exponent(), 0x3FFF_u16);
610 EXPECT_EQ(one.get_mantissa(), 0_u128);
611 EXPECT_EQ(one.uintval(), 0x3FFF0000'00000000'00000000'00000000_u128);
612 EXPECT_STREQ(LIBC_NAMESPACE::str(one).c_str(),
613 "0x3FFF0000000000000000000000000000 = "
614 "(S: 0, E: 0x3FFF, M: 0x00000000000000000000000000000000)");
615
616 Float128Bits negone(float128(-1.0));
617 EXPECT_TRUE(negone.is_neg());
618 EXPECT_EQ(negone.get_biased_exponent(), 0x3FFF_u16);
619 EXPECT_EQ(negone.get_mantissa(), 0_u128);
620 EXPECT_EQ(negone.uintval(), 0xBFFF0000'00000000'00000000'00000000_u128);
621 EXPECT_STREQ(LIBC_NAMESPACE::str(negone).c_str(),
622 "0xBFFF0000000000000000000000000000 = "
623 "(S: 1, E: 0x3FFF, M: 0x00000000000000000000000000000000)");
624
625 Float128Bits num(float128(1.125));
626 EXPECT_TRUE(num.is_pos());
627 EXPECT_EQ(num.get_biased_exponent(), 0x3FFF_u16);
628 EXPECT_EQ(num.get_mantissa(), 0x2000'00000000'00000000'00000000_u128);
629 EXPECT_EQ(num.uintval(), 0x3FFF2000'00000000'00000000'00000000_u128);
630 EXPECT_STREQ(LIBC_NAMESPACE::str(num).c_str(),
631 "0x3FFF2000000000000000000000000000 = "
632 "(S: 0, E: 0x3FFF, M: 0x00002000000000000000000000000000)");
633
634 Float128Bits negnum(float128(-1.125));
635 EXPECT_TRUE(negnum.is_neg());
636 EXPECT_EQ(negnum.get_biased_exponent(), 0x3FFF_u16);
637 EXPECT_EQ(negnum.get_mantissa(), 0x2000'00000000'00000000'00000000_u128);
638 EXPECT_EQ(negnum.uintval(), 0xBFFF2000'00000000'00000000'00000000_u128);
639 EXPECT_STREQ(LIBC_NAMESPACE::str(negnum).c_str(),
640 "0xBFFF2000000000000000000000000000 = "
641 "(S: 1, E: 0x3FFF, M: 0x00002000000000000000000000000000)");
642
643 Float128Bits quiet_nan = Float128Bits::quiet_nan();
644 EXPECT_EQ(quiet_nan.is_quiet_nan(), true);
645}
646#endif // LIBC_TYPES_HAS_FLOAT128
647

source code of libc/test/src/__support/FPUtil/fpbits_test.cpp