blob: bc4b8f859a4ba23d8ebb076fd37e9ba05e3d4635 [file] [log] [blame]
Szabolcs Nagyd69e5042018-06-05 16:15:27 +01001/*
2 * Double-precision log2(x) function.
3 *
4 * Copyright (c) 2018, Arm Limited.
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19
20#include <math.h>
21#include <stdint.h>
22#include "math_config.h"
23
24#define T __log2_data.tab
25#define T2 __log2_data.tab2
26#define B __log2_data.poly1
27#define A __log2_data.poly
28#define InvLn2hi __log2_data.invln2hi
29#define InvLn2lo __log2_data.invln2lo
30#define N (1 << LOG2_TABLE_BITS)
31#define OFF 0x3fe6000000000000
32
33static inline uint32_t
34top16 (double x)
35{
36 return asuint64 (x) >> 48;
37}
38
39double
40log2 (double x)
41{
42 /* double_t for better performance on targets with FLT_EVAL_METHOD==2. */
43 double_t z, r, r2, r4, y, invc, logc, kd, hi, lo, t1, t2, t3, p;
44 uint64_t ix, iz, tmp;
45 uint32_t top;
46 int k, i;
47
48 ix = asuint64 (x);
49 top = top16 (x);
50
51#if LOG2_POLY1_ORDER == 11
52# define LO asuint64 (1.0 - 0x1.5b51p-5)
53# define HI asuint64 (1.0 + 0x1.6ab2p-5)
54#endif
55 if (unlikely (ix - LO < HI - LO))
56 {
57 /* Handle close to 1.0 inputs separately. */
58 /* Fix sign of zero with downward rounding when x==1. */
59 if (WANT_ROUNDING && unlikely (ix == asuint64 (1.0)))
60 return 0;
61 r = x - 1.0;
62#if HAVE_FAST_FMA
63 hi = r*InvLn2hi;
64 lo = r*InvLn2lo + fma (r, InvLn2hi, -hi);
65#else
66 double_t rhi, rlo;
67 rhi = asdouble (asuint64 (r) & -1ULL<<32);
68 rlo = r - rhi;
69 hi = rhi*InvLn2hi;
70 lo = rlo*InvLn2hi + r*InvLn2lo;
71#endif
72 r2 = r * r; /* rounding error: 0x1p-62. */
73 r4 = r2 * r2;
74#if LOG2_POLY1_ORDER == 11
75 /* Worst-case error is less than 0.54 ULP (0.55 ULP without fma). */
76 p = r2*(B[0] + r*B[1]);
77 y = hi + p;
78 lo += hi - y + p;
79 lo += r4*(B[2] + r*B[3] + r2*(B[4] + r*B[5])
80 + r4*(B[6] + r*B[7] + r2*(B[8] + r*B[9])));
81 y += lo;
82#endif
83 return y;
84 }
85 if (unlikely (top - 0x0010 >= 0x7ff0 - 0x0010))
86 {
87 /* x < 0x1p-1022 or inf or nan. */
88 if (ix * 2 == 0)
89 return __math_divzero (1);
90 if (ix == asuint64 (INFINITY)) /* log(inf) == inf. */
91 return x;
92 if ((top & 0x8000) || (top & 0x7ff0) == 0x7ff0)
93 return __math_invalid (x);
94 /* x is subnormal, normalize it. */
95 ix = asuint64 (x * 0x1p52);
96 ix -= 52ULL << 52;
97 }
98
99 /* x = 2^k z; where z is in range [OFF,2*OFF) and exact.
100 The range is split into N subintervals.
101 The ith subinterval contains z and c is near its center. */
102 tmp = ix - OFF;
103 i = (tmp >> (52 - LOG2_TABLE_BITS)) % N;
104 k = (int64_t) tmp >> 52; /* arithmetic shift */
105 iz = ix - (tmp & 0xfffULL << 52);
106 invc = T[i].invc;
107 logc = T[i].logc;
108 z = asdouble (iz);
109 kd = (double_t) k;
110
111 /* log2(x) = log2(z/c) + log2(c) + k. */
112 /* r ~= z/c - 1, |r| < 1/(2*N). */
113#if HAVE_FAST_FMA
114 /* rounding error: 0x1p-55/N. */
115 r = fma (z, invc, -1.0);
116 t1 = r*InvLn2hi;
117 t2 = r*InvLn2lo + fma (r, InvLn2hi, -t1);
118#else
119 double_t rhi, rlo;
120 /* rounding error: 0x1p-55/N + 0x1p-65. */
121 r = (z - T2[i].chi - T2[i].clo)*invc;
122 rhi = asdouble (asuint64 (r) & -1ULL << 32);
123 rlo = r - rhi;
124 t1 = rhi*InvLn2hi;
125 t2 = rlo*InvLn2hi + r*InvLn2lo;
126#endif
127
128 /* hi + lo = r/ln2 + log2(c) + k. */
129 t3 = kd + logc;
130 hi = t3 + t1;
131 lo = t3 - hi + t1 + t2;
132
133 /* log2(r+1) = r/ln2 + r^2*poly(r). */
134 /* Evaluation is optimized assuming superscalar pipelined execution. */
135 r2 = r * r; /* rounding error: 0x1p-54/N^2. */
136 r4 = r2 * r2;
137#if LOG2_POLY_ORDER == 7
138 /* Worst-case error if |y| > 0x1p-4: 0.547 ULP (0.550 ULP without fma).
139 ~ 0.5 + 2/N/ln2 + abs-poly-error*0x1p56 ULP (+ 0.003 ULP without fma). */
140 p = A[0] + r*A[1] + r2*(A[2] + r*A[3]) + r4*(A[4] + r*A[5]);
141 y = lo + r2*p + hi;
142#endif
143 return y;
144}