xref: /freebsd/contrib/arm-optimized-routines/math/aarch64/sve/sv_log1pf_inline.h (revision f3087bef11543b42e0d69b708f367097a4118d24)
1*f3087befSAndrew Turner /*
2*f3087befSAndrew Turner  * Helper for SVE routines which calculate log(1 + x) and do not
3*f3087befSAndrew Turner  * need special-case handling
4*f3087befSAndrew Turner  *
5*f3087befSAndrew Turner  * Copyright (c) 2023-2024, Arm Limited.
6*f3087befSAndrew Turner  * SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception
7*f3087befSAndrew Turner  */
8*f3087befSAndrew Turner 
9*f3087befSAndrew Turner #ifndef MATH_SV_LOG1PF_INLINE_H
10*f3087befSAndrew Turner #define MATH_SV_LOG1PF_INLINE_H
11*f3087befSAndrew Turner 
12*f3087befSAndrew Turner #define SignExponentMask 0xff800000
13*f3087befSAndrew Turner 
14*f3087befSAndrew Turner static const struct sv_log1pf_data
15*f3087befSAndrew Turner {
16*f3087befSAndrew Turner   float c0, c2, c4, c6;
17*f3087befSAndrew Turner   float c1, c3, c5, c7;
18*f3087befSAndrew Turner   float ln2, exp_bias, quarter;
19*f3087befSAndrew Turner   uint32_t four, three_quarters;
20*f3087befSAndrew Turner } sv_log1pf_data = {
21*f3087befSAndrew Turner   /* Do not store first term of polynomial, which is -0.5, as
22*f3087befSAndrew Turner      this can be fmov-ed directly instead of including it in
23*f3087befSAndrew Turner      the main load-and-mla polynomial schedule.  */
24*f3087befSAndrew Turner   .c0 = 0x1.5555aap-2f,		.c1 = -0x1.000038p-2f, .c2 = 0x1.99675cp-3f,
25*f3087befSAndrew Turner   .c3 = -0x1.54ef78p-3f,	.c4 = 0x1.28a1f4p-3f,  .c5 = -0x1.0da91p-3f,
26*f3087befSAndrew Turner   .c6 = 0x1.abcb6p-4f,		.c7 = -0x1.6f0d5ep-5f, .ln2 = 0x1.62e43p-1f,
27*f3087befSAndrew Turner   .exp_bias = 0x1p-23f,		.quarter = 0x1p-2f,    .four = 0x40800000,
28*f3087befSAndrew Turner   .three_quarters = 0x3f400000,
29*f3087befSAndrew Turner };
30*f3087befSAndrew Turner 
31*f3087befSAndrew Turner static inline svfloat32_t
sv_log1pf_inline(svfloat32_t x,svbool_t pg)32*f3087befSAndrew Turner sv_log1pf_inline (svfloat32_t x, svbool_t pg)
33*f3087befSAndrew Turner {
34*f3087befSAndrew Turner   const struct sv_log1pf_data *d = ptr_barrier (&sv_log1pf_data);
35*f3087befSAndrew Turner 
36*f3087befSAndrew Turner   /* With x + 1 = t * 2^k (where t = m + 1 and k is chosen such that m
37*f3087befSAndrew Turner 			 is in [-0.25, 0.5]):
38*f3087befSAndrew Turner    log1p(x) = log(t) + log(2^k) = log1p(m) + k*log(2).
39*f3087befSAndrew Turner 
40*f3087befSAndrew Turner    We approximate log1p(m) with a polynomial, then scale by
41*f3087befSAndrew Turner    k*log(2). Instead of doing this directly, we use an intermediate
42*f3087befSAndrew Turner    scale factor s = 4*k*log(2) to ensure the scale is representable
43*f3087befSAndrew Turner    as a normalised fp32 number.  */
44*f3087befSAndrew Turner   svfloat32_t m = svadd_x (pg, x, 1);
45*f3087befSAndrew Turner 
46*f3087befSAndrew Turner   /* Choose k to scale x to the range [-1/4, 1/2].  */
47*f3087befSAndrew Turner   svint32_t k
48*f3087befSAndrew Turner       = svand_x (pg, svsub_x (pg, svreinterpret_s32 (m), d->three_quarters),
49*f3087befSAndrew Turner 		 sv_s32 (SignExponentMask));
50*f3087befSAndrew Turner 
51*f3087befSAndrew Turner   /* Scale x by exponent manipulation.  */
52*f3087befSAndrew Turner   svfloat32_t m_scale = svreinterpret_f32 (
53*f3087befSAndrew Turner       svsub_x (pg, svreinterpret_u32 (x), svreinterpret_u32 (k)));
54*f3087befSAndrew Turner 
55*f3087befSAndrew Turner   /* Scale up to ensure that the scale factor is representable as normalised
56*f3087befSAndrew Turner      fp32 number, and scale m down accordingly.  */
57*f3087befSAndrew Turner   svfloat32_t s = svreinterpret_f32 (svsubr_x (pg, k, d->four));
58*f3087befSAndrew Turner   svfloat32_t fconst = svld1rq_f32 (svptrue_b32 (), &d->ln2);
59*f3087befSAndrew Turner   m_scale = svadd_x (pg, m_scale, svmla_lane_f32 (sv_f32 (-1), s, fconst, 2));
60*f3087befSAndrew Turner 
61*f3087befSAndrew Turner   /* Evaluate polynomial on reduced interval.  */
62*f3087befSAndrew Turner   svfloat32_t ms2 = svmul_x (svptrue_b32 (), m_scale, m_scale);
63*f3087befSAndrew Turner 
64*f3087befSAndrew Turner   svfloat32_t c1357 = svld1rq_f32 (svptrue_b32 (), &d->c1);
65*f3087befSAndrew Turner   svfloat32_t p01 = svmla_lane_f32 (sv_f32 (d->c0), m_scale, c1357, 0);
66*f3087befSAndrew Turner   svfloat32_t p23 = svmla_lane_f32 (sv_f32 (d->c2), m_scale, c1357, 1);
67*f3087befSAndrew Turner   svfloat32_t p45 = svmla_lane_f32 (sv_f32 (d->c4), m_scale, c1357, 2);
68*f3087befSAndrew Turner   svfloat32_t p67 = svmla_lane_f32 (sv_f32 (d->c6), m_scale, c1357, 3);
69*f3087befSAndrew Turner 
70*f3087befSAndrew Turner   svfloat32_t p = svmla_x (pg, p45, p67, ms2);
71*f3087befSAndrew Turner   p = svmla_x (pg, p23, p, ms2);
72*f3087befSAndrew Turner   p = svmla_x (pg, p01, p, ms2);
73*f3087befSAndrew Turner 
74*f3087befSAndrew Turner   p = svmad_x (pg, m_scale, p, -0.5);
75*f3087befSAndrew Turner   p = svmla_x (pg, m_scale, m_scale, svmul_x (pg, m_scale, p));
76*f3087befSAndrew Turner 
77*f3087befSAndrew Turner   /* The scale factor to be applied back at the end - by multiplying float(k)
78*f3087befSAndrew Turner    by 2^-23 we get the unbiased exponent of k.  */
79*f3087befSAndrew Turner   svfloat32_t scale_back = svmul_lane_f32 (svcvt_f32_x (pg, k), fconst, 1);
80*f3087befSAndrew Turner   return svmla_lane_f32 (p, scale_back, fconst, 0);
81*f3087befSAndrew Turner }
82*f3087befSAndrew Turner 
83*f3087befSAndrew Turner #endif //  SV_LOG1PF_INLINE_H
84