Path: blob/main/contrib/arm-optimized-routines/math/aarch64/advsimd/atan2f.c
48378 views
/*1* Single-precision vector atan2(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"1011static const struct data12{13float32x4_t c0, pi_over_2, c4, c6, c2;14float c1, c3, c5, c7;15uint32x4_t comp_const;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.c0 = V4 (-0x1.55555p-2f), .c1 = 0x1.99935ep-3f,21.c2 = V4 (-0x1.24051ep-3f), .c3 = 0x1.bd7368p-4f,22.c4 = V4 (-0x1.491f0ep-4f), .c5 = 0x1.93a2c0p-5f,23.c6 = V4 (-0x1.4c3c60p-6f), .c7 = 0x1.01fd88p-8f,24.pi_over_2 = V4 (0x1.921fb6p+0f), .comp_const = V4 (2 * 0x7f800000lu - 1),25};2627#define SignMask v_u32 (0x80000000)2829/* Special cases i.e. 0, infinity and nan (fall back to scalar calls). */30static float32x4_t VPCS_ATTR NOINLINE31special_case (float32x4_t y, float32x4_t x, float32x4_t ret,32uint32x4_t sign_xy, uint32x4_t cmp)33{34/* Account for the sign of y. */35ret = vreinterpretq_f32_u32 (36veorq_u32 (vreinterpretq_u32_f32 (ret), sign_xy));37return v_call2_f32 (atan2f, y, x, ret, cmp);38}3940/* Returns 1 if input is the bit representation of 0, infinity or nan. */41static inline uint32x4_t42zeroinfnan (uint32x4_t i, const struct data *d)43{44/* 2 * i - 1 >= 2 * 0x7f800000lu - 1. */45return vcgeq_u32 (vsubq_u32 (vmulq_n_u32 (i, 2), v_u32 (1)), d->comp_const);46}4748/* Fast implementation of vector atan2f. Maximum observed error is492.95 ULP in [0x1.9300d6p+6 0x1.93c0c6p+6] x [0x1.8c2dbp+6 0x1.8cea6p+6]:50_ZGVnN4vv_atan2f (0x1.93836cp+6, 0x1.8cae1p+6) got 0x1.967f06p-151want 0x1.967f00p-1. */52float32x4_t VPCS_ATTR NOINLINE V_NAME_F2 (atan2) (float32x4_t y, float32x4_t x)53{54const struct data *d = ptr_barrier (&data);5556uint32x4_t ix = vreinterpretq_u32_f32 (x);57uint32x4_t iy = vreinterpretq_u32_f32 (y);5859uint32x4_t special_cases60= vorrq_u32 (zeroinfnan (ix, d), zeroinfnan (iy, d));6162uint32x4_t sign_x = vandq_u32 (ix, SignMask);63uint32x4_t sign_y = vandq_u32 (iy, SignMask);64uint32x4_t sign_xy = veorq_u32 (sign_x, sign_y);6566float32x4_t ax = vabsq_f32 (x);67float32x4_t ay = vabsq_f32 (y);6869uint32x4_t pred_xlt0 = vcltzq_f32 (x);70uint32x4_t pred_aygtax = vcgtq_f32 (ay, ax);7172/* Set up z for call to atanf. */73float32x4_t n = vbslq_f32 (pred_aygtax, vnegq_f32 (ax), ay);74float32x4_t q = vbslq_f32 (pred_aygtax, ay, ax);75float32x4_t z = vdivq_f32 (n, q);7677/* Work out the correct shift. */78float32x4_t shift = vreinterpretq_f32_u32 (79vandq_u32 (pred_xlt0, vreinterpretq_u32_f32 (v_f32 (-2.0f))));80shift = vbslq_f32 (pred_aygtax, vaddq_f32 (shift, v_f32 (1.0f)), shift);81shift = vmulq_f32 (shift, d->pi_over_2);8283/* Calculate the polynomial approximation.84Use 2-level Estrin scheme for P(z^2) with deg(P)=7. However,85a standard implementation using z8 creates spurious underflow86in the very last fma (when z^8 is small enough).87Therefore, we split the last fma into a mul and an fma.88Horner and single-level Estrin have higher errors that exceed89threshold. */90float32x4_t z2 = vmulq_f32 (z, z);91float32x4_t z4 = vmulq_f32 (z2, z2);9293float32x4_t c1357 = vld1q_f32 (&d->c1);94float32x4_t p01 = vfmaq_laneq_f32 (d->c0, z2, c1357, 0);95float32x4_t p23 = vfmaq_laneq_f32 (d->c2, z2, c1357, 1);96float32x4_t p45 = vfmaq_laneq_f32 (d->c4, z2, c1357, 2);97float32x4_t p67 = vfmaq_laneq_f32 (d->c6, z2, c1357, 3);98float32x4_t p03 = vfmaq_f32 (p01, z4, p23);99float32x4_t p47 = vfmaq_f32 (p45, z4, p67);100101float32x4_t ret = vfmaq_f32 (p03, z4, vmulq_f32 (z4, p47));102103/* y = shift + z * P(z^2). */104ret = vaddq_f32 (vfmaq_f32 (z, ret, vmulq_f32 (z2, z)), shift);105106if (unlikely (v_any_u32 (special_cases)))107{108return special_case (y, x, ret, sign_xy, special_cases);109}110111/* Account for the sign of y. */112return vreinterpretq_f32_u32 (113veorq_u32 (vreinterpretq_u32_f32 (ret), sign_xy));114}115116HALF_WIDTH_ALIAS_F2 (atan2)117118/* Arity of 2 means no mathbench entry emitted. See test/mathbench_funcs.h. */119TEST_SIG (V, F, 2, atan2)120TEST_DISABLE_FENV (V_NAME_F2 (atan2))121TEST_ULP (V_NAME_F2 (atan2), 2.46)122TEST_INTERVAL (V_NAME_F2 (atan2), -10.0, 10.0, 50000)123TEST_INTERVAL (V_NAME_F2 (atan2), -1.0, 1.0, 40000)124TEST_INTERVAL (V_NAME_F2 (atan2), 0.0, 1.0, 40000)125TEST_INTERVAL (V_NAME_F2 (atan2), 1.0, 100.0, 40000)126TEST_INTERVAL (V_NAME_F2 (atan2), 1e6, 1e32, 40000)127128129