]> git.ipfire.org Git - thirdparty/glibc.git/commitdiff
math: Use exp2m1f from CORE-MATH
authorAdhemerval Zanella <adhemerval.zanella@linaro.org>
Fri, 25 Oct 2024 18:21:48 +0000 (15:21 -0300)
committerAdhemerval Zanella <adhemerval.zanella@linaro.org>
Fri, 1 Nov 2024 14:27:35 +0000 (11:27 -0300)
The CORE-MATH implementation is correctly rounded (for any rounding mode)
and shows better performance compared to the generic exp2m1f.

The code was adapted to glibc style and to use the definition of
math_config.h (to handle errno, overflow, and underflow).  The
only change is to handle FLT_MAX_EXP for FE_DOWNWARD or FE_TOWARDZERO.

The benchmark inputs are based on exp2f ones.

Benchtest on x64_64 (Ryzen 9 5900X, gcc 14.2.1), aarch64 (Neoverse-N1,
gcc 13.3.1), and powerpc (POWER10, gcc 13.2.1):

Latency                      master        patched   improvement
x86_64                      40.6042        48.7104       -19.96%
x86_64v2                    40.7506        35.9032        11.90%
x86_64v3                    35.2301        31.7956        9.75%
i686                        102.094        94.6657        7.28%
aarch64                     18.2704        15.1387        17.14%
power10                     11.9444         8.2402        31.01%

reciprocal-throughput        master        patched   improvement
x86_64                      20.8683        16.1428        22.64%
x86_64v2                    19.5076        10.4474        46.44%
x86_64v3                    19.2106        10.4014        45.86%
i686                        56.4054        59.3004        -5.13%
aarch64                     12.0781         7.3953        38.77%
power10                      6.5306         5.9388         9.06%

The generic implementation calls __ieee754_exp2f and x86_64 provides
an optimized ifunc version (built with -mfma -mavx2, not correctly
rounded).  This explains the performance difference for x86_64.

Same for i686, where the ABI provides an optimized __ieee754_exp2f
version built with '-msse2 -mfpmath=sse'.  When built wth same
flags, the new algorithm shows a better performance:

                            master        patched    improvement
latency                    102.094        91.2823         10.59%
reciprocal-throughput      56.4054        52.7984          6.39%

Signed-off-by: Alexei Sibidanov <sibid@uvic.ca>
Signed-off-by: Paul Zimmermann <Paul.Zimmermann@inria.fr>
Signed-off-by: Adhemerval Zanella <adhemerval.zanella@linaro.org>
Reviewed-by: DJ Delorie <dj@redhat.com>
20 files changed:
SHARED-FILES
sysdeps/aarch64/libm-test-ulps
sysdeps/arc/fpu/libm-test-ulps
sysdeps/arc/nofpu/libm-test-ulps
sysdeps/arm/libm-test-ulps
sysdeps/hppa/fpu/libm-test-ulps
sysdeps/i386/fpu/libm-test-ulps
sysdeps/i386/i686/fpu/multiarch/libm-test-ulps
sysdeps/ieee754/flt-32/s_exp2m1f.c [new file with mode: 0644]
sysdeps/loongarch/lp64/libm-test-ulps
sysdeps/m68k/m680x0/fpu/libm-test-ulps
sysdeps/mips/mips32/libm-test-ulps
sysdeps/mips/mips64/libm-test-ulps
sysdeps/powerpc/fpu/libm-test-ulps
sysdeps/powerpc/nofpu/libm-test-ulps
sysdeps/riscv/nofpu/libm-test-ulps
sysdeps/riscv/rvd/libm-test-ulps
sysdeps/s390/fpu/libm-test-ulps
sysdeps/sparc/fpu/libm-test-ulps
sysdeps/x86_64/fpu/libm-test-ulps

