]> git.ipfire.org Git - thirdparty/glibc.git/commitdiff
math: Use tanpif from CORE-MATH
authorAdhemerval Zanella <adhemerval.zanella@linaro.org>
Fri, 20 Dec 2024 16:38:40 +0000 (13:38 -0300)
committerAdhemerval Zanella <adhemerval.zanella@linaro.org>
Wed, 12 Feb 2025 19:31:57 +0000 (16:31 -0300)
The CORE-MATH implementation is correctly rounded (for any rounding mode)
and shows better performance to the generic tanpif.

The code was adapted to glibc style and to use the definition of
math_config.h (to handle errno, overflow, and underflow).

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                      85.1683        47.7990        43.88%
x86_64v2                    76.8219        41.4679        46.02%
x86_64v3                    73.7775        37.7734        48.80%
aarch64 (Neoverse)          35.4514        18.0742        49.02%
power8                      22.7604        10.1054        55.60%
power10                     22.1358         9.9553        55.03%

reciprocal-throughput        master        patched   improvement
x86_64                      41.0174        19.4718        52.53%
x86_64v2                    34.8565        11.3761        67.36%
x86_64v3                    34.0325         9.6989        71.50%
aarch64 (Neoverse)          25.4349         9.2017        63.82%
power8                      13.8626         3.8486        72.24%
power10                     11.7933         3.6420        69.12%

Reviewed-by: DJ Delorie <dj@redhat.com>
21 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/math_config.h
sysdeps/ieee754/flt-32/s_tanpif.c [new file with mode: 0644]
sysdeps/loongarch/lp64/libm-test-ulps
sysdeps/mips/mips64/libm-test-ulps
sysdeps/or1k/fpu/libm-test-ulps
sysdeps/or1k/nofpu/libm-test-ulps
sysdeps/powerpc/fpu/libm-test-ulps
sysdeps/powerpc/fpu/math_private.h
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 c108f3b308d855c493b6ceb89066203fbc0a6124..25ece987f16cdcad15a2e734d610103722433f20 100644 (file)
@@ -358,3 +358,7 @@ sysdeps/ieee754/flt-32/s_sinpif.c:
   (src/binary32/sinpi/sinpif.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_tanpif.c:
+  (src/binary32/tanpi/tanpif.c in CORE-MATH)
+  - the code was adapted to use glibc code style and internal
+    functions to handle errno, overflow, and underflow.
index c6c93aa0e43c0445530580b427a969d0b733a4e9..3bcd0e5ae46fd9d6d77685712a642f968c3cf6e1 100644 (file)
@@ -1681,7 +1681,6 @@ ldouble: 3
 
 Function: "tanpi":
 double: 3
-float: 3
 ldouble: 3
 
 Function: "tanpi_advsimd":
@@ -1690,7 +1689,6 @@ float: 2
 
 Function: "tanpi_downward":
 double: 2
-float: 3
 ldouble: 4
 
 Function: "tanpi_sve":
@@ -1699,12 +1697,10 @@ float: 2
 
 Function: "tanpi_towardzero":
 double: 2
-float: 3
 ldouble: 4
 
 Function: "tanpi_upward":
 double: 2
-float: 4
 ldouble: 4
 
 Function: "tgamma":
index 65ebf6f9a0fe33e5f5e0dc02846aea692c401482..24e603695481ca4da138382b5b924cd1808d9078 100644 (file)
@@ -1137,19 +1137,15 @@ double: 3
 
 Function: "tanpi":
 double: 3
-float: 3
 
 Function: "tanpi_downward":
 double: 2
-float: 3
 
 Function: "tanpi_towardzero":
 double: 2
-float: 3
 
 Function: "tanpi_upward":
 double: 2
-float: 4
 
 Function: "tgamma":
 double: 9
index 3ba4f01cbffa9f647b2de5d37b105080af0f9131..6359d6e03890182176c798015d96420a50465b66 100644 (file)
@@ -271,7 +271,6 @@ double: 2
 
 Function: "tanpi":
 double: 3
-float: 3
 
 Function: "tgamma":
 double: 9
index f887712d8e2a920914854595b276795443440bf6..273c54dd4da4cfc969e67cb3bb45adfec7d7ec48 100644 (file)
@@ -1130,19 +1130,15 @@ double: 3
 
 Function: "tanpi":
 double: 3
-float: 3
 
 Function: "tanpi_downward":
 double: 2
-float: 3
 
 Function: "tanpi_towardzero":
 double: 2
-float: 3
 
 Function: "tanpi_upward":
 double: 2
-float: 4
 
 Function: "tgamma":
 double: 9
index 10f7f2ebdea4f9d54cc0c24d909904570b48e2c7..723cb79d1250f67e6b10d4b833670721855cc896 100644 (file)
@@ -1160,19 +1160,15 @@ double: 3
 
 Function: "tanpi":
 double: 3
-float: 3
 
 Function: "tanpi_downward":
 double: 2
-float: 3
 
 Function: "tanpi_towardzero":
 double: 2
-float: 3
 
 Function: "tanpi_upward":
 double: 2
-float: 4
 
 Function: "tgamma":
 double: 9
index 77aa7155dbe16dfcef5fd3a3eecc44a53d47cfe4..8107d2fa2c3bede9619eadfed2771f0d9b003861 100644 (file)
@@ -1750,25 +1750,21 @@ ldouble: 4
 
 Function: "tanpi":
 double: 3
-float: 3
 float128: 3
 ldouble: 3
 
 Function: "tanpi_downward":
 double: 2
-float: 3
 float128: 4
 ldouble: 4
 
 Function: "tanpi_towardzero":
 double: 2
-float: 3
 float128: 4
 ldouble: 4
 
 Function: "tanpi_upward":
 double: 2
-float: 4
 float128: 4
 ldouble: 4
 
index 7168d577d89e7d0c64accb32f6ddc8791ca2242a..b99c50214ce2f97184989f69a71e6d5df7db9f72 100644 (file)
@@ -1755,25 +1755,21 @@ ldouble: 4
 
 Function: "tanpi":
 double: 3
-float: 3
 float128: 3
 ldouble: 3
 
 Function: "tanpi_downward":
 double: 2
-float: 3
 float128: 4
 ldouble: 4
 
 Function: "tanpi_towardzero":
 double: 2
-float: 3
 float128: 4
 ldouble: 4
 
 Function: "tanpi_upward":
 double: 2
-float: 4
 float128: 4
 ldouble: 4
 
index 035461199c7aca3e2f46c2b9acdc9a456dd8c406..8d9c8ee3add173e9e6777e94dfe17caaee071eaa 100644 (file)
@@ -84,6 +84,31 @@ roundeven_finite (double x)
 #endif
 }
 
