Path: blob/main/contrib/arm-optimized-routines/math/aarch64/advsimd/cosf.c
48375 views
/*1* Single-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{14float32x4_t poly[4];15float32x4_t range_val, inv_pi, pi_1, pi_2, pi_3;16} data = {17/* 1.886 ulp error. */18.poly = { V4 (-0x1.555548p-3f), V4 (0x1.110df4p-7f), V4 (-0x1.9f42eap-13f),19V4 (0x1.5b2e76p-19f) },2021.pi_1 = V4 (0x1.921fb6p+1f),22.pi_2 = V4 (-0x1.777a5cp-24f),23.pi_3 = V4 (-0x1.ee59dap-49f),2425.inv_pi = V4 (0x1.45f306p-2f),26.range_val = V4 (0x1p20f)27};2829#define C(i) d->poly[i]3031static float32x4_t VPCS_ATTR NOINLINE32special_case (float32x4_t x, float32x4_t y, uint32x4_t odd, uint32x4_t cmp)33{34/* Fall back to scalar code. */35y = vreinterpretq_f32_u32 (veorq_u32 (vreinterpretq_u32_f32 (y), odd));36return v_call_f32 (cosf, x, y, cmp);37}3839float32x4_t VPCS_ATTR NOINLINE V_NAME_F1 (cos) (float32x4_t x)40{41const struct data *d = ptr_barrier (&data);42float32x4_t n, r, r2, r3, y;43uint32x4_t odd, cmp;4445#if WANT_SIMD_EXCEPT46r = vabsq_f32 (x);47cmp = vcgeq_u32 (vreinterpretq_u32_f32 (r),48vreinterpretq_u32_f32 (d->range_val));49if (unlikely (v_any_u32 (cmp)))50/* If fenv exceptions are to be triggered correctly, set any special lanes51to 1 (which is neutral w.r.t. fenv). These lanes will be fixed by52special-case handler later. */53r = vbslq_f32 (cmp, v_f32 (1.0f), r);54#else55cmp = vcageq_f32 (x, d->range_val);56r = x;57#endif5859/* n = rint((|x|+pi/2)/pi) - 0.5. */60n = vrndaq_f32 (vfmaq_f32 (v_f32 (0.5), r, d->inv_pi));61odd = vshlq_n_u32 (vreinterpretq_u32_s32 (vcvtq_s32_f32 (n)), 31);62n = vsubq_f32 (n, v_f32 (0.5f));6364/* r = |x| - n*pi (range reduction into -pi/2 .. pi/2). */65r = vfmsq_f32 (r, d->pi_1, n);66r = vfmsq_f32 (r, d->pi_2, n);67r = vfmsq_f32 (r, d->pi_3, n);6869/* y = sin(r). */70r2 = vmulq_f32 (r, r);71r3 = vmulq_f32 (r2, r);72y = vfmaq_f32 (C (2), C (3), r2);73y = vfmaq_f32 (C (1), y, r2);74y = vfmaq_f32 (C (0), y, r2);75y = vfmaq_f32 (r, y, r3);7677if (unlikely (v_any_u32 (cmp)))78return special_case (x, y, odd, cmp);79return vreinterpretq_f32_u32 (veorq_u32 (vreinterpretq_u32_f32 (y), odd));80}8182HALF_WIDTH_ALIAS_F1 (cos)8384TEST_SIG (V, F, 1, cos, -3.1, 3.1)85TEST_ULP (V_NAME_F1 (cos), 1.4)86TEST_DISABLE_FENV_IF_NOT (V_NAME_F1 (cos), WANT_SIMD_EXCEPT)87TEST_SYM_INTERVAL (V_NAME_F1 (cos), 0, 0x1p20, 500000)88TEST_SYM_INTERVAL (V_NAME_F1 (cos), 0x1p20, inf, 10000)899091