1 | /* Double-precision vector (SVE) log2 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 N (1 << V_LOG2_TABLE_BITS) |
24 | #define Off 0x3fe6900900000000 |
25 | #define Max (0x7ff0000000000000) |
26 | #define Min (0x0010000000000000) |
27 | #define Thresh (0x7fe0000000000000) /* Max - Min. */ |
28 | |
29 | static svfloat64_t NOINLINE |
30 | special_case (svfloat64_t x, svfloat64_t y, svbool_t cmp) |
31 | { |
32 | return sv_call_f64 (f: log2, x, y, cmp); |
33 | } |
34 | |
35 | /* Double-precision SVE log2 routine. |
36 | Implements the same algorithm as AdvSIMD log10, with coefficients and table |
37 | entries scaled in extended precision. |
38 | The maximum observed error is 2.58 ULP: |
39 | SV_NAME_D1 (log2)(0x1.0b556b093869bp+0) got 0x1.fffb34198d9dap-5 |
40 | want 0x1.fffb34198d9ddp-5. */ |
41 | svfloat64_t SV_NAME_D1 (log2) (svfloat64_t x, const svbool_t pg) |
42 | { |
43 | svuint64_t ix = svreinterpret_u64 (x); |
44 | svbool_t special = svcmpge (pg, svsub_x (pg, ix, Min), Thresh); |
45 | |
46 | /* x = 2^k z; where z is in range [Off,2*Off) and exact. |
47 | The range is split into N subintervals. |
48 | The ith subinterval contains z and c is near its center. */ |
49 | svuint64_t tmp = svsub_x (pg, ix, Off); |
50 | svuint64_t i = svlsr_x (pg, tmp, 51 - V_LOG2_TABLE_BITS); |
51 | i = svand_x (pg, i, (N - 1) << 1); |
52 | svfloat64_t k = svcvt_f64_x (pg, svasr_x (pg, svreinterpret_s64 (tmp), 52)); |
53 | svfloat64_t z = svreinterpret_f64 ( |
54 | svsub_x (pg, ix, svand_x (pg, tmp, 0xfffULL << 52))); |
55 | |
56 | svfloat64_t invc = svld1_gather_index (pg, &__v_log2_data.table[0].invc, i); |
57 | svfloat64_t log2c |
58 | = svld1_gather_index (pg, &__v_log2_data.table[0].log2c, i); |
59 | |
60 | /* log2(x) = log1p(z/c-1)/log(2) + log2(c) + k. */ |
61 | |
62 | svfloat64_t r = svmad_x (pg, invc, z, -1.0); |
63 | svfloat64_t w = svmla_x (pg, log2c, r, __v_log2_data.invln2); |
64 | |
65 | svfloat64_t r2 = svmul_x (pg, r, r); |
66 | svfloat64_t y = sv_pw_horner_4_f64_x (pg, x: r, x2: r2, poly: __v_log2_data.poly); |
67 | w = svadd_x (pg, k, w); |
68 | |
69 | if (__glibc_unlikely (svptest_any (pg, special))) |
70 | return special_case (x, y: svmla_x (svnot_z (pg, special), w, r2, y), |
71 | cmp: special); |
72 | return svmla_x (pg, w, r2, y); |
73 | } |
74 | |