xref: /freebsd/contrib/arm-optimized-routines/pl/math/sv_log_2u5.c (revision 5ca8e32633c4ffbbcd6762e5888b6a4ba0708c6c)
1 /*
2  * Double-precision SVE log(x) function.
3  *
4  * Copyright (c) 2020-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 #define P(i) sv_f64 (__v_log_data.poly[i])
13 #define N (1 << V_LOG_TABLE_BITS)
14 #define Off (0x3fe6900900000000)
15 #define MaxTop (0x7ff)
16 #define MinTop (0x001)
17 #define ThreshTop (0x7fe) /* MaxTop - MinTop.  */
18 
19 static svfloat64_t NOINLINE
20 special_case (svfloat64_t x, svfloat64_t y, svbool_t cmp)
21 {
22   return sv_call_f64 (log, x, y, cmp);
23 }
24 
25 /* SVE port of AdvSIMD log algorithm.
26    Maximum measured error is 2.17 ulp:
27    SV_NAME_D1 (log)(0x1.a6129884398a3p+0) got 0x1.ffffff1cca043p-2
28 					 want 0x1.ffffff1cca045p-2.  */
29 svfloat64_t SV_NAME_D1 (log) (svfloat64_t x, const svbool_t pg)
30 {
31   svuint64_t ix = svreinterpret_u64 (x);
32   svuint64_t top = svlsr_x (pg, ix, 52);
33   svbool_t cmp = svcmpge (pg, svsub_x (pg, top, MinTop), sv_u64 (ThreshTop));
34 
35   /* x = 2^k z; where z is in range [Off,2*Off) and exact.
36      The range is split into N subintervals.
37      The ith subinterval contains z and c is near its center.  */
38   svuint64_t tmp = svsub_x (pg, ix, Off);
39   /* Calculate table index = (tmp >> (52 - V_LOG_TABLE_BITS)) % N.
40      The actual value of i is double this due to table layout.  */
41   svuint64_t i
42       = svand_x (pg, svlsr_x (pg, tmp, (51 - V_LOG_TABLE_BITS)), (N - 1) << 1);
43   svint64_t k
44       = svasr_x (pg, svreinterpret_s64 (tmp), 52); /* Arithmetic shift.  */
45   svuint64_t iz = svsub_x (pg, ix, svand_x (pg, tmp, 0xfffULL << 52));
46   svfloat64_t z = svreinterpret_f64 (iz);
47   /* Lookup in 2 global lists (length N).  */
48   svfloat64_t invc = svld1_gather_index (pg, &__v_log_data.table[0].invc, i);
49   svfloat64_t logc = svld1_gather_index (pg, &__v_log_data.table[0].logc, i);
50 
51   /* log(x) = log1p(z/c-1) + log(c) + k*Ln2.  */
52   svfloat64_t r = svmad_x (pg, invc, z, -1);
53   svfloat64_t kd = svcvt_f64_x (pg, k);
54   /* hi = r + log(c) + k*Ln2.  */
55   svfloat64_t hi = svmla_x (pg, svadd_x (pg, logc, r), kd, __v_log_data.ln2);
56   /* y = r2*(A0 + r*A1 + r2*(A2 + r*A3 + r2*A4)) + hi.  */
57   svfloat64_t r2 = svmul_x (pg, r, r);
58   svfloat64_t y = svmla_x (pg, P (2), r, P (3));
59   svfloat64_t p = svmla_x (pg, P (0), r, P (1));
60   y = svmla_x (pg, y, r2, P (4));
61   y = svmla_x (pg, p, r2, y);
62 
63   if (unlikely (svptest_any (pg, cmp)))
64     return special_case (x, svmla_x (svnot_z (pg, cmp), hi, r2, y), cmp);
65   return svmla_x (pg, hi, r2, y);
66 }
67 
68 PL_SIG (SV, D, 1, log, 0.01, 11.1)
69 PL_TEST_ULP (SV_NAME_D1 (log), 1.68)
70 PL_TEST_INTERVAL (SV_NAME_D1 (log), -0.0, -inf, 1000)
71 PL_TEST_INTERVAL (SV_NAME_D1 (log), 0, 0x1p-149, 1000)
72 PL_TEST_INTERVAL (SV_NAME_D1 (log), 0x1p-149, 0x1p-126, 4000)
73 PL_TEST_INTERVAL (SV_NAME_D1 (log), 0x1p-126, 0x1p-23, 50000)
74 PL_TEST_INTERVAL (SV_NAME_D1 (log), 0x1p-23, 1.0, 50000)
75 PL_TEST_INTERVAL (SV_NAME_D1 (log), 1.0, 100, 50000)
76 PL_TEST_INTERVAL (SV_NAME_D1 (log), 100, inf, 50000)
77