From patchwork Wed Sep 23 21:50:52 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella X-Patchwork-Id: 144519 X-Patchwork-Delegate: Wilco.Dijkstra@arm.com Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id C7B8E4BB3B96 for ; Wed, 23 Sep 2026 21:54:11 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C7B8E4BB3B96 Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=linaro.org header.i=@linaro.org header.a=rsa-sha256 header.s=google header.b=JvMU0Eji X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-vs2-x0e.google.com (mail-vs2-x0e.google.com [IPv6:2a00:1450:4864:3a::e]) by sourceware.org (Postfix) with ESMTPS id 86B354BB3BFB for ; Wed, 23 Sep 2026 21:51:53 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 86B354BB3BFB Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=linaro.org ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 86B354BB3BFB Authentication-Results: sourceware.org; arc=none smtp.remote-ip=2a00:1450:4864:3a::e ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1790200313; cv=none; b=qFPK06d8IrZ1cawDK9MisD/+4IYX6DJbkvCNhyGgeQYRWjnAXEoL1bj788Y3dJ12d+2HtjCDiLueReIVAwa19AfH7ciF0Yg9uGKdeUILJuplUVIl++3sni/zKYhWQESXyWrZzeEbI7JmnMFW99HqB9oBcXIHVGoV7nIqEj0N12g= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1790200313; c=relaxed/simple; bh=TtxOS5DMpvw5tWmJsiSy7MyRZSDB1LrfY2rx4vlOcOk=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=YGrq0GCnFct2UzGby0dFA71k8NehZsYO5VKe/ymgqmDXW/K5cf6JjzPaLEybwRWoDaYKkP1pg5eiKcvgGc4RmYdftR8VMVfS05MSW1Vjx8lq3I34a3HEJKyIOQfLxWaQVyAvK/n3zzZBbdeHwn2Vt51WsJte+irsY27W3TC5egM= ARC-Authentication-Results: i=1; sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=linaro.org header.i=@linaro.org header.a=rsa-sha256 header.s=google header.b=JvMU0Eji DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 86B354BB3BFB Received: by mail-vs2-x0e.google.com with SMTP id 71dfb90a1353d-5c67e53002bso923681e0c.0 for ; Wed, 23 Sep 2026 14:51:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1790200313; x=1790805113; darn=sourceware.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=Fig9fK4cTpoN+473iHGXJ74xBLKJLpYXNQP9ibZ/A1s=; b=JvMU0EjiFCPu85pzWtJUpRD267eQI4BgC8jBw/l0PnkCWC/IAdMFA93GBs/uyAUP2r sUmXj+vGA8EUbI5yRrqwPRx4HuKONUsocaferH1bXaF3WklysbDwrt6/aEPWFgR3m2mJ 8jMihRb07waIKc6j5ro/tqlmwFAVguDo89jsi3Bti2m+yZb112gS9tlV1zgqv3+dcGBC Jw1c9xbHU7DShPqfyo16E+JfrG0W0tEaU9+sBSbiLDGTVIp9CDXU8etZ1yInwKm4j6Wn BeGw9ewq91TOlHaUgbQMIiEDQ3Dt2R0MnS54mafMH1s2Sws3sF+8TTwByenlfZ4taGXq Ejgw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1790200313; x=1790805113; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=Fig9fK4cTpoN+473iHGXJ74xBLKJLpYXNQP9ibZ/A1s=; b=SP9D2e0DA/Cu8pQXjC7/IZvUScaYh63vF0RV8VGDVP9dqBqydHHfAtA5BGZHp11+hE 3gFLIw8yIanNW2psRhIgYGFsqKk2oBWqVupeO4Kgt+tExiOsKnycgiBuheEz8bm9jHTA oLVC3SA8MbzDVXxsNRcplhtg1PwS7eYCWawlr+8Yo7k7SZKsxVk8dvpPHRxSULIFQRqe t0ZFFvPh/CoHJcaMDiCoFpEx7sLAtilvmAZk2OC5nUusAfTJedoXJA+4/Qq4d17l+DYZ 97roKV6rVMuVqkJmC6OjoaT8PtJVs9VZrUXmIzek64p/FGaX5SrMFLsH+hehcyLoCYaO celA== X-Gm-Message-State: AFuF++m9yymhjUvF62dbyBiPf//7R3uE5VADZ1MHRtqfnFjTJt6he/ef M1FVy8fvJvTXKmXIrFtlAZOj/K3/Hg5Ai3BoDwX/kjIkBHp2LThah3WwCVenTftsPo6ATTHWhjU uXd92 X-Gm-Gg: AYBFou37f2scun9AE00/WQspDddKNh4C61LtadG5/0Jez/ESfImmgG9+VAO4YW1+qct AZpBBcF+pKeUAz+LmmV3JiGlv+8BngpLQO4qoyJZLi/FVcP+77CpuwA6gZTIBsV81mujA+Tec9r pATCMpcKnDDzLNaNf9JuXQA9enAdKbJMIvdDDFc/4gUXDayN1B0W4Inh9G+z22ORReeW84D6AYr kzqO8+uDTzPKsbfy6o1ZArL9z1AzUCB1QsevkjMHzGX8zM7p2EmzNknNs4A3X6wsWvvaLZRcyvB P3aqRWrvVEP5+dFvcaKjyfld/c5IbYNyF8GKeUo4kX6c4SGwL7GVbjy+jaBnYpeNCu203xo5L/s BjpM6Ne5HsXYD+5tU+zP8K4zHd8l54S71sBpVzOpyV+NxhR7r0RZXlxHyikmtSoeyzrhxcetH5v s2y58lgl7wEmJdBd6xK1hwSHWR6tzGrePoavfmOpG9GY/HokOAZRcmCZBe3sEI/NtILHC1XF113 +MkFHHj4m6uAthMnIlXwwxgDJ1XjR02nZaeeaXw X-Received: by 2002:a05:6122:f8c:b0:5c7:9445:f468 with SMTP id 71dfb90a1353d-5cb09707140mr549234e0c.6.1790200312857; Wed, 23 Sep 2026 14:51:52 -0700 (PDT) Received: from mandiga ([2804:1b3:a7c1:43a:16f0:724c:a6c5:976d]) by smtp.gmail.com with ESMTPSA id 71dfb90a1353d-5ca541220c5sm2505498e0c.0.2026.09.23.14.51.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 23 Sep 2026 14:51:52 -0700 (PDT) From: Adhemerval Zanella To: libc-alpha@sourceware.org Cc: Wilco Dijkstra , Paul Zimmermann Subject: [PATCH v3 11/12] math: Shorten the sinf/cosf/sincosf fast path Date: Wed, 23 Sep 2026 18:50:52 -0300 Message-ID: <20260923215118.2608685-12-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260923215118.2608685-1-adhemerval.zanella@linaro.org> References: <20260923215118.2608685-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-12.5 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, SPF_HELO_NONE, SPF_NONE, TXREP shortcircuit=no autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces~patchwork=sourceware.org@sourceware.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 {