+#ifndef ROUNDEVENF_INTRINSICS
+/* When set, roundevenf_finite will route to the internal roundevenf function.  */
+# define ROUNDEVENF_INTRINSICS 1
+#endif
+
+static inline float
+roundevenf_finite (float x)
+{
+  if (!isfinite (x))
+    __builtin_unreachable ();
+#if ROUNDEVENF_INTRINSICS
+  return roundevenf (x);
+#else
+  float y = roundf (x);
+  if (fabs (x - y) == 0.5)
+    {
+      union { float f; uint32_t i; } u = {y};
+      union { float f; uint32_t i; } v = {y - copysignf (1.0, x)};
+      if (__builtin_ctzl (v.i) > __builtin_ctzl (u.i))
+        y = v.f;
+    }
+  return y;
+#endif
+}
+
 static inline uint32_t
 asuint (float f)
 {
diff --git a/sysdeps/ieee754/flt-32/s_tanpif.c b/sysdeps/ieee754/flt-32/s_tanpif.c
new file mode 100644 (file)
index 0000000..a268549
--- /dev/null
@@ -0,0 +1,90 @@
+/* Correctly-rounded tangent of binary32 value for angles in half-revolutions
+
+Copyright (c) 2022-2025 Alexei Sibidanov.
+
+The original version of this file was copied from the CORE-MATH
+project (src/binary32/tanpi/tanpif.c, revision 3bbf907).
+
+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 <stdint.h>
+#include <errno.h>
+#include <libm-alias-float.h>
+#include "math_config.h"
+
+float
+__tanpif (float x)
+{
+  uint32_t ix = asuint (x);
+  uint32_t e = ix & (0xff << 23);
+  if (__glibc_unlikely (e > (150 << 23))) /* |x| > 2^23 */
+    {
+      if (e == (0xff << 23)) /* x = nan or inf */
+       {
+         if (!(ix << 9)) /* x = inf */
+           return __math_invalidf (x);
+         return x + x; /* x = nan */
+       }
+      return copysign (0.0f, x);
+    }
+  float x4 = 4.0f * x;
+  float nx4 = roundevenf_finite (x4);
+  float dx4 = x4 - nx4;
+  float ni = roundevenf_finite (x);
+  float zf = x - ni;
+  if (__glibc_unlikely (dx4 == 0.0f)) /* 4*x integer */
+    {
+      int k = x4;
+      if (k & 1)
+       return copysignf (1.0f, zf); /* x = 1/4 mod 1/2 */
+      k &= 7;
+      if (k == 0)
+       return copysignf (0.0f, x); /* x = 0 mod 2 */
+      if (k == 4)
+       return -copysignf (0.0f, x); /*  x = 1 mod 2 */
+      __set_errno (ERANGE);
+      if (k == 2)
+       return 1.0f / 0.0f; /* x = 1/2 mod 2 */
+      if (k == 6)
+       return -1.0f / 0.0f; /* x = -1/2 mod 2 */
+    }
+  ix = asuint (zf);
+  uint32_t a = ix & (~0u >> 1);
+  /* x=0x1.267004p-2 is not correctly rounded for RNDZ/RNDD by the code below */
+  if (__glibc_unlikely (a == 0x3e933802u))
+    return copysignf (0x1.44cfbap+0f, zf) + copysignf (0x1p-25f, zf);
+  /* x=-0x1.e4cd0ap-14 is not correctly rounded for RNDU by the code below */
+  if (__glibc_unlikely (a == 0x38f26685u))
+    return copysignf (0x1.7cc304p-12, zf) + copysignf (0x1p-37f, zf);
+
+  double z = zf, z2 = z * z;
+
+  static const double cn[] = { 0x1.921fb54442d19p-1, -0x1.1f458b3e1f8d6p-2,
+                              0x1.68a34bd0b8f6ap-6, -0x1.e4866f7a25f99p-13 };
+  static const double cd[] = { 0x1p+0, -0x1.4b4b98d2df3a7p-1,
+                              0x1.8e9926d2bb901p-4, -0x1.a6f77fd847eep-9 };
+  double z4
+      = z2 * z2,
+      r = (z - z * z2) * ((cn[0] + z2 * cn[1]) + z4 * (cn[2] + z2 * cn[3]))
+         / (((cd[0] + z2 * cd[1]) + z4 * (cd[2] + z2 * cd[3])) * (0.25 - z2));
+  return r;
+}
+libm_alias_float (__tanpi, tanpi)
index 4fadba43c25ad0edde7ae333a7ec0ba26ef75c85..b4a6a3ba357b7457d407b63a4263ce2cb3fafd22 100644 (file)
@@ -1437,22 +1437,18 @@ ldouble: 3
 
 Function: "tanpi":
 double: 3
