[libc-commits] [libc] [libc][NFC] Use FPBits builders instead of custom constructs (PR #75942)
Guillaume Chatelet via libc-commits
libc-commits at lists.llvm.org
Tue Dec 19 07:25:17 PST 2023
https://github.com/gchatelet created https://github.com/llvm/llvm-project/pull/75942
None
>From a42c5633dc564ae0d31ec65157ab509dbcf830dc Mon Sep 17 00:00:00 2001
From: Guillaume Chatelet <gchatelet at google.com>
Date: Tue, 19 Dec 2023 15:24:54 +0000
Subject: [PATCH] [libc][NFC] Use FPBits builders instead of custom constructs
---
libc/src/math/generic/coshf.cpp | 2 +-
libc/src/math/generic/exp.cpp | 4 ++--
libc/src/math/generic/exp10.cpp | 4 ++--
libc/src/math/generic/exp10f_impl.h | 2 +-
libc/src/math/generic/exp2.cpp | 4 ++--
libc/src/math/generic/expf.cpp | 4 ++--
libc/src/math/generic/expm1.cpp | 2 +-
libc/src/math/generic/expm1f.cpp | 2 +-
libc/src/math/generic/sinhf.cpp | 4 ++--
libc/test/src/math/FmaTest.h | 10 ++++------
libc/test/src/math/HypotTest.h | 18 +++++++++---------
libc/test/src/math/smoke/FmaTest.h | 10 ++++------
libc/test/src/math/smoke/HypotTest.h | 18 +++++++++---------
13 files changed, 40 insertions(+), 44 deletions(-)
diff --git a/libc/src/math/generic/coshf.cpp b/libc/src/math/generic/coshf.cpp
index 8bfcfa82e972bc..4a55b6940ff8c2 100644
--- a/libc/src/math/generic/coshf.cpp
+++ b/libc/src/math/generic/coshf.cpp
@@ -35,7 +35,7 @@ LLVM_LIBC_FUNCTION(float, coshf, (float x)) {
int rounding = fputil::quick_get_round();
if (LIBC_UNLIKELY(rounding == FE_DOWNWARD || rounding == FE_TOWARDZERO))
- return FPBits(FPBits::MAX_NORMAL).get_val();
+ return FPBits::max_normal();
fputil::set_errno_if_required(ERANGE);
fputil::raise_except_if_required(FE_OVERFLOW);
diff --git a/libc/src/math/generic/exp.cpp b/libc/src/math/generic/exp.cpp
index ebfd14dd6cc166..82a58aed8e15a5 100644
--- a/libc/src/math/generic/exp.cpp
+++ b/libc/src/math/generic/exp.cpp
@@ -199,7 +199,7 @@ double set_exceptional(double x) {
return x;
if (fputil::quick_get_round() == FE_UPWARD)
- return static_cast<double>(FPBits(FPBits::MIN_SUBNORMAL));
+ return FPBits::min_denormal();
fputil::set_errno_if_required(ERANGE);
fputil::raise_except_if_required(FE_UNDERFLOW);
return 0.0;
@@ -210,7 +210,7 @@ double set_exceptional(double x) {
if (x_u < 0x7ff0'0000'0000'0000ULL) {
int rounding = fputil::quick_get_round();
if (rounding == FE_DOWNWARD || rounding == FE_TOWARDZERO)
- return static_cast<double>(FPBits(FPBits::MAX_NORMAL));
+ return FPBits::max_normal();
fputil::set_errno_if_required(ERANGE);
fputil::raise_except_if_required(FE_OVERFLOW);
diff --git a/libc/src/math/generic/exp10.cpp b/libc/src/math/generic/exp10.cpp
index 4e1babcee541bd..9e911f286a17a3 100644
--- a/libc/src/math/generic/exp10.cpp
+++ b/libc/src/math/generic/exp10.cpp
@@ -246,7 +246,7 @@ double set_exceptional(double x) {
return x;
if (fputil::quick_get_round() == FE_UPWARD)
- return static_cast<double>(FPBits(FPBits::MIN_SUBNORMAL));
+ return FPBits::min_denormal();
fputil::set_errno_if_required(ERANGE);
fputil::raise_except_if_required(FE_UNDERFLOW);
return 0.0;
@@ -260,7 +260,7 @@ double set_exceptional(double x) {
if (x_u < 0x7ff0'0000'0000'0000ULL) {
int rounding = fputil::quick_get_round();
if (rounding == FE_DOWNWARD || rounding == FE_TOWARDZERO)
- return static_cast<double>(FPBits(FPBits::MAX_NORMAL));
+ return FPBits::max_normal();
fputil::set_errno_if_required(ERANGE);
fputil::raise_except_if_required(FE_OVERFLOW);
diff --git a/libc/src/math/generic/exp10f_impl.h b/libc/src/math/generic/exp10f_impl.h
index 24888b7d4c98f5..a2ec5391ba50a7 100644
--- a/libc/src/math/generic/exp10f_impl.h
+++ b/libc/src/math/generic/exp10f_impl.h
@@ -42,7 +42,7 @@ LIBC_INLINE float exp10f(float x) {
if (xbits.is_nan())
return x;
if (fputil::fenv_is_round_up())
- return static_cast<float>(FPBits(FPBits::MIN_SUBNORMAL));
+ return FPBits::min_denormal();
fputil::set_errno_if_required(ERANGE);
fputil::raise_except_if_required(FE_UNDERFLOW);
return 0.0f;
diff --git a/libc/src/math/generic/exp2.cpp b/libc/src/math/generic/exp2.cpp
index 07691ca0e7b62a..d2753d723d85b9 100644
--- a/libc/src/math/generic/exp2.cpp
+++ b/libc/src/math/generic/exp2.cpp
@@ -221,7 +221,7 @@ double set_exceptional(double x) {
return x;
if (fputil::quick_get_round() == FE_UPWARD)
- return static_cast<double>(FPBits(FPBits::MIN_SUBNORMAL));
+ return FPBits::min_denormal();
fputil::set_errno_if_required(ERANGE);
fputil::raise_except_if_required(FE_UNDERFLOW);
return 0.0;
@@ -235,7 +235,7 @@ double set_exceptional(double x) {
if (x_u < 0x7ff0'0000'0000'0000ULL) {
int rounding = fputil::quick_get_round();
if (rounding == FE_DOWNWARD || rounding == FE_TOWARDZERO)
- return static_cast<double>(FPBits(FPBits::MAX_NORMAL));
+ return FPBits::max_normal();
fputil::set_errno_if_required(ERANGE);
fputil::raise_except_if_required(FE_OVERFLOW);
diff --git a/libc/src/math/generic/expf.cpp b/libc/src/math/generic/expf.cpp
index 12f62960fc10ff..d0bb681df9e9dd 100644
--- a/libc/src/math/generic/expf.cpp
+++ b/libc/src/math/generic/expf.cpp
@@ -50,7 +50,7 @@ LLVM_LIBC_FUNCTION(float, expf, (float x)) {
if (xbits.is_nan())
return x;
if (fputil::fenv_is_round_up())
- return static_cast<float>(FPBits(FPBits::MIN_SUBNORMAL));
+ return FPBits::min_denormal();
fputil::set_errno_if_required(ERANGE);
fputil::raise_except_if_required(FE_UNDERFLOW);
return 0.0f;
@@ -61,7 +61,7 @@ LLVM_LIBC_FUNCTION(float, expf, (float x)) {
if (xbits.uintval() < 0x7f80'0000U) {
int rounding = fputil::quick_get_round();
if (rounding == FE_DOWNWARD || rounding == FE_TOWARDZERO)
- return static_cast<float>(FPBits(FPBits::MAX_NORMAL));
+ return FPBits::max_normal();
fputil::set_errno_if_required(ERANGE);
fputil::raise_except_if_required(FE_OVERFLOW);
diff --git a/libc/src/math/generic/expm1.cpp b/libc/src/math/generic/expm1.cpp
index a0d47f00828ce4..fc69934730a8f1 100644
--- a/libc/src/math/generic/expm1.cpp
+++ b/libc/src/math/generic/expm1.cpp
@@ -261,7 +261,7 @@ double set_exceptional(double x) {
if (x_u < 0x7ff0'0000'0000'0000ULL) {
int rounding = fputil::quick_get_round();
if (rounding == FE_DOWNWARD || rounding == FE_TOWARDZERO)
- return static_cast<double>(FPBits(FPBits::MAX_NORMAL));
+ return FPBits::max_normal();
fputil::set_errno_if_required(ERANGE);
fputil::raise_except_if_required(FE_OVERFLOW);
diff --git a/libc/src/math/generic/expm1f.cpp b/libc/src/math/generic/expm1f.cpp
index c51cb1d555a91e..3739a7bbbaa84c 100644
--- a/libc/src/math/generic/expm1f.cpp
+++ b/libc/src/math/generic/expm1f.cpp
@@ -68,7 +68,7 @@ LLVM_LIBC_FUNCTION(float, expm1f, (float x)) {
if (xbits.uintval() < 0x7f80'0000U) {
int rounding = fputil::quick_get_round();
if (rounding == FE_DOWNWARD || rounding == FE_TOWARDZERO)
- return static_cast<float>(FPBits(FPBits::MAX_NORMAL));
+ return FPBits::max_normal();
fputil::set_errno_if_required(ERANGE);
fputil::raise_except_if_required(FE_OVERFLOW);
diff --git a/libc/src/math/generic/sinhf.cpp b/libc/src/math/generic/sinhf.cpp
index db6794620b068c..f174a0f4fc34c2 100644
--- a/libc/src/math/generic/sinhf.cpp
+++ b/libc/src/math/generic/sinhf.cpp
@@ -57,10 +57,10 @@ LLVM_LIBC_FUNCTION(float, sinhf, (float x)) {
int rounding = fputil::quick_get_round();
if (sign) {
if (LIBC_UNLIKELY(rounding == FE_UPWARD || rounding == FE_TOWARDZERO))
- return FPBits(FPBits::MAX_NORMAL | FPBits::SIGN_MASK).get_val();
+ return -FPBits::max_normal();
} else {
if (LIBC_UNLIKELY(rounding == FE_DOWNWARD || rounding == FE_TOWARDZERO))
- return FPBits(FPBits::MAX_NORMAL).get_val();
+ return FPBits::max_normal();
}
fputil::set_errno_if_required(ERANGE);
diff --git a/libc/test/src/math/FmaTest.h b/libc/test/src/math/FmaTest.h
index 57eb43821c5a4e..af895e29f33c3e 100644
--- a/libc/test/src/math/FmaTest.h
+++ b/libc/test/src/math/FmaTest.h
@@ -50,16 +50,14 @@ class FmaTestTemplate : public LIBC_NAMESPACE::testing::Test {
EXPECT_FP_EQ(func(inf, neg_inf, nan), nan);
// Test underflow rounding up.
- EXPECT_FP_EQ(func(T(0.5), T(FPBits(FPBits::MIN_SUBNORMAL)),
- T(FPBits(FPBits::MIN_SUBNORMAL))),
+ EXPECT_FP_EQ(func(T(0.5), FPBits::min_denormal(), FPBits::min_denormal()),
T(FPBits(StorageType(2))));
// Test underflow rounding down.
T v = T(FPBits(FPBits::MIN_NORMAL + StorageType(1)));
- EXPECT_FP_EQ(func(T(1) / T(FPBits::MIN_NORMAL << 1), v,
- T(FPBits(FPBits::MIN_NORMAL))),
- v);
+ EXPECT_FP_EQ(
+ func(T(1) / T(FPBits::MIN_NORMAL << 1), v, FPBits::min_normal()), v);
// Test overflow.
- T z = T(FPBits(FPBits::MAX_NORMAL));
+ T z = FPBits::max_normal();
EXPECT_FP_EQ(func(T(1.75), z, -z), T(0.75) * z);
// Exact cancellation.
EXPECT_FP_EQ(func(T(3.0), T(5.0), -T(15.0)), T(0.0));
diff --git a/libc/test/src/math/HypotTest.h b/libc/test/src/math/HypotTest.h
index b9567c4b464164..4d8ef86fa9c461 100644
--- a/libc/test/src/math/HypotTest.h
+++ b/libc/test/src/math/HypotTest.h
@@ -24,15 +24,15 @@ class HypotTestTemplate : public LIBC_NAMESPACE::testing::Test {
using Func = T (*)(T, T);
using FPBits = LIBC_NAMESPACE::fputil::FPBits<T>;
using StorageType = typename FPBits::StorageType;
- const T nan = T(FPBits::build_quiet_nan(1));
- const T inf = T(FPBits::inf());
- const T neg_inf = T(FPBits::neg_inf());
- const T zero = T(FPBits::zero());
- const T neg_zero = T(FPBits::neg_zero());
- const T max_normal = T(FPBits(FPBits::MAX_NORMAL));
- const T min_normal = T(FPBits(FPBits::MIN_NORMAL));
- const T max_subnormal = T(FPBits(FPBits::MAX_SUBNORMAL));
- const T min_subnormal = T(FPBits(FPBits::MIN_SUBNORMAL));
+ const T nan = FPBits::build_quiet_nan(1);
+ const T inf = FPBits::inf();
+ const T neg_inf = FPBits::neg_inf();
+ const T zero = FPBits::zero();
+ const T neg_zero = FPBits::neg_zero();
+ const T max_normal = FPBits::max_normal();
+ const T min_normal = FPBits::min_normal();
+ const T max_subnormal = FPBits::max_denormal();
+ const T min_subnormal = FPBits::min_denormal();
public:
void test_special_numbers(Func func) {
diff --git a/libc/test/src/math/smoke/FmaTest.h b/libc/test/src/math/smoke/FmaTest.h
index 0b4c7001f936ef..9dd8dbb534a68e 100644
--- a/libc/test/src/math/smoke/FmaTest.h
+++ b/libc/test/src/math/smoke/FmaTest.h
@@ -37,16 +37,14 @@ class FmaTestTemplate : public LIBC_NAMESPACE::testing::Test {
EXPECT_FP_EQ(func(inf, neg_inf, nan), nan);
// Test underflow rounding up.
- EXPECT_FP_EQ(func(T(0.5), T(FPBits(FPBits::MIN_SUBNORMAL)),
- T(FPBits(FPBits::MIN_SUBNORMAL))),
+ EXPECT_FP_EQ(func(T(0.5), FPBits::min_denormal(), FPBits::min_denormal()),
T(FPBits(StorageType(2))));
// Test underflow rounding down.
T v = T(FPBits(FPBits::MIN_NORMAL + StorageType(1)));
- EXPECT_FP_EQ(func(T(1) / T(FPBits::MIN_NORMAL << 1), v,
- T(FPBits(FPBits::MIN_NORMAL))),
- v);
+ EXPECT_FP_EQ(
+ func(T(1) / T(FPBits::MIN_NORMAL << 1), v, FPBits::min_normal()), v);
// Test overflow.
- T z = T(FPBits(FPBits::MAX_NORMAL));
+ T z = FPBits::max_normal();
EXPECT_FP_EQ(func(T(1.75), z, -z), T(0.75) * z);
// Exact cancellation.
EXPECT_FP_EQ(func(T(3.0), T(5.0), -T(15.0)), T(0.0));
diff --git a/libc/test/src/math/smoke/HypotTest.h b/libc/test/src/math/smoke/HypotTest.h
index 308dbb50c95b78..30200d120ed9d8 100644
--- a/libc/test/src/math/smoke/HypotTest.h
+++ b/libc/test/src/math/smoke/HypotTest.h
@@ -21,15 +21,15 @@ class HypotTestTemplate : public LIBC_NAMESPACE::testing::Test {
using Func = T (*)(T, T);
using FPBits = LIBC_NAMESPACE::fputil::FPBits<T>;
using StorageType = typename FPBits::StorageType;
- const T nan = T(FPBits::build_quiet_nan(1));
- const T inf = T(FPBits::inf());
- const T neg_inf = T(FPBits::neg_inf());
- const T zero = T(FPBits::zero());
- const T neg_zero = T(FPBits::neg_zero());
- const T max_normal = T(FPBits(FPBits::MAX_NORMAL));
- const T min_normal = T(FPBits(FPBits::MIN_NORMAL));
- const T max_subnormal = T(FPBits(FPBits::MAX_SUBNORMAL));
- const T min_subnormal = T(FPBits(FPBits::MIN_SUBNORMAL));
+ const T nan = FPBits::build_quiet_nan(1);
+ const T inf = FPBits::inf();
+ const T neg_inf = FPBits::neg_inf();
+ const T zero = FPBits::zero();
+ const T neg_zero = FPBits::neg_zero();
+ const T max_normal = FPBits::max_normal();
+ const T min_normal = FPBits::min_normal();
+ const T max_subnormal = FPBits::max_denormal();
+ const T min_subnormal = FPBits::min_denormal();
public:
void test_special_numbers(Func func) {
More information about the libc-commits
mailing list