Skip to content

Commit 0f4b3c4

Browse files
authored
[libc][math] Add tests and fix some issues with FTZ/DAZ modes. (#113744)
1 parent 0df70c2 commit 0f4b3c4

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+1135
-38
lines changed

libc/src/math/generic/atan2.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,8 +230,8 @@ LLVM_LIBC_FUNCTION(double, atan2, (double y, double x)) {
230230
if (LIBC_UNLIKELY(max_exp > 0x7ffU - 128U || min_exp < 128U)) {
231231
if (x_bits.is_nan() || y_bits.is_nan())
232232
return FPBits::quiet_nan().get_val();
233-
unsigned x_except = x_abs == 0 ? 0 : (FPBits(x_abs).is_inf() ? 2 : 1);
234-
unsigned y_except = y_abs == 0 ? 0 : (FPBits(y_abs).is_inf() ? 2 : 1);
233+
unsigned x_except = x == 0.0 ? 0 : (FPBits(x_abs).is_inf() ? 2 : 1);
234+
unsigned y_except = y == 0.0 ? 0 : (FPBits(y_abs).is_inf() ? 2 : 1);
235235

236236
// Exceptional cases:
237237
// EXCEPT[y_except][x_except][x_is_neg]

libc/src/math/generic/cbrt.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,9 +151,10 @@ LLVM_LIBC_FUNCTION(double, cbrt, (double x)) {
151151

152152
if (LIBC_UNLIKELY(x_abs < FPBits::min_normal().uintval() ||
153153
x_abs >= FPBits::inf().uintval())) {
154-
if (x_abs == 0 || x_abs >= FPBits::inf().uintval())
154+
if (x == 0.0 || x_abs >= FPBits::inf().uintval())
155155
// x is 0, Inf, or NaN.
156-
return x;
156+
// Make sure it works for FTZ/DAZ modes.
157+
return static_cast<double>(x + x);
157158

158159
// x is non-zero denormal number.
159160
// Normalize x.

libc/src/math/generic/cbrtf.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,10 @@ LLVM_LIBC_FUNCTION(float, cbrtf, (float x)) {
9393
uint32_t x_abs = x_bits.uintval() & 0x7fff'ffff;
9494
uint32_t sign_bit = (x_bits.uintval() >> 31) << DoubleBits::EXP_LEN;
9595

96-
if (LIBC_UNLIKELY(x_abs == 0 || x_abs >= 0x7f80'0000)) {
96+
if (LIBC_UNLIKELY(x == 0.0f || x_abs >= 0x7f80'0000)) {
9797
// x is 0, Inf, or NaN.
98-
return x;
98+
// Make sure it works for FTZ/DAZ modes.
99+
return x + x;
99100
}
100101

101102
double xd = static_cast<double>(x);

libc/src/math/generic/log.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -749,7 +749,7 @@ LLVM_LIBC_FUNCTION(double, log, (double x)) {
749749

750750
if (LIBC_UNLIKELY(xbits.uintval() < FPBits_t::min_normal().uintval() ||
751751
xbits.uintval() > FPBits_t::max_normal().uintval())) {
752-
if (xbits.is_zero()) {
752+
if (x == 0.0) {
753753
// return -Inf and raise FE_DIVBYZERO.
754754
fputil::set_errno_if_required(ERANGE);
755755
fputil::raise_except_if_required(FE_DIVBYZERO);

libc/src/math/generic/log10.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -751,7 +751,7 @@ LLVM_LIBC_FUNCTION(double, log10, (double x)) {
751751

752752
if (LIBC_UNLIKELY(xbits.uintval() < FPBits_t::min_normal().uintval() ||
753753
xbits.uintval() > FPBits_t::max_normal().uintval())) {
754-
if (xbits.is_zero()) {
754+
if (x == 0.0) {
755755
// return -Inf and raise FE_DIVBYZERO.
756756
fputil::set_errno_if_required(ERANGE);
757757
fputil::raise_except_if_required(FE_DIVBYZERO);

libc/src/math/generic/log10f.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ LLVM_LIBC_FUNCTION(float, log10f, (float x)) {
164164

165165
if (LIBC_UNLIKELY(x_u < FPBits::min_normal().uintval() ||
166166
x_u > FPBits::max_normal().uintval())) {
167-
if (xbits.is_zero()) {
167+
if (x == 0.0f) {
168168
// Return -inf and raise FE_DIVBYZERO
169169
fputil::set_errno_if_required(ERANGE);
170170
fputil::raise_except_if_required(FE_DIVBYZERO);

libc/src/math/generic/log1p.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -927,8 +927,8 @@ LLVM_LIBC_FUNCTION(double, log1p, (double x)) {
927927
// log(1 + x) = nextafter(x, -inf) for FE_DOWNWARD, or
928928
// FE_TOWARDZERO and x > 0,
929929
// = x otherwise.
930-
if (LIBC_UNLIKELY(xbits.is_zero()))
931-
return x;
930+
if (x == 0.0)
931+
return x + x; // Handle FTZ/DAZ correctly.
932932

933933
volatile float tp = 1.0f;
934934
volatile float tn = -1.0f;
@@ -943,7 +943,7 @@ LLVM_LIBC_FUNCTION(double, log1p, (double x)) {
943943
return FPBits_t(x_u + 1).get_val();
944944
}
945945

946-
return x;
946+
return (x + x == 0.0) ? x + x : x;
947947
}
948948
x_dd = fputil::exact_add(1.0, x);
949949
}

libc/src/math/generic/log2.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -871,7 +871,7 @@ LLVM_LIBC_FUNCTION(double, log2, (double x)) {
871871

872872
if (LIBC_UNLIKELY(xbits.uintval() < FPBits_t::min_normal().uintval() ||
873873
xbits.uintval() > FPBits_t::max_normal().uintval())) {
874-
if (xbits.is_zero()) {
874+
if (x == 0.0) {
875875
// return -Inf and raise FE_DIVBYZERO.
876876
fputil::set_errno_if_required(ERANGE);
877877
fputil::raise_except_if_required(FE_DIVBYZERO);

libc/src/math/generic/log2f.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ LLVM_LIBC_FUNCTION(float, log2f, (float x)) {
7272
// Exceptional inputs.
7373
if (LIBC_UNLIKELY(x_u < FPBits::min_normal().uintval() ||
7474
x_u > FPBits::max_normal().uintval())) {
75-
if (xbits.is_zero()) {
75+
if (x == 0.0f) {
7676
fputil::set_errno_if_required(ERANGE);
7777
fputil::raise_except_if_required(FE_DIVBYZERO);
7878
return FPBits::inf(Sign::NEG).get_val();

libc/src/math/generic/logf.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ LLVM_LIBC_FUNCTION(float, logf, (float x)) {
8282
}
8383
// Subnormal inputs.
8484
if (LIBC_UNLIKELY(x_u < FPBits::min_normal().uintval())) {
85-
if (x_u == 0) {
85+
if (x == 0.0f) {
8686
// Return -inf and raise FE_DIVBYZERO
8787
fputil::set_errno_if_required(ERANGE);
8888
fputil::raise_except_if_required(FE_DIVBYZERO);

0 commit comments

Comments
 (0)