xref: /llvm-project/libc/AOR_v20.02/math/v_log.c (revision 0928368f623a0f885894f9c3ef1b740b060c0d9c)
1*0928368fSKristof Beyls /*
2*0928368fSKristof Beyls  * Double-precision vector log(x) function.
3*0928368fSKristof Beyls  *
4*0928368fSKristof Beyls  * Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5*0928368fSKristof Beyls  * See https://llvm.org/LICENSE.txt for license information.
6*0928368fSKristof Beyls  * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7*0928368fSKristof Beyls  */
8*0928368fSKristof Beyls 
9*0928368fSKristof Beyls #include "mathlib.h"
10*0928368fSKristof Beyls #include "v_math.h"
11*0928368fSKristof Beyls #include "v_log.h"
12*0928368fSKristof Beyls #if V_SUPPORTED
13*0928368fSKristof Beyls 
14*0928368fSKristof Beyls /* Worst-case error: 1.17 + 0.5 ulp.  */
15*0928368fSKristof Beyls 
16*0928368fSKristof Beyls static const f64_t Poly[] = {
17*0928368fSKristof Beyls   /* rel error: 0x1.6272e588p-56 in [ -0x1.fc1p-9 0x1.009p-8 ].  */
18*0928368fSKristof Beyls   -0x1.ffffffffffff7p-2,
19*0928368fSKristof Beyls    0x1.55555555170d4p-2,
20*0928368fSKristof Beyls   -0x1.0000000399c27p-2,
21*0928368fSKristof Beyls    0x1.999b2e90e94cap-3,
22*0928368fSKristof Beyls   -0x1.554e550bd501ep-3,
23*0928368fSKristof Beyls };
24*0928368fSKristof Beyls 
25*0928368fSKristof Beyls #define A0 v_f64 (Poly[0])
26*0928368fSKristof Beyls #define A1 v_f64 (Poly[1])
27*0928368fSKristof Beyls #define A2 v_f64 (Poly[2])
28*0928368fSKristof Beyls #define A3 v_f64 (Poly[3])
29*0928368fSKristof Beyls #define A4 v_f64 (Poly[4])
30*0928368fSKristof Beyls #define Ln2 v_f64 (0x1.62e42fefa39efp-1)
31*0928368fSKristof Beyls #define N (1 << V_LOG_TABLE_BITS)
32*0928368fSKristof Beyls #define OFF v_u64 (0x3fe6900900000000)
33*0928368fSKristof Beyls 
34*0928368fSKristof Beyls struct entry
35*0928368fSKristof Beyls {
36*0928368fSKristof Beyls   v_f64_t invc;
37*0928368fSKristof Beyls   v_f64_t logc;
38*0928368fSKristof Beyls };
39*0928368fSKristof Beyls 
40*0928368fSKristof Beyls static inline struct entry
lookup(v_u64_t i)41*0928368fSKristof Beyls lookup (v_u64_t i)
42*0928368fSKristof Beyls {
43*0928368fSKristof Beyls   struct entry e;
44*0928368fSKristof Beyls #ifdef SCALAR
45*0928368fSKristof Beyls   e.invc = __v_log_data[i].invc;
46*0928368fSKristof Beyls   e.logc = __v_log_data[i].logc;
47*0928368fSKristof Beyls #else
48*0928368fSKristof Beyls   e.invc[0] = __v_log_data[i[0]].invc;
49*0928368fSKristof Beyls   e.logc[0] = __v_log_data[i[0]].logc;
50*0928368fSKristof Beyls   e.invc[1] = __v_log_data[i[1]].invc;
51*0928368fSKristof Beyls   e.logc[1] = __v_log_data[i[1]].logc;
52*0928368fSKristof Beyls #endif
53*0928368fSKristof Beyls   return e;
54*0928368fSKristof Beyls }
55*0928368fSKristof Beyls 
56*0928368fSKristof Beyls VPCS_ATTR
57*0928368fSKristof Beyls __attribute__ ((noinline)) static v_f64_t
specialcase(v_f64_t x,v_f64_t y,v_u64_t cmp)58*0928368fSKristof Beyls specialcase (v_f64_t x, v_f64_t y, v_u64_t cmp)
59*0928368fSKristof Beyls {
60*0928368fSKristof Beyls   return v_call_f64 (log, x, y, cmp);
61*0928368fSKristof Beyls }
62*0928368fSKristof Beyls 
63*0928368fSKristof Beyls VPCS_ATTR
64*0928368fSKristof Beyls v_f64_t
V_NAME(log)65*0928368fSKristof Beyls V_NAME(log) (v_f64_t x)
66*0928368fSKristof Beyls {
67*0928368fSKristof Beyls   v_f64_t z, r, r2, p, y, kd, hi;
68*0928368fSKristof Beyls   v_u64_t ix, iz, tmp, top, i, cmp;
69*0928368fSKristof Beyls   v_s64_t k;
70*0928368fSKristof Beyls   struct entry e;
71*0928368fSKristof Beyls 
72*0928368fSKristof Beyls   ix = v_as_u64_f64 (x);
73*0928368fSKristof Beyls   top = ix >> 48;
74*0928368fSKristof Beyls   cmp = v_cond_u64 (top - v_u64 (0x0010) >= v_u64 (0x7ff0 - 0x0010));
75*0928368fSKristof Beyls 
76*0928368fSKristof Beyls   /* x = 2^k z; where z is in range [OFF,2*OFF) and exact.
77*0928368fSKristof Beyls      The range is split into N subintervals.
78*0928368fSKristof Beyls      The ith subinterval contains z and c is near its center.  */
79*0928368fSKristof Beyls   tmp = ix - OFF;
80*0928368fSKristof Beyls   i = (tmp >> (52 - V_LOG_TABLE_BITS)) % N;
81*0928368fSKristof Beyls   k = v_as_s64_u64 (tmp) >> 52; /* arithmetic shift */
82*0928368fSKristof Beyls   iz = ix - (tmp & v_u64 (0xfffULL << 52));
83*0928368fSKristof Beyls   z = v_as_f64_u64 (iz);
84*0928368fSKristof Beyls   e = lookup (i);
85*0928368fSKristof Beyls 
86*0928368fSKristof Beyls   /* log(x) = log1p(z/c-1) + log(c) + k*Ln2.  */
87*0928368fSKristof Beyls   r = v_fma_f64 (z, e.invc, v_f64 (-1.0));
88*0928368fSKristof Beyls   kd = v_to_f64_s64 (k);
89*0928368fSKristof Beyls 
90*0928368fSKristof Beyls   /* hi = r + log(c) + k*Ln2.  */
91*0928368fSKristof Beyls   hi = v_fma_f64 (kd, Ln2, e.logc + r);
92*0928368fSKristof Beyls   /* y = r2*(A0 + r*A1 + r2*(A2 + r*A3 + r2*A4)) + hi.  */
93*0928368fSKristof Beyls   r2 = r * r;
94*0928368fSKristof Beyls   y = v_fma_f64 (A3, r, A2);
95*0928368fSKristof Beyls   p = v_fma_f64 (A1, r, A0);
96*0928368fSKristof Beyls   y = v_fma_f64 (A4, r2, y);
97*0928368fSKristof Beyls   y = v_fma_f64 (y, r2, p);
98*0928368fSKristof Beyls   y = v_fma_f64 (y, r2, hi);
99*0928368fSKristof Beyls 
100*0928368fSKristof Beyls   if (unlikely (v_any_u64 (cmp)))
101*0928368fSKristof Beyls     return specialcase (x, y, cmp);
102*0928368fSKristof Beyls   return y;
103*0928368fSKristof Beyls }
104*0928368fSKristof Beyls VPCS_ALIAS
105*0928368fSKristof Beyls #endif
106