1//===-- Single-precision x^y function -------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "src/math/powf.h"
10#include "common_constants.h" // Lookup tables EXP_M1 and EXP_M2.
11#include "src/__support/CPP/bit.h"
12#include "src/__support/CPP/optional.h"
13#include "src/__support/FPUtil/FPBits.h"
14#include "src/__support/FPUtil/PolyEval.h"
15#include "src/__support/FPUtil/double_double.h"
16#include "src/__support/FPUtil/except_value_utils.h"
17#include "src/__support/FPUtil/multiply_add.h"
18#include "src/__support/FPUtil/nearest_integer.h"
19#include "src/__support/FPUtil/rounding_mode.h"
20#include "src/__support/FPUtil/sqrt.h" // Speedup for powf(x, 1/2) = sqrtf(x)
21#include "src/__support/common.h"
22#include "src/__support/macros/optimization.h" // LIBC_UNLIKELY
23
24#include "exp10f_impl.h" // Speedup for powf(10, y) = exp10f(y)
25#include "exp2f_impl.h" // Speedup for powf(2, y) = exp2f(y)
26
27#include <errno.h>
28
29namespace LIBC_NAMESPACE {
30
31using fputil::DoubleDouble;
32using fputil::TripleDouble;
33
34namespace {
35
36#ifdef LIBC_TARGET_CPU_HAS_FMA
37constexpr uint64_t ERR = 64;
38#else
39constexpr uint64_t ERR = 128;
40#endif // LIBC_TARGET_CPU_HAS_FMA
41
42// We choose the precision of the high part to be 53 - 24 - 8, so that when
43// y * (e_x + LOG2_R_DD[i].hi) is exact.
44// Generated by Sollya with:
45// > for i from 0 to 127 do {
46// r = 2^-8 * ceil(2^8 * (1 - 2^-8) / (1 + i * 2^-7) );
47// a = -log2(r);
48// b = round(1 + a, 53 - 24 - 8, RN) - 1;
49// c = round(a - b, D, RN);
50// d = round(a - b - c, D, RN);
51// print("{", d, ",", c, ", ", b, "},");
52// };
53static constexpr TripleDouble LOG2_R_TD[128] = {
54 {.lo: 0.0, .mid: 0.0, .hi: 0.0},
55 {.lo: 0x1.84a2c615b70adp-79, .mid: -0x1.177c23362928cp-25, .hi: 0x1.72c8p-7},
56 {.lo: -0x1.f27b820fd03eap-76, .mid: -0x1.179e0caa9c9abp-22, .hi: 0x1.744p-6},
57 {.lo: -0x1.f27ef487c8f34p-77, .mid: -0x1.c6cea541f5b7p-23, .hi: 0x1.184cp-5},
58 {.lo: -0x1.e3f80fbc71454p-76, .mid: -0x1.66c4d4e554434p-22, .hi: 0x1.773ap-5},
59 {.lo: -0x1.9f8ef14d5f6eep-79, .mid: -0x1.70700a00fdd55p-24, .hi: 0x1.d6ecp-5},
60 {.lo: 0x1.452bbce7398c1p-77, .mid: 0x1.53002a4e86631p-23, .hi: 0x1.1bb3p-4},
61 {.lo: -0x1.990555535afdp-81, .mid: 0x1.fcd15f101c142p-25, .hi: 0x1.4c56p-4},
62 {.lo: 0x1.447e30ad393eep-78, .mid: 0x1.25b3eed319cedp-22, .hi: 0x1.7d6p-4},
63 {.lo: 0x1.b7759da88a2dap-76, .mid: -0x1.4195120d8486fp-22, .hi: 0x1.960dp-4},
64 {.lo: 0x1.cee7766ece702p-78, .mid: 0x1.45b878e27d0d9p-23, .hi: 0x1.c7b5p-4},
65 {.lo: -0x1.a55c745ecdc2fp-77, .mid: 0x1.770744593a4cbp-22, .hi: 0x1.f9c9p-4},
66 {.lo: 0x1.f7ec992caa67fp-77, .mid: 0x1.c673032495d24p-22, .hi: 0x1.097ep-3},
67 {.lo: -0x1.433638c6ece3ep-77, .mid: -0x1.1eaa65b49696ep-22, .hi: 0x1.22dbp-3},
68 {.lo: 0x1.58f27b6518824p-76, .mid: 0x1.b2866f2850b22p-22, .hi: 0x1.3c6f8p-3},
69 {.lo: -0x1.86bdcfdfd4a4cp-79, .mid: 0x1.8ee37cd2ea9d3p-25, .hi: 0x1.494f8p-3},
70 {.lo: -0x1.ff7044a68a7fap-80, .mid: 0x1.7e86f9c2154fbp-24, .hi: 0x1.633a8p-3},
71 {.lo: -0x1.aa21694561327p-81, .mid: 0x1.8e3cfc25f0ce6p-26, .hi: 0x1.7046p-3},
72 {.lo: -0x1.d209f2d4239c6p-87, .mid: 0x1.57f7a64ccd537p-28, .hi: 0x1.8a898p-3},
73 {.lo: -0x1.a55e97e60e632p-76, .mid: -0x1.a761c09fbd2aep-22, .hi: 0x1.97c2p-3},
74 {.lo: 0x1.261179225541ep-76, .mid: 0x1.24bea9a2c66f3p-22, .hi: 0x1.b26p-3},
75 {.lo: -0x1.08fa30510fca9p-82, .mid: -0x1.60002ccfe43f5p-25, .hi: 0x1.bfc68p-3},
76 {.lo: -0x1.63ec8d56242f9p-76, .mid: 0x1.69f220e97f22cp-22, .hi: 0x1.dac2p-3},
77 {.lo: 0x1.8bcdaf0534365p-76, .mid: -0x1.6164f64c210ep-22, .hi: 0x1.e858p-3},
78 {.lo: 0x1.1003282896056p-78, .mid: -0x1.0c1678ae89767p-24, .hi: 0x1.01d9cp-2},
79 {.lo: 0x1.01bcc7025fa92p-78, .mid: -0x1.f26a05c813d57p-22, .hi: 0x1.08bdp-2},
80 {.lo: -0x1.fe8a8648e9ebcp-80, .mid: 0x1.4d8fc561c8d44p-24, .hi: 0x1.169cp-2},
81 {.lo: 0x1.08dfb23650c75p-79, .mid: -0x1.362ad8f7ca2dp-22, .hi: 0x1.1d984p-2},
82 {.lo: -0x1.f8d5a89861a5ep-79, .mid: 0x1.2b13cd6c4d042p-22, .hi: 0x1.249ccp-2},
83 {.lo: -0x1.a1c872983511ep-76, .mid: -0x1.1c8f11979a5dbp-22, .hi: 0x1.32cp-2},
84 {.lo: 0x1.e8e21bff3336bp-77, .mid: 0x1.c2ab3edefe569p-23, .hi: 0x1.39de8p-2},
85 {.lo: 0x1.fd1994fb2c4a1p-80, .mid: 0x1.7c3eca28e69cap-26, .hi: 0x1.4106p-2},
86 {.lo: 0x1.6b94b51cf76b1p-80, .mid: -0x1.34c4e99e1c6c6p-24, .hi: 0x1.4f6fcp-2},
87 {.lo: -0x1.31d55da1d0f66p-76, .mid: -0x1.194a871b63619p-22, .hi: 0x1.56b24p-2},
88 {.lo: -0x1.378b22691e28bp-77, .mid: 0x1.e3dd5c1c885aep-23, .hi: 0x1.5dfdcp-2},
89 {.lo: 0x1.99e302970e411p-83, .mid: -0x1.6ccf3b1129b7cp-23, .hi: 0x1.6552cp-2},
90 {.lo: 0x1.20164a049664dp-82, .mid: -0x1.2f346e2bf924bp-23, .hi: 0x1.6cb1p-2},
91 {.lo: -0x1.d14aac4d864c3p-77, .mid: -0x1.fa61aaa59c1d8p-23, .hi: 0x1.7b8ap-2},
92 {.lo: 0x1.496ab4e4b293fp-79, .mid: 0x1.90c11fd32a3abp-22, .hi: 0x1.8304cp-2},
93 {.lo: -0x1.d209f2d4239c6p-86, .mid: 0x1.57f7a64ccd537p-27, .hi: 0x1.8a898p-2},
94 {.lo: 0x1.eae3326327babp-81, .mid: 0x1.249ba76fee235p-27, .hi: 0x1.9218p-2},
95 {.lo: 0x1.fa05bddfded8cp-77, .mid: -0x1.aad2729b21ae5p-23, .hi: 0x1.99b08p-2},
96 {.lo: -0x1.624140d175ba2p-77, .mid: 0x1.71810a5e1818p-22, .hi: 0x1.a8ff8p-2},
97 {.lo: 0x1.f1c5160c515c1p-81, .mid: -0x1.6172fe015e13cp-27, .hi: 0x1.b0b68p-2},
98 {.lo: -0x1.86a6204eec8cp-79, .mid: 0x1.5ec6c1bfbf89ap-24, .hi: 0x1.b877cp-2},
99 {.lo: 0x1.718f761dd3915p-78, .mid: 0x1.678bf6cdedf51p-24, .hi: 0x1.c0438p-2},
100 {.lo: -0x1.d4ee66c3700e4p-76, .mid: 0x1.c2d45fe43895ep-22, .hi: 0x1.c819cp-2},
101 {.lo: -0x1.7d14533586306p-77, .mid: -0x1.9ee52ed49d71dp-22, .hi: 0x1.cffbp-2},
102 {.lo: 0x1.5ce9fb5a7bb5bp-81, .mid: 0x1.5786af187a96bp-27, .hi: 0x1.d7e6cp-2},
103 {.lo: -0x1.ae6face57ad3bp-77, .mid: 0x1.3ab0dc56138c9p-23, .hi: 0x1.dfdd8p-2},
104 {.lo: 0x1.5ac93b443d55fp-78, .mid: 0x1.fe538ab34efb5p-22, .hi: 0x1.e7df4p-2},
105 {.lo: 0x1.f1753e0ae1e8fp-76, .mid: -0x1.e4fee07aa4b68p-22, .hi: 0x1.efec8p-2},
106 {.lo: 0x1.cdfd4c297069bp-76, .mid: -0x1.172f32fe67287p-22, .hi: 0x1.f804cp-2},
107 {.lo: 0x1.97a0e8f3ba742p-79, .mid: -0x1.9a83ff9ab9cc8p-22, .hi: 0x1.00144p-1},
108 {.lo: -0x1.800450f5b2357p-78, .mid: -0x1.68cb06cece193p-22, .hi: 0x1.042bep-1},
109 {.lo: -0x1.a839041241fe7p-78, .mid: 0x1.8cd71ddf82e2p-22, .hi: 0x1.08494p-1},
110 {.lo: 0x1.ed0b8eeccca86p-78, .mid: 0x1.5e18ab2df3ae6p-22, .hi: 0x1.0c6cap-1},
111 {.lo: 0x1.3dd41df9689b3p-79, .mid: 0x1.5dee4d9d8a273p-25, .hi: 0x1.1096p-1},
112 {.lo: -0x1.990555535afdp-82, .mid: 0x1.fcd15f101c142p-26, .hi: 0x1.14c56p-1},
113 {.lo: -0x1.1773d02c9055cp-77, .mid: -0x1.2474b0f992ba1p-23, .hi: 0x1.18faep-1},
114 {.lo: -0x1.4aeef330c53c1p-78, .mid: 0x1.4b5a92a606047p-24, .hi: 0x1.1d368p-1},
115 {.lo: 0x1.8e6ff749ebacbp-77, .mid: 0x1.16186fcf54bbdp-22, .hi: 0x1.21786p-1},
116 {.lo: 0x1.c09d761c548ebp-84, .mid: 0x1.18efabeb7d722p-27, .hi: 0x1.25c0ap-1},
117 {.lo: 0x1.aaa73a428e1e4p-78, .mid: -0x1.e5fc7d238691dp-24, .hi: 0x1.2a0f4p-1},
118 {.lo: -0x1.af2f3d8b63fbap-79, .mid: 0x1.f5809faf6283cp-22, .hi: 0x1.2e644p-1},
119 {.lo: -0x1.af2f3d8b63fbap-79, .mid: 0x1.f5809faf6283cp-22, .hi: 0x1.2e644p-1},
120 {.lo: 0x1.78de359f2bb88p-77, .mid: 0x1.c6e1dcd0cb449p-22, .hi: 0x1.32bfep-1},
121 {.lo: -0x1.415ae1a715618p-76, .mid: 0x1.76e0e8f74b4d5p-22, .hi: 0x1.37222p-1},
122 {.lo: -0x1.4991b5375621fp-79, .mid: -0x1.cb82c89692d99p-24, .hi: 0x1.3b8b2p-1},
123 {.lo: -0x1.827d37deb2236p-76, .mid: -0x1.63161c5432aebp-22, .hi: 0x1.3ffaep-1},
124 {.lo: 0x1.9576edac01c78p-77, .mid: 0x1.458104c41b901p-22, .hi: 0x1.44716p-1},
125 {.lo: 0x1.9576edac01c78p-77, .mid: 0x1.458104c41b901p-22, .hi: 0x1.44716p-1},
126 {.lo: -0x1.05a27b81e2219p-77, .mid: -0x1.cd9d0cde578d5p-22, .hi: 0x1.48efp-1},
127 {.lo: 0x1.237616778b4bap-82, .mid: 0x1.b9884591add87p-26, .hi: 0x1.4d738p-1},
128 {.lo: 0x1.3b7d7e5d148bbp-76, .mid: 0x1.c6042978605ffp-22, .hi: 0x1.51ff2p-1},
129 {.lo: -0x1.cc3f936a5977cp-79, .mid: -0x1.fc4c96b37dcf6p-22, .hi: 0x1.56922p-1},
130 {.lo: 0x1.20164a049664dp-83, .mid: -0x1.2f346e2bf924bp-24, .hi: 0x1.5b2c4p-1},
131 {.lo: 0x1.20164a049664dp-83, .mid: -0x1.2f346e2bf924bp-24, .hi: 0x1.5b2c4p-1},
132 {.lo: -0x1.a212919a92f7ap-77, .mid: 0x1.c4e4fbb68a4d1p-22, .hi: 0x1.5fcdcp-1},
133 {.lo: -0x1.b64b03f7230ddp-77, .mid: -0x1.9d499bd9b3226p-23, .hi: 0x1.6476ep-1},
134 {.lo: -0x1.1ec6379e6e3b9p-77, .mid: -0x1.f89b355ede26fp-23, .hi: 0x1.69278p-1},
135 {.lo: -0x1.1ec6379e6e3b9p-77, .mid: -0x1.f89b355ede26fp-23, .hi: 0x1.69278p-1},
136 {.lo: -0x1.4ba44c03bfbbdp-78, .mid: 0x1.53c7e319f6e92p-24, .hi: 0x1.6ddfcp-1},
137 {.lo: -0x1.c36fc650d030fp-77, .mid: -0x1.b291f070528c7p-22, .hi: 0x1.729fep-1},
138 {.lo: -0x1.69e5693a7f067p-80, .mid: 0x1.2967a451a7b48p-25, .hi: 0x1.7767cp-1},
139 {.lo: -0x1.69e5693a7f067p-80, .mid: 0x1.2967a451a7b48p-25, .hi: 0x1.7767cp-1},
140 {.lo: 0x1.6598aae91499ap-76, .mid: 0x1.244fcff690fcep-22, .hi: 0x1.7c37ap-1},
141 {.lo: 0x1.99d61ec432837p-77, .mid: 0x1.46fd97f5dc572p-23, .hi: 0x1.810fap-1},
142 {.lo: 0x1.99d61ec432837p-77, .mid: 0x1.46fd97f5dc572p-23, .hi: 0x1.810fap-1},
143 {.lo: 0x1.855c42078f81bp-76, .mid: -0x1.f3a7352663e5p-22, .hi: 0x1.85efep-1},
144 {.lo: -0x1.59408e815107p-77, .mid: 0x1.b3cda690370b5p-23, .hi: 0x1.8ad84p-1},
145 {.lo: -0x1.59408e815107p-77, .mid: 0x1.b3cda690370b5p-23, .hi: 0x1.8ad84p-1},
146 {.lo: 0x1.33b318085e50ap-78, .mid: 0x1.3226b211bf1d9p-23, .hi: 0x1.8fc92p-1},
147 {.lo: 0x1.343fe7c9cb4aep-79, .mid: 0x1.d24b136c101eep-23, .hi: 0x1.94c28p-1},
148 {.lo: 0x1.343fe7c9cb4aep-79, .mid: 0x1.d24b136c101eep-23, .hi: 0x1.94c28p-1},
149 {.lo: -0x1.d19522e56fe6p-76, .mid: 0x1.7c40c7907e82ap-22, .hi: 0x1.99c48p-1},
150 {.lo: -0x1.23b9d8ea55c3ep-77, .mid: -0x1.e81781d97ee91p-22, .hi: 0x1.9ecf6p-1},
151 {.lo: -0x1.23b9d8ea55c3ep-77, .mid: -0x1.e81781d97ee91p-22, .hi: 0x1.9ecf6p-1},
152 {.lo: 0x1.829440c24aeb6p-78, .mid: -0x1.6a77813f94e01p-22, .hi: 0x1.a3e3p-1},
153 {.lo: -0x1.624140d175ba2p-76, .mid: -0x1.1cfdeb43cfdp-22, .hi: 0x1.a8ffap-1},
154 {.lo: -0x1.624140d175ba2p-76, .mid: -0x1.1cfdeb43cfdp-22, .hi: 0x1.a8ffap-1},
155 {.lo: 0x1.afa6f024fb045p-77, .mid: -0x1.f983f74d3138fp-23, .hi: 0x1.ae256p-1},
156 {.lo: -0x1.603ad3a5d326dp-78, .mid: -0x1.e278ae1a1f51fp-23, .hi: 0x1.b3546p-1},
157 {.lo: -0x1.603ad3a5d326dp-78, .mid: -0x1.e278ae1a1f51fp-23, .hi: 0x1.b3546p-1},
158 {.lo: -0x1.0c1e0e5855d6ap-77, .mid: -0x1.97552b7b5ea45p-23, .hi: 0x1.b88ccp-1},
159 {.lo: -0x1.0c1e0e5855d6ap-77, .mid: -0x1.97552b7b5ea45p-23, .hi: 0x1.b88ccp-1},
160 {.lo: 0x1.c817ad56baa16p-78, .mid: -0x1.19b4f3c72c4f8p-24, .hi: 0x1.bdceap-1},
161 {.lo: 0x1.44c47ac1bf62bp-77, .mid: 0x1.f7402d26f1a12p-23, .hi: 0x1.c31a2p-1},
162 {.lo: 0x1.44c47ac1bf62bp-77, .mid: 0x1.f7402d26f1a12p-23, .hi: 0x1.c31a2p-1},
163 {.lo: -0x1.69b9465eae1e6p-78, .mid: -0x1.2056d5dd31d96p-23, .hi: 0x1.c86f8p-1},
164 {.lo: -0x1.69b9465eae1e6p-78, .mid: -0x1.2056d5dd31d96p-23, .hi: 0x1.c86f8p-1},
165 {.lo: -0x1.24a6d9d1d1904p-79, .mid: -0x1.6e46335aae723p-24, .hi: 0x1.cdcecp-1},
166 {.lo: -0x1.3826144575ac4p-76, .mid: -0x1.beb244c59f331p-22, .hi: 0x1.d3382p-1},
167 {.lo: -0x1.3826144575ac4p-76, .mid: -0x1.beb244c59f331p-22, .hi: 0x1.d3382p-1},
168 {.lo: 0x1.dbc96b3b12b25p-81, .mid: 0x1.16c071e93fd97p-27, .hi: 0x1.d8abap-1},
169 {.lo: 0x1.dbc96b3b12b25p-81, .mid: 0x1.16c071e93fd97p-27, .hi: 0x1.d8abap-1},
170 {.lo: 0x1.68a8ccdbd1f33p-77, .mid: 0x1.d8175819530c2p-22, .hi: 0x1.de298p-1},
171 {.lo: 0x1.68a8ccdbd1f33p-77, .mid: 0x1.d8175819530c2p-22, .hi: 0x1.de298p-1},
172 {.lo: 0x1.e586711df5ea1p-79, .mid: 0x1.51bd552842c1cp-23, .hi: 0x1.e3b2p-1},
173 {.lo: 0x1.e586711df5ea1p-79, .mid: 0x1.51bd552842c1cp-23, .hi: 0x1.e3b2p-1},
174 {.lo: -0x1.bc25adf042483p-79, .mid: 0x1.914e204f19d94p-22, .hi: 0x1.e9452p-1},
175 {.lo: -0x1.bc25adf042483p-79, .mid: 0x1.914e204f19d94p-22, .hi: 0x1.e9452p-1},
176 {.lo: 0x1.d7d82b65c5686p-76, .mid: 0x1.c55d997da24fdp-22, .hi: 0x1.eee32p-1},
177 {.lo: 0x1.d7d82b65c5686p-76, .mid: 0x1.c55d997da24fdp-22, .hi: 0x1.eee32p-1},
178 {.lo: -0x1.3f108c0857ca3p-77, .mid: -0x1.685c2d2298a6ep-22, .hi: 0x1.f48c4p-1},
179 {.lo: -0x1.3f108c0857ca3p-77, .mid: -0x1.685c2d2298a6ep-22, .hi: 0x1.f48c4p-1},
180 {.lo: -0x1.bd800bca7a221p-78, .mid: 0x1.7a4887bd74039p-22, .hi: 0x1.fa406p-1},
181 {.lo: 0.0, .mid: 0.0, .hi: 1.0},
182};
183
184// Look up table for the second range reduction step:
185// Generated by Sollya with:
186// > for i from -64 to 128 do {
187// r = 2^-16 * nearestint(2^16 / (1 + i * 2^-14) );
188// a = -log2(r);
189// b = round(a, D, RN);
190// c = round(a - b, D, RN);
191// print("{", c, ", ", b, "},");
192// };
193static constexpr DoubleDouble LOG2_R2_DD[] = {
194 {.lo: 0x1.ff25180953e64p-62, .hi: -0x1.720c2ab2312a9p-8},
195 {.lo: -0x1.15ffd79560d8fp-62, .hi: -0x1.6c4c92b1478ffp-8},
196 {.lo: 0x1.b8d6d6f2e3579p-62, .hi: -0x1.668ce3c873549p-8},
197 {.lo: -0x1.5bfc3f0d5ef71p-62, .hi: -0x1.60cd1df6fde91p-8},
198 {.lo: -0x1.d1f7a8777984ap-64, .hi: -0x1.5b0d413c30b5ep-8},
199 {.lo: 0x1.8e858515b8343p-66, .hi: -0x1.554d4d97551abp-8},
200 {.lo: 0x1.e165c4014c1f2p-62, .hi: -0x1.4f8d4307b46ecp-8},
201 {.lo: 0x1.0f84b2cc14c7ep-63, .hi: -0x1.49cd218c9800bp-8},
202 {.lo: 0x1.de618ed0db9a6p-62, .hi: -0x1.440ce9254916cp-8},
203 {.lo: -0x1.f6b8587e64f22p-62, .hi: -0x1.3e4c99d110ee7p-8},
204 {.lo: -0x1.7f793c84cfa63p-64, .hi: -0x1.388c338f38bdp-8},
205 {.lo: -0x1.7d7ecf6258c9ap-65, .hi: -0x1.32cbb65f09aeep-8},
206 {.lo: -0x1.810bc5ac188f5p-62, .hi: -0x1.2d0b223fcce81p-8},
207 {.lo: -0x1.950035fc5b67cp-62, .hi: -0x1.274a7730cb841p-8},
208 {.lo: 0x1.4f47f3048cdadp-62, .hi: -0x1.2189b5314e95dp-8},
209 {.lo: 0x1.269519861e298p-68, .hi: -0x1.1bc8dc409f279p-8},
210 {.lo: -0x1.5c2b0a46a7e2fp-62, .hi: -0x1.1607ec5e063b3p-8},
211 {.lo: 0x1.5001ac8f0bda8p-63, .hi: -0x1.1046e588cccap-8},
212 {.lo: 0x1.106f246af5d41p-62, .hi: -0x1.0a85c7c03bc4ap-8},
213 {.lo: 0x1.82a00583b34bap-66, .hi: -0x1.0354423e3c666p-8},
214 {.lo: 0x1.b6f37deb3137p-65, .hi: -0x1.fb25e19f11aecp-9},
215 {.lo: -0x1.44a2140444811p-63, .hi: -0x1.efa310d6550ecp-9},
216 {.lo: 0x1.f5e68a763133fp-63, .hi: -0x1.e4201220d4858p-9},
217 {.lo: 0x1.692083115f0b9p-63, .hi: -0x1.d89ce57d219a6p-9},
218 {.lo: 0x1.144bb17b9ac9cp-63, .hi: -0x1.cd198ae9cdc3dp-9},
219 {.lo: 0x1.ee7f086d32c05p-63, .hi: -0x1.c19602656a671p-9},
220 {.lo: -0x1.d4f1167538dbep-63, .hi: -0x1.b6124bee88d82p-9},
221 {.lo: 0x1.7df8d226c67ep-63, .hi: -0x1.aa8e6783ba5a2p-9},
222 {.lo: 0x1.60545d61b9512p-63, .hi: -0x1.9f0a5523901ebp-9},
223 {.lo: 0x1.54c99c291702p-63, .hi: -0x1.938614cc9b468p-9},
224 {.lo: -0x1.a7e678d7280dep-64, .hi: -0x1.8801a67d6ce1p-9},
225 {.lo: -0x1.6d419bbeb223ap-64, .hi: -0x1.7c7d0a3495ec9p-9},
226 {.lo: 0x1.ce2b9892e27e9p-64, .hi: -0x1.70f83ff0a7565p-9},
227 {.lo: -0x1.a4db4eff7bd61p-63, .hi: -0x1.657347b031fa2p-9},
228 {.lo: 0x1.5bb04682fab82p-63, .hi: -0x1.59ee2171c6a2fp-9},
229 {.lo: -0x1.78b8bfe6a3adep-64, .hi: -0x1.4e68cd33f60a3p-9},
230 {.lo: 0x1.574c3ce9b89b1p-63, .hi: -0x1.42e34af550d87p-9},
231 {.lo: 0x1.08fb216647b7bp-63, .hi: -0x1.375d9ab467a4dp-9},
232 {.lo: 0x1.ed5a50e7b919cp-66, .hi: -0x1.2bd7bc6fcaf56p-9},
233 {.lo: 0x1.91ad7a23f86fep-63, .hi: -0x1.2051b0260b3fp-9},
234 {.lo: 0x1.3ab2c932b8b0ap-64, .hi: -0x1.14cb75d5b8e54p-9},
235 {.lo: -0x1.c63bcdf120f7ap-63, .hi: -0x1.09450d7d643a9p-9},
236 {.lo: 0x1.8af8c4ab4e82dp-64, .hi: -0x1.fb7cee373b008p-10},
237 {.lo: 0x1.a52c2ca9d8b9bp-65, .hi: -0x1.e46f655de9cc6p-10},
238 {.lo: -0x1.460b177a58742p-64, .hi: -0x1.cd61806bf5166p-10},
239 {.lo: 0x1.611089de8d12ap-66, .hi: -0x1.b6533f5e7cf9bp-10},
240 {.lo: -0x1.4209853cee70cp-69, .hi: -0x1.9f44a232a16eep-10},
241 {.lo: 0x1.964e032541a28p-64, .hi: -0x1.8835a8e5824c3p-10},
242 {.lo: -0x1.fa9f94392637bp-66, .hi: -0x1.712653743f454p-10},
243 {.lo: -0x1.3293693721a53p-64, .hi: -0x1.5a16a1dbf7eb6p-10},
244 {.lo: -0x1.6e2af03c83c6ep-68, .hi: -0x1.43069419cbad5p-10},
245 {.lo: -0x1.b5f05b9d5bd29p-65, .hi: -0x1.2bf62a2ad9d74p-10},
246 {.lo: 0x1.3db883c072f72p-64, .hi: -0x1.14e5640c4193p-10},
247 {.lo: -0x1.a675a1c045304p-68, .hi: -0x1.fba8837643cf6p-11},
248 {.lo: 0x1.3b9c2aeb00068p-66, .hi: -0x1.cd85866933743p-11},
249 {.lo: -0x1.2911a381901ebp-66, .hi: -0x1.9f61d0eb8f98bp-11},
250 {.lo: -0x1.5ea75a74def03p-68, .hi: -0x1.713d62f7957c3p-11},
251 {.lo: -0x1.305b92f93ffep-67, .hi: -0x1.43183c878218dp-11},
252 {.lo: 0x1.b7c8c8dd40d35p-68, .hi: -0x1.14f25d959223ap-11},
253 {.lo: 0x1.dc915d58a62f6p-66, .hi: -0x1.cd978c3804191p-12},
254 {.lo: 0x1.c7bc3fe53cd94p-66, .hi: -0x1.7148ec2a1bfc9p-12},
255 {.lo: -0x1.427ce595cc53cp-67, .hi: -0x1.14f8daf5e3bcfp-12},
256 {.lo: -0x1.d523885ac824cp-67, .hi: -0x1.714eb11fa5363p-13},
257 {.lo: -0x1.945957f63330ap-69, .hi: -0x1.715193b17d35dp-14},
258 {.lo: 0, .hi: 0},
259 {.lo: -0x1.88fb2ea8bf9eap-70, .hi: 0x1.7157590356aeep-14},
260 {.lo: -0x1.5aeaee345d04ep-68, .hi: 0x1.715a3bc3593d5p-13},
261 {.lo: -0x1.7fce430230132p-66, .hi: 0x1.1505d6ee104c5p-12},
262 {.lo: -0x1.9a480f204ff09p-70, .hi: 0x1.716001718cb2bp-12},
263 {.lo: -0x1.00e7233f2d8bdp-68, .hi: 0x1.cdbb9d77ae5a8p-12},
264 {.lo: 0x1.09d379fa18c5dp-67, .hi: 0x1.150c5586012b8p-11},
265 {.lo: 0x1.b6b9d90a104d3p-65, .hi: 0x1.433b951d0b231p-11},
266 {.lo: 0x1.4d9a3ea651885p-65, .hi: 0x1.716b8d86bc285p-11},
267 {.lo: -0x1.7590b3a76f0f9p-67, .hi: 0x1.9f9c3ec8db94fp-11},
268 {.lo: 0x1.f183ca5b21bfep-65, .hi: 0x1.cdcda8e93107fp-11},
269 {.lo: -0x1.a7e3465ba127p-66, .hi: 0x1.fbffcbed8465fp-11},
270 {.lo: -0x1.7821f738d1221p-64, .hi: 0x1.151953edceec6p-10},
271 {.lo: 0x1.3bb4c0fb95359p-65, .hi: 0x1.2c331e5ca2e7dp-10},
272 {.lo: 0x1.236028e962f8p-64, .hi: 0x1.434d4546227fcp-10},
273 {.lo: 0x1.aaaa64d30f184p-66, .hi: 0x1.5a67c8ad32315p-10},
274 {.lo: -0x1.a821b7cc57a7ap-64, .hi: 0x1.7182a894b69c6p-10},
275 {.lo: -0x1.13d9d78aace21p-64, .hi: 0x1.889de4ff94838p-10},
276 {.lo: -0x1.2f249a6b923ap-64, .hi: 0x1.9fb97df0b0cc2p-10},
277 {.lo: -0x1.d47dc3664be7ap-68, .hi: 0x1.b6d5736af07e6p-10},
278 {.lo: 0x1.bd1522c6418fbp-64, .hi: 0x1.cdf1c57138c53p-10},
279 {.lo: -0x1.bacdbb22d2163p-64, .hi: 0x1.e50e74066eee6p-10},
280 {.lo: -0x1.ca7604812d77bp-64, .hi: 0x1.fc2b7f2d786a5p-10},
281 {.lo: -0x1.2b6832f8830bfp-63, .hi: 0x1.09a473749d663p-9},
282 {.lo: 0x1.4e712033d0457p-65, .hi: 0x1.1533559e4de55p-9},
283 {.lo: -0x1.473dd044017b5p-66, .hi: 0x1.20c26615409f1p-9},
284 {.lo: -0x1.e033bcac726d3p-63, .hi: 0x1.2c51a4dae8915p-9},
285 {.lo: -0x1.4a47a2b18a0fap-63, .hi: 0x1.37e111f0b8cb5p-9},
286 {.lo: 0x1.6f3615771c17bp-66, .hi: 0x1.4370ad58246ddp-9},
287 {.lo: 0x1.c0ee6c32d6236p-65, .hi: 0x1.4f0077129eabp-9},
288 {.lo: 0x1.fa94c99761b8fp-64, .hi: 0x1.5a906f219ac67p-9},
289 {.lo: -0x1.979e6b473fbf8p-64, .hi: 0x1.662095868c153p-9},
290 {.lo: 0x1.30edde8d24c7bp-64, .hi: 0x1.71b0ea42e5fdap-9},
291 {.lo: -0x1.d01594fe1421cp-64, .hi: 0x1.7d416d581bf7cp-9},
292 {.lo: 0x1.50bf7b995b49ap-63, .hi: 0x1.88d21ec7a18cdp-9},
293 {.lo: -0x1.28ea2bcec5018p-63, .hi: 0x1.9462fe92ea57cp-9},
294 {.lo: 0x1.ed6add489c30bp-65, .hi: 0x1.9ff40cbb6a04bp-9},
295 {.lo: 0x1.201d5c3bbeb69p-64, .hi: 0x1.ab85494294517p-9},
296 {.lo: -0x1.a05d0d4461ea9p-64, .hi: 0x1.b716b429dd0d3p-9},
297 {.lo: -0x1.7c974c8a392fdp-63, .hi: 0x1.c2a84d72b8189p-9},
298 {.lo: -0x1.f068238451bdep-64, .hi: 0x1.ce3a151e9965bp-9},
299 {.lo: -0x1.5e4d95c6259c3p-66, .hi: 0x1.d9cc0b2ef4f83p-9},
300 {.lo: -0x1.1fc262efaad6cp-63, .hi: 0x1.e55e2fa53ee53p-9},
301 {.lo: 0x1.49eee7abc7716p-63, .hi: 0x1.f0f08282eb533p-9},
302 {.lo: -0x1.903de284d2782p-65, .hi: 0x1.fc8303c96e7a6p-9},
303 {.lo: -0x1.ec564845134cbp-63, .hi: 0x1.040ad9bd1e522p-8},
304 {.lo: -0x1.7692b7791cf1fp-66, .hi: 0x1.0861eadabc3dcp-8},
305 {.lo: -0x1.37829afb11c1p-62, .hi: 0x1.0e2b6b51e4f7ep-8},
306 {.lo: 0x1.6706b91c3b0bap-62, .hi: 0x1.13f5030033459p-8},
307 {.lo: -0x1.7558ccd710756p-62, .hi: 0x1.19beb1e6616c9p-8},
308 {.lo: 0x1.79f72a5bbe9dep-62, .hi: 0x1.1f88780529bb1p-8},
309 {.lo: -0x1.e1297c110b25p-62, .hi: 0x1.2552555d46886p-8},
310 {.lo: 0x1.29930d567ca26p-62, .hi: 0x1.2b1c49ef72343p-8},
311 {.lo: 0x1.a08cbd7592a17p-65, .hi: 0x1.30e655bc67275p-8},
312 {.lo: 0x1.e4f9d4ac5db83p-62, .hi: 0x1.36b078c4dfd31p-8},
313 {.lo: -0x1.ed1b0aafd30c2p-62, .hi: 0x1.3c7ab30996b1cp-8},
314 {.lo: 0x1.e78f0aa014b32p-62, .hi: 0x1.4245048b46462p-8},
315 {.lo: 0x1.8594548038a0fp-69, .hi: 0x1.480f6d4aa91c2p-8},
316 {.lo: 0x1.3df498168a333p-63, .hi: 0x1.4dd9ed4879c82p-8},
317 {.lo: 0x1.b1c502544f82ap-62, .hi: 0x1.53a4848572e77p-8},
318 {.lo: -0x1.dc50552fe0da9p-63, .hi: 0x1.596f33024f203p-8},
319 {.lo: -0x1.671d85c357d5ep-62, .hi: 0x1.5f39f8bfc9212p-8},
320 {.lo: 0x1.1c670cabccefap-64, .hi: 0x1.6504d5be9ba1ep-8},
321 {.lo: -0x1.9983a9e98f318p-62, .hi: 0x1.6acfc9ff8162fp-8},
322 {.lo: 0x1.ae1a26af3eebep-62, .hi: 0x1.709ad583352d6p-8},
323 {.lo: 0x1.655eb510bfda3p-62, .hi: 0x1.7665f84a71d35p-8},
324 {.lo: -0x1.e287bc0192e15p-64, .hi: 0x1.7c313255f22f8p-8},
325 {.lo: 0x1.cc4944139ccbfp-63, .hi: 0x1.81fc83a671257p-8},
326 {.lo: 0x1.4e09b4cb8645bp-62, .hi: 0x1.87c7ec3ca9a19p-8},
327 {.lo: -0x1.5becc991e3a5fp-64, .hi: 0x1.8d936c1956991p-8},
328 {.lo: -0x1.ddfa3f1e15ba8p-62, .hi: 0x1.935f033d3309ep-8},
329 {.lo: -0x1.b7b06ea3fb362p-62, .hi: 0x1.992ab1a8f9facp-8},
330 {.lo: 0x1.32d614904e46cp-62, .hi: 0x1.9ef6775d667b4p-8},
331 {.lo: -0x1.7186892b5bfaep-64, .hi: 0x1.a4c2545b33a3ep-8},
332 {.lo: -0x1.d4de10b28dfd8p-62, .hi: 0x1.aa8e48a31c95cp-8},
333 {.lo: 0x1.4bb4b3bdc8175p-62, .hi: 0x1.b05a5435dc7adp-8},
334 {.lo: 0x1.9cedbd1d7fba5p-62, .hi: 0x1.b62677142e86p-8},
335 {.lo: -0x1.0ed3379beaffdp-66, .hi: 0x1.bbf2b13ecdf2fp-8},
336 {.lo: 0x1.6e86a125567a6p-62, .hi: 0x1.c1bf02b67606p-8},
337 {.lo: -0x1.35038e0c0a52cp-62, .hi: 0x1.c6184f1b326d9p-8},
338 {.lo: 0x1.05ef8bf5adf5ep-67, .hi: 0x1.cbe4c95b6c5abp-8},
339 {.lo: -0x1.b7338b99a6b26p-65, .hi: 0x1.d1b15aeab217cp-8},
340 {.lo: 0x1.9e901c30c427ep-63, .hi: 0x1.d77e03c9bf0a4p-8},
341 {.lo: -0x1.1f28a9c0b3d47p-62, .hi: 0x1.dd4ac3f94ea0ap-8},
342 {.lo: -0x1.140ef760d3b63p-62, .hi: 0x1.e3179b7a1c52p-8},
343 {.lo: -0x1.ab65b1037f517p-63, .hi: 0x1.e8e48a4ce39e7p-8},
344 {.lo: -0x1.76940c457ce6dp-63, .hi: 0x1.eeb19072600edp-8},
345 {.lo: 0x1.da3ae65a605cfp-64, .hi: 0x1.f47eadeb4d34dp-8},
346 {.lo: 0x1.b15d0bce2ede6p-62, .hi: 0x1.fa4be2b866abp-8},
347 {.lo: 0x1.e02aa1fa9dc57p-61, .hi: 0x1.000c976d340a6p-7},
348 {.lo: 0x1.6be971a5565b9p-62, .hi: 0x1.02f34929068f3p-7},
349 {.lo: -0x1.8a9319a6ed164p-64, .hi: 0x1.05da069008be7p-7},
350 {.lo: 0x1.825079f1e0ec5p-62, .hi: 0x1.08c0cfa298771p-7},
351 {.lo: 0x1.60d5749321466p-63, .hi: 0x1.0ba7a461139c8p-7},
352 {.lo: -0x1.5b8f4c479e2ep-61, .hi: 0x1.0e8e84cbd8169p-7},
353 {.lo: -0x1.e3e1248004e29p-62, .hi: 0x1.117570e343d17p-7},
354 {.lo: 0x1.9ac06487c375p-63, .hi: 0x1.145c68a7b4bddp-7},
355 {.lo: 0x1.f657ea5c03ea4p-62, .hi: 0x1.17436c1988d0dp-7},
356 {.lo: -0x1.5a965659a05e2p-61, .hi: 0x1.1a2a7b391e04p-7},
357 {.lo: -0x1.21ce9b9bfc512p-61, .hi: 0x1.1d119606d2554p-7},
358 {.lo: -0x1.30fda247ad0e1p-61, .hi: 0x1.1ff8bc8303c7p-7},
359 {.lo: -0x1.382c78a45cdeap-62, .hi: 0x1.22dfeeae10601p-7},
360 {.lo: 0x1.46ae4a64073d4p-61, .hi: 0x1.250d5bf952374p-7},
361 {.lo: -0x1.dcad2cec3b84bp-62, .hi: 0x1.27f4a29740a2fp-7},
362 {.lo: -0x1.413fbeb0b0635p-61, .hi: 0x1.2adbf4e50cdf9p-7},
363 {.lo: 0x1.f28e6a48bcb9p-61, .hi: 0x1.2dc352e315049p-7},
364 {.lo: -0x1.96f286e1eb086p-61, .hi: 0x1.30aabc91b72ep-7},
365 {.lo: -0x1.f88c04206dfa1p-61, .hi: 0x1.339231f1517c1p-7},
366 {.lo: -0x1.11ea20e195841p-61, .hi: 0x1.3679b30242139p-7},
367 {.lo: -0x1.d6e71452b674ap-63, .hi: 0x1.39613fc4e71dcp-7},
368 {.lo: -0x1.57c578233b1b3p-61, .hi: 0x1.3c48d8399ec85p-7},
369 {.lo: -0x1.ec430f03b76ep-63, .hi: 0x1.3f307c60c7455p-7},
370 {.lo: 0x1.e00dd1902ffb9p-61, .hi: 0x1.42182c3abecb5p-7},
371 {.lo: -0x1.f22bcd96afe38p-61, .hi: 0x1.44ffe7c7e3957p-7},
372 {.lo: 0x1.08fd90f841d3p-61, .hi: 0x1.47e7af0893e2fp-7},
373 {.lo: 0x1.09594c5552bccp-62, .hi: 0x1.4acf81fd2df7ep-7},
374 {.lo: -0x1.01a8a652e5602p-61, .hi: 0x1.4db760a6101c9p-7},
375 {.lo: -0x1.826168febb3dp-64, .hi: 0x1.509f4b03989dcp-7},
376 {.lo: -0x1.7eb21a35021e3p-62, .hi: 0x1.5387411625cccp-7},
377 {.lo: -0x1.66cbc818e175p-61, .hi: 0x1.566f42de15ff4p-7},
378 {.lo: 0x1.9b784dd6cebdap-64, .hi: 0x1.5957505bc78f6p-7},
379 {.lo: 0x1.2b121ab482456p-61, .hi: 0x1.5b8562298c65bp-7},
380 {.lo: -0x1.5d29869dd8233p-62, .hi: 0x1.5e6d842633702p-7},
381 {.lo: -0x1.572a1b6cd63cfp-61, .hi: 0x1.6155b1d99f672p-7},
382 {.lo: -0x1.a1f355360e877p-62, .hi: 0x1.643deb442eb59p-7},
383 {.lo: -0x1.b6f1cd2e1c03fp-61, .hi: 0x1.672630663fcadp-7},
384 {.lo: -0x1.2aaa11ccddcaep-61, .hi: 0x1.6a0e8140311aap-7},
385 {.lo: 0x1.3d979ddf4746cp-61, .hi: 0x1.6cf6ddd2611d4p-7},
386 {.lo: -0x1.dc930484501f8p-63, .hi: 0x1.6fdf461d2e4f8p-7},
387};
388
389LIBC_INLINE bool is_odd_integer(float x) {
390 using FPBits = typename fputil::FPBits<float>;
391 uint32_t x_u = cpp::bit_cast<uint32_t>(from: x);
392 int32_t x_e =
393 static_cast<int32_t>((x_u & FPBits::EXP_MASK) >> FPBits::FRACTION_LEN);
394 int32_t lsb = cpp::countr_zero(value: x_u | FPBits::EXP_MASK);
395 constexpr int32_t UNIT_EXPONENT =
396 FPBits::EXP_BIAS + static_cast<int32_t>(FPBits::FRACTION_LEN);
397 return (x_e + lsb == UNIT_EXPONENT);
398}
399
400LIBC_INLINE bool is_integer(float x) {
401 using FPBits = typename fputil::FPBits<float>;
402 uint32_t x_u = cpp::bit_cast<uint32_t>(from: x);
403 int32_t x_e =
404 static_cast<int32_t>((x_u & FPBits::EXP_MASK) >> FPBits::FRACTION_LEN);
405 int32_t lsb = cpp::countr_zero(value: x_u | FPBits::EXP_MASK);
406 constexpr int32_t UNIT_EXPONENT =
407 FPBits::EXP_BIAS + static_cast<int32_t>(FPBits::FRACTION_LEN);
408 return (x_e + lsb >= UNIT_EXPONENT);
409}
410
411LIBC_INLINE bool larger_exponent(double a, double b) {
412 using DoubleBits = typename fputil::FPBits<double>;
413 return DoubleBits(a).get_biased_exponent() >=
414 DoubleBits(b).get_biased_exponent();
415}
416
417// Calculate 2^(y * log2(x)) in double-double precision.
418// At this point we can reuse the following values:
419// idx_x: index for extra precision of log2 for the middle part of log2(x).
420// dx: the reduced argument for log2(x)
421// y6: 2^6 * y.
422// lo6_hi: the high part of 2^6 * (y - (hi + mid))
423// exp2_hi_mid: high part of 2^(hi + mid)
424double powf_double_double(int idx_x, double dx, double y6, double lo6_hi,
425 const DoubleDouble &exp2_hi_mid) {
426 using DoubleBits = typename fputil::FPBits<double>;
427
428 // Perform a second range reduction step:
429 // idx2 = round(2^14 * (dx + 2^-8)) = round ( dx * 2^14 + 2^6)
430 // dx2 = (1 + dx) * r2 - 1
431 // Output range:
432 // -0x1.3ffcp-15 <= dx2 <= 0x1.3e3dp-15
433 int idx2 = static_cast<int>(
434 fputil::nearest_integer(x: fputil::multiply_add(x: dx, y: 0x1.0p14, z: 0x1.0p6)));
435 double dx2 = fputil::multiply_add(x: 1.0 + dx, y: R2[idx2], z: -1.0); // Exact
436
437 // Degree-5 polynomial approximation of log2(1 + x)/x in double-double
438 // precision. Generate by Solya with:
439 // > P = fpminimax(log2(1 + x)/x, 5, [|DD...|],
440 // [-0x1.3ffcp-15, 0x1.3e3dp-15]);
441 // > dirtyinfnorm(log2(1 + x)/x - P, [-0x1.3ffcp-15, 0x1.3e3dp-15]);
442 // 0x1.8be5...p-96.
443 constexpr DoubleDouble COEFFS[] = {
444 {.lo: 0x1.777d0ffda25ep-56, .hi: 0x1.71547652b82fep0},
445 {.lo: -0x1.777d101cf0a84p-57, .hi: -0x1.71547652b82fep-1},
446 {.lo: 0x1.ce04b5140d867p-56, .hi: 0x1.ec709dc3a03fdp-2},
447 {.lo: 0x1.137b47e635be5p-56, .hi: -0x1.71547652b82fbp-2},
448 {.lo: -0x1.b5a30b3bdb318p-58, .hi: 0x1.2776c516a92a2p-2},
449 {.lo: 0x1.2d2fbd081e657p-57, .hi: -0x1.ec70af1929ca6p-3},
450 };
451
452 DoubleDouble dx_dd({.lo: 0.0, .hi: dx2});
453 DoubleDouble p = fputil::polyeval(x: dx_dd, a0: COEFFS[0], a: COEFFS[1], a: COEFFS[2],
454 a: COEFFS[3], a: COEFFS[4], a: COEFFS[5]);
455 // log2(1 + dx2) ~ dx2 * P(dx2)
456 DoubleDouble log2_x_lo = fputil::quick_mult(a: dx2, b: p);
457 // Lower parts of (e_x - log2(r1)) of the first range reduction constant
458 DoubleDouble log2_x_mid({.lo: LOG2_R_TD[idx_x].lo, .hi: LOG2_R_TD[idx_x].mid});
459 // -log2(r2) + lower part of (e_x - log2(r1))
460 DoubleDouble log2_x_m = fputil::add(a: LOG2_R2_DD[idx2], b: log2_x_mid);
461 // log2(1 + dx2) - log2(r2) + lower part of (e_x - log2(r1))
462 // Since we don't know which one has larger exponent to apply Fast2Sum
463 // algorithm, we need to check them before calling double-double addition.
464 DoubleDouble log2_x = larger_exponent(a: log2_x_m.hi, b: log2_x_lo.hi)
465 ? fputil::add(a: log2_x_m, b: log2_x_lo)
466 : fputil::add(a: log2_x_lo, b: log2_x_m);
467 DoubleDouble lo6_hi_dd({.lo: 0.0, .hi: lo6_hi});
468 // 2^6 * y * (log2(1 + dx2) - log2(r2) + lower part of (e_x - log2(r1)))
469 DoubleDouble prod = fputil::quick_mult(a: y6, b: log2_x);
470 // 2^6 * (y * log2(x) - (hi + mid)) = 2^6 * lo
471 DoubleDouble lo6 = larger_exponent(a: prod.hi, b: lo6_hi)
472 ? fputil::add(a: prod, b: lo6_hi_dd)
473 : fputil::add(a: lo6_hi_dd, b: prod);
474
475 constexpr DoubleDouble EXP2_COEFFS[] = {
476 {.lo: 0, .hi: 0x1p0},
477 {.lo: 0x1.abc9e3b398024p-62, .hi: 0x1.62e42fefa39efp-7},
478 {.lo: -0x1.5e43a5429bddbp-69, .hi: 0x1.ebfbdff82c58fp-15},
479 {.lo: -0x1.d33162491268fp-77, .hi: 0x1.c6b08d704a0cp-23},
480 {.lo: 0x1.4fb32d240a14ep-86, .hi: 0x1.3b2ab6fba4e77p-31},
481 {.lo: 0x1.e84e916be83ep-97, .hi: 0x1.5d87fe78a6731p-40},
482 {.lo: -0x1.9a447bfddc5e6p-103, .hi: 0x1.430912f86bfb8p-49},
483 {.lo: -0x1.31a55719de47fp-113, .hi: 0x1.ffcbfc588ded9p-59},
484 {.lo: -0x1.0ba57164eb36bp-122, .hi: 0x1.62c034beb8339p-68},
485 {.lo: -0x1.8483eabd9642dp-132, .hi: 0x1.b5251ff97bee1p-78},
486 };
487
488 DoubleDouble pp = fputil::polyeval(
489 x: lo6, a0: EXP2_COEFFS[0], a: EXP2_COEFFS[1], a: EXP2_COEFFS[2], a: EXP2_COEFFS[3],
490 a: EXP2_COEFFS[4], a: EXP2_COEFFS[5], a: EXP2_COEFFS[6], a: EXP2_COEFFS[7],
491 a: EXP2_COEFFS[8], a: EXP2_COEFFS[9]);
492 DoubleDouble rr = fputil::quick_mult(a: exp2_hi_mid, b: pp);
493
494 // Make sure the sum is normalized:
495 DoubleDouble r = fputil::exact_add(a: rr.hi, b: rr.lo);
496 // Round to odd.
497 uint64_t r_bits = cpp::bit_cast<uint64_t>(from: r.hi);
498 if (LIBC_UNLIKELY(((r_bits & 0xfff'ffff) == 0) && (r.lo != 0.0))) {
499 Sign hi_sign = DoubleBits(r.hi).sign();
500 Sign lo_sign = DoubleBits(r.lo).sign();
501 if (hi_sign == lo_sign) {
502 ++r_bits;
503 } else if ((r_bits & DoubleBits::FRACTION_MASK) > 0) {
504 --r_bits;
505 }
506 }
507
508 return cpp::bit_cast<double>(from: r_bits);
509}
510
511} // namespace
512
513LLVM_LIBC_FUNCTION(float, powf, (float x, float y)) {
514 using FloatBits = typename fputil::FPBits<float>;
515 using DoubleBits = typename fputil::FPBits<double>;
516
517 FloatBits xbits(x), ybits(y);
518
519 uint32_t x_u = xbits.uintval();
520 uint32_t x_abs = xbits.abs().uintval();
521 uint32_t y_u = ybits.uintval();
522 uint32_t y_abs = ybits.abs().uintval();
523
524 ///////// BEGIN - Check exceptional cases ////////////////////////////////////
525
526 // The single precision number that is closest to 1 is (1 - 2^-24), which has
527 // log2(1 - 2^-24) ~ -1.715...p-24.
528 // So if |y| > 151 * 2^24, and x is finite:
529 // |y * log2(x)| = 0 or > 151.
530 // Hence x^y will either overflow or underflow if x is not zero.
531 if (LIBC_UNLIKELY((y_abs & 0x007f'ffff) == 0) || (y_abs > 0x4f170000)) {
532 // Exceptional exponents.
533 switch (y_abs) {
534 case 0x0000'0000: { // y = +-0.0f
535 return 1.0f;
536 }
537 case 0x7f80'0000: { // y = +-Inf
538 if (x_abs > 0x7f80'0000) {
539 // pow(NaN, +-Inf) = NaN
540 return x;
541 }
542 if (x_abs == 0x3f80'0000) {
543 // pow(+-1, +-Inf) = 1.0f
544 return 1.0f;
545 }
546 if (x_abs == 0 && y_u == 0xff80'0000) {
547 // pow(+-0, -Inf) = +inf and raise FE_DIVBYZERO
548 fputil::set_errno_if_required(EDOM);
549 fputil::raise_except_if_required(FE_DIVBYZERO);
550 return FloatBits::inf().get_val();
551 }
552 // pow (|x| < 1, -inf) = +inf
553 // pow (|x| < 1, +inf) = 0.0f
554 // pow (|x| > 1, -inf) = 0.0f
555 // pow (|x| > 1, +inf) = +inf
556 return ((x_abs < 0x3f80'0000) == (y_u == 0xff80'0000))
557 ? FloatBits::inf().get_val()
558 : 0.0f;
559 }
560 default:
561 // Speed up for common exponents
562 switch (y_u) {
563 case 0x3f00'0000: // y = 0.5f
564 // pow(x, 1/2) = sqrt(x)
565 return fputil::sqrt(x);
566 case 0x3f80'0000: // y = 1.0f
567 return x;
568 case 0x4000'0000: // y = 2.0f
569 // pow(x, 2) = x^2
570 return x * x;
571 // TODO: Enable special case speed-up for x^(-1/2) when rsqrt is ready.
572 // case 0xbf00'0000: // pow(x, -1/2) = rsqrt(x)
573 // return rsqrt(x);
574 }
575 if (y_abs > 0x4f17'0000) {
576 if (y_abs > 0x7f80'0000) {
577 // y is NaN
578 if (x_u == 0x3f80'0000) { // x = 1.0f
579 // pow(1, NaN) = 1
580 return 1.0f;
581 }
582 // pow(x, NaN) = NaN
583 return y;
584 }
585 // x^y will be overflow / underflow in single precision. Set y to a
586 // large enough exponent but not too large, so that the computations
587 // won't be overflow in double precision.
588 y = cpp::bit_cast<float>(from: (y_u & FloatBits::SIGN_MASK) + 0x4f800000U);
589 }
590 }
591 }
592
593 int ex = -FloatBits::EXP_BIAS;
594 uint64_t sign = 0;
595
596 // y is finite and non-zero.
597 if (LIBC_UNLIKELY(((x_u & 0x801f'ffffU) == 0) || x_u >= 0x7f80'0000U ||
598 x_u < 0x0080'0000U)) {
599 switch (x_u) {
600 case 0x3f80'0000: // x = 1.0f
601 return 1.0f;
602 // TODO: Put these 2 entrypoint dependency under control flag.
603 case 0x4000'0000: // x = 2.0f
604 // pow(2, y) = exp2(y)
605 return generic::exp2f(x: y);
606 case 0x4120'0000: // x = 10.0f
607 // pow(10, y) = exp10(y)
608 return generic::exp10f(x: y);
609 }
610
611 const bool x_is_neg = x_u >= FloatBits::SIGN_MASK;
612
613 switch (x_abs) {
614 case 0x0000'0000: { // x = +-0.0f
615 const bool out_is_neg =
616 x_is_neg && is_odd_integer(x: FloatBits(y_u).get_val());
617 if (y_u > 0x8000'0000U) {
618 // pow(0, negative number) = inf
619 fputil::set_errno_if_required(EDOM);
620 fputil::raise_except_if_required(FE_DIVBYZERO);
621 return FloatBits::inf(sign: out_is_neg ? Sign::NEG : Sign::POS).get_val();
622 }
623 // pow(0, positive number) = 0
624 return out_is_neg ? -0.0f : 0.0f;
625 }
626 case 0x7f80'0000: { // x = +-Inf
627 const bool out_is_neg =
628 x_is_neg && is_odd_integer(x: FloatBits(y_u).get_val());
629 if (y_u >= FloatBits::SIGN_MASK) {
630 return out_is_neg ? -0.0f : 0.0f;
631 }
632 return FloatBits::inf(sign: out_is_neg ? Sign::NEG : Sign::POS).get_val();
633 }
634 }
635
636 if (x_abs > 0x7f80'0000) {
637 // x is NaN.
638 // pow (aNaN, 0) is already taken care above.
639 return x;
640 }
641
642 // Normalize denormal inputs.
643 if (x_abs < 0x0080'0000U) {
644 ex -= 64;
645 x *= 0x1.0p64f;
646 }
647
648 // x is finite and negative, and y is a finite integer.
649 if (x_is_neg) {
650 if (is_integer(x: y)) {
651 x = -x;
652 if (is_odd_integer(x: y)) {
653 // sign = -1.0;
654 sign = 0x8000'0000'0000'0000ULL;
655 }
656 } else {
657 // pow( negative, non-integer ) = NaN
658 fputil::set_errno_if_required(EDOM);
659 fputil::raise_except_if_required(FE_INVALID);
660 return FloatBits::quiet_nan().get_val();
661 }
662 }
663 }
664
665 ///////// END - Check exceptional cases //////////////////////////////////////
666
667 // x^y = 2^( y * log2(x) )
668 // = 2^( y * ( e_x + log2(m_x) ) )
669 // First we compute log2(x) = e_x + log2(m_x)
670 x_u = FloatBits(x).uintval();
671
672 // Extract exponent field of x.
673 ex += (x_u >> FloatBits::FRACTION_LEN);
674 double e_x = static_cast<double>(ex);
675 // Use the highest 7 fractional bits of m_x as the index for look up tables.
676 uint32_t x_mant = x_u & FloatBits::FRACTION_MASK;
677 int idx_x = static_cast<int>(x_mant >> (FloatBits::FRACTION_LEN - 7));
678 // Add the hidden bit to the mantissa.
679 // 1 <= m_x < 2
680 float m_x = cpp::bit_cast<float>(from: x_mant | 0x3f800000);
681
682 // Reduced argument for log2(m_x):
683 // dx = r * m_x - 1.
684 // The computation is exact, and -2^-8 <= dx < 2^-7.
685 // Then m_x = (1 + dx) / r, and
686 // log2(m_x) = log2( (1 + dx) / r )
687 // = log2(1 + dx) - log2(r).
688 double dx;
689#ifdef LIBC_TARGET_CPU_HAS_FMA
690 dx = static_cast<double>(fputil::multiply_add(x: m_x, y: R[idx_x], z: -1.0f)); // Exact
691#else
692 dx = fputil::multiply_add(static_cast<double>(m_x), RD[idx_x], -1.0); // Exact
693#endif // LIBC_TARGET_CPU_HAS_FMA
694
695 // Degree-5 polynomial approximation:
696 // dx * P(dx) ~ log2(1 + dx)
697 // Generated by Sollya with:
698 // > P = fpminimax(log2(1 + x)/x, 5, [|D...|], [-2^-8, 2^-7]);
699 // > dirtyinfnorm(log2(1 + x)/x - P, [-2^-8, 2^-7]);
700 // 0x1.653...p-52
701 constexpr double COEFFS[] = {0x1.71547652b82fep0, -0x1.71547652b7a07p-1,
702 0x1.ec709dc458db1p-2, -0x1.715479c2266c9p-2,
703 0x1.2776ae1ddf8fp-2, -0x1.e7b2178870157p-3};
704
705 double dx2 = dx * dx; // Exact
706 double c0 = fputil::multiply_add(x: dx, y: COEFFS[1], z: COEFFS[0]);
707 double c1 = fputil::multiply_add(x: dx, y: COEFFS[3], z: COEFFS[2]);
708 double c2 = fputil::multiply_add(x: dx, y: COEFFS[5], z: COEFFS[4]);
709
710 double p = fputil::polyeval(x: dx2, a0: c0, a: c1, a: c2);
711
712 //////////////////////////////////////////////////////////////////////////////
713 // NOTE: For some reason, this is significantly less efficient than above!
714 //
715 // > P = fpminimax(log2(1 + x)/x, 4, [|D...|], [-2^-8, 2^-7]);
716 // > dirtyinfnorm(log2(1 + x)/x - P, [-2^-8, 2^-7]);
717 // 0x1.d04...p-44
718 // constexpr double COEFFS[] = {0x1.71547652b8133p0, -0x1.71547652d1e33p-1,
719 // 0x1.ec70a098473dep-2, -0x1.7154c5ccdf121p-2,
720 // 0x1.2514fd90a130ap-2};
721 //
722 // double dx2 = dx * dx;
723 // double c0 = fputil::multiply_add(dx, COEFFS[1], COEFFS[0]);
724 // double c1 = fputil::multiply_add(dx, COEFFS[3], COEFFS[2]);
725 // double p = fputil::polyeval(dx2, c0, c1, COEFFS[4]);
726 //////////////////////////////////////////////////////////////////////////////
727
728 // s = e_x - log2(r) + dx * P(dx)
729 // Approximation errors:
730 // |log2(x) - s| < ulp(e_x) + (bounds on dx) * (error bounds of P(dx))
731 // = ulp(e_x) + 2^-7 * 2^-51
732 // < 2^8 * 2^-52 + 2^-7 * 2^-43
733 // ~ 2^-44 + 2^-50
734 double s = fputil::multiply_add(x: dx, y: p, z: LOG2_R[idx_x] + e_x);
735
736 // To compute 2^(y * log2(x)), we break the exponent into 3 parts:
737 // y * log(2) = hi + mid + lo, where
738 // hi is an integer
739 // mid * 2^6 is an integer
740 // |lo| <= 2^-7
741 // Then:
742 // x^y = 2^(y * log2(x)) = 2^hi * 2^mid * 2^lo,
743 // In which 2^mid is obtained from a look-up table of size 2^6 = 64 elements,
744 // and 2^lo ~ 1 + lo * P(lo).
745 // Thus, we have:
746 // hi + mid = 2^-6 * round( 2^6 * y * log2(x) )
747 // If we restrict the output such that |hi| < 150, (hi + mid) uses (8 + 6)
748 // bits, hence, if we use double precision to perform
749 // round( 2^6 * y * log2(x))
750 // the lo part is bounded by 2^-7 + 2^(-(52 - 14)) = 2^-7 + 2^-38
751
752 // In the following computations:
753 // y6 = 2^6 * y
754 // hm = 2^6 * (hi + mid) = round(2^6 * y * log2(x)) ~ round(y6 * s)
755 // lo6 = 2^6 * lo = 2^6 * (y - (hi + mid)) = y6 * log2(x) - hm.
756 double y6 = static_cast<double>(y * 0x1.0p6f); // Exact.
757 double hm = fputil::nearest_integer(x: s * y6);
758 // lo6 = 2^6 * lo.
759 double lo6_hi =
760 fputil::multiply_add(x: y6, y: e_x + LOG2_R_TD[idx_x].hi, z: -hm); // Exact
761 // Error bounds:
762 // | (y*log2(x) - hm * 2^-6 - lo) / y| < err(dx * p) + err(LOG2_R_DD.lo)
763 // < 2^-51 + 2^-75
764 double lo6 = fputil::multiply_add(
765 x: y6, y: fputil::multiply_add(x: dx, y: p, z: LOG2_R_TD[idx_x].mid), z: lo6_hi);
766
767 // |2^(hi + mid) - exp2_hi_mid| <= ulp(exp2_hi_mid) / 2
768 // Clamp the exponent part into smaller range that fits double precision.
769 // For those exponents that are out of range, the final conversion will round
770 // them correctly to inf/max float or 0/min float accordingly.
771 int64_t hm_i = static_cast<int64_t>(hm);
772 hm_i = (hm_i > (1 << 15)) ? (1 << 15)
773 : (hm_i < (-(1 << 15)) ? -(1 << 15) : hm_i);
774
775 int idx_y = hm_i & 0x3f;
776
777 // 2^hi
778 int64_t exp_hi_i = (hm_i >> 6) << DoubleBits::FRACTION_LEN;
779 // 2^mid
780 int64_t exp_mid_i = cpp::bit_cast<uint64_t>(from: EXP2_MID1[idx_y].hi);
781 // (-1)^sign * 2^hi * 2^mid
782 // Error <= 2^hi * 2^-53
783 uint64_t exp2_hi_mid_i = static_cast<uint64_t>(exp_hi_i + exp_mid_i) + sign;
784 double exp2_hi_mid = cpp::bit_cast<double>(from: exp2_hi_mid_i);
785
786 // Degree-5 polynomial approximation P(lo6) ~ 2^(lo6 / 2^6) = 2^(lo).
787 // Generated by Sollya with:
788 // > P = fpminimax(2^(x/64), 5, [|1, D...|], [-2^-1, 2^-1]);
789 // > dirtyinfnorm(2^(x/64) - P, [-0.5, 0.5]);
790 // 0x1.a2b77e618f5c4c176fd11b7659016cde5de83cb72p-60
791 constexpr double EXP2_COEFFS[] = {0x1p0,
792 0x1.62e42fefa39efp-7,
793 0x1.ebfbdff82a23ap-15,
794 0x1.c6b08d7076268p-23,
795 0x1.3b2ad33f8b48bp-31,
796 0x1.5d870c4d84445p-40};
797
798 double lo6_sqr = lo6 * lo6;
799 double d0 = fputil::multiply_add(x: lo6, y: EXP2_COEFFS[1], z: EXP2_COEFFS[0]);
800 double d1 = fputil::multiply_add(x: lo6, y: EXP2_COEFFS[3], z: EXP2_COEFFS[2]);
801 double d2 = fputil::multiply_add(x: lo6, y: EXP2_COEFFS[5], z: EXP2_COEFFS[4]);
802 double pp = fputil::polyeval(x: lo6_sqr, a0: d0, a: d1, a: d2);
803
804 double r = pp * exp2_hi_mid;
805
806 // Ziv accuracy test.
807 uint64_t r_u = cpp::bit_cast<uint64_t>(from: r);
808 float r_upper = static_cast<float>(cpp::bit_cast<double>(from: r_u + ERR));
809 float r_lower = static_cast<float>(cpp::bit_cast<double>(from: r_u - ERR));
810
811 if (LIBC_LIKELY(r_upper == r_lower)) {
812 // Check for overflow or underflow.
813 if (LIBC_UNLIKELY(FloatBits(r_upper).get_mantissa() == 0)) {
814 if (FloatBits(r_upper).is_inf()) {
815 fputil::set_errno_if_required(ERANGE);
816 fputil::raise_except_if_required(FE_OVERFLOW);
817 } else if (r_upper == 0.0f) {
818 fputil::set_errno_if_required(ERANGE);
819 fputil::raise_except_if_required(FE_UNDERFLOW);
820 }
821 }
822 return r_upper;
823 }
824
825 // Scale lower part of 2^(hi + mid)
826 DoubleDouble exp2_hi_mid_dd;
827 exp2_hi_mid_dd.lo =
828 (idx_y != 0)
829 ? cpp::bit_cast<double>(from: exp_hi_i +
830 cpp::bit_cast<int64_t>(from: EXP2_MID1[idx_y].mid))
831 : 0.0;
832 exp2_hi_mid_dd.hi = exp2_hi_mid;
833
834 return static_cast<float>(
835 powf_double_double(idx_x, dx, y6, lo6_hi, exp2_hi_mid: exp2_hi_mid_dd)) +
836 0.0f;
837 // return static_cast<float>(r);
838}
839
840} // namespace LIBC_NAMESPACE
841

source code of libc/src/math/generic/powf.cpp