-float: 3
 ldouble: 3
 
 Function: "tanpi_downward":
 double: 2
-float: 3
 ldouble: 4
 
 Function: "tanpi_towardzero":
 double: 2
-float: 3
 ldouble: 4
 
 Function: "tanpi_upward":
 double: 2
-float: 4
 ldouble: 4
 
 Function: "tgamma":
index 5177b54557c6488125e9c70c548c323b13f07162..3b1c725fae696d21f69a80cd345c406dfd1e7e7c 100644 (file)
@@ -1449,22 +1449,18 @@ ldouble: 3
 
 Function: "tanpi":
 double: 3
-float: 3
 ldouble: 3
 
 Function: "tanpi_downward":
 double: 2
-float: 3
 ldouble: 4
 
 Function: "tanpi_towardzero":
 double: 2
-float: 3
 ldouble: 4
 
 Function: "tanpi_upward":
 double: 2
-float: 4
 ldouble: 4
 
 Function: "tgamma":
index 1fb4ec57c0f339c6f276af11946ccdba58f9aca1..accf30904d051dbcc3bb1fe1d7c44d11b057d171 100644 (file)
@@ -1115,19 +1115,15 @@ double: 3
 
 Function: "tanpi":
 double: 3
-float: 3
 
 Function: "tanpi_downward":
 double: 2
-float: 3
 
 Function: "tanpi_towardzero":
 double: 2
-float: 3
 
 Function: "tanpi_upward":
 double: 2
-float: 4
 
 Function: "tgamma":
 double: 9
