1 /* 2 * Single-precision vector atan2(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 "v_math.h" 9 #include "pl_sig.h" 10 #include "pl_test.h" 11 12 #if V_SUPPORTED 13 14 #include "atanf_common.h" 15 16 /* Useful constants. */ 17 #define PiOver2 v_f32 (0x1.921fb6p+0f) 18 #define SignMask v_u32 (0x80000000) 19 20 /* Special cases i.e. 0, infinity and nan (fall back to scalar calls). */ 21 VPCS_ATTR 22 NOINLINE static v_f32_t 23 specialcase (v_f32_t y, v_f32_t x, v_f32_t ret, v_u32_t cmp) 24 { 25 return v_call2_f32 (atan2f, y, x, ret, cmp); 26 } 27 28 /* Returns 1 if input is the bit representation of 0, infinity or nan. */ 29 static inline v_u32_t 30 zeroinfnan (v_u32_t i) 31 { 32 return v_cond_u32 (2 * i - 1 >= v_u32 (2 * 0x7f800000lu - 1)); 33 } 34 35 /* Fast implementation of vector atan2f. Maximum observed error is 36 2.95 ULP in [0x1.9300d6p+6 0x1.93c0c6p+6] x [0x1.8c2dbp+6 0x1.8cea6p+6]: 37 v_atan2(0x1.93836cp+6, 0x1.8cae1p+6) got 0x1.967f06p-1 38 want 0x1.967f00p-1. */ 39 VPCS_ATTR 40 v_f32_t V_NAME (atan2f) (v_f32_t y, v_f32_t x) 41 { 42 v_u32_t ix = v_as_u32_f32 (x); 43 v_u32_t iy = v_as_u32_f32 (y); 44 45 v_u32_t special_cases = zeroinfnan (ix) | zeroinfnan (iy); 46 47 v_u32_t sign_x = ix & SignMask; 48 v_u32_t sign_y = iy & SignMask; 49 v_u32_t sign_xy = sign_x ^ sign_y; 50 51 v_f32_t ax = v_abs_f32 (x); 52 v_f32_t ay = v_abs_f32 (y); 53 54 v_u32_t pred_xlt0 = x < 0.0f; 55 v_u32_t pred_aygtax = ay > ax; 56 57 /* Set up z for call to atanf. */ 58 v_f32_t n = v_sel_f32 (pred_aygtax, -ax, ay); 59 v_f32_t d = v_sel_f32 (pred_aygtax, ay, ax); 60 v_f32_t z = v_div_f32 (n, d); 61 62 /* Work out the correct shift. */ 63 v_f32_t shift = v_sel_f32 (pred_xlt0, v_f32 (-2.0f), v_f32 (0.0f)); 64 shift = v_sel_f32 (pred_aygtax, shift + 1.0f, shift); 65 shift *= PiOver2; 66 67 v_f32_t ret = eval_poly (z, z, shift); 68 69 /* Account for the sign of y. */ 70 ret = v_as_f32_u32 (v_as_u32_f32 (ret) ^ sign_xy); 71 72 if (unlikely (v_any_u32 (special_cases))) 73 { 74 return specialcase (y, x, ret, special_cases); 75 } 76 77 return ret; 78 } 79 VPCS_ALIAS 80 81 /* Arity of 2 means no mathbench entry emitted. See test/mathbench_funcs.h. */ 82 PL_SIG (V, F, 2, atan2) 83 PL_TEST_ULP (V_NAME (atan2f), 2.46) 84 PL_TEST_INTERVAL (V_NAME (atan2f), -10.0, 10.0, 50000) 85 PL_TEST_INTERVAL (V_NAME (atan2f), -1.0, 1.0, 40000) 86 PL_TEST_INTERVAL (V_NAME (atan2f), 0.0, 1.0, 40000) 87 PL_TEST_INTERVAL (V_NAME (atan2f), 1.0, 100.0, 40000) 88 PL_TEST_INTERVAL (V_NAME (atan2f), 1e6, 1e32, 40000) 89 #endif 90