index 8ea383b370838c178029a1975adddcdda4719723..0c85e2e1b591f5dac669e191171f3e7d8ec5fe18 100644 (file)
@@ -244,3 +244,7 @@ sysdeps/ieee754/flt-32/s_exp10m1f.c
   (file src/binary32/exp10m1/exp10m1f.c in CORE-MATH)
   - The code was adapted to use glibc code style and internal
     functions to handle errno, overflow, and underflow.
+sysdeps/ieee754/flt-32/s_exp2m1f.c
+  (file src/binary32/exp2m1/exp2m1f.c in CORE-MATH)
+  - The code was adapted to use glibc code style and internal
+    functions to handle errno, overflow, and underflow.
index 00832393f6ed2b5be21b0bedc6ed130e91905550..29ccd1dfe9fa4ecdd9c6a5529ceceb19dd4a1c83 100644 (file)
@@ -1127,22 +1127,18 @@ ldouble: 2
 
 Function: "exp2m1":
 double: 2
-float: 2
 ldouble: 2
 
 Function: "exp2m1_downward":
 double: 3
-float: 3
 ldouble: 3
 
 Function: "exp2m1_towardzero":
 double: 3
-float: 2
 ldouble: 4
 
 Function: "exp2m1_upward":
 double: 3
-float: 3
 ldouble: 5
 
 Function: "exp_advsimd":
index 4a8ad5142cfd07ddd3171b181ecaade230abd4d5..a5f9d50c39575dcd3443c0894bf2813ed29a016c 100644 (file)
@@ -815,19 +815,15 @@ float: 1
 
 Function: "exp2m1":
 double: 2
-float: 2
 
 Function: "exp2m1_downward":
 double: 3
-float: 3
 
 Function: "exp2m1_towardzero":
 double: 3
-float: 2
 
 Function: "exp2m1_upward":
 double: 3
-float: 3
 
 Function: "exp_downward":
 double: 1
index 4cecea691955a0d0bbcfcb10693294156bef01ae..574ae8b8be5bb3e0403d56f9fb25d0d13d417e15 100644 (file)
@@ -200,7 +200,6 @@ double: 1
 
 Function: "exp2m1":
 double: 2
-float: 2
 
 Function: "expm1":
 double: 1
index bcbe82ca3416d60987c2d062275c2fd05f20b3b7..28718c7d2c1797c341dfecbee4745cbf632ba197 100644 (file)
@@ -807,19 +807,15 @@ float: 1
 
 Function: "exp2m1":
 double: 2
-float: 2
 
 Function: "exp2m1_downward":
 double: 3
-float: 3
 
 Function: "exp2m1_towardzero":
 double: 3
-float: 2
 
 Function: "exp2m1_upward":
 double: 3
-float: 3
 
 Function: "exp_downward":
 double: 1
index 3dc1f2f8c3c222f7acaa1ed40ee7a6e11ef21024..338ca77adeebdd846c6c0c732747cd630b1642ee 100644 (file)
@@ -826,19 +826,15 @@ float: 1
 
 Function: "exp2m1":
 double: 2
-float: 2
 
 Function: "exp2m1_downward":
 double: 3
-float: 3
 
 Function: "exp2m1_towardzero":
 double: 3
-float: 2
 
 Function: "exp2m1_upward":
 double: 3
-float: 3
 
 Function: "exp_downward":
 double: 1
index 96d02334e4a5913f76400dd3c53ef6ac205710b3..98a5a538ff217dcd1c0766fbbb7b68ebd07a0ac2 100644 (file)
@@ -1194,25 +1194,21 @@ ldouble: 1
 
 Function: "exp2m1":
 double: 2
-float: 1
 float128: 2
 ldouble: 3
 
 Function: "exp2m1_downward":
 double: 3
-float: 3
 float128: 3
 ldouble: 6
 
 Function: "exp2m1_towardzero":
 double: 3
-float: 2
 float128: 4
 ldouble: 5
 
 Function: "exp2m1_upward":
 double: 3
-float: 3
 float128: 5
 ldouble: 6
 
