[libc-commits] [libc] 52b0574 - [libc][math] Report range errors at half precision logarithm poles (#236471)
via libc-commits
libc-commits at lists.llvm.org
Mon Nov 9 09:00:57 PST 2026
Author: sriramshastry
Date: 2026-11-09T12:00:50-05:00
New Revision: 52b0574f653e218922982434b8e54df6d55d7674
URL: https://github.com/llvm/llvm-project/commit/52b0574f653e218922982434b8e54df6d55d7674
DIFF: https://github.com/llvm/llvm-project/commit/52b0574f653e218922982434b8e54df6d55d7674.diff
LOG: [libc][math] Report range errors at half precision logarithm poles (#236471)
Set ERANGE on the existing pole paths: zero for logf16, log2f16 and
log10f16, and -1 for log2p1f16 and log10p1f16. Keep the existing
negative
infinity result and FE_DIVBYZERO behavior. Use the conditional errno
helper
and update the smoke tests.
Tests: 20 smoke configurations per compiler with GCC 13 and Clang 18 on
x86-64 Linux.
Assisted-by: GitHub Copilot
Signed-off-by: sriramshastry <sriramshastry at gmail.com>
Added:
Modified:
libc/src/__support/math/log10f16.h
libc/src/__support/math/log10p1f16.h
libc/src/__support/math/log2f16.h
libc/src/__support/math/log2p1f16.h
libc/src/__support/math/logf16.h
libc/test/src/math/smoke/log10f16_test.cpp
libc/test/src/math/smoke/log10p1f16_test.cpp
libc/test/src/math/smoke/log2f16_test.cpp
libc/test/src/math/smoke/log2p1f16_test.cpp
libc/test/src/math/smoke/logf16_test.cpp
Removed:
################################################################################
diff --git a/libc/src/__support/math/log10f16.h b/libc/src/__support/math/log10f16.h
index 0c0b9a98086d9..424cf5f39a087 100644
--- a/libc/src/__support/math/log10f16.h
+++ b/libc/src/__support/math/log10f16.h
@@ -105,6 +105,7 @@ LIBC_INLINE float16 log10f16(float16 x) {
// log10(+/-0) = −inf
if ((x_u & 0x7fffU) == 0U) {
+ fputil::set_errno_if_required(ERANGE);
fputil::raise_except_if_required(FE_DIVBYZERO);
return FPBits::inf(Sign::NEG).get_val();
}
diff --git a/libc/src/__support/math/log10p1f16.h b/libc/src/__support/math/log10p1f16.h
index 54533c994df81..6184a5795741b 100644
--- a/libc/src/__support/math/log10p1f16.h
+++ b/libc/src/__support/math/log10p1f16.h
@@ -114,6 +114,7 @@ LIBC_INLINE float16 log10p1f16(float16 x) {
// log10p1(-1) = -inf
if (LIBC_UNLIKELY(x_u == 0xbc00U)) {
+ fputil::set_errno_if_required(ERANGE);
fputil::raise_except_if_required(FE_DIVBYZERO);
return FPBits::inf(Sign::NEG).get_val();
}
diff --git a/libc/src/__support/math/log2f16.h b/libc/src/__support/math/log2f16.h
index df9402f2c8175..4a738b5d5b8d4 100644
--- a/libc/src/__support/math/log2f16.h
+++ b/libc/src/__support/math/log2f16.h
@@ -93,6 +93,7 @@ LIBC_INLINE float16 log2f16(float16 x) {
// log2(+/-0) = −inf
if ((x_u & 0x7fffU) == 0U) {
+ fputil::set_errno_if_required(ERANGE);
fputil::raise_except_if_required(FE_DIVBYZERO);
return FPBits::inf(Sign::NEG).get_val();
}
diff --git a/libc/src/__support/math/log2p1f16.h b/libc/src/__support/math/log2p1f16.h
index 4fe9de42d71b3..c60c94dee2474 100644
--- a/libc/src/__support/math/log2p1f16.h
+++ b/libc/src/__support/math/log2p1f16.h
@@ -120,6 +120,7 @@ LIBC_INLINE float16 log2p1f16(float16 x) {
// log2p1(-1) = -inf
if (LIBC_UNLIKELY(x_u == 0xbc00U)) {
+ fputil::set_errno_if_required(ERANGE);
fputil::raise_except_if_required(FE_DIVBYZERO);
return FPBits::inf(Sign::NEG).get_val();
}
diff --git a/libc/src/__support/math/logf16.h b/libc/src/__support/math/logf16.h
index 77682e6082e0b..8915f60e972a8 100644
--- a/libc/src/__support/math/logf16.h
+++ b/libc/src/__support/math/logf16.h
@@ -100,6 +100,7 @@ LIBC_INLINE float16 logf16(float16 x) {
// log(+/-0) = −inf
if ((x_u & 0x7fffU) == 0U) {
+ fputil::set_errno_if_required(ERANGE);
fputil::raise_except_if_required(FE_DIVBYZERO);
return FPBits::inf(Sign::NEG).get_val();
}
diff --git a/libc/test/src/math/smoke/log10f16_test.cpp b/libc/test/src/math/smoke/log10f16_test.cpp
index 0e8d054cbbb84..cb3b69da6cf3b 100644
--- a/libc/test/src/math/smoke/log10f16_test.cpp
+++ b/libc/test/src/math/smoke/log10f16_test.cpp
@@ -30,11 +30,11 @@ TEST_F(LlvmLibcLog10f16Test, SpecialNumbers) {
EXPECT_FP_EQ_WITH_EXCEPTION_ALL_ROUNDING(
neg_inf, LIBC_NAMESPACE::log10f16(zero), FE_DIVBYZERO);
- EXPECT_MATH_ERRNO(0);
+ EXPECT_MATH_ERRNO(ERANGE);
EXPECT_FP_EQ_WITH_EXCEPTION_ALL_ROUNDING(
neg_inf, LIBC_NAMESPACE::log10f16(neg_zero), FE_DIVBYZERO);
- EXPECT_MATH_ERRNO(0);
+ EXPECT_MATH_ERRNO(ERANGE);
EXPECT_FP_EQ_ALL_ROUNDING(
zero,
diff --git a/libc/test/src/math/smoke/log10p1f16_test.cpp b/libc/test/src/math/smoke/log10p1f16_test.cpp
index b4afd89546264..6e9040951ccc1 100644
--- a/libc/test/src/math/smoke/log10p1f16_test.cpp
+++ b/libc/test/src/math/smoke/log10p1f16_test.cpp
@@ -39,7 +39,7 @@ TEST_F(LlvmLibcLog10p1f16Test, SpecialNumbers) {
neg_inf,
LIBC_NAMESPACE::log10p1f16(LIBC_NAMESPACE::fputil::cast<float16>(-1.0)),
FE_DIVBYZERO);
- EXPECT_MATH_ERRNO(0);
+ EXPECT_MATH_ERRNO(ERANGE);
EXPECT_FP_EQ_ALL_ROUNDING(
aNaN,
diff --git a/libc/test/src/math/smoke/log2f16_test.cpp b/libc/test/src/math/smoke/log2f16_test.cpp
index abefc67963c10..9debfc6815106 100644
--- a/libc/test/src/math/smoke/log2f16_test.cpp
+++ b/libc/test/src/math/smoke/log2f16_test.cpp
@@ -30,11 +30,11 @@ TEST_F(LlvmLibcLog2f16Test, SpecialNumbers) {
EXPECT_FP_EQ_WITH_EXCEPTION_ALL_ROUNDING(
neg_inf, LIBC_NAMESPACE::log2f16(zero), FE_DIVBYZERO);
- EXPECT_MATH_ERRNO(0);
+ EXPECT_MATH_ERRNO(ERANGE);
EXPECT_FP_EQ_WITH_EXCEPTION_ALL_ROUNDING(
neg_inf, LIBC_NAMESPACE::log2f16(neg_zero), FE_DIVBYZERO);
- EXPECT_MATH_ERRNO(0);
+ EXPECT_MATH_ERRNO(ERANGE);
EXPECT_FP_EQ_ALL_ROUNDING(
zero,
diff --git a/libc/test/src/math/smoke/log2p1f16_test.cpp b/libc/test/src/math/smoke/log2p1f16_test.cpp
index d7b8826552e23..151f406f2b5f7 100644
--- a/libc/test/src/math/smoke/log2p1f16_test.cpp
+++ b/libc/test/src/math/smoke/log2p1f16_test.cpp
@@ -39,7 +39,7 @@ TEST_F(LlvmLibcLog2p1f16Test, SpecialNumbers) {
neg_inf,
LIBC_NAMESPACE::log2p1f16(LIBC_NAMESPACE::fputil::cast<float16>(-1.0)),
FE_DIVBYZERO);
- EXPECT_MATH_ERRNO(0);
+ EXPECT_MATH_ERRNO(ERANGE);
EXPECT_FP_EQ_ALL_ROUNDING(
aNaN,
diff --git a/libc/test/src/math/smoke/logf16_test.cpp b/libc/test/src/math/smoke/logf16_test.cpp
index a8332c624c74e..cffb71587f2e3 100644
--- a/libc/test/src/math/smoke/logf16_test.cpp
+++ b/libc/test/src/math/smoke/logf16_test.cpp
@@ -30,11 +30,11 @@ TEST_F(LlvmLibcLogf16Test, SpecialNumbers) {
EXPECT_FP_EQ_WITH_EXCEPTION_ALL_ROUNDING(
neg_inf, LIBC_NAMESPACE::logf16(zero), FE_DIVBYZERO);
- EXPECT_MATH_ERRNO(0);
+ EXPECT_MATH_ERRNO(ERANGE);
EXPECT_FP_EQ_WITH_EXCEPTION_ALL_ROUNDING(
neg_inf, LIBC_NAMESPACE::logf16(neg_zero), FE_DIVBYZERO);
- EXPECT_MATH_ERRNO(0);
+ EXPECT_MATH_ERRNO(ERANGE);
EXPECT_FP_EQ_ALL_ROUNDING(
zero, LIBC_NAMESPACE::logf16(LIBC_NAMESPACE::fputil::cast<float16>(1.0)));
More information about the libc-commits
mailing list