xref: /freebsd/contrib/arm-optimized-routines/pl/math/v_logf_inline.h (revision 5a02ffc32e777041dd2dad4e651ed2a0865a0a5d)
1*5a02ffc3SAndrew Turner /*
2*5a02ffc3SAndrew Turner  * Single-precision vector log function - inline version
3*5a02ffc3SAndrew Turner  *
4*5a02ffc3SAndrew Turner  * Copyright (c) 2019-2023, Arm Limited.
5*5a02ffc3SAndrew Turner  * SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception
6*5a02ffc3SAndrew Turner  */
7*5a02ffc3SAndrew Turner 
8*5a02ffc3SAndrew Turner #include "v_math.h"
9*5a02ffc3SAndrew Turner 
10*5a02ffc3SAndrew Turner struct v_logf_data
11*5a02ffc3SAndrew Turner {
12*5a02ffc3SAndrew Turner   float32x4_t poly[7];
13*5a02ffc3SAndrew Turner   float32x4_t ln2;
14*5a02ffc3SAndrew Turner   uint32x4_t off, mantissa_mask;
15*5a02ffc3SAndrew Turner };
16*5a02ffc3SAndrew Turner 
17*5a02ffc3SAndrew Turner #define V_LOGF_CONSTANTS                                                      \
18*5a02ffc3SAndrew Turner   {                                                                           \
19*5a02ffc3SAndrew Turner     .poly                                                                     \
20*5a02ffc3SAndrew Turner 	= { V4 (-0x1.3e737cp-3f), V4 (0x1.5a9aa2p-3f),	V4 (-0x1.4f9934p-3f), \
21*5a02ffc3SAndrew Turner 	    V4 (0x1.961348p-3f),  V4 (-0x1.00187cp-2f), V4 (0x1.555d7cp-2f),  \
22*5a02ffc3SAndrew Turner 	    V4 (-0x1.ffffc8p-2f) },                                           \
23*5a02ffc3SAndrew Turner 	.ln2 = V4 (0x1.62e43p-1f), .off = V4 (0x3f2aaaab),                    \
24*5a02ffc3SAndrew Turner 	.mantissa_mask = V4 (0x007fffff)                                      \
25*5a02ffc3SAndrew Turner   }
26*5a02ffc3SAndrew Turner 
27*5a02ffc3SAndrew Turner #define P(i) d->poly[7 - i]
28*5a02ffc3SAndrew Turner 
29*5a02ffc3SAndrew Turner static inline float32x4_t
v_logf_inline(float32x4_t x,const struct v_logf_data * d)30*5a02ffc3SAndrew Turner v_logf_inline (float32x4_t x, const struct v_logf_data *d)
31*5a02ffc3SAndrew Turner {
32*5a02ffc3SAndrew Turner   float32x4_t n, p, q, r, r2, y;
33*5a02ffc3SAndrew Turner   uint32x4_t u;
34*5a02ffc3SAndrew Turner 
35*5a02ffc3SAndrew Turner   u = vreinterpretq_u32_f32 (x);
36*5a02ffc3SAndrew Turner 
37*5a02ffc3SAndrew Turner   /* x = 2^n * (1+r), where 2/3 < 1+r < 4/3.  */
38*5a02ffc3SAndrew Turner   u = vsubq_u32 (u, d->off);
39*5a02ffc3SAndrew Turner   n = vcvtq_f32_s32 (
40*5a02ffc3SAndrew Turner       vshrq_n_s32 (vreinterpretq_s32_u32 (u), 23)); /* signextend.  */
41*5a02ffc3SAndrew Turner   u = vandq_u32 (u, d->mantissa_mask);
42*5a02ffc3SAndrew Turner   u = vaddq_u32 (u, d->off);
43*5a02ffc3SAndrew Turner   r = vsubq_f32 (vreinterpretq_f32_u32 (u), v_f32 (1.0f));
44*5a02ffc3SAndrew Turner 
45*5a02ffc3SAndrew Turner   /* y = log(1+r) + n*ln2.  */
46*5a02ffc3SAndrew Turner   r2 = vmulq_f32 (r, r);
47*5a02ffc3SAndrew Turner   /* n*ln2 + r + r2*(P1 + r*P2 + r2*(P3 + r*P4 + r2*(P5 + r*P6 + r2*P7))).  */
48*5a02ffc3SAndrew Turner   p = vfmaq_f32 (P (5), P (6), r);
49*5a02ffc3SAndrew Turner   q = vfmaq_f32 (P (3), P (4), r);
50*5a02ffc3SAndrew Turner   y = vfmaq_f32 (P (1), P (2), r);
51*5a02ffc3SAndrew Turner   p = vfmaq_f32 (p, P (7), r2);
52*5a02ffc3SAndrew Turner   q = vfmaq_f32 (q, p, r2);
53*5a02ffc3SAndrew Turner   y = vfmaq_f32 (y, q, r2);
54*5a02ffc3SAndrew Turner   p = vfmaq_f32 (r, d->ln2, n);
55*5a02ffc3SAndrew Turner 
56*5a02ffc3SAndrew Turner   return vfmaq_f32 (p, y, r2);
57*5a02ffc3SAndrew Turner }
58*5a02ffc3SAndrew Turner 
59*5a02ffc3SAndrew Turner #undef P
60