[libc-commits] [libc] [libc][math][c23] Fix X86_Binary80 special cases for canonicalize functions. (PR #86924)
Shourya Goel via libc-commits
libc-commits at lists.llvm.org
Thu Mar 28 11:38:32 PDT 2024
https://github.com/Sh0g0-1758 updated https://github.com/llvm/llvm-project/pull/86924
>From 3ba418899bacffc8076befad68140f272a4ff5c4 Mon Sep 17 00:00:00 2001
From: Sh0g0-1758 <shouryagoel10000 at gmail.com>
Date: Thu, 28 Mar 2024 14:48:06 +0530
Subject: [PATCH 1/4] fix infinity
---
libc/src/__support/FPUtil/BasicOperations.h | 10 ++++++----
libc/test/src/math/smoke/CanonicalizeTest.h | 6 +++---
2 files changed, 9 insertions(+), 7 deletions(-)
diff --git a/libc/src/__support/FPUtil/BasicOperations.h b/libc/src/__support/FPUtil/BasicOperations.h
index a47931bb33900a..bdb91a98a0d357 100644
--- a/libc/src/__support/FPUtil/BasicOperations.h
+++ b/libc/src/__support/FPUtil/BasicOperations.h
@@ -185,7 +185,7 @@ LIBC_INLINE int canonicalize(T &cx, const T &x) {
// More precisely :
// Exponent | Significand | Meaning
// | Bits 63-62 | Bits 61-0 |
- // All Ones | 00 | Zero | Pseudo Infinity, Value = Infinty
+ // All Ones | 00 | Zero | Pseudo Infinity, Value = SNaN
// All Ones | 00 | Non-Zero | Pseudo NaN, Value = SNaN
// All Ones | 01 | Anything | Pseudo NaN, Value = SNaN
// | Bit 63 | Bits 62-0 |
@@ -199,9 +199,11 @@ LIBC_INLINE int canonicalize(T &cx, const T &x) {
int exponent = sx.get_biased_exponent();
if (exponent == 0x7FFF) {
if (!bit63 && !bit62) {
- if (mantissa == 0)
- cx = FPBits<T>::inf(sx.sign()).get_val();
- else {
+ if (mantissa == 0) {
+ cx = FPBits<T>::quiet_nan(sx.sign(), mantissa).get_val();
+ raise_except_if_required(FE_INVALID);
+ return 1;
+ } else {
cx = FPBits<T>::quiet_nan(sx.sign(), mantissa).get_val();
raise_except_if_required(FE_INVALID);
return 1;
diff --git a/libc/test/src/math/smoke/CanonicalizeTest.h b/libc/test/src/math/smoke/CanonicalizeTest.h
index c8af83f1983881..1891d47922cf88 100644
--- a/libc/test/src/math/smoke/CanonicalizeTest.h
+++ b/libc/test/src/math/smoke/CanonicalizeTest.h
@@ -55,12 +55,12 @@ class CanonicalizeTest : public LIBC_NAMESPACE::testing::Test {
T cx;
// Exponent | Significand | Meaning
// | Bits 63-62 | Bits 61-0 |
- // All Ones | 00 | Zero | Pseudo Infinity, Value = Infinty
+ // All Ones | 00 | Zero | Pseudo Infinity, Value = SNaN
FPBits test1((UInt128(0x7FFF) << 64) + UInt128(0x0000000000000000));
const T test1_val = test1.get_val();
- TEST_SPECIAL(cx, test1_val, 0, 0);
- EXPECT_FP_EQ(cx, inf);
+ TEST_SPECIAL(cx, test1_val, 1, FE_INVALID);
+ EXPECT_FP_EQ(cx, aNaN);
// Exponent | Significand | Meaning
// | Bits 63-62 | Bits 61-0 |
>From 4aa60bf6905e40af7b67a0265d77115ee7d91999 Mon Sep 17 00:00:00 2001
From: Sh0g0-1758 <shouryagoel10000 at gmail.com>
Date: Thu, 28 Mar 2024 23:20:08 +0530
Subject: [PATCH 2/4] Update unnormal
Formatter
---
libc/src/__support/FPUtil/BasicOperations.h | 19 ++++++------
libc/test/src/math/smoke/CanonicalizeTest.h | 34 +++++++++------------
2 files changed, 24 insertions(+), 29 deletions(-)
diff --git a/libc/src/__support/FPUtil/BasicOperations.h b/libc/src/__support/FPUtil/BasicOperations.h
index bdb91a98a0d357..6e4156497618e2 100644
--- a/libc/src/__support/FPUtil/BasicOperations.h
+++ b/libc/src/__support/FPUtil/BasicOperations.h
@@ -191,8 +191,8 @@ LIBC_INLINE int canonicalize(T &cx, const T &x) {
// | Bit 63 | Bits 62-0 |
// All zeroes | One | Anything | Pseudo Denormal, Value =
// | | | (−1)**s × m × 2**−16382
- // All Other | Zero | Anything | Unnormal, Value =
- // Values | | | (−1)**s × m × 2**−16382
+ // All Other | Zero | Anything | Unnormal, Value = SNaN
+ // Values | | |
bool bit63 = sx.get_implicit_bit();
UInt128 mantissa = sx.get_explicit_mantissa();
bool bit62 = static_cast<bool>((mantissa & (1ULL << 62)) >> 62);
@@ -203,11 +203,10 @@ LIBC_INLINE int canonicalize(T &cx, const T &x) {
cx = FPBits<T>::quiet_nan(sx.sign(), mantissa).get_val();
raise_except_if_required(FE_INVALID);
return 1;
- } else {
- cx = FPBits<T>::quiet_nan(sx.sign(), mantissa).get_val();
- raise_except_if_required(FE_INVALID);
- return 1;
}
+ cx = FPBits<T>::quiet_nan(sx.sign(), mantissa).get_val();
+ raise_except_if_required(FE_INVALID);
+ return 1;
} else if (!bit63 && bit62) {
cx = FPBits<T>::quiet_nan(sx.sign(), mantissa).get_val();
raise_except_if_required(FE_INVALID);
@@ -221,9 +220,11 @@ LIBC_INLINE int canonicalize(T &cx, const T &x) {
cx = x;
} else if (exponent == 0 && bit63)
cx = FPBits<T>::make_value(mantissa, 0).get_val();
- else if (exponent != 0 && !bit63)
- cx = FPBits<T>::make_value(mantissa, 0).get_val();
- else if (LIBC_UNLIKELY(sx.is_signaling_nan())) {
+ else if (exponent != 0 && !bit63) {
+ cx = FPBits<T>::quiet_nan(sx.sign(), mantissa).get_val();
+ raise_except_if_required(FE_INVALID);
+ return 1;
+ } else if (LIBC_UNLIKELY(sx.is_signaling_nan())) {
cx =
FPBits<T>::quiet_nan(sx.sign(), sx.get_explicit_mantissa()).get_val();
raise_except_if_required(FE_INVALID);
diff --git a/libc/test/src/math/smoke/CanonicalizeTest.h b/libc/test/src/math/smoke/CanonicalizeTest.h
index 1891d47922cf88..5b05396cd9ae93 100644
--- a/libc/test/src/math/smoke/CanonicalizeTest.h
+++ b/libc/test/src/math/smoke/CanonicalizeTest.h
@@ -135,44 +135,38 @@ class CanonicalizeTest : public LIBC_NAMESPACE::testing::Test {
// Exponent | Significand | Meaning
// | Bit 63 | Bits 62-0 |
- // All Other | Zero | Anything | Unnormal, Value =
- // Values | | | (−1)**s × m × 2**−16382
+ // All Other | Zero | Anything | Unnormal, Value = SNaN
+ // Values | | |
FPBits test5_1(UInt128(0x0000000000000001));
const T test5_1_val = test5_1.get_val();
- TEST_SPECIAL(cx, test5_1_val, 0, 0);
- EXPECT_FP_EQ(
- cx, FPBits::make_value(test5_1.get_explicit_mantissa(), 0).get_val());
+ TEST_SPECIAL(cx, test5_1_val, 1, FE_INVALID);
+ EXPECT_FP_EQ(cx, aNaN);
FPBits test5_2(UInt128(0x0000004270000001));
const T test5_2_val = test5_2.get_val();
- TEST_SPECIAL(cx, test5_2_val, 0, 0);
- EXPECT_FP_EQ(
- cx, FPBits::make_value(test5_2.get_explicit_mantissa(), 0).get_val());
+ TEST_SPECIAL(cx, test5_2_val, 1, FE_INVALID);
+ EXPECT_FP_EQ(cx, aNaN);
FPBits test5_3(UInt128(0x0000000008261001));
const T test5_3_val = test5_3.get_val();
- TEST_SPECIAL(cx, test5_3_val, 0, 0);
- EXPECT_FP_EQ(
- cx, FPBits::make_value(test5_3.get_explicit_mantissa(), 0).get_val());
+ TEST_SPECIAL(cx, test5_3_val, 1, FE_INVALID);
+ EXPECT_FP_EQ(cx, aNaN);
FPBits test5_4(UInt128(0x0000002816000000));
const T test5_4_val = test5_4.get_val();
- TEST_SPECIAL(cx, test5_4_val, 0, 0);
- EXPECT_FP_EQ(
- cx, FPBits::make_value(test5_4.get_explicit_mantissa(), 0).get_val());
+ TEST_SPECIAL(cx, test5_4_val, 1, FE_INVALID);
+ EXPECT_FP_EQ(cx, aNaN);
FPBits test5_5(UInt128(0x0000004270000001));
const T test5_5_val = test5_5.get_val();
- TEST_SPECIAL(cx, test5_5_val, 0, 0);
- EXPECT_FP_EQ(
- cx, FPBits::make_value(test5_5.get_explicit_mantissa(), 0).get_val());
+ TEST_SPECIAL(cx, test5_5_val, 1, FE_INVALID);
+ EXPECT_FP_EQ(cx, aNaN);
FPBits test5_6(UInt128(0x0000000008261001));
const T test5_6_val = test5_6.get_val();
- TEST_SPECIAL(cx, test5_6_val, 0, 0);
- EXPECT_FP_EQ(
- cx, FPBits::make_value(test5_6.get_explicit_mantissa(), 0).get_val());
+ TEST_SPECIAL(cx, test5_6_val, 1, FE_INVALID);
+ EXPECT_FP_EQ(cx, aNaN);
}
}
>From de9d0998b7e8dd5f8af0ab9b338084b7718f9be7 Mon Sep 17 00:00:00 2001
From: Sh0g0-1758 <shouryagoel10000 at gmail.com>
Date: Fri, 29 Mar 2024 00:06:26 +0530
Subject: [PATCH 3/4] Updated tests
---
libc/src/__support/FPUtil/BasicOperations.h | 2 +-
libc/test/src/math/smoke/CanonicalizeTest.h | 12 ++++++------
2 files changed, 7 insertions(+), 7 deletions(-)
diff --git a/libc/src/__support/FPUtil/BasicOperations.h b/libc/src/__support/FPUtil/BasicOperations.h
index 6e4156497618e2..ec00d49a28633f 100644
--- a/libc/src/__support/FPUtil/BasicOperations.h
+++ b/libc/src/__support/FPUtil/BasicOperations.h
@@ -192,7 +192,7 @@ LIBC_INLINE int canonicalize(T &cx, const T &x) {
// All zeroes | One | Anything | Pseudo Denormal, Value =
// | | | (−1)**s × m × 2**−16382
// All Other | Zero | Anything | Unnormal, Value = SNaN
- // Values | | |
+ // Values | | |
bool bit63 = sx.get_implicit_bit();
UInt128 mantissa = sx.get_explicit_mantissa();
bool bit62 = static_cast<bool>((mantissa & (1ULL << 62)) >> 62);
diff --git a/libc/test/src/math/smoke/CanonicalizeTest.h b/libc/test/src/math/smoke/CanonicalizeTest.h
index 5b05396cd9ae93..1e5b4b770d25ab 100644
--- a/libc/test/src/math/smoke/CanonicalizeTest.h
+++ b/libc/test/src/math/smoke/CanonicalizeTest.h
@@ -138,32 +138,32 @@ class CanonicalizeTest : public LIBC_NAMESPACE::testing::Test {
// All Other | Zero | Anything | Unnormal, Value = SNaN
// Values | | |
- FPBits test5_1(UInt128(0x0000000000000001));
+ FPBits test5_1((UInt128(0x0040) << 64) + UInt128(0x0000000000000001));
const T test5_1_val = test5_1.get_val();
TEST_SPECIAL(cx, test5_1_val, 1, FE_INVALID);
EXPECT_FP_EQ(cx, aNaN);
- FPBits test5_2(UInt128(0x0000004270000001));
+ FPBits test5_2((UInt128(0x0230) << 64) + UInt128(0x0000004270000001));
const T test5_2_val = test5_2.get_val();
TEST_SPECIAL(cx, test5_2_val, 1, FE_INVALID);
EXPECT_FP_EQ(cx, aNaN);
- FPBits test5_3(UInt128(0x0000000008261001));
+ FPBits test5_3((UInt128(0x0560) << 64) + UInt128(0x0000000008261001));
const T test5_3_val = test5_3.get_val();
TEST_SPECIAL(cx, test5_3_val, 1, FE_INVALID);
EXPECT_FP_EQ(cx, aNaN);
- FPBits test5_4(UInt128(0x0000002816000000));
+ FPBits test5_4((UInt128(0x0780) << 64) + UInt128(0x0000002816000000));
const T test5_4_val = test5_4.get_val();
TEST_SPECIAL(cx, test5_4_val, 1, FE_INVALID);
EXPECT_FP_EQ(cx, aNaN);
- FPBits test5_5(UInt128(0x0000004270000001));
+ FPBits test5_5((UInt128(0x0900) << 64) + UInt128(0x0000004270000001));
const T test5_5_val = test5_5.get_val();
TEST_SPECIAL(cx, test5_5_val, 1, FE_INVALID);
EXPECT_FP_EQ(cx, aNaN);
- FPBits test5_6(UInt128(0x0000000008261001));
+ FPBits test5_6((UInt128(0x0ab0) << 64) + UInt128(0x0000000008261001));
const T test5_6_val = test5_6.get_val();
TEST_SPECIAL(cx, test5_6_val, 1, FE_INVALID);
EXPECT_FP_EQ(cx, aNaN);
>From af1c272f19f0fbcaa7c213a40e6b132c979ffc71 Mon Sep 17 00:00:00 2001
From: Sh0g0-1758 <shouryagoel10000 at gmail.com>
Date: Fri, 29 Mar 2024 00:07:18 +0530
Subject: [PATCH 4/4] Ran formatter
---
libc/src/__support/FPUtil/BasicOperations.h | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/libc/src/__support/FPUtil/BasicOperations.h b/libc/src/__support/FPUtil/BasicOperations.h
index ec00d49a28633f..6e4156497618e2 100644
--- a/libc/src/__support/FPUtil/BasicOperations.h
+++ b/libc/src/__support/FPUtil/BasicOperations.h
@@ -192,7 +192,7 @@ LIBC_INLINE int canonicalize(T &cx, const T &x) {
// All zeroes | One | Anything | Pseudo Denormal, Value =
// | | | (−1)**s × m × 2**−16382
// All Other | Zero | Anything | Unnormal, Value = SNaN
- // Values | | |
+ // Values | | |
bool bit63 = sx.get_implicit_bit();
UInt128 mantissa = sx.get_explicit_mantissa();
bool bit62 = static_cast<bool>((mantissa & (1ULL << 62)) >> 62);
More information about the libc-commits
mailing list