public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: Adhemerval Zanella <adhemerval.zanella@linaro.org>
To: libc-alpha@sourceware.org
Cc: Wilco Dijkstra <Wilco.Dijkstra@arm.com>,
	Paul Zimmermann <Paul.Zimmermann@inria.fr>
Subject: [PATCH v3 11/12] math: Shorten the sinf/cosf/sincosf fast path
Date: Wed, 23 Sep 2026 18:50:52 -0300	[thread overview]
Message-ID: <20260923215118.2608685-12-adhemerval.zanella@linaro.org> (raw)
In-Reply-To: <20260923215118.2608685-1-adhemerval.zanella@linaro.org>

Converting the rounded value with (int) id gives the same low bits and
lets gcc on aarch64 fold the rounding and the conversion into a
single fcvtns of the scaled argument (and it s cvttsd2si on x86_64).

The polynomial coefficients and the sin/cos tables are also grouped i
one hidden object, so a function materializes one base address instead of
one per table.

On aarch64 with gcc 16 the fast path goes from 59 to 54 instructions
for sinf, 55 to 50 for cosf and 63 to 58 for sincosf, with 4 instead
of 8 register moves and 3 instead of 5 adrp.

Measured against the previous commit (gcc 16.2.1) with the benchtests
inputs that preceded the workload rewrite of this series (random
[-2pi, 2pi], large [-64, 64], huge [-1024, 1024]), geomean of the
reciprocal-throughput and latency, negative is faster:

                      sinf     cosf  sincosf
x86_64           +1.3%    -0.4%    +0.3%
x86_64-fma       +0.9%    -2.6%    -1.1%
aarch64          -0.1%    +0.4%    -6.0%
power10          +2.3%    +2.9%    +0.2%
loongarch        -0.6%    -1.4%    -0.5%

The aarch64 sincosf gain comes from the large and huge inputs (-8% and
-9% reciprocal-throughput).

Checked on x86_64-linux-gnu, aarch64-linux-gnu, powerpc64le-linux-gnu
and loongarch64-linux-gnuf64.
---
 sysdeps/ieee754/flt-32/s_sincosf_common.h |  4 +-
 sysdeps/ieee754/flt-32/s_sincosf_data.c   | 76 ++++++++++++-----------
 sysdeps/ieee754/flt-32/s_sincosf_data.h   | 21 ++++---
 3 files changed, 53 insertions(+), 48 deletions(-)

diff --git a/sysdeps/ieee754/flt-32/s_sincosf_common.h b/sysdeps/ieee754/flt-32/s_sincosf_common.h
index a15a0afe806..c7f385d07eb 100644
--- a/sysdeps/ieee754/flt-32/s_sincosf_common.h
+++ b/sysdeps/ieee754/flt-32/s_sincosf_common.h
@@ -34,7 +34,7 @@ static inline double
 rltl0 (double x, int *q)
 {
   double idh = 0x1.45f306dc9c883p+2 * x, id = roundeven_finite (idh);
-  *q = asuint64 (0x1.8p52 + id);
+  *q = (int) id;
   return idh - id;
 }
 
@@ -44,7 +44,7 @@ rltl (float z, int *q)
   double x = z;
   double idl = -0x1.b1bbead603d8bp-29 * x, idh = 0x1.45f306ep+2 * x,
 	 id = roundeven_finite (idh);
-  *q = asuint64 (0x1.8p52 + id);
+  *q = (int) id;
   return (idh - id) + idl;
 }
 
diff --git a/sysdeps/ieee754/flt-32/s_sincosf_data.c b/sysdeps/ieee754/flt-32/s_sincosf_data.c
index a86640c4048..d248db8c08f 100644
--- a/sysdeps/ieee754/flt-32/s_sincosf_data.c
+++ b/sysdeps/ieee754/flt-32/s_sincosf_data.c
@@ -33,44 +33,46 @@ const uint64_t __sinf_ipi[] =
   0xa2f9836e4e441529
 };
 
