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 12 #if SV_SUPPORTED 13 14 #include "sv_atanf_common.h" 15 16 /* Useful constants. */ 17 #define PiOver2 sv_f32 (0x1.921fb6p+0f) 18 #define SignMask sv_u32 (0x80000000) 19 20 /* Special cases i.e. 0, infinity, nan (fall back to scalar calls). */ 21 static inline sv_f32_t 22 specialcase (sv_f32_t y, sv_f32_t x, sv_f32_t ret, const svbool_t cmp) 23 { 24 return sv_call2_f32 (atan2f, y, x, ret, cmp); 25 } 26 27 /* Returns a predicate indicating true if the input is the bit representation of 28 0, infinity or nan. */ 29 static inline svbool_t 30 zeroinfnan (sv_u32_t i, const svbool_t pg) 31 { 32 return svcmpge_u32 (pg, svsub_n_u32_x (pg, svlsl_n_u32_x (pg, i, 1), 1), 33 sv_u32 (2 * 0x7f800000lu - 1)); 34 } 35 36 /* Fast implementation of SVE atan2f based on atan(x) ~ shift + z + z^3 * P(z^2) 37 with reduction to [0,1] using z=1/x and shift = pi/2. 38 Maximum observed error is 2.95 ULP: 39 __sv_atan2f(0x1.93836cp+6, 0x1.8cae1p+6) got 0x1.967f06p-1 40 want 0x1.967f00p-1. */ 41 sv_f32_t 42 __sv_atan2f_x (sv_f32_t y, sv_f32_t x, const svbool_t pg) 43 { 44 sv_u32_t ix = sv_as_u32_f32 (x); 45 sv_u32_t iy = sv_as_u32_f32 (y); 46 47 svbool_t cmp_x = zeroinfnan (ix, pg); 48 svbool_t cmp_y = zeroinfnan (iy, pg); 49 svbool_t cmp_xy = svorr_b_z (pg, cmp_x, cmp_y); 50 51 sv_u32_t sign_x = svand_u32_x (pg, ix, SignMask); 52 sv_u32_t sign_y = svand_u32_x (pg, iy, SignMask); 53 sv_u32_t sign_xy = sveor_u32_x (pg, sign_x, sign_y); 54 55 sv_f32_t ax = svabs_f32_x (pg, x); 56 sv_f32_t ay = svabs_f32_x (pg, y); 57 58 svbool_t pred_xlt0 = svcmplt_f32 (pg, x, sv_f32 (0.0)); 59 svbool_t pred_aygtax = svcmpgt_f32 (pg, ay, ax); 60 61 /* Set up z for call to atan. */ 62 sv_f32_t n = svsel_f32 (pred_aygtax, svneg_f32_x (pg, ax), ay); 63 sv_f32_t d = svsel_f32 (pred_aygtax, ay, ax); 64 sv_f32_t z = svdiv_f32_x (pg, n, d); 65 66 /* Work out the correct shift. */ 67 sv_f32_t shift = svsel_f32 (pred_xlt0, sv_f32 (-2.0), sv_f32 (0.0)); 68 shift = svsel_f32 (pred_aygtax, svadd_n_f32_x (pg, shift, 1.0), shift); 69 shift = svmul_f32_x (pg, shift, PiOver2); 70 71 sv_f32_t ret = __sv_atanf_common (pg, pg, z, z, shift); 72 73 /* Account for the sign of x and y. */ 74 ret = sv_as_f32_u32 (sveor_u32_x (pg, sv_as_u32_f32 (ret), sign_xy)); 75 76 if (unlikely (svptest_any (pg, cmp_xy))) 77 { 78 return specialcase (y, x, ret, cmp_xy); 79 } 80 81 return ret; 82 } 83 84 PL_ALIAS (__sv_atan2f_x, _ZGVsMxvv_atan2f) 85 86 /* Arity of 2 means no mathbench entry emitted. See test/mathbench_funcs.h. */ 87 PL_SIG (SV, F, 2, atan2) 88 PL_TEST_ULP (__sv_atan2f, 2.45) 89 PL_TEST_INTERVAL (__sv_atan2f, -10.0, 10.0, 50000) 90 PL_TEST_INTERVAL (__sv_atan2f, -1.0, 1.0, 40000) 91 PL_TEST_INTERVAL (__sv_atan2f, 0.0, 1.0, 40000) 92 PL_TEST_INTERVAL (__sv_atan2f, 1.0, 100.0, 40000) 93 PL_TEST_INTERVAL (__sv_atan2f, 1e6, 1e32, 40000) 94 #endif 95