[libc-commits] [libc] [libc][math][c23] Refactor expf16 (PR #101373)

via libc-commits libc-commits at lists.llvm.org
Wed Jul 31 10:41:19 PDT 2024


https://github.com/overmighty created https://github.com/llvm/llvm-project/pull/101373

Also updates and sorts CMake target dependencies, and corrects the smoke
test that expected expf16(sNaN) to return sNaN instead of aNaN, although
the test still passed, as FPMatcher only checks whether both sides are
NaN, not whether they're the same NaN value.


>From a6a61d2524065957c2ea3c89d66d8060bf3ce524 Mon Sep 17 00:00:00 2001
From: OverMighty <its.overmighty at gmail.com>
Date: Wed, 31 Jul 2024 19:23:27 +0200
Subject: [PATCH] [libc][math][c23] Refactor expf16

Also updates and sorts CMake target dependencies, and corrects the smoke
test that expected expf16(sNaN) to return sNaN instead of aNaN, although
the test still passed, as FPMatcher only checks whether both sides are
NaN, not whether they're the same NaN value.
---
 libc/src/math/generic/CMakeLists.txt                   | 3 ++-
 libc/src/math/generic/expf16.cpp                       | 7 +++----
 libc/test/src/math/performance_testing/expf16_perf.cpp | 2 +-
 libc/test/src/math/smoke/expf16_test.cpp               | 2 +-
 4 files changed, 7 insertions(+), 7 deletions(-)

diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index a4c1318f8a168..e707615e69b97 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -1236,11 +1236,12 @@ add_entrypoint_object(
     libc.hdr.errno_macros
     libc.hdr.fenv_macros
     libc.src.__support.CPP.array
+    libc.src.__support.FPUtil.except_value_utils
     libc.src.__support.FPUtil.fenv_impl
     libc.src.__support.FPUtil.fp_bits
-    libc.src.__support.FPUtil.polyeval
     libc.src.__support.FPUtil.multiply_add
     libc.src.__support.FPUtil.nearest_integer
+    libc.src.__support.FPUtil.polyeval
     libc.src.__support.FPUtil.rounding_mode
     libc.src.__support.macros.optimization
   COMPILE_OPTIONS
diff --git a/libc/src/math/generic/expf16.cpp b/libc/src/math/generic/expf16.cpp
index b618edc36a046..b198c559dfedb 100644
--- a/libc/src/math/generic/expf16.cpp
+++ b/libc/src/math/generic/expf16.cpp
@@ -127,9 +127,8 @@ LLVM_LIBC_FUNCTION(float16, expf16, (float16 x)) {
       //   > display = hexadecimal;
       //   > P = fpminimax(expm1(x)/x, 2, [|SG...|], [-2^-5, 2^-5]);
       //   > 1 + x * P;
-      float r =
-          fputil::polyeval(xf, 0x1p+0f, 0x1p+0f, 0x1.0004p-1f, 0x1.555778p-3f);
-      return static_cast<float16>(r);
+      return static_cast<float16>(
+          fputil::polyeval(xf, 0x1p+0f, 0x1p+0f, 0x1.0004p-1f, 0x1.555778p-3f));
     }
   }
 
@@ -150,7 +149,7 @@ LLVM_LIBC_FUNCTION(float16, expf16, (float16 x)) {
   // respectively.  exp(lo) is computed using a degree-3 minimax polynomial
   // generated by Sollya.
 
-  float xf = static_cast<float>(x);
+  float xf = x;
   float kf = fputil::nearest_integer(xf * 0x1.0p+3f);
   int x_hi_mid = static_cast<int>(kf);
   int x_hi = x_hi_mid >> 3;
diff --git a/libc/test/src/math/performance_testing/expf16_perf.cpp b/libc/test/src/math/performance_testing/expf16_perf.cpp
index c1213689ff5e7..bc9d9f05559a3 100644
--- a/libc/test/src/math/performance_testing/expf16_perf.cpp
+++ b/libc/test/src/math/performance_testing/expf16_perf.cpp
@@ -1,4 +1,4 @@
-//===-- Performancel test for expf16 --------------------------------------===//
+//===-- Performance test for expf16 ---------------------------------------===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
diff --git a/libc/test/src/math/smoke/expf16_test.cpp b/libc/test/src/math/smoke/expf16_test.cpp
index f05ecd0dc4d0e..969870fe247bc 100644
--- a/libc/test/src/math/smoke/expf16_test.cpp
+++ b/libc/test/src/math/smoke/expf16_test.cpp
@@ -21,7 +21,7 @@ TEST_F(LlvmLibcExpf16Test, SpecialNumbers) {
   EXPECT_FP_EQ_ALL_ROUNDING(aNaN, LIBC_NAMESPACE::expf16(aNaN));
   EXPECT_MATH_ERRNO(0);
 
-  EXPECT_FP_EQ_WITH_EXCEPTION(sNaN, LIBC_NAMESPACE::expf16(sNaN), FE_INVALID);
+  EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, LIBC_NAMESPACE::expf16(sNaN), FE_INVALID);
   EXPECT_MATH_ERRNO(0);
 
   EXPECT_FP_EQ_ALL_ROUNDING(inf, LIBC_NAMESPACE::expf16(inf));



More information about the libc-commits mailing list