xref: /freebsd/contrib/arm-optimized-routines/math/aarch64/sve/log10f.c (revision f3087bef11543b42e0d69b708f367097a4118d24)
1*f3087befSAndrew Turner /*
2*f3087befSAndrew Turner  * Single-precision SVE log10 function.
3*f3087befSAndrew Turner  *
4*f3087befSAndrew Turner  * Copyright (c) 2022-2024, Arm Limited.
5*f3087befSAndrew Turner  * SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception
6*f3087befSAndrew Turner  */
7*f3087befSAndrew Turner 
8*f3087befSAndrew Turner #include "sv_math.h"
9*f3087befSAndrew Turner #include "test_sig.h"
10*f3087befSAndrew Turner #include "test_defs.h"
11*f3087befSAndrew Turner 
12*f3087befSAndrew Turner static const struct data
13*f3087befSAndrew Turner {
14*f3087befSAndrew Turner   float poly_0246[4];
15*f3087befSAndrew Turner   float poly_1357[4];
16*f3087befSAndrew Turner   float ln2, inv_ln10;
17*f3087befSAndrew Turner   uint32_t off, lower;
18*f3087befSAndrew Turner } data = {
19*f3087befSAndrew Turner   .poly_1357 = {
20*f3087befSAndrew Turner     /* Coefficients copied from the AdvSIMD routine, then rearranged so that coeffs
21*f3087befSAndrew Turner        1, 3, 5 and 7 can be loaded as a single quad-word, hence used with _lane
22*f3087befSAndrew Turner        variant of MLA intrinsic.  */
23*f3087befSAndrew Turner     0x1.2879c8p-3f, 0x1.6408f8p-4f, 0x1.f0e514p-5f, 0x1.f5f76ap-5f
24*f3087befSAndrew Turner   },
25*f3087befSAndrew Turner   .poly_0246 = { -0x1.bcb79cp-3f, -0x1.bcd472p-4f, -0x1.246f8p-4f,
26*f3087befSAndrew Turner 		 -0x1.0fc92cp-4f },
27*f3087befSAndrew Turner   .ln2 = 0x1.62e43p-1f,
28*f3087befSAndrew Turner   .inv_ln10 = 0x1.bcb7b2p-2f,
29*f3087befSAndrew Turner   .off = 0x3f2aaaab,
30*f3087befSAndrew Turner   /* Lower bound is the smallest positive normal float 0x00800000. For
31*f3087befSAndrew Turner      optimised register use subnormals are detected after offset has been
32*f3087befSAndrew Turner      subtracted, so lower bound is 0x0080000 - offset (which wraps around).  */
33*f3087befSAndrew Turner   .lower = 0x00800000 - 0x3f2aaaab
34*f3087befSAndrew Turner };
35*f3087befSAndrew Turner 
36*f3087befSAndrew Turner #define Thres 0x7f000000 /* asuint32(inf) - 0x00800000.  */
37*f3087befSAndrew Turner #define MantissaMask 0x007fffff
38*f3087befSAndrew Turner 
39*f3087befSAndrew Turner static svfloat32_t NOINLINE
special_case(svuint32_t u_off,svfloat32_t p,svfloat32_t r2,svfloat32_t y,svbool_t cmp)40*f3087befSAndrew Turner special_case (svuint32_t u_off, svfloat32_t p, svfloat32_t r2, svfloat32_t y,
41*f3087befSAndrew Turner 	      svbool_t cmp)
42*f3087befSAndrew Turner {
43*f3087befSAndrew Turner   return sv_call_f32 (
44*f3087befSAndrew Turner       log10f, svreinterpret_f32 (svadd_x (svptrue_b32 (), u_off, data.off)),
45*f3087befSAndrew Turner       svmla_x (svptrue_b32 (), p, r2, y), cmp);
46*f3087befSAndrew Turner }
47*f3087befSAndrew Turner 
48*f3087befSAndrew Turner /* Optimised implementation of SVE log10f using the same algorithm and
49*f3087befSAndrew Turner    polynomial as AdvSIMD log10f.
50*f3087befSAndrew Turner    Maximum error is 3.31ulps:
51*f3087befSAndrew Turner    SV_NAME_F1 (log10)(0x1.555c16p+0) got 0x1.ffe2fap-4
52*f3087befSAndrew Turner 				    want 0x1.ffe2f4p-4.  */
SV_NAME_F1(log10)53*f3087befSAndrew Turner svfloat32_t SV_NAME_F1 (log10) (svfloat32_t x, const svbool_t pg)
54*f3087befSAndrew Turner {
55*f3087befSAndrew Turner   const struct data *d = ptr_barrier (&data);
56*f3087befSAndrew Turner 
57*f3087befSAndrew Turner   svuint32_t u_off = svreinterpret_u32 (x);
58*f3087befSAndrew Turner 
59*f3087befSAndrew Turner   u_off = svsub_x (pg, u_off, d->off);
60*f3087befSAndrew Turner   svbool_t special = svcmpge (pg, svsub_x (pg, u_off, d->lower), Thres);
61*f3087befSAndrew Turner 
62*f3087befSAndrew Turner   /* x = 2^n * (1+r), where 2/3 < 1+r < 4/3.  */
63*f3087befSAndrew Turner   svfloat32_t n = svcvt_f32_x (
64*f3087befSAndrew Turner       pg, svasr_x (pg, svreinterpret_s32 (u_off), 23)); /* signextend.  */
65*f3087befSAndrew Turner   svuint32_t ix = svand_x (pg, u_off, MantissaMask);
66*f3087befSAndrew Turner   ix = svadd_x (pg, ix, d->off);
67*f3087befSAndrew Turner   svfloat32_t r = svsub_x (pg, svreinterpret_f32 (ix), 1.0f);
68*f3087befSAndrew Turner 
69*f3087befSAndrew Turner   /* y = log10(1+r) + n*log10(2)
70*f3087befSAndrew Turner      log10(1+r) ~ r * InvLn(10) + P(r)
71*f3087befSAndrew Turner      where P(r) is a polynomial. Use order 9 for log10(1+x), i.e. order 8 for
72*f3087befSAndrew Turner      log10(1+x)/x, with x in [-1/3, 1/3] (offset=2/3).  */
73*f3087befSAndrew Turner   svfloat32_t r2 = svmul_x (svptrue_b32 (), r, r);
74*f3087befSAndrew Turner   svfloat32_t r4 = svmul_x (svptrue_b32 (), r2, r2);
75*f3087befSAndrew Turner   svfloat32_t p_1357 = svld1rq (svptrue_b32 (), &d->poly_1357[0]);
76*f3087befSAndrew Turner   svfloat32_t q_01 = svmla_lane (sv_f32 (d->poly_0246[0]), r, p_1357, 0);
77*f3087befSAndrew Turner   svfloat32_t q_23 = svmla_lane (sv_f32 (d->poly_0246[1]), r, p_1357, 1);
78*f3087befSAndrew Turner   svfloat32_t q_45 = svmla_lane (sv_f32 (d->poly_0246[2]), r, p_1357, 2);
79*f3087befSAndrew Turner   svfloat32_t q_67 = svmla_lane (sv_f32 (d->poly_0246[3]), r, p_1357, 3);
80*f3087befSAndrew Turner   svfloat32_t q_47 = svmla_x (pg, q_45, r2, q_67);
81*f3087befSAndrew Turner   svfloat32_t q_03 = svmla_x (pg, q_01, r2, q_23);
82*f3087befSAndrew Turner   svfloat32_t y = svmla_x (pg, q_03, r4, q_47);
83*f3087befSAndrew Turner 
84*f3087befSAndrew Turner   /* Using hi = Log10(2)*n + r*InvLn(10) is faster but less accurate.  */
85*f3087befSAndrew Turner   svfloat32_t hi = svmla_x (pg, r, n, d->ln2);
86*f3087befSAndrew Turner   hi = svmul_x (pg, hi, d->inv_ln10);
87*f3087befSAndrew Turner 
88*f3087befSAndrew Turner   if (unlikely (svptest_any (pg, special)))
89*f3087befSAndrew Turner     return special_case (u_off, hi, r2, y, special);
90*f3087befSAndrew Turner   return svmla_x (svptrue_b32 (), hi, r2, y);
91*f3087befSAndrew Turner }
92*f3087befSAndrew Turner 
93*f3087befSAndrew Turner TEST_SIG (SV, F, 1, log10, 0.01, 11.1)
94*f3087befSAndrew Turner TEST_ULP (SV_NAME_F1 (log10), 2.82)
95*f3087befSAndrew Turner TEST_DISABLE_FENV (SV_NAME_F1 (log10))
96*f3087befSAndrew Turner TEST_INTERVAL (SV_NAME_F1 (log10), -0.0, -0x1p126, 100)
97*f3087befSAndrew Turner TEST_INTERVAL (SV_NAME_F1 (log10), 0x1p-149, 0x1p-126, 4000)
98*f3087befSAndrew Turner TEST_INTERVAL (SV_NAME_F1 (log10), 0x1p-126, 0x1p-23, 50000)
99*f3087befSAndrew Turner TEST_INTERVAL (SV_NAME_F1 (log10), 0x1p-23, 1.0, 50000)
100*f3087befSAndrew Turner TEST_INTERVAL (SV_NAME_F1 (log10), 1.0, 100, 50000)
101*f3087befSAndrew Turner TEST_INTERVAL (SV_NAME_F1 (log10), 100, inf, 50000)
102*f3087befSAndrew Turner CLOSE_SVE_ATTR
103