Path: blob/main/contrib/arm-optimized-routines/math/aarch64/advsimd/atanf.c
48378 views
/*1* Single-precision vector atan(x) function.2*3* Copyright (c) 2021-2024, Arm Limited.4* SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception5*/67#include "v_math.h"8#include "test_sig.h"9#include "test_defs.h"10#include "v_poly_f32.h"1112static const struct data13{14float32x4_t poly[8];15float32x4_t pi_over_2;16} data = {17/* Coefficients of polynomial P such that atan(x)~x+x*P(x^2) on18[2**-128, 1.0].19Generated using fpminimax between FLT_MIN and 1. */20.poly = { V4 (-0x1.55555p-2f), V4 (0x1.99935ep-3f), V4 (-0x1.24051ep-3f),21V4 (0x1.bd7368p-4f), V4 (-0x1.491f0ep-4f), V4 (0x1.93a2c0p-5f),22V4 (-0x1.4c3c60p-6f), V4 (0x1.01fd88p-8f) },23.pi_over_2 = V4 (0x1.921fb6p+0f),24};2526#define SignMask v_u32 (0x80000000)2728#define P(i) d->poly[i]2930#define TinyBound 0x30800000 /* asuint(0x1p-30). */31#define BigBound 0x4e800000 /* asuint(0x1p30). */3233#if WANT_SIMD_EXCEPT34static float32x4_t VPCS_ATTR NOINLINE35special_case (float32x4_t x, float32x4_t y, uint32x4_t special)36{37return v_call_f32 (atanf, x, y, special);38}39#endif4041/* Fast implementation of vector atanf based on42atan(x) ~ shift + z + z^3 * P(z^2) with reduction to [0,1]43using z=-1/x and shift = pi/2. Maximum observed error is 2.9ulps:44_ZGVnN4v_atanf (0x1.0468f6p+0) got 0x1.967f06p-1 want 0x1.967fp-1. */45float32x4_t VPCS_ATTR NOINLINE V_NAME_F1 (atan) (float32x4_t x)46{47const struct data *d = ptr_barrier (&data);4849/* Small cases, infs and nans are supported by our approximation technique,50but do not set fenv flags correctly. Only trigger special case if we need51fenv. */52uint32x4_t ix = vreinterpretq_u32_f32 (x);53uint32x4_t sign = vandq_u32 (ix, SignMask);5455#if WANT_SIMD_EXCEPT56uint32x4_t ia = vandq_u32 (ix, v_u32 (0x7ff00000));57uint32x4_t special = vcgtq_u32 (vsubq_u32 (ia, v_u32 (TinyBound)),58v_u32 (BigBound - TinyBound));59/* If any lane is special, fall back to the scalar routine for all lanes. */60if (unlikely (v_any_u32 (special)))61return special_case (x, x, v_u32 (-1));62#endif6364/* Argument reduction:65y := arctan(x) for x < 166y := pi/2 + arctan(-1/x) for x > 167Hence, use z=-1/a if x>=1, otherwise z=a. */68uint32x4_t red = vcagtq_f32 (x, v_f32 (1.0));69/* Avoid dependency in abs(x) in division (and comparison). */70float32x4_t z = vbslq_f32 (red, vdivq_f32 (v_f32 (1.0f), x), x);71float32x4_t shift = vreinterpretq_f32_u32 (72vandq_u32 (red, vreinterpretq_u32_f32 (d->pi_over_2)));73/* Use absolute value only when needed (odd powers of z). */74float32x4_t az = vbslq_f32 (75SignMask, vreinterpretq_f32_u32 (vandq_u32 (SignMask, red)), z);7677/* Calculate the polynomial approximation.78Use 2-level Estrin scheme for P(z^2) with deg(P)=7. However,79a standard implementation using z8 creates spurious underflow80in the very last fma (when z^8 is small enough).81Therefore, we split the last fma into a mul and an fma.82Horner and single-level Estrin have higher errors that exceed83threshold. */84float32x4_t z2 = vmulq_f32 (z, z);85float32x4_t z4 = vmulq_f32 (z2, z2);8687float32x4_t y = vfmaq_f32 (88v_pairwise_poly_3_f32 (z2, z4, d->poly), z4,89vmulq_f32 (z4, v_pairwise_poly_3_f32 (z2, z4, d->poly + 4)));9091/* y = shift + z * P(z^2). */92y = vaddq_f32 (vfmaq_f32 (az, y, vmulq_f32 (z2, az)), shift);9394/* y = atan(x) if x>0, -atan(-x) otherwise. */95y = vreinterpretq_f32_u32 (veorq_u32 (vreinterpretq_u32_f32 (y), sign));9697return y;98}99100HALF_WIDTH_ALIAS_F1 (atan)101102TEST_SIG (V, F, 1, atan, -10.0, 10.0)103TEST_ULP (V_NAME_F1 (atan), 2.5)104TEST_DISABLE_FENV_IF_NOT (V_NAME_F1 (atan), WANT_SIMD_EXCEPT)105TEST_SYM_INTERVAL (V_NAME_F1 (atan), 0, 0x1p-30, 5000)106TEST_SYM_INTERVAL (V_NAME_F1 (atan), 0x1p-30, 1, 40000)107TEST_SYM_INTERVAL (V_NAME_F1 (atan), 1, 0x1p30, 40000)108TEST_SYM_INTERVAL (V_NAME_F1 (atan), 0x1p30, inf, 1000)109110111