From patchwork Wed Sep 23 21:50:49 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella X-Patchwork-Id: 144515 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 1CB284BB3BCD for ; Wed, 23 Sep 2026 21:52:41 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 1CB284BB3BCD 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=rsZRPtPb X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-vs2-x10.google.com (mail-vs2-x10.google.com [IPv6:2a00:1450:4864:3a::10]) by sourceware.org (Postfix) with ESMTPS id 3B5094BB3BE1 for ; Wed, 23 Sep 2026 21:51:47 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 3B5094BB3BE1 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 3B5094BB3BE1 Authentication-Results: sourceware.org; arc=none smtp.remote-ip=2a00:1450:4864:3a::10 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1790200307; cv=none; b=tCUm4JCZr/X1MfY+TFfDgTUUU2XnKNrvN/f65EcHSgBCCLRXZ6iDQ+3SGXFSZR9vcdFOZo8OFqYC2FwySt3xVoSP6RllY3YAbeUQroLWAmI/UX1NVKBLsFv30H/WmWJJLVptMhpH0olA554enSaHKC+kWDX+yl4WPLRIz6iw6Tc= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1790200307; c=relaxed/simple; bh=fRnHmWdBOLqGdsskUHeeLbU8ulRy6wr68HCQ8ejKMZA=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=pGIaYObYq5Qy60zcpjR71+mJk6reNaJ0shPIfOExUj60bQqjY93dzMBEYz/zm+qzL6djJ87UYpcd0yII2x63Qrv/bjscirSPS+nsVi0Pl5aP/PTdlJjznz/+Kg42dmtdnoLd8kYONVTrsPXCRjrx+HBXfJwV3iggjf6wx3DcxNE= 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=rsZRPtPb DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 3B5094BB3BE1 Received: by mail-vs2-x10.google.com with SMTP id 71dfb90a1353d-5c67e529da9so565881e0c.2 for ; Wed, 23 Sep 2026 14:51:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1790200307; x=1790805107; 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=ZoUTWCqjlAMILGpwe9BGntqewJj44054bP14/HGE1rI=; b=rsZRPtPbtSpzu3BtCHAnz1HKaHnpgbD260Yi0xNmnyjcwoGHEVogFWk36BUhpFa2PZ 1AfmyUmuVNWTCPWAoKWT+5iyQaMZoLu+8lRMuqCWuoV7LRzXta6mHf4hIgHGtsVzksnf +ZyexW6bw/7NPWV+dWT+arFZ4Y8M1midJz4x3AND4baBkafPVhWuFbvE9TKpZ4eBT/kC hGBMShqwNqTCgOom3GQ4/XtjS6IhHag5UiIgS37ViAfPLxl0KsVZY/TTLZ8xqTOAIfTC 0vi1RD3TRtk7wPGmZP9B1WBYsz9fgIzVDgD12cBsrScuZh7uOkkS+osxaqpkiXGMtJNz WgoA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1790200307; x=1790805107; 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=ZoUTWCqjlAMILGpwe9BGntqewJj44054bP14/HGE1rI=; b=OemvHeYeqokYrLPi+lYc3JzBPLkcIdiBfMUktMmFOkVM/C3UE2JqjBGx/a/33hXFdg sClYVDrJuPO8VJhkdAbuSMUoj3RXKJA8U/nxXQ2Cc3+ivNvBmXztWia/owL5x3zfX1/+ Y2c+EdB5zvwwc3swU2r0KBq5KpD2heeEpvDxla76FuUeqxAjUT1OHeOI7204h37m/rfr DoBbrC8gJOEYq7byJf+6SgINEAhjXVuuRiZgniqqwrOPreDxyL4m3+w6Tmqk1C2lOg2r hYYsxWblggTriI7gvANJJmCznZeNeHTKGlFW2tY68CWKu21/fIaqt96EwocBdPCuXL9Z V2WQ== X-Gm-Message-State: AFuF++l6VlQ8TRiXw+cqm9SBGndj8Al12Rq1LByYOUyhBGuc97JZDDNh wIsIf8EInIXyTkJHOkPF7G4QfmkxMh7CCcTZvnWoOzTPCQiJSLtD551mPYBSiXuaQ28+csxNIJR 4+jqo X-Gm-Gg: AYBFou1PbFrim6KYg6euK3R9GD1T/5rwtfBhjHyqbdBIh/WSRz1SzLpVNYKhajplKn0 m8MXcXNqinhovjs1a03xJxUyUeG6jzayixLXj311hc1PXbtrkx9bqd7+CzPc0T+7os03/sgRuT9 rv4vY95SYqaPkCoSSAPWgQ6etTcywWYT/69eibTilkeCwurXvC5u4x5gM4T6Zkr1kgU0iqETypf VIJZpV5ZCiEVFyWOX5GS5j5lgxCWEapWh6jCm8U8r6TBF3BOFkv6RNQAxlje91+XHqIAVvUk65a edOWEQzNR8yk78ca+wj5lm1WbmpR0sO6jnaDvDHDtRBAKgmUPbfk+Ta8wBC1VgGU7QY3vpZdlzB +XYW6gfIxY89Sd9OdSxXRimwj82jsL/4OlxAK6OHooMEaFFe/2wicBZHbT4YZVGLyvbtCfwOmTe QWpAmMDC+jU0JJCgieL26oZR7+22ZTQEiUHq0X3fYL3IFqGTUefhZ3+hyJDU4TH53GwTYw+xUoi ZIgTTGpMd3ySMLFgWKYTV+bo1PV9GMSKE99ePngRgeqleudHg== X-Received: by 2002:a05:6122:2490:b0:5c9:a60b:e5d4 with SMTP id 71dfb90a1353d-5cb0b67d543mr527129e0c.14.1790200306256; Wed, 23 Sep 2026 14:51:46 -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.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 23 Sep 2026 14:51:45 -0700 (PDT) From: Adhemerval Zanella To: libc-alpha@sourceware.org Cc: Wilco Dijkstra , Paul Zimmermann Subject: [PATCH v3 08/12] math: Use sincosf from CORE-MATH Date: Wed, 23 Sep 2026 18:50:49 -0300 Message-ID: <20260923215118.2608685-9-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.3 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_LOTSOFHASH, KAM_SHORT, RCVD_IN_DNSWL_NONE, 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 The CORE-MATH implementation is correctly rounded (in any rounding mode) and performs similarly. * latency - input: random [-pi, pi] master patched improvement x86_64 12.5512 11.1108 12.96% x86_64v2 12.6094 11.1648 12.94% x86_64v3 12.4137 11.1563 11.27% aarch64 9.1758 10.1564 -9.65% power10 5.0943 4.9589 2.73% loongarch 2.5712 2.5029 2.73% - input: moderate [3*pi, 2^26) master patched improvement x86_64 17.5750 12.5725 39.79% x86_64v2 17.6042 13.3637 31.73% x86_64v3 17.8084 13.3737 33.16% aarch64 10.6784 11.3802 -6.17% power10 6.7397 5.1304 31.37% loongarch 2.7792 2.6993 2.96% - input: huge [2^26, FLT_MAX] master patched improvement x86_64 18.5452 28.0835 -33.96% x86_64v2 18.6174 28.0867 -33.71% x86_64v3 19.0829 26.8577 -28.95% aarch64 10.0751 19.5618 -48.50% power10 7.6855 9.1092 -15.63% loongarch 2.6530 4.0716 -34.84% - input: spec2017.blender (526.blender_r sin trace) master patched improvement x86_64 13.3663 11.2555 18.75% x86_64v2 13.4356 11.1571 20.42% x86_64v3 13.3409 11.1726 19.41% aarch64 9.1439 10.1583 -9.99% power10 5.5150 4.8806 13.00% loongarch 2.5423 2.5025 1.59% * reciprocal-throughput - input: random [-pi, pi] master patched improvement x86_64 12.3122 11.1080 10.84% x86_64v2 12.3726 11.1606 10.86% x86_64v3 12.3469 11.1644 10.59% aarch64 9.1767 10.1239 -9.36% power10 5.1895 4.9360 5.14% loongarch 2.5718 2.5026 2.77% - input: moderate [3*pi, 2^26) master patched improvement x86_64 17.5620 12.0521 45.72% x86_64v2 17.6172 12.5897 39.93% x86_64v3 17.8137 13.3711 33.23% aarch64 10.6574 11.3751 -6.31% power10 6.6460 5.1526 28.98% loongarch 2.7793 2.6959 3.09% - input: huge [2^26, FLT_MAX] master patched improvement x86_64 18.5399 28.0494 -33.90% x86_64v2 18.6146 28.0991 -33.75% x86_64v3 19.0775 26.8567 -28.97% aarch64 10.1080 19.5576 -48.32% power10 7.6618 9.0993 -15.80% loongarch 2.6532 4.0697 -34.81% - input: spec2017.blender (526.blender_r sin trace) master patched improvement x86_64 13.2879 11.2453 18.16% x86_64v2 13.3692 11.1517 19.88% x86_64v3 13.2673 11.1726 18.75% aarch64 9.1475 10.1232 -9.64% power10 5.5467 4.8679 13.94% loongarch 2.5429 2.5030 1.59% All the benchmarks were run with gcc 16.2.1 and --enable-stack-protector=strong. The x86_64 row is the -march=x86-64 build on a Ryzen 9 5900X, where the FMA/AVX2 ifunc variant is selected; x86_64v2 and x86_64v3 are builds with -march=x86-64-v2 and -march=x86-64-v3; aarch64 is a Neoverse-N1, power10 a POWER10 built with -mcpu=power8, and loongarch a Loongson-3C5000L-LL from the compiler farm. The random and moderate workloads, which cover every argument seen in the SPEC CPU2017 traces, improve on every target except on aarch64. The huge workload (|x| >= 2^26, no traced caller) is slower everywhere, since the correctly rounded large-argument reduction (a 128-bit fixed-point product of the mantissa by 4/pi) replaces the 96-bit integer sequence of the current code. The code is adapted to glibc style and to use the math_config definition.h (to handle errno, overflow, and underflow). Checked on x86_64-linux-gnu, i686-linux-gnu, aarch64-linux-gnu, and powerpc64le-linux-gnu. --- SHARED-FILES | 4 +- math/auto-libm-test-in | 1 + math/auto-libm-test-out-sincos | 25 ++ sysdeps/ieee754/flt-32/libm-test-ulps | 12 + sysdeps/ieee754/flt-32/s_sincosf.c | 254 +++++++++++++----- sysdeps/ieee754/flt-32/s_sincosf_data.h | 13 + .../ieee754/flt-32/s_sincosf_data_generic.c | 13 + 7 files changed, 247 insertions(+), 75 deletions(-) diff --git a/SHARED-FILES b/SHARED-FILES index 6853931c7a4..3049ea52ecc 100644 --- a/SHARED-FILES +++ b/SHARED-FILES @@ -330,6 +330,8 @@ core-math: # src/binary32/sin/sinf.c, revision ac1993d8 sysdeps/ieee754/flt-32/s_sinf.c sysdeps/ieee754/flt-32/s_sincosf_data_generic.c + # src/binary32/sincos/sincosf.c, revision ac1993d8 + sysdeps/ieee754/flt-32/s_sincosf.c # src/binary32/sinpi/sinpif.c, revision bbfabd99d sysdeps/ieee754/flt-32/s_sinpif.c # src/binary32/tan/tanf.c, revision 59d21d7 @@ -373,8 +375,6 @@ arm-optimized-routines: sysdeps/ieee754/flt-32/e_powf.c # math/powf_log2_data.c, revision 189dfef sysdeps/ieee754/flt-32/e_powf_log2_data.c - # math/sincosf.c, revision af29d39 - sysdeps/ieee754/flt-32/s_sincosf.c # math/sincosf.h, revision aed553c sysdeps/ieee754/flt-32/s_sincosf.h # math/sincosf_data.c, revision 189dfef diff --git a/math/auto-libm-test-in b/math/auto-libm-test-in index 939da2c8334..4cc03998f49 100644 --- a/math/auto-libm-test-in +++ b/math/auto-libm-test-in @@ -9526,6 +9526,7 @@ sincos 1e22 sincos 0x1p1023 sincos 0x1p16383 sincos 0x1p+120 +sincos 0x1p-126 sincos 0x1p+127 sincos 0x1.fffff8p+127 sincos 0x1.fffffep+127 diff --git a/math/auto-libm-test-out-sincos b/math/auto-libm-test-out-sincos index f0457631a9c..085b9f421ae 100644 --- a/math/auto-libm-test-out-sincos +++ b/math/auto-libm-test-out-sincos @@ -1197,6 +1197,31 @@ sincos 0x1p+120 = sincos tonearest ibm128 0x1p+120 : 0x6.0b8d19579bf2db5e5f1aa933f4p-4 -0xe.d06685b36c66c4cf35c11f6518p-4 : inexact-ok = sincos towardzero ibm128 0x1p+120 : 0x6.0b8d19579bf2db5e5f1aa933f2p-4 -0xe.d06685b36c66c4cf35c11f6518p-4 : inexact-ok = sincos upward ibm128 0x1p+120 : 0x6.0b8d19579bf2db5e5f1aa933f4p-4 -0xe.d06685b36c66c4cf35c11f6518p-4 : inexact-ok +sincos 0x1p-126 += sincos downward binary32 0x4p-128 : 0x3.fffff8p-128 0xf.fffffp-4 : inexact-ok underflow-ok errno-erange-ok += sincos tonearest binary32 0x4p-128 : 0x4p-128 0x1p+0 : inexact-ok underflow-ok errno-erange-ok += sincos towardzero binary32 0x4p-128 : 0x3.fffff8p-128 0xf.fffffp-4 : inexact-ok underflow-ok errno-erange-ok += sincos upward binary32 0x4p-128 : 0x4p-128 0x1p+0 : inexact-ok underflow-ok errno-erange-ok += sincos downward binary64 0x4p-128 : 0x3.ffffffffffffep-128 0xf.ffffffffffff8p-4 : inexact-ok += sincos tonearest binary64 0x4p-128 : 0x4p-128 0x1p+0 : inexact-ok += sincos towardzero binary64 0x4p-128 : 0x3.ffffffffffffep-128 0xf.ffffffffffff8p-4 : inexact-ok += sincos upward binary64 0x4p-128 : 0x4p-128 0x1p+0 : inexact-ok += sincos downward intel96 0x4p-128 : 0x3.fffffffffffffffcp-128 0xf.fffffffffffffffp-4 : inexact-ok += sincos tonearest intel96 0x4p-128 : 0x4p-128 0x1p+0 : inexact-ok += sincos towardzero intel96 0x4p-128 : 0x3.fffffffffffffffcp-128 0xf.fffffffffffffffp-4 : inexact-ok += sincos upward intel96 0x4p-128 : 0x4p-128 0x1p+0 : inexact-ok += sincos downward m68k96 0x4p-128 : 0x3.fffffffffffffffcp-128 0xf.fffffffffffffffp-4 : inexact-ok += sincos tonearest m68k96 0x4p-128 : 0x4p-128 0x1p+0 : inexact-ok += sincos towardzero m68k96 0x4p-128 : 0x3.fffffffffffffffcp-128 0xf.fffffffffffffffp-4 : inexact-ok += sincos upward m68k96 0x4p-128 : 0x4p-128 0x1p+0 : inexact-ok += sincos downward binary128 0x4p-128 : 0x3.fffffffffffffffffffffffffffep-128 0xf.fffffffffffffffffffffffffff8p-4 : inexact-ok += sincos tonearest binary128 0x4p-128 : 0x4p-128 0x1p+0 : inexact-ok += sincos towardzero binary128 0x4p-128 : 0x3.fffffffffffffffffffffffffffep-128 0xf.fffffffffffffffffffffffffff8p-4 : inexact-ok += sincos upward binary128 0x4p-128 : 0x4p-128 0x1p+0 : inexact-ok += sincos downward ibm128 0x4p-128 : 0x3.ffffffffffffffffffffffffffp-128 0xf.fffffffffffffffffffffffffcp-4 : inexact-ok += sincos tonearest ibm128 0x4p-128 : 0x4p-128 0x1p+0 : inexact-ok += sincos towardzero ibm128 0x4p-128 : 0x3.ffffffffffffffffffffffffffp-128 0xf.fffffffffffffffffffffffffcp-4 : inexact-ok += sincos upward ibm128 0x4p-128 : 0x4p-128 0x1p+0 : inexact-ok sincos 0x1p+127 = sincos downward binary32 0x8p+124 : 0x9.f9631p-4 0xc.82b8ep-4 : inexact-ok = sincos tonearest binary32 0x8p+124 : 0x9.f9631p-4 0xc.82b8fp-4 : inexact-ok diff --git a/sysdeps/ieee754/flt-32/libm-test-ulps b/sysdeps/ieee754/flt-32/libm-test-ulps index 01792230183..dfe78a63c8f 100644 --- a/sysdeps/ieee754/flt-32/libm-test-ulps +++ b/sysdeps/ieee754/flt-32/libm-test-ulps @@ -275,6 +275,18 @@ float: 0 Function: "sin_upward": float: 0 +Function: "sincos": +float: 0 + +Function: "sincos_downward": +float: 0 + +Function: "sincos_towardzero": +float: 0 + +Function: "sincos_upward": +float: 0 + Function: "sinh": float: 0 diff --git a/sysdeps/ieee754/flt-32/s_sincosf.c b/sysdeps/ieee754/flt-32/s_sincosf.c index fd49d20b58e..83556e5372b 100644 --- a/sysdeps/ieee754/flt-32/s_sincosf.c +++ b/sysdeps/ieee754/flt-32/s_sincosf.c @@ -1,28 +1,35 @@ -/* Compute sine and cosine of argument. - Copyright (C) 2018-2026 Free Software Foundation, Inc. - This file is part of the GNU C Library. +/* Correctly-rounded sincos of binary32 value. - The GNU C Library is free software; you can redistribute it and/or - modify it under the terms of the GNU Lesser General Public - License as published by the Free Software Foundation; either - version 2.1 of the License, or (at your option) any later version. +Copyright (c) 2024-2025 Alexei Sibidanov - The GNU C Library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Lesser General Public License for more details. +The original version of this file was copied from the CORE-MATH +project (file src/binary32/sincos/sincosf.c, revision ac1993d8). - You should have received a copy of the GNU Lesser General Public - License along with the GNU C Library; if not, see - . */ +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: -#include -#include +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 #include -#include #include #include "math_config.h" -#include "s_sincosf.h" +#include +#include #ifndef SECTION # define SECTION @@ -34,73 +41,174 @@ # define SINCOSF_FUNC SINCOSF #endif -/* Fast sincosf implementation. Worst-case ULP is 0.5607, maximum relative - error is 0.5303 * 2^-23. A single-step range reduction is used for - small values. Large inputs have their range reduced using fast integer - arithmetic. */ -void -SECTION -SINCOSF_FUNC (float y, float *sinp, float *cosp) +static double __attribute__ ((noinline)) +rbig (uint32_t u, int *q) { - double x = y; - double s; - int n; - const sincos_t *p = &__sincosf_table[0]; - - if (abstop12 (y) < abstop12 (pio4)) + int e = (u >> 23) & 0xff, i; + uint64_t m = (u & (~0u >> 9)) | 1 << 23; + u128 p0 = u128_mul (u128_from_u64 (m), u128_from_u64 (IPI[0])); + u128 p1 = u128_mul (u128_from_u64 (m), u128_from_u64 (IPI[1])); + p1 = u128_add (p1, u128_rshift (p0, 64)); + u128 p2 = u128_mul (u128_from_u64 (m), u128_from_u64 (IPI[2])); + p2 = u128_add (p2, u128_rshift (p1, 64)); + u128 p3 = u128_mul (u128_from_u64 (m), u128_from_u64 (IPI[3])); + p3 = u128_add (p3, u128_rshift (p2, 64)); + uint64_t p3h = u128_high (p3), p3l = u128_low (p3), p2l = u128_low (p2), + p1l = u128_low (p1); + int64_t a; + int k = e - 124, s = k - 23; + /* in cr_sinf(), rbig() is called in the case 127+28 <= e < 0xff + thus 155 <= e <= 254, which yields 28 <= k <= 127 and 5 <= s <= 104 */ + if (s < 64) { - double x2 = x * x; + i = p3h << s | p3l >> (64 - s); + a = p3l << s | p2l >> (64 - s); + } + else if (s == 64) + { + i = p3l; + a = p2l; + } + else + { /* s > 64 */ + i = p3l << (s - 64) | p2l >> (128 - s); + a = p2l << (s - 64) | p1l >> (128 - s); + } + int sgn = u; + sgn >>= 31; + int64_t sm = a >> 63; + i -= sm; + double z = (a ^ sgn) * 0x1p-64; + i = (i ^ sgn) - sgn; + *q = i; + return z; +} - if (__glibc_unlikely (abstop12 (y) < abstop12 (0x1p-12f))) +static inline double +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); + return (idh - id) + idl; +} + +static inline double +rltl0 (double x, int *q) +{ + double idh = 0x1.45f306dc9c883p+2 * x, id = roundeven_finite (idh); + *q = asuint64 (0x1.8p52 + id); + return idh - id; +} + +static inline float +add_sign (float x, float rh, float rl) +{ + float sgn = copysignf (1.0f, x); + return sgn * rh + sgn * rl; +} + +static void __attribute__ ((noinline)) +as_sincosf_database (float x, float *sout, float *cout) +{ + uint32_t t = asuint (x); + uint32_t ax = t & (~0u >> 1); + for (unsigned i = 0; i < array_length (ST_SINCOSF); i++) + if (__glibc_unlikely (ST_SINCOSF[i].uarg == ax)) { - /* Force underflow for tiny y. */ - if (__glibc_unlikely (abstop12 (y) < abstop12 (0x1p-126f))) - math_force_eval ((float)x2); - *sinp = y; - *cosp = 1.0f; - return; + *sout = add_sign (x, ST_SINCOSF[i].sh, ST_SINCOSF[i].sl); + *cout = ST_SINCOSF[i].ch + ST_SINCOSF[i].cl; + break; } +} - sincosf_poly (x, x2, p, 0, sinp, cosp); +static void __attribute__ ((noinline)) +as_sincosf_big (float x, float *sout, float *cout) +{ + uint32_t t = asuint (x); + uint32_t ax = t << 1; + if (__glibc_unlikely (ax >= 0xffu << 24)) + { // nan or +-inf + if (ax << 8) + { + *sout = x + x; + *cout = x + x; + return; // nan + } + *sout = *cout = x - x; + __math_invalidf (x + x); + return; } - else if (abstop12 (y) < abstop12 (120.0f)) - { - x = reduce_fast (x, p, &n); + int ia; + double z = rbig (t, &ia); + double z2 = z * z, z4 = z2 * z2; + double aa = (A[0] + z2 * A[1]) + z4 * (A[2] + z2 * A[3]); + double bb = (B[0] + z2 * B[1]) + z4 * (B[2] + z2 * B[3]); + bb *= z; + double s0 = TB[ia & 31], c0 = TB[(ia + 8u) & 31]; + double s = s0 + z * (aa * c0 - bb * s0); + double c = c0 - z * (aa * s0 + bb * c0); + *sout = s; + *cout = c; + uint64_t tr = asuint64 (c); + uint64_t tail = (tr + 6) & (~UINT64_C(0) >> 36); + if (__glibc_unlikely (tail <= 12)) + return as_sincosf_database (x, sout, cout); +} - /* Setup the signs for sin and cos. */ - s = p->sign[n & 3]; - - if (n & 2) - p = &__sincosf_table[1]; - - sincosf_poly (x * s, x * x, p, n, sinp, cosp); - } - else if (__glibc_likely (abstop12 (y) < abstop12 (INFINITY))) - { - uint32_t xi = asuint (y); - int sign = xi >> 31; - - x = reduce_large (xi, &n); - - /* Setup signs for sin and cos - include original sign. */ - s = p->sign[(n + sign) & 3]; - - if ((n + sign) & 2) - p = &__sincosf_table[1]; - - sincosf_poly (x * s, x * x, p, n, sinp, cosp); +void SECTION +SINCOSF_FUNC (float x, float *sout, float *cout) +{ + uint32_t ax = asuint (x) << 1; + int ia; + double z0 = x, z; + if (__glibc_likely (ax < 0x822d97c8u)) + { // |x| < 0x1.2d97c8p+3 + if (__glibc_unlikely (ax < 0x73000000u)) + { // |x| < 0x1p-12 + if (__glibc_unlikely (ax < 0x66000000u)) + { // |x| < 0x1p-25 + if (__glibc_unlikely (ax == 0u)) + { + *sout = x; + *cout = 1.0f; + } + else + { + *sout = fmaf (-x, fabsf (x), x); + *cout = 1.0f - 0x1p-25f; + } + } + else + { + *sout = (-0x1.555556p-3f * x) * (x * x) + x; + *cout = (-0x1p-1f * x) * x + 1.0f; + } + return; + } + if (__glibc_unlikely (ax == 0x812d97c8u)) + return as_sincosf_database (x, sout, cout); + z = rltl0 (z0, &ia); } else { - /* Return NaN if Inf or NaN for both sin and cos. */ - *sinp = *cosp = y - y; -#if WANT_ERRNO - /* Needed to set errno for +-Inf, the add is a hack to work - around a gcc register allocation issue: just passing y - affects code generation in the fast path (PR86901). */ - __math_invalidf (y + y); -#endif + if (__glibc_unlikely (ax > 0x99000000u)) + return as_sincosf_big (x, sout, cout); + if (__glibc_unlikely (ax == 0x8c333330u)) + return as_sincosf_database (x, sout, cout); + z = rltl (z0, &ia); } + double z2 = z * z, z4 = z2 * z2; + double aa = (A[0] + z2 * A[1]) + z4 * (A[2] + z2 * A[3]); + double bb = (B[0] + z2 * B[1]) + z4 * (B[2] + z2 * B[3]); + aa *= z; + bb *= z2; + double s0 = TB[ia & 31], c0 = TB[(ia + 8) & 31]; + double rs = s0 + (aa * c0 - bb * s0); + double rc = c0 - (aa * s0 + bb * c0); + *sout = rs; + *cout = rc; } #ifndef SINCOSF diff --git a/sysdeps/ieee754/flt-32/s_sincosf_data.h b/sysdeps/ieee754/flt-32/s_sincosf_data.h index 8bea0042f3c..354e9b6a885 100644 --- a/sysdeps/ieee754/flt-32/s_sincosf_data.h +++ b/sysdeps/ieee754/flt-32/s_sincosf_data.h @@ -46,4 +46,17 @@ extern const sincosf_database_t __sinf_st[4] attribute_hidden; extern const sincosf_database_t __cosf_st[5] attribute_hidden; #define ST_COSF __cosf_st +typedef struct +{ + union + { + float arg; + uint32_t uarg; + }; + float sh, sl; + float ch, cl; +} sincosf2_database_t; +extern const sincosf2_database_t __sincosf_st[9] attribute_hidden; +#define ST_SINCOSF __sincosf_st + #endif diff --git a/sysdeps/ieee754/flt-32/s_sincosf_data_generic.c b/sysdeps/ieee754/flt-32/s_sincosf_data_generic.c index e9bed60f92d..a86640c4048 100644 --- a/sysdeps/ieee754/flt-32/s_sincosf_data_generic.c +++ b/sysdeps/ieee754/flt-32/s_sincosf_data_generic.c @@ -89,3 +89,16 @@ const sincosf_database_t __cosf_st[] = { { 0x1.3170fp+63 }, 0x1.fe2976p-1, 0x1p-26 }, { { 0x1.2b9622p+67 }, 0x1.f0285ep-1, -0x1p-26 }, }; + +const sincosf2_database_t __sincosf_st[] = +{ + { { 0x1.33333p+13 }, -0x1.63f4bap-2, -0x1p-27, -0x1.e01216p-1, -0x1p-26 }, + { { 0x1.75b8a2p-1 }, 0x1.55688ap-1, -0x1p-26, 0x1.7d8e1ep-1, 0x1p-26 }, + { { 0x1.4f0654p+0 }, 0x1.ee836cp-1, -0x1p-26, 0x1.09558p-2, -0x1p-27 }, + { { 0x1.2d97c8p+3 }, -0x1.99bc5ap-26, -0x1p-51, -0x1p+0, 0x1p-25 }, + { { 0x1.2d97c8p+2 }, -0x1p+0, 0x1p-25, 0x1.99bc5cp-27, -0x1p-52 }, + { { 0x1.4555p+51 }, -0x1.b0ea44p-1, 0x1p-26, 0x1.115d7ep-1, -0x1p-26 }, + { { 0x1.48a858p+54 }, 0x1.beac8cp-1, 0x1p-26, 0x1.f48148p-2, 0x1p-27 }, + { { 0x1.3170fp+63 }, 0x1.5ac1eep-4, -0x1p-30, 0x1.fe2976p-1, 0x1p-26 }, + { { 0x1.2b9622p+67 }, -0x1.f983c2p-3, 0x1p-28, 0x1.f0285ep-1, -0x1p-26 }, +};