[libc-commits] [PATCH] D130502: [libc] Use nearest_integer instructions to improve expm1f performance.

Tue Ly via Phabricator via libc-commits libc-commits at lists.llvm.org
Mon Jul 25 10:46:12 PDT 2022


lntue created this revision.
lntue added reviewers: michaelrj, sivachandra, orex, zimmermann6.
Herald added subscribers: ecnelises, tschuett, mgorny.
Herald added projects: libc-project, All.
lntue requested review of this revision.

Use nearest_integer instructions to improve expf performance.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D130502

Files:
  libc/docs/math.rst
  libc/src/math/generic/CMakeLists.txt
  libc/src/math/generic/expm1f.cpp


Index: libc/src/math/generic/expm1f.cpp
===================================================================
--- libc/src/math/generic/expm1f.cpp
+++ libc/src/math/generic/expm1f.cpp
@@ -13,6 +13,8 @@
 #include "src/__support/FPUtil/FMA.h"
 #include "src/__support/FPUtil/FPBits.h"
 #include "src/__support/FPUtil/PolyEval.h"
+#include "src/__support/FPUtil/multiply_add.h"
+#include "src/__support/FPUtil/nearest_integer.h"
 #include "src/__support/common.h"
 
 #include <errno.h>
@@ -133,10 +135,10 @@
   // generated by Sollya.
 
   // x_hi = hi + mid.
-  int x_hi = static_cast<int>(x * 0x1.0p7f + (xbits.get_sign() ? -0.5f : 0.5f));
+  float kf = fputil::nearest_integer(x * 0x1.0p7f);
+  int x_hi = static_cast<int>(kf);
   // Subtract (hi + mid) from x to get lo.
-  x -= static_cast<float>(x_hi) * 0x1.0p-7f;
-  double xd = static_cast<double>(x);
+  double xd = static_cast<double>(fputil::multiply_add(kf, -0x1.0p-7f, x));
   x_hi += 104 << 7;
   // hi = x_hi >> 7
   double exp_hi = EXP_M1[x_hi >> 7];
Index: libc/src/math/generic/CMakeLists.txt
===================================================================
--- libc/src/math/generic/CMakeLists.txt
+++ libc/src/math/generic/CMakeLists.txt
@@ -513,6 +513,7 @@
     .common_constants
     libc.src.__support.FPUtil.fputil
     libc.src.__support.FPUtil.multiply_add
+    libc.src.__support.FPUtil.nearest_integer
     libc.src.__support.FPUtil.polyeval
     libc.include.math
   COMPILE_OPTIONS
Index: libc/docs/math.rst
===================================================================
--- libc/docs/math.rst
+++ libc/docs/math.rst
@@ -203,7 +203,7 @@
 +--------------+-----------+-------------------+-----------+-------------------+-------------------------------------+------------+-------------------------+--------------+---------------+
 | exp2f        |        25 |                 8 |        81 |                37 | :math:`[-10, 10]`                   | Ryzen 1700 | Ubuntu 20.04 LTS x86_64 | Clang 12.0.0 | FMA           |
 +--------------+-----------+-------------------+-----------+-------------------+-------------------------------------+------------+-------------------------+--------------+---------------+
-| expm1f       |        14 |                53 |        59 |               146 | :math:`[-10, 10]`                   | Ryzen 1700 | Ubuntu 20.04 LTS x86_64 | Clang 12.0.0 | FMA           |
+| expm1f       |         9 |                44 |        42 |               121 | :math:`[-10, 10]`                   | Ryzen 1700 | Ubuntu 20.04 LTS x86_64 | Clang 12.0.0 | FMA           |
 +--------------+-----------+-------------------+-----------+-------------------+-------------------------------------+------------+-------------------------+--------------+---------------+
 | fmodf        |        73 |               263 |        -  |                 - | [MIN_NORMAL, MAX_NORMAL]            | i5 mobile  | Ubuntu 20.04 LTS x86_64 | Clang 12.0.0 |               |
 |              +-----------+-------------------+-----------+-------------------+-------------------------------------+------------+-------------------------+--------------+---------------+


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D130502.447407.patch
Type: text/x-patch
Size: 3140 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libc-commits/attachments/20220725/df844dec/attachment.bin>


More information about the libc-commits mailing list