[libc-commits] [libc] 1f3f90a - [libc] Make log2f correctly rounded for all rounding modes when FMA is not available.

Tue Ly via libc-commits libc-commits at lists.llvm.org
Thu Jan 20 13:16:24 PST 2022


Author: Tue Ly
Date: 2022-01-20T16:16:11-05:00
New Revision: 1f3f90ab8869887561acadb2540836e7959419f5

URL: https://github.com/llvm/llvm-project/commit/1f3f90ab8869887561acadb2540836e7959419f5
DIFF: https://github.com/llvm/llvm-project/commit/1f3f90ab8869887561acadb2540836e7959419f5.diff

LOG: [libc] Make log2f correctly rounded for all rounding modes when FMA is not available.

Add to log2f 2 more exceptional cases got when not using fma for polyeval.

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D117812

Added: 
    

Modified: 
    libc/src/math/generic/log2f.cpp
    libc/test/src/math/log2f_test.cpp

Removed: 
    


################################################################################
diff  --git a/libc/src/math/generic/log2f.cpp b/libc/src/math/generic/log2f.cpp
index d2f40fe6763d0..dc5a6b670afd5 100644
--- a/libc/src/math/generic/log2f.cpp
+++ b/libc/src/math/generic/log2f.cpp
@@ -105,11 +105,24 @@ LLVM_LIBC_FUNCTION(float, log2f, (float x)) {
   int m = 0;
 
   // Hard to round value(s).
-  if (FPBits(x).uintval() == 0x3f81d0b5U) {
+  switch (FPBits(x).uintval()) {
+  case 0x3f81d0b5U: {
     int rounding_mode = fputil::get_round();
     if (rounding_mode == FE_DOWNWARD || rounding_mode == FE_TOWARDZERO) {
       return 0x1.4cdc4cp-6f;
     }
+    break;
+  }
+  case 0x3f7e3274U:
+    if (fputil::get_round() == FE_TONEAREST) {
+      return -0x1.4e1d16p-7f;
+    }
+    break;
+  case 0x3f7d57f5U:
+    if (fputil::get_round() == FE_TOWARDZERO) {
+      return -0x1.ed1c32p-7f;
+    }
+    break;
   }
 
   // Exceptional inputs.

diff  --git a/libc/test/src/math/log2f_test.cpp b/libc/test/src/math/log2f_test.cpp
index 1708552f0c291..ecedfb68cc7a7 100644
--- a/libc/test/src/math/log2f_test.cpp
+++ b/libc/test/src/math/log2f_test.cpp
@@ -31,10 +31,10 @@ TEST(LlvmLibcLog2fTest, SpecialNumbers) {
 }
 
 TEST(LlvmLibcLog2fTest, TrickyInputs) {
-  constexpr int N = 9;
-  constexpr uint32_t INPUTS[N] = {0x3f7d57f5U, 0x3f7ed848U, 0x3f7fd6ccU,
-                                  0x3f7fffffU, 0x3f80079bU, 0x3f81d0b5U,
-                                  0x3f82e602U, 0x3f83c98dU, 0x3f8cba39U};
+  constexpr int N = 10;
+  constexpr uint32_t INPUTS[N] = {
+      0x3f7d57f5U, 0x3f7e3274U, 0x3f7ed848U, 0x3f7fd6ccU, 0x3f7fffffU,
+      0x3f80079bU, 0x3f81d0b5U, 0x3f82e602U, 0x3f83c98dU, 0x3f8cba39U};
 
   for (int i = 0; i < N; ++i) {
     float x = float(FPBits(INPUTS[i]));


        


More information about the libc-commits mailing list