Path: blob/main/contrib/arm-optimized-routines/math/aarch64/advsimd/cospif.c
48378 views
/*1* Single-precision vector cospi function.2*3* Copyright (c) 2023-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 "v_poly_f32.h"10#include "test_sig.h"11#include "test_defs.h"1213static const struct data14{15float32x4_t poly[6];16float32x4_t range_val;17} data = {18/* Taylor series coefficents for sin(pi * x). */19.poly = { V4 (0x1.921fb6p1f), V4 (-0x1.4abbcep2f), V4 (0x1.466bc6p1f),20V4 (-0x1.32d2ccp-1f), V4 (0x1.50783p-4f), V4 (-0x1.e30750p-8f) },21.range_val = V4 (0x1p31f),22};2324static float32x4_t VPCS_ATTR NOINLINE25special_case (float32x4_t x, float32x4_t y, uint32x4_t odd, uint32x4_t cmp)26{27y = vreinterpretq_f32_u32 (veorq_u32 (vreinterpretq_u32_f32 (y), odd));28return v_call_f32 (arm_math_cospif, x, y, cmp);29}3031/* Approximation for vector single-precision cospi(x)32Maximum Error: 3.17 ULP:33_ZGVnN4v_cospif(0x1.d341a8p-5) got 0x1.f7cd56p-134want 0x1.f7cd5p-1. */35float32x4_t VPCS_ATTR NOINLINE V_NAME_F1 (cospi) (float32x4_t x)36{37const struct data *d = ptr_barrier (&data);3839#if WANT_SIMD_EXCEPT40float32x4_t r = vabsq_f32 (x);41uint32x4_t cmp = vcaleq_f32 (v_f32 (0x1p32f), x);4243/* When WANT_SIMD_EXCEPT = 1, special lanes should be zero'd44to avoid them overflowing and throwing exceptions. */45r = v_zerofy_f32 (r, cmp);46uint32x4_t odd = vshlq_n_u32 (vcvtnq_u32_f32 (r), 31);4748#else49float32x4_t r = x;50uint32x4_t cmp = vcageq_f32 (r, d->range_val);5152uint32x4_t odd53= vshlq_n_u32 (vreinterpretq_u32_s32 (vcvtaq_s32_f32 (r)), 31);5455#endif5657/* r = x - rint(x). */58r = vsubq_f32 (r, vrndaq_f32 (r));5960/* cospi(x) = sinpi(0.5 - abs(x)) for values -1/2 .. 1/2. */61r = vsubq_f32 (v_f32 (0.5f), vabsq_f32 (r));6263/* Pairwise Horner approximation for y = sin(r * pi). */64float32x4_t r2 = vmulq_f32 (r, r);65float32x4_t r4 = vmulq_f32 (r2, r2);66float32x4_t y = vmulq_f32 (v_pw_horner_5_f32 (r2, r4, d->poly), r);6768/* Fallback to scalar. */69if (unlikely (v_any_u32 (cmp)))70return special_case (x, y, odd, cmp);7172/* Reintroduce the sign bit for inputs which round to odd. */73return vreinterpretq_f32_u32 (veorq_u32 (vreinterpretq_u32_f32 (y), odd));74}7576HALF_WIDTH_ALIAS_F1 (cospi)7778#if WANT_TRIGPI_TESTS79TEST_ULP (V_NAME_F1 (cospi), 2.67)80TEST_DISABLE_FENV_IF_NOT (V_NAME_F1 (cospi), WANT_SIMD_EXCEPT)81TEST_SYM_INTERVAL (V_NAME_F1 (cospi), 0, 0x1p-31, 5000)82TEST_SYM_INTERVAL (V_NAME_F1 (cospi), 0x1p-31, 0.5, 10000)83TEST_SYM_INTERVAL (V_NAME_F1 (cospi), 0.5, 0x1p32f, 10000)84TEST_SYM_INTERVAL (V_NAME_F1 (cospi), 0x1p32f, inf, 10000)85#endif868788