Path: blob/main/contrib/arm-optimized-routines/math/aarch64/advsimd/cos.c
48375 views
/*1* Double-precision vector cos function.2*3* Copyright (c) 2019-2024, Arm Limited.4* SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception5*/67#include "mathlib.h"8#include "v_math.h"9#include "test_defs.h"10#include "test_sig.h"1112static const struct data13{14float64x2_t poly[7];15float64x2_t range_val, inv_pi, pi_1, pi_2, pi_3;16} data = {17/* Worst-case error is 3.3 ulp in [-pi/2, pi/2]. */18.poly = { V2 (-0x1.555555555547bp-3), V2 (0x1.1111111108a4dp-7),19V2 (-0x1.a01a019936f27p-13), V2 (0x1.71de37a97d93ep-19),20V2 (-0x1.ae633919987c6p-26), V2 (0x1.60e277ae07cecp-33),21V2 (-0x1.9e9540300a1p-41) },22.inv_pi = V2 (0x1.45f306dc9c883p-2),23.pi_1 = V2 (0x1.921fb54442d18p+1),24.pi_2 = V2 (0x1.1a62633145c06p-53),25.pi_3 = V2 (0x1.c1cd129024e09p-106),26.range_val = V2 (0x1p23)27};2829#define C(i) d->poly[i]3031static float64x2_t VPCS_ATTR NOINLINE32special_case (float64x2_t x, float64x2_t y, uint64x2_t odd, uint64x2_t cmp)33{34y = vreinterpretq_f64_u64 (veorq_u64 (vreinterpretq_u64_f64 (y), odd));35return v_call_f64 (cos, x, y, cmp);36}3738float64x2_t VPCS_ATTR V_NAME_D1 (cos) (float64x2_t x)39{40const struct data *d = ptr_barrier (&data);41float64x2_t n, r, r2, r3, r4, t1, t2, t3, y;42uint64x2_t odd, cmp;4344#if WANT_SIMD_EXCEPT45r = vabsq_f64 (x);46cmp = vcgeq_u64 (vreinterpretq_u64_f64 (r),47vreinterpretq_u64_f64 (d->range_val));48if (unlikely (v_any_u64 (cmp)))49/* If fenv exceptions are to be triggered correctly, set any special lanes50to 1 (which is neutral w.r.t. fenv). These lanes will be fixed by51special-case handler later. */52r = vbslq_f64 (cmp, v_f64 (1.0), r);53#else54cmp = vcageq_f64 (x, d->range_val);55r = x;56#endif5758/* n = rint((|x|+pi/2)/pi) - 0.5. */59n = vrndaq_f64 (vfmaq_f64 (v_f64 (0.5), r, d->inv_pi));60odd = vshlq_n_u64 (vreinterpretq_u64_s64 (vcvtq_s64_f64 (n)), 63);61n = vsubq_f64 (n, v_f64 (0.5f));6263/* r = |x| - n*pi (range reduction into -pi/2 .. pi/2). */64r = vfmsq_f64 (r, d->pi_1, n);65r = vfmsq_f64 (r, d->pi_2, n);66r = vfmsq_f64 (r, d->pi_3, n);6768/* sin(r) poly approx. */69r2 = vmulq_f64 (r, r);70r3 = vmulq_f64 (r2, r);71r4 = vmulq_f64 (r2, r2);7273t1 = vfmaq_f64 (C (4), C (5), r2);74t2 = vfmaq_f64 (C (2), C (3), r2);75t3 = vfmaq_f64 (C (0), C (1), r2);7677y = vfmaq_f64 (t1, C (6), r4);78y = vfmaq_f64 (t2, y, r4);79y = vfmaq_f64 (t3, y, r4);80y = vfmaq_f64 (r, y, r3);8182if (unlikely (v_any_u64 (cmp)))83return special_case (x, y, odd, cmp);84return vreinterpretq_f64_u64 (veorq_u64 (vreinterpretq_u64_f64 (y), odd));85}8687TEST_SIG (V, D, 1, cos, -3.1, 3.1)88TEST_ULP (V_NAME_D1 (cos), 3.0)89TEST_DISABLE_FENV_IF_NOT (V_NAME_D1 (cos), WANT_SIMD_EXCEPT)90TEST_SYM_INTERVAL (V_NAME_D1 (cos), 0, 0x1p23, 500000)91TEST_SYM_INTERVAL (V_NAME_D1 (cos), 0x1p23, inf, 10000)929394