xref: /aosp_15_r20/external/arm-optimized-routines/pl/math/v_log1pf_inline.h (revision 412f47f9e737e10ed5cc46ec6a8d7fa2264f8a14)
1*412f47f9SXin Li /*
2*412f47f9SXin Li  * Helper for single-precision routines which calculate log(1 + x) and do not
3*412f47f9SXin Li  * need special-case handling
4*412f47f9SXin Li  *
5*412f47f9SXin Li  * Copyright (c) 2022-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_V_LOG1PF_INLINE_H
10*412f47f9SXin Li #define PL_MATH_V_LOG1PF_INLINE_H
11*412f47f9SXin Li 
12*412f47f9SXin Li #include "v_math.h"
13*412f47f9SXin Li #include "poly_advsimd_f32.h"
14*412f47f9SXin Li 
15*412f47f9SXin Li struct v_log1pf_data
16*412f47f9SXin Li {
17*412f47f9SXin Li   float32x4_t poly[8], ln2;
18*412f47f9SXin Li   uint32x4_t four;
19*412f47f9SXin Li   int32x4_t three_quarters;
20*412f47f9SXin Li };
21*412f47f9SXin Li 
22*412f47f9SXin Li /* Polynomial generated using FPMinimax in [-0.25, 0.5]. First two coefficients
23*412f47f9SXin Li    (1, -0.5) are not stored as they can be generated more efficiently.  */
24*412f47f9SXin Li #define V_LOG1PF_CONSTANTS_TABLE                                              \
25*412f47f9SXin Li   {                                                                           \
26*412f47f9SXin Li     .poly                                                                     \
27*412f47f9SXin Li 	= { V4 (0x1.5555aap-2f),  V4 (-0x1.000038p-2f), V4 (0x1.99675cp-3f),  \
28*412f47f9SXin Li 	    V4 (-0x1.54ef78p-3f), V4 (0x1.28a1f4p-3f),	V4 (-0x1.0da91p-3f),  \
29*412f47f9SXin Li 	    V4 (0x1.abcb6p-4f),	  V4 (-0x1.6f0d5ep-5f) },                     \
30*412f47f9SXin Li 	.ln2 = V4 (0x1.62e43p-1f), .four = V4 (0x40800000),                   \
31*412f47f9SXin Li 	.three_quarters = V4 (0x3f400000)                                     \
32*412f47f9SXin Li   }
33*412f47f9SXin Li 
34*412f47f9SXin Li static inline float32x4_t
eval_poly(float32x4_t m,const float32x4_t * c)35*412f47f9SXin Li eval_poly (float32x4_t m, const float32x4_t *c)
36*412f47f9SXin Li {
37*412f47f9SXin Li   /* Approximate log(1+m) on [-0.25, 0.5] using pairwise Horner (main routine
38*412f47f9SXin Li      uses split Estrin, but this way reduces register pressure in the calling
39*412f47f9SXin Li      routine).  */
40*412f47f9SXin Li   float32x4_t q = vfmaq_f32 (v_f32 (-0.5), m, c[0]);
41*412f47f9SXin Li   float32x4_t m2 = vmulq_f32 (m, m);
42*412f47f9SXin Li   q = vfmaq_f32 (m, m2, q);
43*412f47f9SXin Li   float32x4_t p = v_pw_horner_6_f32 (m, m2, c + 1);
44*412f47f9SXin Li   p = vmulq_f32 (m2, p);
45*412f47f9SXin Li   return vfmaq_f32 (q, m2, p);
46*412f47f9SXin Li }
47*412f47f9SXin Li 
48*412f47f9SXin Li static inline float32x4_t
log1pf_inline(float32x4_t x,const struct v_log1pf_data d)49*412f47f9SXin Li log1pf_inline (float32x4_t x, const struct v_log1pf_data d)
50*412f47f9SXin Li {
51*412f47f9SXin Li   /* Helper for calculating log(x + 1). Copied from log1pf_2u1.c, with no
52*412f47f9SXin Li      special-case handling. See that file for details of the algorithm.  */
53*412f47f9SXin Li   float32x4_t m = vaddq_f32 (x, v_f32 (1.0f));
54*412f47f9SXin Li   int32x4_t k
55*412f47f9SXin Li       = vandq_s32 (vsubq_s32 (vreinterpretq_s32_f32 (m), d.three_quarters),
56*412f47f9SXin Li 		   v_s32 (0xff800000));
57*412f47f9SXin Li   uint32x4_t ku = vreinterpretq_u32_s32 (k);
58*412f47f9SXin Li   float32x4_t s = vreinterpretq_f32_u32 (vsubq_u32 (d.four, ku));
59*412f47f9SXin Li   float32x4_t m_scale
60*412f47f9SXin Li       = vreinterpretq_f32_u32 (vsubq_u32 (vreinterpretq_u32_f32 (x), ku));
61*412f47f9SXin Li   m_scale = vaddq_f32 (m_scale, vfmaq_f32 (v_f32 (-1.0f), v_f32 (0.25f), s));
62*412f47f9SXin Li   float32x4_t p = eval_poly (m_scale, d.poly);
63*412f47f9SXin Li   float32x4_t scale_back = vmulq_f32 (vcvtq_f32_s32 (k), v_f32 (0x1.0p-23f));
64*412f47f9SXin Li   return vfmaq_f32 (p, scale_back, d.ln2);
65*412f47f9SXin Li }
66*412f47f9SXin Li 
67*412f47f9SXin Li #endif //  PL_MATH_V_LOG1PF_INLINE_H
68