index c18e2bc8de88fd789f11dcc11d5e87eb6db86e02..9afb63bc0f6b07d88c641d50e93780baf6809122 100644 (file)
@@ -1197,25 +1197,21 @@ ldouble: 1
 
 Function: "exp2m1":
 double: 2
-float: 1
 float128: 2
 ldouble: 3
 
 Function: "exp2m1_downward":
 double: 3
-float: 3
 float128: 3
 ldouble: 6
 
 Function: "exp2m1_towardzero":
 double: 3
-float: 2
 float128: 4
 ldouble: 6
 
 Function: "exp2m1_upward":
 double: 3
-float: 3
 float128: 5
 ldouble: 6
 
diff --git a/sysdeps/ieee754/flt-32/s_exp2m1f.c b/sysdeps/ieee754/flt-32/s_exp2m1f.c
new file mode 100644 (file)
index 0000000..f899152
--- /dev/null
@@ -0,0 +1,192 @@
+/* Correctly-rounded base-2 exponent function biased by 1 for binary32 value.
+
+Copyright (c) 2022-2024 Alexei Sibidanov.
+
+The original version of this file was copied from the CORE-MATH
+project (file src/binary32/exp2m1/exp2m1f.c, revision bc385c2).
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
+*/
+
+#include <fenv.h>
+#include <math.h>
+#include "math_config.h"
+#include <libm-alias-float.h>
+#include <math-narrow-eval.h>
+#include <float.h>
+
+float
+__exp2m1f (float x)
+{
+  double z = x;
+  uint32_t ux = asuint (x);
+  uint32_t ax = ux & (~0u >> 1);
+  if (__glibc_unlikely (ux >= 0xc1c80000u))
+    { /* x <= -25 */
+      if (ax > (0xffu << 23))
+       return x + x; /* nan */
+      return (ux == 0xff800000) ? -0x1p+0 : -0x1p+0 + 0x1p-26f;
+    }
+  else if (__glibc_unlikely (ax >= 0x43000000u))
+    { /* x >= 128 */
+      if (ax >= asuint (INFINITY))
+       return x + x; /* +Inf or NaN */
+      /* exp2m1 (MAX_EXP) should not overflow when rounding towards zero
+         or towards -Inf.  We round FLT_MAX + 2^103 which is in the middle
+         between FLT_MAX and 2^128 (the next number with unbounded range).  */
+      float ret = math_narrow_eval (FLT_MAX + 0x1p103f);
+      if (x == FLT_MAX_EXP && ret == FLT_MAX)
+        return ret;
+      return __math_oflowf (0);
+    }
+  else if (__glibc_unlikely (ax < 0x3df95f1fu))
+    { /* |x| < 8.44e-2/log(2) */
+      double z2 = z * z, r;
+      if (__glibc_unlikely (ax < 0x3d67a4ccu))
+       { /* |x| < 3.92e-2/log(2) */
+         if (__glibc_unlikely (ax < 0x3caa2feeu))
+           { /* |x| < 1.44e-2/log(2) */
+             if (__glibc_unlikely (ax < 0x3bac1405u))
+               { /* |x| < 3.64e-3/log(2) */
+                 if (__glibc_unlikely (ax < 0x3a358876u))
+                   { /* |x| < 4.8e-4/log(2) */
+                     if (__glibc_unlikely (ax < 0x37d32ef6u))
+                       { /* |x| < 1.745e-5/log(2) */
+                         if (__glibc_unlikely (ax < 0x331fdd82u))
+                           { /* |x| < 2.58e-8/log(2) */
+                             if (__glibc_unlikely (ax < 0x2538aa3bu))
+                               /* |x| < 1.60171e-16 */
+                               r = 0x1.62e42fefa39efp-1;
+                             else
+                               r = 0x1.62e42fefa39fp-1
+                                 + z * 0x1.ebfbdff82c58fp-3;
+                           }
+                         else
+                           {
+                             if (__glibc_unlikely (ux == 0xb3d85005u))
+                               return -0x1.2bdf76p-24 - 0x1.8p-77;
+                             if (__glibc_unlikely (ux == 0x3338428du))
+                               return 0x1.fee08ap-26 + 0x1p-80;
+                             static const double c[] =
+                               {
+                                 0x1.62e42fefa39efp-1, 0x1.ebfbdff8548fdp-3,
+                                 0x1.c6b08d704a06dp-5
+                               };
+                             r = c[0] + z * (c[1] + z * c[2]);
+                           }
+                       }
+                     else
+                       {
+                         if (__glibc_unlikely (ux == 0x388bca4fu))
+                           return 0x1.839702p-15 - 0x1.8p-68;
+                         static const double c[] =
+                           {
+                             0x1.62e42fefa39efp-1, 0x1.ebfbdff82c58fp-3,
+                             0x1.c6b08dc82b347p-5, 0x1.3b2ab6fbad172p-7
+                           };
+                         r = (c[0] + z * c[1]) + z2 * (c[2] + z * c[3]);
+                       }
+                   }
+                 else
+                   {
+                     static const double c[] =
+                       {
+                         0x1.62e42fefa39efp-1, 0x1.ebfbdff82c068p-3,
+                         0x1.c6b08d704a6dcp-5, 0x1.3b2ac262c3eedp-7,
+                         0x1.5d87fe7af779ap-10
+                       };
+                     r = (c[0] + z * c[1])
+                         + z2 * (c[2] + z * (c[3] + z * c[4]));
+                   }
+               }
+             else
+               {
+                 static const double c[] =
+                   {
+                     0x1.62e42fefa39fp-1,   0x1.ebfbdff82c58dp-3,
+                     0x1.c6b08d7011d13p-5,  0x1.3b2ab6fbd267dp-7,
+                     0x1.5d88a81cea49ep-10, 0x1.430912ea9b963p-13
+                   };
+                 r = (c[0] + z * c[1])
+                     + z2 * ((c[2] + z * c[3]) + z2 * (c[4] + z * c[5]));
+               }
+           }
+         else
+           {
+             static const double c[] =
+               {
+                 0x1.62e42fefa39efp-1,  0x1.ebfbdff82c639p-3,
+                 0x1.c6b08d7049f1cp-5,  0x1.3b2ab6f5243bdp-7,
+                 0x1.5d87fe80a9e6cp-10, 0x1.430d0b9257fa8p-13,
+                 0x1.ffcbfc4cf0952p-17
+               };
+             r = (c[0] + z * c[1])
+                 + z2 * ((c[2] + z * c[3])
+                         + z2 * (c[4] + z * (c[5] + z * c[6])));
+           }
+       }
+      else
+       {
+         static const double c[] =
+           {
+             0x1.62e42fefa39efp-1,  0x1.ebfbdff82c591p-3,
+             0x1.c6b08d704cf6bp-5,  0x1.3b2ab6fba00cep-7,
+             0x1.5d87fdfdaadb4p-10, 0x1.4309137333066p-13,
+             0x1.ffe5e90daf7ddp-17, 0x1.62c0220eed731p-20
+           };
+         r = ((c[0] + z * c[1]) + z2 * (c[2] + z * c[3]))
+             + (z2 * z2) * ((c[4] + z * c[5]) + z2 * (c[6] + z * c[7]));
+       }
+      r *= z;
+      return r;
+    }
+  else
+    {
+      static const double c[] =
+       {
+         0x1.62e42fefa398bp-5,  0x1.ebfbdff84555ap-11,
+         0x1.c6b08d4ad86d3p-17, 0x1.3b2ad1b1716a2p-23,
+         0x1.5d7472718ce9dp-30, 0x1.4a1d7f457ac56p-37
+       };
+      static const double tb[] =
+       {
+         0x1p+0,               0x1.0b5586cf9890fp+0,  0x1.172b83c7d517bp+0,
+         0x1.2387a6e756238p+0, 0x1.306fe0a31b715p+0,  0x1.3dea64c123422p+0,
+         0x1.4bfdad5362a27p+0, 0x1.5ab07dd485429p+0,  0x1.6a09e667f3bcdp+0,
+         0x1.7a11473eb0187p+0, 0x1.8ace5422aa0dap+0,  0x1.9c49182a3f09p+0,
+         0x1.ae89f995ad3adp+0, 0x1.c199bdd85529cp+0,  0x1.d5818dcfba487p+0,
+         0x1.ea4afa2a490dap+0
+       };
+      double a = 16.0 * z;
+      double ia = floor (a);
+      double h = a - ia;
+      double h2 = h * h;
+      int64_t i = ia, j = i & 0xf, e = i - j;
+      e >>= 4;
+      double s = tb[j];
+      s *= asdouble ((e + 0x3ffull) << 52);
+      double c0 = c[0] + h * c[1];
+      double c2 = c[2] + h * c[3];
+      double c4 = c[4] + h * c[5];
+      c0 += h2 * (c2 + h2 * c4);
+      double w = s * h;
+      return (s - 1.0) + w * c0;
+    }
+}
+libm_alias_float (__exp2m1, exp2m1)
index de7e5260088cc8b6c50a520dd620b6e821ed00eb..c6fc9fd4ed8774fe5443af7f3805ad284a7ee3cf 100644 (file)
@@ -1010,22 +1010,18 @@ ldouble: 2
 
 Function: "exp2m1":
 double: 2
