1 /*
2 * Helper for SVE routines which calculate log(1 + x) and do not
3 * need special-case handling
4 *
5 * Copyright (c) 2023, Arm Limited.
6 * SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception
7 */
8
9 #ifndef PL_MATH_SV_LOG1PF_INLINE_H
10 #define PL_MATH_SV_LOG1PF_INLINE_H
11
12 #include "v_math.h"
13 #include "math_config.h"
14 #include "poly_sve_f32.h"
15
16 static const struct sv_log1pf_data
17 {
18 float32_t poly[9];
19 float32_t ln2;
20 float32_t scale_back;
21 } sv_log1pf_data = {
22 /* Polynomial generated using FPMinimax in [-0.25, 0.5]. */
23 .poly = { -0x1p-1f, 0x1.5555aap-2f, -0x1.000038p-2f, 0x1.99675cp-3f,
24 -0x1.54ef78p-3f, 0x1.28a1f4p-3f, -0x1.0da91p-3f, 0x1.abcb6p-4f,
25 -0x1.6f0d5ep-5f },
26 .scale_back = 0x1.0p-23f,
27 .ln2 = 0x1.62e43p-1f,
28 };
29
30 static inline svfloat32_t
eval_poly(svfloat32_t m,const float32_t * c,svbool_t pg)31 eval_poly (svfloat32_t m, const float32_t *c, svbool_t pg)
32 {
33 svfloat32_t p_12 = svmla_x (pg, sv_f32 (c[0]), m, sv_f32 (c[1]));
34 svfloat32_t m2 = svmul_x (pg, m, m);
35 svfloat32_t q = svmla_x (pg, m, m2, p_12);
36 svfloat32_t p = sv_pw_horner_6_f32_x (pg, m, m2, c + 2);
37 p = svmul_x (pg, m2, p);
38
39 return svmla_x (pg, q, m2, p);
40 }
41
42 static inline svfloat32_t
sv_log1pf_inline(svfloat32_t x,svbool_t pg)43 sv_log1pf_inline (svfloat32_t x, svbool_t pg)
44 {
45 const struct sv_log1pf_data *d = ptr_barrier (&sv_log1pf_data);
46
47 svfloat32_t m = svadd_x (pg, x, 1.0f);
48
49 svint32_t ks = svsub_x (pg, svreinterpret_s32 (m),
50 svreinterpret_s32 (svdup_f32 (0.75f)));
51 ks = svand_x (pg, ks, 0xff800000);
52 svuint32_t k = svreinterpret_u32 (ks);
53 svfloat32_t s = svreinterpret_f32 (
54 svsub_x (pg, svreinterpret_u32 (svdup_f32 (4.0f)), k));
55
56 svfloat32_t m_scale
57 = svreinterpret_f32 (svsub_x (pg, svreinterpret_u32 (x), k));
58 m_scale
59 = svadd_x (pg, m_scale, svmla_x (pg, sv_f32 (-1.0f), sv_f32 (0.25f), s));
60 svfloat32_t p = eval_poly (m_scale, d->poly, pg);
61 svfloat32_t scale_back = svmul_x (pg, svcvt_f32_x (pg, k), d->scale_back);
62 return svmla_x (pg, p, scale_back, d->ln2);
63 }
64
65 #endif // PL_MATH_SV_LOG1PF_INLINE_H