1/* Double-precision vector (SVE) log10 function
2
3 Copyright (C) 2023-2024 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
5
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
10
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <https://www.gnu.org/licenses/>. */
19
20#include "sv_math.h"
21#include "poly_sve_f64.h"
22
23#define Min 0x0010000000000000
24#define Max 0x7ff0000000000000
25#define Thres 0x7fe0000000000000 /* Max - Min. */
26#define N (1 << V_LOG10_TABLE_BITS)
27
28static const struct data
29{
30 double c0, c2;
31 double c1, c3;
32 double invln10, log10_2;
33 double c4;
34 uint64_t off;
35} data = {
36 .c0 = -0x1.bcb7b1526e506p-3,
37 .c1 = 0x1.287a7636be1d1p-3,
38 .c2 = -0x1.bcb7b158af938p-4,
39 .c3 = 0x1.63c78734e6d07p-4,
40 .c4 = -0x1.287461742fee4p-4,
41 .invln10 = 0x1.bcb7b1526e50ep-2,
42 .log10_2 = 0x1.34413509f79ffp-2,
43 .off = 0x3fe6900900000000,
44};
45
46static svfloat64_t NOINLINE
47special_case (svfloat64_t hi, svuint64_t tmp, svfloat64_t y, svfloat64_t r2,
48 svbool_t special, const struct data *d)
49{
50 svfloat64_t x = svreinterpret_f64 (svadd_x (svptrue_b64 (), tmp, d->off));
51 return sv_call_f64 (f: log10, x, y: svmla_x (svptrue_b64 (), hi, r2, y), cmp: special);
52}
53
54/* Double-precision SVE log10 routine.
55 Maximum measured error is 2.46 ulps.
56 SV_NAME_D1 (log10)(0x1.131956cd4b627p+0) got 0x1.fffbdf6eaa669p-6
57 want 0x1.fffbdf6eaa667p-6. */
58svfloat64_t SV_NAME_D1 (log10) (svfloat64_t x, const svbool_t pg)
59{
60 const struct data *d = ptr_barrier (&data);
61
62 svuint64_t ix = svreinterpret_u64 (x);
63 svbool_t special = svcmpge (pg, svsub_x (pg, ix, Min), Thres);
64
65 /* x = 2^k z; where z is in range [Off,2*Off) and exact.
66 The range is split into N subintervals.
67 The ith subinterval contains z and c is near its center. */
68 svuint64_t tmp = svsub_x (pg, ix, d->off);
69 svuint64_t i = svlsr_x (pg, tmp, 51 - V_LOG10_TABLE_BITS);
70 i = svand_x (pg, i, (N - 1) << 1);
71 svfloat64_t k = svcvt_f64_x (pg, svasr_x (pg, svreinterpret_s64 (tmp), 52));
72 svfloat64_t z = svreinterpret_f64 (
73 svsub_x (pg, ix, svand_x (pg, tmp, 0xfffULL << 52)));
74
75 /* log(x) = k*log(2) + log(c) + log(z/c). */
76 svfloat64_t invc = svld1_gather_index (pg, &__v_log10_data.table[0].invc, i);
77 svfloat64_t logc
78 = svld1_gather_index (pg, &__v_log10_data.table[0].log10c, i);
79
80 /* We approximate log(z/c) with a polynomial P(x) ~= log(x + 1):
81 r = z/c - 1 (we look up precomputed 1/c)
82 log(z/c) ~= P(r). */
83 svfloat64_t r = svmad_x (pg, invc, z, -1.0);
84
85 /* hi = log(c) + k*log(2). */
86 svfloat64_t invln10_log10_2 = svld1rq_f64 (svptrue_b64 (), &d->invln10);
87 svfloat64_t w = svmla_lane_f64 (logc, r, invln10_log10_2, 0);
88 svfloat64_t hi = svmla_lane_f64 (w, k, invln10_log10_2, 1);
89
90 /* y = r2*(A0 + r*A1 + r2*(A2 + r*A3 + r2*A4)) + hi. */
91 svfloat64_t odd_coeffs = svld1rq_f64 (svptrue_b64 (), &d->c1);
92 svfloat64_t r2 = svmul_x (svptrue_b64 (), r, r);
93 svfloat64_t y = svmla_lane_f64 (sv_f64 (x: d->c2), r, odd_coeffs, 1);
94 svfloat64_t p = svmla_lane_f64 (sv_f64 (x: d->c0), r, odd_coeffs, 0);
95 y = svmla_x (pg, y, r2, d->c4);
96 y = svmla_x (pg, p, r2, y);
97
98 if (__glibc_unlikely (svptest_any (pg, special)))
99 return special_case (hi, tmp, y, r2, special, d);
100 return svmla_x (pg, hi, r2, y);
101}
102

source code of glibc/sysdeps/aarch64/fpu/log10_sve.c