1 /* 2 * Single-precision vector atan2f(x) function. 3 * 4 * Copyright (c) 2021-2023, Arm Limited. 5 * SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception 6 */ 7 8 #include "sv_math.h" 9 #include "pl_sig.h" 10 #include "pl_test.h" 11 #include "poly_sve_f32.h" 12 13 static const struct data 14 { 15 float32_t poly[8]; 16 float32_t pi_over_2; 17 } data = { 18 /* Coefficients of polynomial P such that atan(x)~x+x*P(x^2) on 19 [2**-128, 1.0]. */ 20 .poly = { -0x1.55555p-2f, 0x1.99935ep-3f, -0x1.24051ep-3f, 0x1.bd7368p-4f, 21 -0x1.491f0ep-4f, 0x1.93a2c0p-5f, -0x1.4c3c60p-6f, 0x1.01fd88p-8f }, 22 .pi_over_2 = 0x1.921fb6p+0f, 23 }; 24 25 #define SignMask sv_u32 (0x80000000) 26 27 /* Special cases i.e. 0, infinity, nan (fall back to scalar calls). */ 28 static inline svfloat32_t 29 special_case (svfloat32_t y, svfloat32_t x, svfloat32_t ret, 30 const svbool_t cmp) 31 { 32 return sv_call2_f32 (atan2f, y, x, ret, cmp); 33 } 34 35 /* Returns a predicate indicating true if the input is the bit representation 36 of 0, infinity or nan. */ 37 static inline svbool_t 38 zeroinfnan (svuint32_t i, const svbool_t pg) 39 { 40 return svcmpge (pg, svsub_x (pg, svlsl_x (pg, i, 1), 1), 41 sv_u32 (2 * 0x7f800000lu - 1)); 42 } 43 44 /* Fast implementation of SVE atan2f based on atan(x) ~ shift + z + z^3 * 45 P(z^2) with reduction to [0,1] using z=1/x and shift = pi/2. Maximum 46 observed error is 2.95 ULP: 47 _ZGVsMxvv_atan2f (0x1.93836cp+6, 0x1.8cae1p+6) got 0x1.967f06p-1 48 want 0x1.967f00p-1. */ 49 svfloat32_t SV_NAME_F2 (atan2) (svfloat32_t y, svfloat32_t x, const svbool_t pg) 50 { 51 const struct data *data_ptr = ptr_barrier (&data); 52 53 svuint32_t ix = svreinterpret_u32 (x); 54 svuint32_t iy = svreinterpret_u32 (y); 55 56 svbool_t cmp_x = zeroinfnan (ix, pg); 57 svbool_t cmp_y = zeroinfnan (iy, pg); 58 svbool_t cmp_xy = svorr_z (pg, cmp_x, cmp_y); 59 60 svuint32_t sign_x = svand_x (pg, ix, SignMask); 61 svuint32_t sign_y = svand_x (pg, iy, SignMask); 62 svuint32_t sign_xy = sveor_x (pg, sign_x, sign_y); 63 64 svfloat32_t ax = svabs_x (pg, x); 65 svfloat32_t ay = svabs_x (pg, y); 66 67 svbool_t pred_xlt0 = svcmplt (pg, x, 0.0); 68 svbool_t pred_aygtax = svcmpgt (pg, ay, ax); 69 70 /* Set up z for call to atan. */ 71 svfloat32_t n = svsel (pred_aygtax, svneg_x (pg, ax), ay); 72 svfloat32_t d = svsel (pred_aygtax, ay, ax); 73 svfloat32_t z = svdiv_x (pg, n, d); 74 75 /* Work out the correct shift. */ 76 svfloat32_t shift = svsel (pred_xlt0, sv_f32 (-2.0), sv_f32 (0.0)); 77 shift = svsel (pred_aygtax, svadd_x (pg, shift, 1.0), shift); 78 shift = svmul_x (pg, shift, sv_f32 (data_ptr->pi_over_2)); 79 80 /* Use split Estrin scheme for P(z^2) with deg(P)=7. */ 81 svfloat32_t z2 = svmul_x (pg, z, z); 82 svfloat32_t z4 = svmul_x (pg, z2, z2); 83 svfloat32_t z8 = svmul_x (pg, z4, z4); 84 85 svfloat32_t ret = sv_estrin_7_f32_x (pg, z2, z4, z8, data_ptr->poly); 86 87 /* ret = shift + z + z^3 * P(z^2). */ 88 svfloat32_t z3 = svmul_x (pg, z2, z); 89 ret = svmla_x (pg, z, z3, ret); 90 91 ret = svadd_m (pg, ret, shift); 92 93 /* Account for the sign of x and y. */ 94 ret = svreinterpret_f32 (sveor_x (pg, svreinterpret_u32 (ret), sign_xy)); 95 96 if (unlikely (svptest_any (pg, cmp_xy))) 97 return special_case (y, x, ret, cmp_xy); 98 99 return ret; 100 } 101 102 /* Arity of 2 means no mathbench entry emitted. See test/mathbench_funcs.h. */ 103 PL_SIG (SV, F, 2, atan2) 104 PL_TEST_ULP (SV_NAME_F2 (atan2), 2.45) 105 PL_TEST_INTERVAL (SV_NAME_F2 (atan2), 0.0, 1.0, 40000) 106 PL_TEST_INTERVAL (SV_NAME_F2 (atan2), 1.0, 100.0, 40000) 107 PL_TEST_INTERVAL (SV_NAME_F2 (atan2), 100, inf, 40000) 108 PL_TEST_INTERVAL (SV_NAME_F2 (atan2), -0, -inf, 40000) 109