-const double __sinf_b[] =
+const sincosf_tables_t __sincosf_tables =
 {
-   0x1.3bd3cc9be45dcp-6, -0x1.03c1f081b0833p-14,  0x1.55d3c6fc9ac1fp-24,
-  -0x1.e1d3ff281b40dp-35
-};
-const double __sinf_a[] =
-{
-   0x1.921fb54442d17p-3, -0x1.4abbce6256a39p-10,   0x1.466bc5a518c16p-19,
-  -0x1.32bdc61074ff6p-29
-};
-const double __sinf_tb[] =
-{
-   0x0p+0,                0x1.8f8b83c69a60bp-3,  0x1.87de2a6aea963p-2,
-   0x1.1c73b39ae68c8p-1,  0x1.6a09e667f3bcdp-1,  0x1.a9b66290ea1a3p-1,
-   0x1.d906bcf328d46p-1,  0x1.f6297cff75cbp-1,   0x1p+0,
-   0x1.f6297cff75cbp-1,   0x1.d906bcf328d46p-1,  0x1.a9b66290ea1a3p-1,
-   0x1.6a09e667f3bcdp-1,  0x1.1c73b39ae68c8p-1,  0x1.87de2a6aea963p-2,
-   0x1.8f8b83c69a60bp-3,  0x0p+0,               -0x1.8f8b83c69a60bp-3,
-  -0x1.87de2a6aea963p-2, -0x1.1c73b39ae68c8p-1, -0x1.6a09e667f3bcdp-1,
-  -0x1.a9b66290ea1a3p-1, -0x1.d906bcf328d46p-1, -0x1.f6297cff75cbp-1,
-  -0x1p+0,               -0x1.f6297cff75cbp-1,  -0x1.d906bcf328d46p-1,
-  -0x1.a9b66290ea1a3p-1, -0x1.6a09e667f3bcdp-1, -0x1.1c73b39ae68c8p-1,
-  -0x1.87de2a6aea963p-2, -0x1.8f8b83c69a60bp-3
-};
-
-const double __cosf_tb[] =
-{
-   0x1p+0,                0x1.f6297cff75cbp-1,   0x1.d906bcf328d46p-1,
-   0x1.a9b66290ea1a3p-1,  0x1.6a09e667f3bcdp-1,  0x1.1c73b39ae68c8p-1,
-   0x1.87de2a6aea963p-2,  0x1.8f8b83c69a60bp-3,  0x0p+0,
-  -0x1.8f8b83c69a60bp-3, -0x1.87de2a6aea963p-2, -0x1.1c73b39ae68c8p-1,
-  -0x1.6a09e667f3bcdp-1, -0x1.a9b66290ea1a3p-1, -0x1.d906bcf328d46p-1,
-  -0x1.f6297cff75cbp-1,  -0x1p+0,               -0x1.f6297cff75cbp-1,
-  -0x1.d906bcf328d46p-1, -0x1.a9b66290ea1a3p-1, -0x1.6a09e667f3bcdp-1,
-  -0x1.1c73b39ae68c8p-1, -0x1.87de2a6aea963p-2, -0x1.8f8b83c69a60bp-3,
-   0x0p+0,                0x1.8f8b83c69a60bp-3,  0x1.87de2a6aea963p-2,
-   0x1.1c73b39ae68c8p-1,  0x1.6a09e667f3bcdp-1,  0x1.a9b66290ea1a3p-1,
-   0x1.d906bcf328d46p-1,  0x1.f6297cff75cbp-1
+  .b =
+  {
+     0x1.3bd3cc9be45dcp-6, -0x1.03c1f081b0833p-14,  0x1.55d3c6fc9ac1fp-24,
+    -0x1.e1d3ff281b40dp-35
+  },
+  .a =
+  {
+     0x1.921fb54442d17p-3, -0x1.4abbce6256a39p-10,   0x1.466bc5a518c16p-19,
+    -0x1.32bdc61074ff6p-29
+  },
+  .tb =
+  {
+     0x0p+0,                0x1.8f8b83c69a60bp-3,  0x1.87de2a6aea963p-2,
+     0x1.1c73b39ae68c8p-1,  0x1.6a09e667f3bcdp-1,  0x1.a9b66290ea1a3p-1,
+     0x1.d906bcf328d46p-1,  0x1.f6297cff75cbp-1,   0x1p+0,
+     0x1.f6297cff75cbp-1,   0x1.d906bcf328d46p-1,  0x1.a9b66290ea1a3p-1,
+     0x1.6a09e667f3bcdp-1,  0x1.1c73b39ae68c8p-1,  0x1.87de2a6aea963p-2,
+     0x1.8f8b83c69a60bp-3,  0x0p+0,               -0x1.8f8b83c69a60bp-3,
+    -0x1.87de2a6aea963p-2, -0x1.1c73b39ae68c8p-1, -0x1.6a09e667f3bcdp-1,
+    -0x1.a9b66290ea1a3p-1, -0x1.d906bcf328d46p-1, -0x1.f6297cff75cbp-1,
+    -0x1p+0,               -0x1.f6297cff75cbp-1,  -0x1.d906bcf328d46p-1,
+    -0x1.a9b66290ea1a3p-1, -0x1.6a09e667f3bcdp-1, -0x1.1c73b39ae68c8p-1,
+    -0x1.87de2a6aea963p-2, -0x1.8f8b83c69a60bp-3
+  },
+  .tb_cosf =
+  {
+     0x1p+0,                0x1.f6297cff75cbp-1,   0x1.d906bcf328d46p-1,
+     0x1.a9b66290ea1a3p-1,  0x1.6a09e667f3bcdp-1,  0x1.1c73b39ae68c8p-1,
+     0x1.87de2a6aea963p-2,  0x1.8f8b83c69a60bp-3,  0x0p+0,
+    -0x1.8f8b83c69a60bp-3, -0x1.87de2a6aea963p-2, -0x1.1c73b39ae68c8p-1,
+    -0x1.6a09e667f3bcdp-1, -0x1.a9b66290ea1a3p-1, -0x1.d906bcf328d46p-1,
+    -0x1.f6297cff75cbp-1,  -0x1p+0,               -0x1.f6297cff75cbp-1,
+    -0x1.d906bcf328d46p-1, -0x1.a9b66290ea1a3p-1, -0x1.6a09e667f3bcdp-1,
+    -0x1.1c73b39ae68c8p-1, -0x1.87de2a6aea963p-2, -0x1.8f8b83c69a60bp-3,
+     0x0p+0,                0x1.8f8b83c69a60bp-3,  0x1.87de2a6aea963p-2,
+     0x1.1c73b39ae68c8p-1,  0x1.6a09e667f3bcdp-1,  0x1.a9b66290ea1a3p-1,
+     0x1.d906bcf328d46p-1,  0x1.f6297cff75cbp-1
+  },
 };
 
 const sincosf_database_t __sinf_st[] =