index aff11b5148988b112b0579af4fd3518d9a1c359f..f3d5604e6a7001ad1f49915572b40df5ca659326 100644 (file)
@@ -1015,7 +1015,6 @@ double: 3
 
 Function: "tanpi":
 double: 3
-float: 3
 
 Function: "tgamma":
 double: 9
index e59c3e47ef61b334c8618b0418baedf525c4e911..404d3afc272d2d4bc6b29d3b00a7f09a3ad31253 100644 (file)
@@ -1857,25 +1857,21 @@ ldouble: 6
 
 Function: "tanpi":
 double: 3
-float: 3
 float128: 2
 ldouble: 2
 
 Function: "tanpi_downward":
 double: 2
-float: 3
 float128: 4
 ldouble: 8
 
 Function: "tanpi_towardzero":
 double: 2
-float: 3
 float128: 4
 ldouble: 8
 
 Function: "tanpi_upward":
 double: 2
-float: 4
 float128: 4
 ldouble: 8
 
index aace1a870884da6136a6d219a8089e9dba90de5a..7065d276c0f719bde9e4686f2250f5b20ea2a5ca 100644 (file)
@@ -62,6 +62,7 @@ __ieee754_sqrtf128 (_Float128 __x)
 #ifdef _ARCH_PWR6
 /* ISA 2.03 provides frin/round() and cntlzw/ctznll().  */
 # define ROUNDEVEN_INTRINSICS 0
+# define ROUNDEVENF_INTRINSICS 0
 #endif
 
 #endif /* _PPC_MATH_PRIVATE_H_ */
index 2545d0e1665d345d7d62ca4f4bc26d1fef587c4a..720250e208a7cad438f7e3166afbf340071b1ae1 100644 (file)
@@ -1306,7 +1306,6 @@ ldouble: 3
 
 Function: "tanpi":
 double: 3
-float: 3
 ldouble: 3
 
 Function: "tgamma":
index 94534a4f808114dfcadcb4d27f1be7eb09eb6d0b..ee5df22f814cfc38a1eac8786be36efc571fd8a4 100644 (file)
@@ -1452,22 +1452,18 @@ ldouble: 3
 
 Function: "tanpi":
 double: 3
-float: 3
 ldouble: 3
 
 Function: "tanpi_downward":
 double: 2
-float: 3
 ldouble: 4
 
 Function: "tanpi_towardzero":
 double: 2
-float: 3
 ldouble: 4
 
 Function: "tanpi_upward":
 double: 2
-float: 4
 ldouble: 4
 
 Function: "tgamma":
index 2c89048b569e4d0e970966ae1b27be059fdfb14f..1491089e8455e106be8176bfcc74a64eed974f6d 100644 (file)
@@ -1434,22 +1434,18 @@ ldouble: 3
 
 Function: "tanpi":
 double: 3
-float: 3
 ldouble: 3
 
 Function: "tanpi_downward":
 double: 2
-float: 3
 ldouble: 4
 
 Function: "tanpi_towardzero":
 double: 2
-float: 3
 ldouble: 4
 
 Function: "tanpi_upward":
 double: 2
-float: 4
 ldouble: 4
 
 Function: "tgamma":
index 3af2355545122d729c81d0571cd20516acdb2400..d894901bbdd63eebbbb6751912e77a81c71b3a7a 100644 (file)
@@ -1449,22 +1449,18 @@ ldouble: 3
 
 Function: "tanpi":
 double: 3
-float: 3
 ldouble: 3
 
 Function: "tanpi_downward":
 double: 2
-float: 3
 ldouble: 4
 
 Function: "tanpi_towardzero":
 double: 2
-float: 3
 ldouble: 4
 
 Function: "tanpi_upward":
 double: 2
-float: 4
 ldouble: 4
 
 Function: "tgamma":
index f6da5ba1864c9cf8bb12d861e78b135cba826b02..a4bd2edcbc205e8cfef9d2abdb428be1ef10cca4 100644 (file)
@@ -2292,25 +2292,21 @@ double: 1
 
 Function: "tanpi":
 double: 3
-float: 3
 float128: 3
 ldouble: 3
 
 Function: "tanpi_downward":
 double: 2
-float: 3
 float128: 4
 ldouble: 4
 
 Function: "tanpi_towardzero":
 double: 2
-float: 3
 float128: 4
 ldouble: 4
 
 Function: "tanpi_upward":
 double: 2
-float: 4
 float128: 4
 ldouble: 4