Path: blob/main/contrib/arm-optimized-routines/math/aarch64/sve/cos.c
48378 views
/*1* Double-precision SVE cos(x) function.2*3* Copyright (c) 2019-2024, Arm Limited.4* SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception5*/67#include "sv_math.h"8#include "test_sig.h"9#include "test_defs.h"1011static const struct data12{13double inv_pio2, pio2_1, pio2_2, pio2_3, shift;14} data = {15/* Polynomial coefficients are hardwired in FTMAD instructions. */16.inv_pio2 = 0x1.45f306dc9c882p-1,17.pio2_1 = 0x1.921fb50000000p+0,18.pio2_2 = 0x1.110b460000000p-26,19.pio2_3 = 0x1.1a62633145c07p-54,20/* Original shift used in AdvSIMD cos,21plus a contribution to set the bit #0 of q22as expected by trigonometric instructions. */23.shift = 0x1.8000000000001p5224};2526#define RangeVal 0x4160000000000000 /* asuint64 (0x1p23). */2728static svfloat64_t NOINLINE29special_case (svfloat64_t x, svfloat64_t y, svbool_t oob)30{31return sv_call_f64 (cos, x, y, oob);32}3334/* A fast SVE implementation of cos based on trigonometric35instructions (FTMAD, FTSSEL, FTSMUL).36Maximum measured error: 2.108 ULPs.37SV_NAME_D1 (cos)(0x1.9b0ba158c98f3p+7) got -0x1.fddd4c65c7f07p-338want -0x1.fddd4c65c7f05p-3. */39svfloat64_t SV_NAME_D1 (cos) (svfloat64_t x, const svbool_t pg)40{41const struct data *d = ptr_barrier (&data);4243svfloat64_t r = svabs_x (pg, x);44svbool_t oob = svcmpge (pg, svreinterpret_u64 (r), RangeVal);4546/* Load some constants in quad-word chunks to minimise memory access. */47svbool_t ptrue = svptrue_b64 ();48svfloat64_t invpio2_and_pio2_1 = svld1rq (ptrue, &d->inv_pio2);49svfloat64_t pio2_23 = svld1rq (ptrue, &d->pio2_2);5051/* n = rint(|x|/(pi/2)). */52svfloat64_t q = svmla_lane (sv_f64 (d->shift), r, invpio2_and_pio2_1, 0);53svfloat64_t n = svsub_x (pg, q, d->shift);5455/* r = |x| - n*(pi/2) (range reduction into -pi/4 .. pi/4). */56r = svmls_lane (r, n, invpio2_and_pio2_1, 1);57r = svmls_lane (r, n, pio2_23, 0);58r = svmls_lane (r, n, pio2_23, 1);5960/* cos(r) poly approx. */61svfloat64_t r2 = svtsmul (r, svreinterpret_u64 (q));62svfloat64_t y = sv_f64 (0.0);63y = svtmad (y, r2, 7);64y = svtmad (y, r2, 6);65y = svtmad (y, r2, 5);66y = svtmad (y, r2, 4);67y = svtmad (y, r2, 3);68y = svtmad (y, r2, 2);69y = svtmad (y, r2, 1);70y = svtmad (y, r2, 0);7172/* Final multiplicative factor: 1.0 or x depending on bit #0 of q. */73svfloat64_t f = svtssel (r, svreinterpret_u64 (q));7475if (unlikely (svptest_any (pg, oob)))76return special_case (x, svmul_x (svnot_z (pg, oob), y, f), oob);7778/* Apply factor. */79return svmul_x (pg, f, y);80}8182TEST_SIG (SV, D, 1, cos, -3.1, 3.1)83TEST_ULP (SV_NAME_D1 (cos), 1.61)84TEST_DISABLE_FENV (SV_NAME_D1 (cos))85TEST_INTERVAL (SV_NAME_D1 (cos), 0, 0xffff0000, 10000)86TEST_INTERVAL (SV_NAME_D1 (cos), 0x1p-4, 0x1p4, 500000)87CLOSE_SVE_ATTR888990