-float: 2
 ldouble: 2
 
 Function: "exp2m1_downward":
 double: 3
-float: 3
 ldouble: 3
 
 Function: "exp2m1_towardzero":
 double: 3
-float: 2
 ldouble: 4
 
 Function: "exp2m1_upward":
 double: 3
-float: 3
 ldouble: 5
 
 Function: "exp_downward":
index 96cd7b3fd56dd823a5654e51ba72c964f1a32af8..7fef8942ae794c2c4e4a7b10b8e020f6aa31a6f4 100644 (file)
@@ -903,19 +903,15 @@ double: 1
 
 Function: "exp2m1":
 double: 1
-float: 1
 
 Function: "exp2m1_downward":
 double: 2
-float: 1
 
 Function: "exp2m1_towardzero":
 double: 2
-float: 1
 
 Function: "exp2m1_upward":
 double: 1
-float: 1
 
 Function: "exp_upward":
 double: 1
index 829da3ebf1027d010eced2119818ba8ea8766d19..d8daca6326d9fcf490ea7d5b8f402539b55b5eeb 100644 (file)
@@ -807,19 +807,15 @@ float: 1
 
 Function: "exp2m1":
 double: 2
-float: 2
 
 Function: "exp2m1_downward":
 double: 3
-float: 3
 
 Function: "exp2m1_towardzero":
 double: 3
