1*f3087befSAndrew Turner /*
2*f3087befSAndrew Turner * Single-precision vector log function.
3*f3087befSAndrew Turner *
4*f3087befSAndrew Turner * Copyright (c) 2019-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_0135[4];
15*f3087befSAndrew Turner float poly_246[3];
16*f3087befSAndrew Turner float ln2;
17*f3087befSAndrew Turner uint32_t off, lower;
18*f3087befSAndrew Turner } data = {
19*f3087befSAndrew Turner .poly_0135 = {
20*f3087befSAndrew Turner /* Coefficients copied from the AdvSIMD routine in math/, then rearranged so
21*f3087befSAndrew Turner that coeffs 0, 1, 3 and 5 can be loaded as a single quad-word, hence used
22*f3087befSAndrew Turner with _lane variant of MLA intrinsic. */
23*f3087befSAndrew Turner -0x1.3e737cp-3f, 0x1.5a9aa2p-3f, 0x1.961348p-3f, 0x1.555d7cp-2f
24*f3087befSAndrew Turner },
25*f3087befSAndrew Turner .poly_246 = { -0x1.4f9934p-3f, -0x1.00187cp-2f, -0x1.ffffc8p-2f },
26*f3087befSAndrew Turner .ln2 = 0x1.62e43p-1f,
27*f3087befSAndrew Turner .off = 0x3f2aaaab,
28*f3087befSAndrew Turner /* Lower bound is the smallest positive normal float 0x00800000. For
29*f3087befSAndrew Turner optimised register use subnormals are detected after offset has been
30*f3087befSAndrew Turner subtracted, so lower bound is 0x0080000 - offset (which wraps around). */
31*f3087befSAndrew Turner .lower = 0x00800000 - 0x3f2aaaab
32*f3087befSAndrew Turner };
33*f3087befSAndrew Turner
34*f3087befSAndrew Turner #define Thresh (0x7f000000) /* asuint32(inf) - 0x00800000. */
35*f3087befSAndrew Turner #define Mask (0x007fffff)
36*f3087befSAndrew Turner
37*f3087befSAndrew Turner static svfloat32_t NOINLINE
special_case(svuint32_t u_off,svfloat32_t p,svfloat32_t r2,svfloat32_t y,svbool_t cmp)38*f3087befSAndrew Turner special_case (svuint32_t u_off, svfloat32_t p, svfloat32_t r2, svfloat32_t y,
39*f3087befSAndrew Turner svbool_t cmp)
40*f3087befSAndrew Turner {
41*f3087befSAndrew Turner return sv_call_f32 (
42*f3087befSAndrew Turner logf, svreinterpret_f32 (svadd_x (svptrue_b32 (), u_off, data.off)),
43*f3087befSAndrew Turner svmla_x (svptrue_b32 (), p, r2, y), cmp);
44*f3087befSAndrew Turner }
45*f3087befSAndrew Turner
46*f3087befSAndrew Turner /* Optimised implementation of SVE logf, using the same algorithm and
47*f3087befSAndrew Turner polynomial as the AdvSIMD routine. Maximum error is 3.34 ULPs:
48*f3087befSAndrew Turner SV_NAME_F1 (log)(0x1.557298p+0) got 0x1.26edecp-2
49*f3087befSAndrew Turner want 0x1.26ede6p-2. */
SV_NAME_F1(log)50*f3087befSAndrew Turner svfloat32_t SV_NAME_F1 (log) (svfloat32_t x, const svbool_t pg)
51*f3087befSAndrew Turner {
52*f3087befSAndrew Turner const struct data *d = ptr_barrier (&data);
53*f3087befSAndrew Turner
54*f3087befSAndrew Turner svuint32_t u_off = svreinterpret_u32 (x);
55*f3087befSAndrew Turner
56*f3087befSAndrew Turner u_off = svsub_x (pg, u_off, d->off);
57*f3087befSAndrew Turner svbool_t cmp = svcmpge (pg, svsub_x (pg, u_off, d->lower), Thresh);
58*f3087befSAndrew Turner
59*f3087befSAndrew Turner /* x = 2^n * (1+r), where 2/3 < 1+r < 4/3. */
60*f3087befSAndrew Turner svfloat32_t n = svcvt_f32_x (
61*f3087befSAndrew Turner pg, svasr_x (pg, svreinterpret_s32 (u_off), 23)); /* Sign-extend. */
62*f3087befSAndrew Turner
63*f3087befSAndrew Turner svuint32_t u = svand_x (pg, u_off, Mask);
64*f3087befSAndrew Turner u = svadd_x (pg, u, d->off);
65*f3087befSAndrew Turner svfloat32_t r = svsub_x (pg, svreinterpret_f32 (u), 1.0f);
66*f3087befSAndrew Turner
67*f3087befSAndrew Turner /* y = log(1+r) + n*ln2. */
68*f3087befSAndrew Turner svfloat32_t r2 = svmul_x (svptrue_b32 (), r, r);
69*f3087befSAndrew Turner /* n*ln2 + r + r2*(P6 + r*P5 + r2*(P4 + r*P3 + r2*(P2 + r*P1 + r2*P0))). */
70*f3087befSAndrew Turner svfloat32_t p_0135 = svld1rq (svptrue_b32 (), &d->poly_0135[0]);
71*f3087befSAndrew Turner svfloat32_t p = svmla_lane (sv_f32 (d->poly_246[0]), r, p_0135, 1);
72*f3087befSAndrew Turner svfloat32_t q = svmla_lane (sv_f32 (d->poly_246[1]), r, p_0135, 2);
73*f3087befSAndrew Turner svfloat32_t y = svmla_lane (sv_f32 (d->poly_246[2]), r, p_0135, 3);
74*f3087befSAndrew Turner p = svmla_lane (p, r2, p_0135, 0);
75*f3087befSAndrew Turner
76*f3087befSAndrew Turner q = svmla_x (pg, q, r2, p);
77*f3087befSAndrew Turner y = svmla_x (pg, y, r2, q);
78*f3087befSAndrew Turner p = svmla_x (pg, r, n, d->ln2);
79*f3087befSAndrew Turner
80*f3087befSAndrew Turner if (unlikely (svptest_any (pg, cmp)))
81*f3087befSAndrew Turner return special_case (u_off, p, r2, y, cmp);
82*f3087befSAndrew Turner return svmla_x (pg, p, r2, y);
83*f3087befSAndrew Turner }
84*f3087befSAndrew Turner
85*f3087befSAndrew Turner TEST_SIG (SV, F, 1, log, 0.01, 11.1)
86*f3087befSAndrew Turner TEST_ULP (SV_NAME_F1 (log), 2.85)
87*f3087befSAndrew Turner TEST_DISABLE_FENV (SV_NAME_F1 (log))
88*f3087befSAndrew Turner TEST_INTERVAL (SV_NAME_F1 (log), -0.0, -inf, 100)
89*f3087befSAndrew Turner TEST_INTERVAL (SV_NAME_F1 (log), 0, 0x1p-126, 100)
90*f3087befSAndrew Turner TEST_INTERVAL (SV_NAME_F1 (log), 0x1p-126, 0x1p-23, 50000)
91*f3087befSAndrew Turner TEST_INTERVAL (SV_NAME_F1 (log), 0x1p-23, 1.0, 50000)
92*f3087befSAndrew Turner TEST_INTERVAL (SV_NAME_F1 (log), 1.0, 100, 50000)
93*f3087befSAndrew Turner TEST_INTERVAL (SV_NAME_F1 (log), 100, inf, 50000)
94*f3087befSAndrew Turner CLOSE_SVE_ATTR
95