xref: /aosp_15_r20/external/arm-optimized-routines/pl/math/atanf_common.h (revision 412f47f9e737e10ed5cc46ec6a8d7fa2264f8a14)
1*412f47f9SXin Li /*
2*412f47f9SXin Li  * Single-precision polynomial evaluation function for scalar
3*412f47f9SXin Li  * atan(x) and atan2(y,x).
4*412f47f9SXin Li  *
5*412f47f9SXin Li  * Copyright (c) 2021-2023, Arm Limited.
6*412f47f9SXin Li  * SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception
7*412f47f9SXin Li  */
8*412f47f9SXin Li 
9*412f47f9SXin Li #ifndef PL_MATH_ATANF_COMMON_H
10*412f47f9SXin Li #define PL_MATH_ATANF_COMMON_H
11*412f47f9SXin Li 
12*412f47f9SXin Li #include "math_config.h"
13*412f47f9SXin Li #include "poly_scalar_f32.h"
14*412f47f9SXin Li 
15*412f47f9SXin Li /* Polynomial used in fast atanf(x) and atan2f(y,x) implementations
16*412f47f9SXin Li    The order 7 polynomial P approximates (atan(sqrt(x))-sqrt(x))/x^(3/2).  */
17*412f47f9SXin Li static inline float
eval_poly(float z,float az,float shift)18*412f47f9SXin Li eval_poly (float z, float az, float shift)
19*412f47f9SXin Li {
20*412f47f9SXin Li   /* Use 2-level Estrin scheme for P(z^2) with deg(P)=7. However,
21*412f47f9SXin Li      a standard implementation using z8 creates spurious underflow
22*412f47f9SXin Li      in the very last fma (when z^8 is small enough).
23*412f47f9SXin Li      Therefore, we split the last fma into a mul and and an fma.
24*412f47f9SXin Li      Horner and single-level Estrin have higher errors that exceed
25*412f47f9SXin Li      threshold.  */
26*412f47f9SXin Li   float z2 = z * z;
27*412f47f9SXin Li   float z4 = z2 * z2;
28*412f47f9SXin Li 
29*412f47f9SXin Li   /* Then assemble polynomial.  */
30*412f47f9SXin Li   float y = fmaf (
31*412f47f9SXin Li       z4, z4 * pairwise_poly_3_f32 (z2, z4, __atanf_poly_data.poly + 4),
32*412f47f9SXin Li       pairwise_poly_3_f32 (z2, z4, __atanf_poly_data.poly));
33*412f47f9SXin Li   /* Finalize:
34*412f47f9SXin Li      y = shift + z * P(z^2).  */
35*412f47f9SXin Li   return fmaf (y, z2 * az, az) + shift;
36*412f47f9SXin Li }
37*412f47f9SXin Li 
38*412f47f9SXin Li #endif // PL_MATH_ATANF_COMMON_H
39