Path: blob/main/contrib/arm-optimized-routines/math/aarch64/sve/atan2f.c
48375 views
/*1* Single-precision vector atan2f(x) function.2*3* Copyright (c) 2021-2024, Arm Limited.4* SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception5*/67#include "sv_math.h"8#include "test_sig.h"9#include "test_defs.h"10#include "sv_poly_f32.h"1112static const struct data13{14float32_t poly[8];15float32_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]. */19.poly = { -0x1.55555p-2f, 0x1.99935ep-3f, -0x1.24051ep-3f, 0x1.bd7368p-4f,20-0x1.491f0ep-4f, 0x1.93a2c0p-5f, -0x1.4c3c60p-6f, 0x1.01fd88p-8f },21.pi_over_2 = 0x1.921fb6p+0f,22};2324/* Special cases i.e. 0, infinity, nan (fall back to scalar calls). */25static svfloat32_t NOINLINE26special_case (svfloat32_t y, svfloat32_t x, svfloat32_t ret,27const svbool_t cmp)28{29return sv_call2_f32 (atan2f, y, x, ret, cmp);30}3132/* Returns a predicate indicating true if the input is the bit representation33of 0, infinity or nan. */34static inline svbool_t35zeroinfnan (svuint32_t i, const svbool_t pg)36{37return svcmpge (pg, svsub_x (pg, svlsl_x (pg, i, 1), 1),38sv_u32 (2 * 0x7f800000lu - 1));39}4041/* Fast implementation of SVE atan2f based on atan(x) ~ shift + z + z^3 *42P(z^2) with reduction to [0,1] using z=1/x and shift = pi/2. Maximum43observed error is 2.95 ULP:44_ZGVsMxvv_atan2f (0x1.93836cp+6, 0x1.8cae1p+6) got 0x1.967f06p-145want 0x1.967f00p-1. */46svfloat32_t SV_NAME_F2 (atan2) (svfloat32_t y, svfloat32_t x,47const svbool_t pg)48{49const struct data *data_ptr = ptr_barrier (&data);5051svuint32_t ix = svreinterpret_u32 (x);52svuint32_t iy = svreinterpret_u32 (y);5354svbool_t cmp_x = zeroinfnan (ix, pg);55svbool_t cmp_y = zeroinfnan (iy, pg);56svbool_t cmp_xy = svorr_z (pg, cmp_x, cmp_y);5758svfloat32_t ax = svabs_x (pg, x);59svfloat32_t ay = svabs_x (pg, y);60svuint32_t iax = svreinterpret_u32 (ax);61svuint32_t iay = svreinterpret_u32 (ay);6263svuint32_t sign_x = sveor_x (pg, ix, iax);64svuint32_t sign_y = sveor_x (pg, iy, iay);65svuint32_t sign_xy = sveor_x (pg, sign_x, sign_y);6667svbool_t pred_aygtax = svcmpgt (pg, ay, ax);6869/* Set up z for call to atan. */70svfloat32_t n = svsel (pred_aygtax, svneg_x (pg, ax), ay);71svfloat32_t d = svsel (pred_aygtax, ay, ax);72svfloat32_t z = svdiv_x (pg, n, d);7374/* Work out the correct shift. */75svfloat32_t shift = svreinterpret_f32 (svlsr_x (pg, sign_x, 1));76shift = svsel (pred_aygtax, sv_f32 (1.0), shift);77shift = svreinterpret_f32 (svorr_x (pg, sign_x, svreinterpret_u32 (shift)));78shift = svmul_x (pg, shift, sv_f32 (data_ptr->pi_over_2));7980/* Use pure Estrin scheme for P(z^2) with deg(P)=7. */81svfloat32_t z2 = svmul_x (pg, z, z);82svfloat32_t z4 = svmul_x (pg, z2, z2);83svfloat32_t z8 = svmul_x (pg, z4, z4);8485svfloat32_t ret = sv_estrin_7_f32_x (pg, z2, z4, z8, data_ptr->poly);8687/* ret = shift + z + z^3 * P(z^2). */88svfloat32_t z3 = svmul_x (pg, z2, z);89ret = svmla_x (pg, z, z3, ret);9091ret = svadd_m (pg, ret, shift);9293/* Account for the sign of x and y. */9495if (unlikely (svptest_any (pg, cmp_xy)))96return special_case (97y, x,98svreinterpret_f32 (sveor_x (pg, svreinterpret_u32 (ret), sign_xy)),99cmp_xy);100101return svreinterpret_f32 (sveor_x (pg, svreinterpret_u32 (ret), sign_xy));102}103104/* Arity of 2 means no mathbench entry emitted. See test/mathbench_funcs.h. */105TEST_SIG (SV, F, 2, atan2)106TEST_ULP (SV_NAME_F2 (atan2), 2.45)107TEST_DISABLE_FENV (SV_NAME_F2 (atan2))108TEST_INTERVAL (SV_NAME_F2 (atan2), 0.0, 1.0, 40000)109TEST_INTERVAL (SV_NAME_F2 (atan2), 1.0, 100.0, 40000)110TEST_INTERVAL (SV_NAME_F2 (atan2), 100, inf, 40000)111TEST_INTERVAL (SV_NAME_F2 (atan2), -0, -inf, 40000)112CLOSE_SVE_ATTR113114115