1 /* Single-precision vector (Advanced SIMD) log function.
2
3 Copyright (C) 2023 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 "v_math.h"
21
22 static const struct data
23 {
24 float32x4_t poly[7];
25 float32x4_t ln2, tiny_bound;
26 uint32x4_t min_norm, special_bound, off, mantissa_mask;
27 } data = {
28 /* 3.34 ulp error. */
29 .poly = { V4 (-0x1.3e737cp-3f), V4 (0x1.5a9aa2p-3f), V4 (-0x1.4f9934p-3f),
30 V4 (0x1.961348p-3f), V4 (-0x1.00187cp-2f), V4 (0x1.555d7cp-2f),
31 V4 (-0x1.ffffc8p-2f) },
32 .ln2 = V4 (0x1.62e43p-1f),
33 .tiny_bound = V4 (0x1p-126),
34 .min_norm = V4 (0x00800000),
35 .special_bound = V4 (0x7f000000), /* asuint32(inf) - min_norm. */
36 .off = V4 (0x3f2aaaab), /* 0.666667. */
37 .mantissa_mask = V4 (0x007fffff)
38 };
39
40 #define P(i) d->poly[7 - i]
41
42 static float32x4_t VPCS_ATTR NOINLINE
43 special_case (float32x4_t x, float32x4_t y, uint32x4_t cmp)
44 {
45 /* Fall back to scalar code. */
46 return v_call_f32 (logf, x, y, cmp);
47 }
48
49 float32x4_t VPCS_ATTR V_NAME_F1 (log) (float32x4_t x)
50 {
51 const struct data *d = ptr_barrier (&data);
52 float32x4_t n, p, q, r, r2, y;
53 uint32x4_t u, cmp;
54
55 u = vreinterpretq_u32_f32 (x);
56 cmp = vcgeq_u32 (vsubq_u32 (u, d->min_norm), d->special_bound);
57
58 /* x = 2^n * (1+r), where 2/3 < 1+r < 4/3. */
59 u = vsubq_u32 (u, d->off);
60 n = vcvtq_f32_s32 (
61 vshrq_n_s32 (vreinterpretq_s32_u32 (u), 23)); /* signextend. */
62 u = vandq_u32 (u, d->mantissa_mask);
63 u = vaddq_u32 (u, d->off);
64 r = vsubq_f32 (vreinterpretq_f32_u32 (u), v_f32 (1.0f));
65
66 /* y = log(1+r) + n*ln2. */
67 r2 = vmulq_f32 (r, r);
68 /* n*ln2 + r + r2*(P1 + r*P2 + r2*(P3 + r*P4 + r2*(P5 + r*P6 + r2*P7))). */
69 p = vfmaq_f32 (P (5), P (6), r);
70 q = vfmaq_f32 (P (3), P (4), r);
71 y = vfmaq_f32 (P (1), P (2), r);
72 p = vfmaq_f32 (p, P (7), r2);
73 q = vfmaq_f32 (q, p, r2);
74 y = vfmaq_f32 (y, q, r2);
75 p = vfmaq_f32 (r, d->ln2, n);
76 y = vfmaq_f32 (p, y, r2);
77
78 if (__glibc_unlikely (v_any_u32 (cmp)))
79 return special_case (x, y, cmp);
80 return y;
81 }