-float: 2
 
 Function: "exp2m1_upward":
 double: 3
-float: 3
 
 Function: "exp_downward":
 double: 1
index 5c7b050b11695bc73526a1902efb0fd023c5c016..39e94d54807c84cc3e63b5feafbfb4337b229f53 100644 (file)
@@ -1014,22 +1014,18 @@ ldouble: 2
 
 Function: "exp2m1":
 double: 2
-float: 2
 ldouble: 2
 
 Function: "exp2m1_downward":
 double: 3
-float: 3
 ldouble: 3
 
 Function: "exp2m1_towardzero":
 double: 3
-float: 2
 ldouble: 4
 
 Function: "exp2m1_upward":
 double: 3
-float: 3
 ldouble: 5
 
 Function: "exp_downward":
index 30ddbaf05f6e89ffbd09ed4bbec6bd5e7cd007e5..dd25aaf94a77affc9ee2b1cf16663502e2e4cf1e 100644 (file)
@@ -1227,25 +1227,21 @@ ldouble: 2
 
 Function: "exp2m1":
 double: 2
-float: 2
 float128: 2
 ldouble: 3
 
 Function: "exp2m1_downward":
 double: 3
-float: 3
 float128: 3
 ldouble: 7
 
 Function: "exp2m1_towardzero":
 double: 3
