xref: /freebsd/contrib/arm-optimized-routines/math/aarch64/advsimd/v_log1pf_inline.h (revision dd21556857e8d40f66bf5ad54754d9d52669ebf7)
1 /*
2  * Helper for single-precision routines which calculate log(1 + x) and do not
3  * need special-case handling
4  *
5  * Copyright (c) 2022-2024, Arm Limited.
6  * SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception
7  */
8 
9 #ifndef MATH_V_LOG1PF_INLINE_H
10 #define MATH_V_LOG1PF_INLINE_H
11 
12 #include "v_math.h"
13 #include "v_poly_f32.h"
14 
15 struct v_log1pf_data
16 {
17   uint32x4_t four;
18   int32x4_t three_quarters;
19   float c0, c3, c5, c7;
20   float32x4_t c4, c6, c1, c2, ln2;
21 };
22 
23 /* Polynomial generated using FPMinimax in [-0.25, 0.5]. First two coefficients
24    (1, -0.5) are not stored as they can be generated more efficiently.  */
25 #define V_LOG1PF_CONSTANTS_TABLE                                              \
26   {                                                                           \
27     .c0 = 0x1.5555aap-2f, .c1 = V4 (-0x1.000038p-2f),                         \
28     .c2 = V4 (0x1.99675cp-3f), .c3 = -0x1.54ef78p-3f,                         \
29     .c4 = V4 (0x1.28a1f4p-3f), .c5 = -0x1.0da91p-3f,                          \
30     .c6 = V4 (0x1.abcb6p-4f), .c7 = -0x1.6f0d5ep-5f,                          \
31     .ln2 = V4 (0x1.62e43p-1f), .four = V4 (0x40800000),                       \
32     .three_quarters = V4 (0x3f400000)                                         \
33   }
34 
35 static inline float32x4_t
36 eval_poly (float32x4_t m, const struct v_log1pf_data *d)
37 {
38   /* Approximate log(1+m) on [-0.25, 0.5] using pairwise Horner.  */
39   float32x4_t c0357 = vld1q_f32 (&d->c0);
40   float32x4_t q = vfmaq_laneq_f32 (v_f32 (-0.5), m, c0357, 0);
41   float32x4_t m2 = vmulq_f32 (m, m);
42   float32x4_t p67 = vfmaq_laneq_f32 (d->c6, m, c0357, 3);
43   float32x4_t p45 = vfmaq_laneq_f32 (d->c4, m, c0357, 2);
44   float32x4_t p23 = vfmaq_laneq_f32 (d->c2, m, c0357, 1);
45   float32x4_t p = vfmaq_f32 (p45, m2, p67);
46   p = vfmaq_f32 (p23, m2, p);
47   p = vfmaq_f32 (d->c1, m, p);
48   p = vmulq_f32 (m2, p);
49   p = vfmaq_f32 (m, m2, p);
50   return vfmaq_f32 (p, m2, q);
51 }
52 
53 static inline float32x4_t
54 log1pf_inline (float32x4_t x, const struct v_log1pf_data *d)
55 {
56   /* Helper for calculating log(x + 1).  */
57 
58   /* With x + 1 = t * 2^k (where t = m + 1 and k is chosen such that m
59 			   is in [-0.25, 0.5]):
60      log1p(x) = log(t) + log(2^k) = log1p(m) + k*log(2).
61 
62      We approximate log1p(m) with a polynomial, then scale by
63      k*log(2). Instead of doing this directly, we use an intermediate
64      scale factor s = 4*k*log(2) to ensure the scale is representable
65      as a normalised fp32 number.  */
66   float32x4_t m = vaddq_f32 (x, v_f32 (1.0f));
67 
68   /* Choose k to scale x to the range [-1/4, 1/2].  */
69   int32x4_t k
70       = vandq_s32 (vsubq_s32 (vreinterpretq_s32_f32 (m), d->three_quarters),
71 		   v_s32 (0xff800000));
72   uint32x4_t ku = vreinterpretq_u32_s32 (k);
73 
74   /* Scale up to ensure that the scale factor is representable as normalised
75      fp32 number, and scale m down accordingly.  */
76   float32x4_t s = vreinterpretq_f32_u32 (vsubq_u32 (d->four, ku));
77 
78   /* Scale x by exponent manipulation.  */
79   float32x4_t m_scale
80       = vreinterpretq_f32_u32 (vsubq_u32 (vreinterpretq_u32_f32 (x), ku));
81   m_scale = vaddq_f32 (m_scale, vfmaq_f32 (v_f32 (-1.0f), v_f32 (0.25f), s));
82 
83   /* Evaluate polynomial on the reduced interval.  */
84   float32x4_t p = eval_poly (m_scale, d);
85 
86   /* The scale factor to be applied back at the end - by multiplying float(k)
87      by 2^-23 we get the unbiased exponent of k.  */
88   float32x4_t scale_back = vmulq_f32 (vcvtq_f32_s32 (k), v_f32 (0x1.0p-23f));
89 
90   /* Apply the scaling back.  */
91   return vfmaq_f32 (p, scale_back, d->ln2);
92 }
93 
94 #endif //  MATH_V_LOG1PF_INLINE_H
95