xref: /freebsd/contrib/arm-optimized-routines/pl/math/sv_logf_3u4.c (revision 5a02ffc32e777041dd2dad4e651ed2a0865a0a5d)
1 /*
2  * Single-precision vector log function.
3  *
4  * Copyright (c) 2019-2023, Arm Limited.
5  * SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception
6  */
7 
8 #include "sv_math.h"
9 #include "pl_sig.h"
10 #include "pl_test.h"
11 
12 static const struct data
13 {
14   float poly_0135[4];
15   float poly_246[3];
16   float ln2;
17 } data = {
18   .poly_0135 = {
19     /* Coefficients copied from the AdvSIMD routine in math/, then rearranged so
20        that coeffs 0, 1, 3 and 5 can be loaded as a single quad-word, hence used
21        with _lane variant of MLA intrinsic.  */
22     -0x1.3e737cp-3f, 0x1.5a9aa2p-3f, 0x1.961348p-3f, 0x1.555d7cp-2f
23   },
24   .poly_246 = { -0x1.4f9934p-3f, -0x1.00187cp-2f, -0x1.ffffc8p-2f },
25   .ln2 = 0x1.62e43p-1f
26 };
27 
28 #define Min (0x00800000)
29 #define Max (0x7f800000)
30 #define Thresh (0x7f000000) /* Max - Min.  */
31 #define Mask (0x007fffff)
32 #define Off (0x3f2aaaab) /* 0.666667.  */
33 
34 float optr_aor_log_f32 (float);
35 
36 static svfloat32_t NOINLINE
special_case(svfloat32_t x,svfloat32_t y,svbool_t cmp)37 special_case (svfloat32_t x, svfloat32_t y, svbool_t cmp)
38 {
39   return sv_call_f32 (optr_aor_log_f32, x, y, cmp);
40 }
41 
42 /* Optimised implementation of SVE logf, using the same algorithm and
43    polynomial as the AdvSIMD routine. Maximum error is 3.34 ULPs:
44    SV_NAME_F1 (log)(0x1.557298p+0) got 0x1.26edecp-2
45 				  want 0x1.26ede6p-2.  */
SV_NAME_F1(log)46 svfloat32_t SV_NAME_F1 (log) (svfloat32_t x, const svbool_t pg)
47 {
48   const struct data *d = ptr_barrier (&data);
49 
50   svuint32_t u = svreinterpret_u32 (x);
51   svbool_t cmp = svcmpge (pg, svsub_x (pg, u, Min), Thresh);
52 
53   /* x = 2^n * (1+r), where 2/3 < 1+r < 4/3.  */
54   u = svsub_x (pg, u, Off);
55   svfloat32_t n = svcvt_f32_x (
56       pg, svasr_x (pg, svreinterpret_s32 (u), 23)); /* Sign-extend.  */
57   u = svand_x (pg, u, Mask);
58   u = svadd_x (pg, u, Off);
59   svfloat32_t r = svsub_x (pg, svreinterpret_f32 (u), 1.0f);
60 
61   /* y = log(1+r) + n*ln2.  */
62   svfloat32_t r2 = svmul_x (pg, r, r);
63   /* n*ln2 + r + r2*(P6 + r*P5 + r2*(P4 + r*P3 + r2*(P2 + r*P1 + r2*P0))).  */
64   svfloat32_t p_0135 = svld1rq (svptrue_b32 (), &d->poly_0135[0]);
65   svfloat32_t p = svmla_lane (sv_f32 (d->poly_246[0]), r, p_0135, 1);
66   svfloat32_t q = svmla_lane (sv_f32 (d->poly_246[1]), r, p_0135, 2);
67   svfloat32_t y = svmla_lane (sv_f32 (d->poly_246[2]), r, p_0135, 3);
68   p = svmla_lane (p, r2, p_0135, 0);
69 
70   q = svmla_x (pg, q, r2, p);
71   y = svmla_x (pg, y, r2, q);
72   p = svmla_x (pg, r, n, d->ln2);
73 
74   if (unlikely (svptest_any (pg, cmp)))
75     return special_case (x, svmla_x (svnot_z (pg, cmp), p, r2, y), cmp);
76   return svmla_x (pg, p, r2, y);
77 }
78 
79 PL_SIG (SV, F, 1, log, 0.01, 11.1)
80 PL_TEST_ULP (SV_NAME_F1 (log), 2.85)
81 PL_TEST_INTERVAL (SV_NAME_F1 (log), -0.0, -inf, 100)
82 PL_TEST_INTERVAL (SV_NAME_F1 (log), 0, 0x1p-126, 100)
83 PL_TEST_INTERVAL (SV_NAME_F1 (log), 0x1p-126, 0x1p-23, 50000)
84 PL_TEST_INTERVAL (SV_NAME_F1 (log), 0x1p-23, 1.0, 50000)
85 PL_TEST_INTERVAL (SV_NAME_F1 (log), 1.0, 100, 50000)
86 PL_TEST_INTERVAL (SV_NAME_F1 (log), 100, inf, 50000)
87