-float: 2
 float128: 4
 ldouble: 8
 
 Function: "exp2m1_upward":
 double: 3
-float: 3
 float128: 5
 ldouble: 9
 
index e15e1f8fc00aa2b8f0c44c05eede2318d179bc65..1e5bae039b42deb0d9e3e00758d5c828429706e7 100644 (file)
@@ -1023,22 +1023,18 @@ ldouble: 2
 
 Function: "exp2m1":
 double: 2
-float: 2
 ldouble: 2
 
 Function: "exp2m1_downward":
 double: 3
-float: 3
 ldouble: 7
 
 Function: "exp2m1_towardzero":
 double: 3
-float: 2
 ldouble: 8
 
 Function: "exp2m1_upward":
 double: 3
-float: 3
 ldouble: 9
 
 Function: "exp_downward":
index 2271e1ee6ce925f23f432195c8c30a04a7180228..11f891988e665e758c855909f65914dba51c6bde 100644 (file)
@@ -995,7 +995,6 @@ ldouble: 2
 
 Function: "exp2m1":
 double: 1
-float: 1
 ldouble: 1
 
 Function: "exp_downward":
index 0bc61bf3c8b7035a2c0780e0359b2adae959d234..7d303fcb6b9c7f35222ea3ad6168a84e5c06a2fa 100644 (file)
@@ -1010,22 +1010,18 @@ ldouble: 2
 
 Function: "exp2m1":
 double: 2
-float: 2
 ldouble: 2
 
 Function: "exp2m1_downward":
 double: 3
-float: 3
 ldouble: 3
 
 Function: "exp2m1_towardzero":
 double: 3
-float: 2
 ldouble: 4
 
 Function: "exp2m1_upward":
 double: 3
-float: 3
 ldouble: 5
 
 Function: "exp_downward":
index 860479f3ff37f430f1c191db36cbd72ab14a7efe..aa7a94a2be05b2dcf3bb8bb6e6e23d9c61139e41 100644 (file)
@@ -1011,22 +1011,18 @@ ldouble: 2
 
 Function: "exp2m1":
 double: 2
-float: 2
 ldouble: 2
 
 Function: "exp2m1_downward":
 double: 3
-float: 3
 ldouble: 3
 
 Function: "exp2m1_towardzero":
 double: 3
-float: 2
 ldouble: 4
 
 Function: "exp2m1_upward":
 double: 3
-float: 3
 ldouble: 5
 
 Function: "exp_downward":
index 4d8064b5433d82f14236b5a66cb75fd29ab8da6a..e7b3da6f1ca5a4c094f4df95e8535c5390a124a2 100644 (file)
@@ -1014,22 +1014,18 @@ ldouble: 2
 
 Function: "exp2m1":
 double: 2
-float: 2
 ldouble: 2
 
 Function: "exp2m1_downward":
 double: 3
-float: 3
 ldouble: 3
 
 Function: "exp2m1_towardzero":
 double: 3
-float: 2
 ldouble: 4
 
 Function: "exp2m1_upward":
 double: 3
-float: 3
 ldouble: 5
 
 Function: "exp_downward":
index 3f438c20904917039ee1fa2df0afdac1a3c86f4b..71697e99e0625bc8d96806ddc6183cc1d757ee82 100644 (file)
@@ -1495,25 +1495,21 @@ float: 1
 
 Function: "exp2m1":
 double: 2
-float: 2
 float128: 2
 ldouble: 3
 
 Function: "exp2m1_downward":
 double: 3
-float: 3
 float128: 3
 ldouble: 6
 
 Function: "exp2m1_towardzero":
 double: 3
-float: 2
 float128: 4
 ldouble: 5
 
 Function: "exp2m1_upward":
 double: 3
-float: 3
 float128: 5
 ldouble: 6