1 /*
2 * Double-precision log(x) function.
3 *
4 * Copyright (c) 2018, Arm Limited.
5 * SPDX-License-Identifier: MIT
6 */
7
8 #include <math.h>
9 #include <stdint.h>
10 #include "math_config.h"
11
12 #define T __log_data.tab
13 #define T2 __log_data.tab2
14 #define B __log_data.poly1
15 #define A __log_data.poly
16 #define Ln2hi __log_data.ln2hi
17 #define Ln2lo __log_data.ln2lo
18 #define N (1 << LOG_TABLE_BITS)
19 #define OFF 0x3fe6000000000000
20
21 /* Top 16 bits of a double. */
22 static inline uint32_t
top16(double x)23 top16 (double x)
24 {
25 return asuint64 (x) >> 48;
26 }
27
28 double
log(double x)29 log (double x)
30 {
31 /* double_t for better performance on targets with FLT_EVAL_METHOD==2. */
32 double_t w, z, r, r2, r3, y, invc, logc, kd, hi, lo;
33 uint64_t ix, iz, tmp;
34 uint32_t top;
35 int k, i;
36
37 ix = asuint64 (x);
38 top = top16 (x);
39
40 #if LOG_POLY1_ORDER == 10 || LOG_POLY1_ORDER == 11
41 # define LO asuint64 (1.0 - 0x1p-5)
42 # define HI asuint64 (1.0 + 0x1.1p-5)
43 #elif LOG_POLY1_ORDER == 12
44 # define LO asuint64 (1.0 - 0x1p-4)
45 # define HI asuint64 (1.0 + 0x1.09p-4)
46 #endif
47 if (unlikely (ix - LO < HI - LO))
48 {
49 /* Handle close to 1.0 inputs separately. */
50 /* Fix sign of zero with downward rounding when x==1. */
51 if (WANT_ROUNDING && unlikely (ix == asuint64 (1.0)))
52 return 0;
53 r = x - 1.0;
54 r2 = r * r;
55 r3 = r * r2;
56 #if LOG_POLY1_ORDER == 10
57 /* Worst-case error is around 0.516 ULP. */
58 y = r3 * (B[1] + r * B[2] + r2 * B[3]
59 + r3 * (B[4] + r * B[5] + r2 * B[6] + r3 * (B[7] + r * B[8])));
60 w = B[0] * r2; /* B[0] == -0.5. */
61 hi = r + w;
62 y += r - hi + w;
63 y += hi;
64 #elif LOG_POLY1_ORDER == 11
65 /* Worst-case error is around 0.516 ULP. */
66 y = r3 * (B[1] + r * B[2]
67 + r2 * (B[3] + r * B[4] + r2 * B[5]
68 + r3 * (B[6] + r * B[7] + r2 * B[8] + r3 * B[9])));
69 w = B[0] * r2; /* B[0] == -0.5. */
70 hi = r + w;
71 y += r - hi + w;
72 y += hi;
73 #elif LOG_POLY1_ORDER == 12
74 y = r3 * (B[1] + r * B[2] + r2 * B[3]
75 + r3 * (B[4] + r * B[5] + r2 * B[6]
76 + r3 * (B[7] + r * B[8] + r2 * B[9] + r3 * B[10])));
77 # if N <= 64
78 /* Worst-case error is around 0.532 ULP. */
79 w = B[0] * r2; /* B[0] == -0.5. */
80 hi = r + w;
81 y += r - hi + w;
82 y += hi;
83 # else
84 /* Worst-case error is around 0.507 ULP. */
85 w = r * 0x1p27;
86 double_t rhi = r + w - w;
87 double_t rlo = r - rhi;
88 w = rhi * rhi * B[0]; /* B[0] == -0.5. */
89 hi = r + w;
90 lo = r - hi + w;
91 lo += B[0] * rlo * (rhi + r);
92 y += lo;
93 y += hi;
94 # endif
95 #endif
96 return eval_as_double (y);
97 }
98 if (unlikely (top - 0x0010 >= 0x7ff0 - 0x0010))
99 {
100 /* x < 0x1p-1022 or inf or nan. */
101 if (ix * 2 == 0)
102 return __math_divzero (1);
103 if (ix == asuint64 (INFINITY)) /* log(inf) == inf. */
104 return x;
105 if ((top & 0x8000) || (top & 0x7ff0) == 0x7ff0)
106 return __math_invalid (x);
107 /* x is subnormal, normalize it. */
108 ix = asuint64 (x * 0x1p52);
109 ix -= 52ULL << 52;
110 }
111
112 /* x = 2^k z; where z is in range [OFF,2*OFF) and exact.
113 The range is split into N subintervals.
114 The ith subinterval contains z and c is near its center. */
115 tmp = ix - OFF;
116 i = (tmp >> (52 - LOG_TABLE_BITS)) % N;
117 k = (int64_t) tmp >> 52; /* arithmetic shift */
118 iz = ix - (tmp & 0xfffULL << 52);
119 invc = T[i].invc;
120 logc = T[i].logc;
121 z = asdouble (iz);
122
123 /* log(x) = log1p(z/c-1) + log(c) + k*Ln2. */
124 /* r ~= z/c - 1, |r| < 1/(2*N). */
125 #if HAVE_FAST_FMA
126 /* rounding error: 0x1p-55/N. */
127 r = fma (z, invc, -1.0);
128 #else
129 /* rounding error: 0x1p-55/N + 0x1p-66. */
130 r = (z - T2[i].chi - T2[i].clo) * invc;
131 #endif
132 kd = (double_t) k;
133
134 /* hi + lo = r + log(c) + k*Ln2. */
135 w = kd * Ln2hi + logc;
136 hi = w + r;
137 lo = w - hi + r + kd * Ln2lo;
138
139 /* log(x) = lo + (log1p(r) - r) + hi. */
140 r2 = r * r; /* rounding error: 0x1p-54/N^2. */
141 /* Worst case error if |y| > 0x1p-5:
142 0.5 + 4.13/N + abs-poly-error*2^57 ULP (+ 0.002 ULP without fma)
143 Worst case error if |y| > 0x1p-4:
144 0.5 + 2.06/N + abs-poly-error*2^56 ULP (+ 0.001 ULP without fma). */
145 #if LOG_POLY_ORDER == 6
146 y = lo + r2 * A[0] + r * r2 * (A[1] + r * A[2] + r2 * (A[3] + r * A[4])) + hi;
147 #elif LOG_POLY_ORDER == 7
148 y = lo
149 + r2 * (A[0] + r * A[1] + r2 * (A[2] + r * A[3])
150 + r2 * r2 * (A[4] + r * A[5]))
151 + hi;
152 #endif
153 return eval_as_double (y);
154 }
155 #if USE_GLIBC_ABI
156 strong_alias (log, __log_finite)
157 hidden_alias (log, __ieee754_log)
158 #endif
159