diff --git a/sysdeps/ieee754/flt-32/s_sincosf_data.h b/sysdeps/ieee754/flt-32/s_sincosf_data.h
index ca7164de52b..7d9a7028f39 100644
--- a/sysdeps/ieee754/flt-32/s_sincosf_data.h
+++ b/sysdeps/ieee754/flt-32/s_sincosf_data.h
@@ -24,15 +24,18 @@
 extern const uint64_t __sinf_ipi[] attribute_hidden;
 #define IPI __sinf_ipi
 
-extern const double __sinf_b[] attribute_hidden;
-#define B __sinf_b
-extern const double __sinf_a[] attribute_hidden;
-#define A __sinf_a
-extern const double __sinf_tb[] attribute_hidden;
-#define TB __sinf_tb
-
-extern const double __cosf_tb[] attribute_hidden;
-#define TB_COSF __cosf_tb
+typedef struct
+{
+  double b[4];
+  double a[4];
+  double tb[32];
+  double tb_cosf[32];
+} sincosf_tables_t;
+extern const sincosf_tables_t __sincosf_tables attribute_hidden;
+#define B __sincosf_tables.b
+#define A __sincosf_tables.a
+#define TB __sincosf_tables.tb
+#define TB_COSF __sincosf_tables.tb_cosf
 
 typedef struct
 {
-- 
2.53.0


  parent reply	other threads:[~2026-09-23 21:51 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-23 21:50 [PATCH v3 00/12] Add sinf/cosf/sincosf CORE-MATH implementations Adhemerval Zanella
2026-09-23 21:50 ` [PATCH v3 01/12] benchtests: Redefine cosf workloads Adhemerval Zanella
2026-09-23 21:50 ` [PATCH v3 02/12] benchtests: Redefine sinf workloads Adhemerval Zanella
2026-09-23 21:50 ` [PATCH v3 03/12] benchtests: Redefine sincosf workloads Adhemerval Zanella
2026-09-23 21:50 ` [PATCH v3 04/12] math: Make roundeven_finite an architecture hook Adhemerval Zanella
2026-09-23 21:50 ` [PATCH v3 05/12] loongarch: Add an inline roundeven_finite Adhemerval Zanella
2026-10-12  1:36   ` Xi Ruoyao
2026-10-29 12:14     ` Adhemerval Zanella Netto
2026-09-23 21:50 ` [PATCH v3 06/12] math: Use sinf from CORE-MATH Adhemerval Zanella
2026-09-23 21:50 ` [PATCH v3 07/12] math: Use cosf " Adhemerval Zanella
2026-09-23 21:50 ` [PATCH v3 08/12] math: Use sincosf " Adhemerval Zanella
2026-09-23 21:50 ` [PATCH v3 09/12] math: Consolidate common definitions for cosf/sinf/tanf/sincosf Adhemerval Zanella
2026-09-23 21:50 ` [PATCH v3 10/12] math: Remove unused definitions for sinf/cosf/sincosf implementation Adhemerval Zanella
2026-10-12 15:12   ` Yury Khrustalev
2026-09-23 21:50 ` Adhemerval Zanella [this message]
2026-09-23 21:50 ` [PATCH v3 12/12] math: Evaluate the sincosf sine and cosine as two-lane vectors Adhemerval Zanella
2026-10-07  1:45 ` [PATCH v3 00/12] Add sinf/cosf/sincosf CORE-MATH implementations anun333
2026-10-07 13:23   ` Adhemerval Zanella Netto
2026-10-07 16:45     ` anun333
2026-10-07 16:58       ` Adhemerval Zanella Netto
2026-10-07 17:23         ` anun333
2026-10-07 17:31           ` Adhemerval Zanella Netto

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260923215118.2608685-12-adhemerval.zanella@linaro.org \
    --to=adhemerval.zanella@linaro.org \
    --cc=Paul.Zimmermann@inria.fr \
    --cc=Wilco.Dijkstra@arm.com \
    --cc=libc-alpha@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).