[libc-commits] [libc] [libc][math] Report range errors at half precision logarithm poles (PR #236471)

via libc-commits libc-commits at lists.llvm.org
Wed Oct 28 20:27:33 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-libc

Author: sriramshastry

<details>
<summary>Changes</summary>

At zero, logf16, log2f16 and log10f16 already return negative infinity and raise
FE_DIVBYZERO. log2p1f16 and log10p1f16 do the same at minus one. Set ERANGE through the
existing conditional helper on these pole paths and update the smoke tests that
currently expect zero errno.

## Validation

Tested independently at ea08830d976fbe61384695d33e0acafee8d88737 on x86-64 Linux,
Release, GCC 13 and Clang 18. With only the revised tests, 20/20 smoke target
configurations fail per compiler; with the repair, 20/20 pass. Current logs and tested
diff are in the local packet's evidence/upstream-latest-20261028/. Changed-hunk
formatting and whitespace checks pass. No approximation or performance claim is made.

## Scope and Review

Keep the five identical pole-reporting corrections together. Confirm that zero remains
valid for log2p1f16/log10p1f16.

## Updated submission base

Submission base: 10c142fa799e258285b6cfb8fce153864586152d.
The standalone baseline/patched results above remain recorded at their original
tested snapshot. At this newer base, the fourteen-only combined suite passes
150 native configurations per compiler and twelve four-mode numerical runs,
with zero value or scoped-reporting mismatches.

Signed-off-by: sriramshastry <sriramshastry@<!-- -->gmail.com>


---
Full diff: https://github.com/llvm/llvm-project/pull/236471.diff


10 Files Affected:

- (modified) libc/src/__support/math/log10f16.h (+1) 
- (modified) libc/src/__support/math/log10p1f16.h (+1) 
- (modified) libc/src/__support/math/log2f16.h (+1) 
- (modified) libc/src/__support/math/log2p1f16.h (+1) 
- (modified) libc/src/__support/math/logf16.h (+1) 
- (modified) libc/test/src/math/smoke/log10f16_test.cpp (+2-2) 
- (modified) libc/test/src/math/smoke/log10p1f16_test.cpp (+1-1) 
- (modified) libc/test/src/math/smoke/log2f16_test.cpp (+2-2) 
- (modified) libc/test/src/math/smoke/log2p1f16_test.cpp (+1-1) 
- (modified) libc/test/src/math/smoke/logf16_test.cpp (+2-2) 


``````````diff
diff --git a/libc/src/__support/math/log10f16.h b/libc/src/__support/math/log10f16.h
index 0c0b9a98086d97..424cf5f39a0872 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 54533c994df812..6184a5795741b1 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 df9402f2c81759..4a738b5d5b8d45 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 4fe9de42d71b3f..c60c94dee24741 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 77682e6082e0bf..8915f60e972a85 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 0e8d054cbbb841..cb3b69da6cf3b4 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 b4afd89546264e..6e9040951ccc17 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 abefc67963c103..9debfc6815106b 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 d7b8826552e239..151f406f2b5f75 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 a8332c624c74ee..cffb71587f2e36 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)));

``````````

</details>


https://github.com/llvm/llvm-project/pull/236471


More information about the libc-commits mailing list