1 /* 2 * Double-precision vector atan(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_f64.h" 12 13 static const struct data 14 { 15 float64_t poly[20]; 16 float64_t pi_over_2; 17 } data = { 18 /* Coefficients of polynomial P such that atan(x)~x+x*P(x^2) on 19 [2**-1022, 1.0]. */ 20 .poly = { -0x1.5555555555555p-2, 0x1.99999999996c1p-3, -0x1.2492492478f88p-3, 21 0x1.c71c71bc3951cp-4, -0x1.745d160a7e368p-4, 0x1.3b139b6a88ba1p-4, 22 -0x1.11100ee084227p-4, 0x1.e1d0f9696f63bp-5, -0x1.aebfe7b418581p-5, 23 0x1.842dbe9b0d916p-5, -0x1.5d30140ae5e99p-5, 0x1.338e31eb2fbbcp-5, 24 -0x1.00e6eece7de8p-5, 0x1.860897b29e5efp-6, -0x1.0051381722a59p-6, 25 0x1.14e9dc19a4a4ep-7, -0x1.d0062b42fe3bfp-9, 0x1.17739e210171ap-10, 26 -0x1.ab24da7be7402p-13, 0x1.358851160a528p-16, }, 27 .pi_over_2 = 0x1.921fb54442d18p+0, 28 }; 29 30 /* Useful constants. */ 31 #define SignMask (0x8000000000000000) 32 33 /* Fast implementation of SVE atan. 34 Based on atan(x) ~ shift + z + z^3 * P(z^2) with reduction to [0,1] using 35 z=1/x and shift = pi/2. Largest errors are close to 1. The maximum observed 36 error is 2.27 ulps: 37 _ZGVsMxv_atan (0x1.0005af27c23e9p+0) got 0x1.9225645bdd7c1p-1 38 want 0x1.9225645bdd7c3p-1. */ 39 svfloat64_t SV_NAME_D1 (atan) (svfloat64_t x, const svbool_t pg) 40 { 41 const struct data *d = ptr_barrier (&data); 42 43 /* No need to trigger special case. Small cases, infs and nans 44 are supported by our approximation technique. */ 45 svuint64_t ix = svreinterpret_u64 (x); 46 svuint64_t sign = svand_x (pg, ix, SignMask); 47 48 /* Argument reduction: 49 y := arctan(x) for x < 1 50 y := pi/2 + arctan(-1/x) for x > 1 51 Hence, use z=-1/a if x>=1, otherwise z=a. */ 52 svbool_t red = svacgt (pg, x, 1.0); 53 /* Avoid dependency in abs(x) in division (and comparison). */ 54 svfloat64_t z = svsel (red, svdivr_x (pg, x, 1.0), x); 55 /* Use absolute value only when needed (odd powers of z). */ 56 svfloat64_t az = svabs_x (pg, z); 57 az = svneg_m (az, red, az); 58 59 /* Use split Estrin scheme for P(z^2) with deg(P)=19. */ 60 svfloat64_t z2 = svmul_x (pg, z, z); 61 svfloat64_t x2 = svmul_x (pg, z2, z2); 62 svfloat64_t x4 = svmul_x (pg, x2, x2); 63 svfloat64_t x8 = svmul_x (pg, x4, x4); 64 65 svfloat64_t y 66 = svmla_x (pg, sv_estrin_7_f64_x (pg, z2, x2, x4, d->poly), 67 sv_estrin_11_f64_x (pg, z2, x2, x4, x8, d->poly + 8), x8); 68 69 /* y = shift + z + z^3 * P(z^2). */ 70 svfloat64_t z3 = svmul_x (pg, z2, az); 71 y = svmla_x (pg, az, z3, y); 72 73 /* Apply shift as indicated by `red` predicate. */ 74 y = svadd_m (red, y, d->pi_over_2); 75 76 /* y = atan(x) if x>0, -atan(-x) otherwise. */ 77 y = svreinterpret_f64 (sveor_x (pg, svreinterpret_u64 (y), sign)); 78 79 return y; 80 } 81 82 PL_SIG (SV, D, 1, atan, -3.1, 3.1) 83 PL_TEST_ULP (SV_NAME_D1 (atan), 1.78) 84 PL_TEST_INTERVAL (SV_NAME_D1 (atan), 0.0, 1.0, 40000) 85 PL_TEST_INTERVAL (SV_NAME_D1 (atan), 1.0, 100.0, 40000) 86 PL_TEST_INTERVAL (SV_NAME_D1 (atan), 100, inf, 40000) 87 PL_TEST_INTERVAL (SV_NAME_D1 (atan), -0, -inf, 40000) 88