xref: /openbsd-src/lib/libm/src/ld128/s_exp2l.c (revision be9b70502a319be1934f8cfd5be7f470be741ce6)
1*be9b7050Sguenther /*	$OpenBSD: s_exp2l.c,v 1.2 2014/07/21 01:51:11 guenther Exp $	*/
2390c8400Smartynas /*-
3390c8400Smartynas  * Copyright (c) 2005-2008 David Schultz <das@FreeBSD.ORG>
4390c8400Smartynas  * All rights reserved.
5390c8400Smartynas  *
6390c8400Smartynas  * Redistribution and use in source and binary forms, with or without
7390c8400Smartynas  * modification, are permitted provided that the following conditions
8390c8400Smartynas  * are met:
9390c8400Smartynas  * 1. Redistributions of source code must retain the above copyright
10390c8400Smartynas  *    notice, this list of conditions and the following disclaimer.
11390c8400Smartynas  * 2. Redistributions in binary form must reproduce the above copyright
12390c8400Smartynas  *    notice, this list of conditions and the following disclaimer in the
13390c8400Smartynas  *    documentation and/or other materials provided with the distribution.
14390c8400Smartynas  *
15390c8400Smartynas  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16390c8400Smartynas  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17390c8400Smartynas  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18390c8400Smartynas  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19390c8400Smartynas  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20390c8400Smartynas  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21390c8400Smartynas  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22390c8400Smartynas  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23390c8400Smartynas  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24390c8400Smartynas  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25390c8400Smartynas  * SUCH DAMAGE.
26390c8400Smartynas  */
27390c8400Smartynas 
28390c8400Smartynas #include <sys/types.h>
29390c8400Smartynas #include <machine/ieee.h>
30390c8400Smartynas #include <float.h>
31390c8400Smartynas #include <math.h>
32390c8400Smartynas #include <stdint.h>
33390c8400Smartynas 
34390c8400Smartynas #define	TBLBITS	7
35390c8400Smartynas #define	TBLSIZE	(1 << TBLBITS)
36390c8400Smartynas 
37390c8400Smartynas #define	BIAS	(LDBL_MAX_EXP - 1)
38390c8400Smartynas #define	EXPMASK	(BIAS + LDBL_MAX_EXP)
39390c8400Smartynas 
40390c8400Smartynas #if 0 /* XXX Prevent gcc from erroneously constant folding this. */
41390c8400Smartynas static const long double twom10000 = 0x1p-10000L;
42390c8400Smartynas #else
43390c8400Smartynas static volatile long double twom10000 = 0x1p-10000L;
44390c8400Smartynas #endif
45390c8400Smartynas 
46390c8400Smartynas static const long double
47390c8400Smartynas     huge      = 0x1p10000L,
48390c8400Smartynas     P1        = 0x1.62e42fefa39ef35793c7673007e6p-1L,
49390c8400Smartynas     P2	      = 0x1.ebfbdff82c58ea86f16b06ec9736p-3L,
50390c8400Smartynas     P3        = 0x1.c6b08d704a0bf8b33a762bad3459p-5L,
51390c8400Smartynas     P4        = 0x1.3b2ab6fba4e7729ccbbe0b4f3fc2p-7L,
52390c8400Smartynas     P5        = 0x1.5d87fe78a67311071dee13fd11d9p-10L,
53390c8400Smartynas     P6        = 0x1.430912f86c7876f4b663b23c5fe5p-13L;
54390c8400Smartynas 
55390c8400Smartynas static const double
56390c8400Smartynas     P7        = 0x1.ffcbfc588b041p-17,
57390c8400Smartynas     P8        = 0x1.62c0223a5c7c7p-20,
58390c8400Smartynas     P9        = 0x1.b52541ff59713p-24,
59390c8400Smartynas     P10       = 0x1.e4cf56a391e22p-28,
60390c8400Smartynas     redux     = 0x1.8p112 / TBLSIZE;
61390c8400Smartynas 
62390c8400Smartynas static const long double tbl[TBLSIZE] = {
63390c8400Smartynas 	0x1.6a09e667f3bcc908b2fb1366dfeap-1L,
64390c8400Smartynas 	0x1.6c012750bdabeed76a99800f4edep-1L,
65390c8400Smartynas 	0x1.6dfb23c651a2ef220e2cbe1bc0d4p-1L,
66390c8400Smartynas 	0x1.6ff7df9519483cf87e1b4f3e1e98p-1L,
67390c8400Smartynas 	0x1.71f75e8ec5f73dd2370f2ef0b148p-1L,
68390c8400Smartynas 	0x1.73f9a48a58173bd5c9a4e68ab074p-1L,
69390c8400Smartynas 	0x1.75feb564267c8bf6e9aa33a489a8p-1L,
70390c8400Smartynas 	0x1.780694fde5d3f619ae02808592a4p-1L,
71390c8400Smartynas 	0x1.7a11473eb0186d7d51023f6ccb1ap-1L,
72390c8400Smartynas 	0x1.7c1ed0130c1327c49334459378dep-1L,
73390c8400Smartynas 	0x1.7e2f336cf4e62105d02ba1579756p-1L,
74390c8400Smartynas 	0x1.80427543e1a11b60de67649a3842p-1L,
75390c8400Smartynas 	0x1.82589994cce128acf88afab34928p-1L,
76390c8400Smartynas 	0x1.8471a4623c7acce52f6b97c6444cp-1L,
77390c8400Smartynas 	0x1.868d99b4492ec80e41d90ac2556ap-1L,
78390c8400Smartynas 	0x1.88ac7d98a669966530bcdf2d4cc0p-1L,
79390c8400Smartynas 	0x1.8ace5422aa0db5ba7c55a192c648p-1L,
80390c8400Smartynas 	0x1.8cf3216b5448bef2aa1cd161c57ap-1L,
81390c8400Smartynas 	0x1.8f1ae991577362b982745c72eddap-1L,
82390c8400Smartynas 	0x1.9145b0b91ffc588a61b469f6b6a0p-1L,
83390c8400Smartynas 	0x1.93737b0cdc5e4f4501c3f2540ae8p-1L,
84390c8400Smartynas 	0x1.95a44cbc8520ee9b483695a0e7fep-1L,
85390c8400Smartynas 	0x1.97d829fde4e4f8b9e920f91e8eb6p-1L,
86390c8400Smartynas 	0x1.9a0f170ca07b9ba3109b8c467844p-1L,
87390c8400Smartynas 	0x1.9c49182a3f0901c7c46b071f28dep-1L,
88390c8400Smartynas 	0x1.9e86319e323231824ca78e64c462p-1L,
89390c8400Smartynas 	0x1.a0c667b5de564b29ada8b8cabbacp-1L,
90390c8400Smartynas 	0x1.a309bec4a2d3358c171f770db1f4p-1L,
91390c8400Smartynas 	0x1.a5503b23e255c8b424491caf88ccp-1L,
92390c8400Smartynas 	0x1.a799e1330b3586f2dfb2b158f31ep-1L,
93390c8400Smartynas 	0x1.a9e6b5579fdbf43eb243bdff53a2p-1L,
94390c8400Smartynas 	0x1.ac36bbfd3f379c0db966a3126988p-1L,
95390c8400Smartynas 	0x1.ae89f995ad3ad5e8734d17731c80p-1L,
96390c8400Smartynas 	0x1.b0e07298db66590842acdfc6fb4ep-1L,
97390c8400Smartynas 	0x1.b33a2b84f15faf6bfd0e7bd941b0p-1L,
98390c8400Smartynas 	0x1.b59728de559398e3881111648738p-1L,
99390c8400Smartynas 	0x1.b7f76f2fb5e46eaa7b081ab53ff6p-1L,
100390c8400Smartynas 	0x1.ba5b030a10649840cb3c6af5b74cp-1L,
101390c8400Smartynas 	0x1.bcc1e904bc1d2247ba0f45b3d06cp-1L,
102390c8400Smartynas 	0x1.bf2c25bd71e088408d7025190cd0p-1L,
103390c8400Smartynas 	0x1.c199bdd85529c2220cb12a0916bap-1L,
104390c8400Smartynas 	0x1.c40ab5fffd07a6d14df820f17deap-1L,
105390c8400Smartynas 	0x1.c67f12e57d14b4a2137fd20f2a26p-1L,
106390c8400Smartynas 	0x1.c8f6d9406e7b511acbc48805c3f6p-1L,
107390c8400Smartynas 	0x1.cb720dcef90691503cbd1e949d0ap-1L,
108390c8400Smartynas 	0x1.cdf0b555dc3f9c44f8958fac4f12p-1L,
109390c8400Smartynas 	0x1.d072d4a07897b8d0f22f21a13792p-1L,
110390c8400Smartynas 	0x1.d2f87080d89f18ade123989ea50ep-1L,
111390c8400Smartynas 	0x1.d5818dcfba48725da05aeb66dff8p-1L,
112390c8400Smartynas 	0x1.d80e316c98397bb84f9d048807a0p-1L,
113390c8400Smartynas 	0x1.da9e603db3285708c01a5b6d480cp-1L,
114390c8400Smartynas 	0x1.dd321f301b4604b695de3c0630c0p-1L,
115390c8400Smartynas 	0x1.dfc97337b9b5eb968cac39ed284cp-1L,
116390c8400Smartynas 	0x1.e264614f5a128a12761fa17adc74p-1L,
117390c8400Smartynas 	0x1.e502ee78b3ff6273d130153992d0p-1L,
118390c8400Smartynas 	0x1.e7a51fbc74c834b548b2832378a4p-1L,
119390c8400Smartynas 	0x1.ea4afa2a490d9858f73a18f5dab4p-1L,
120390c8400Smartynas 	0x1.ecf482d8e67f08db0312fb949d50p-1L,
121390c8400Smartynas 	0x1.efa1bee615a27771fd21a92dabb6p-1L,
122390c8400Smartynas 	0x1.f252b376bba974e8696fc3638f24p-1L,
123390c8400Smartynas 	0x1.f50765b6e4540674f84b762861a6p-1L,
124390c8400Smartynas 	0x1.f7bfdad9cbe138913b4bfe72bd78p-1L,
125390c8400Smartynas 	0x1.fa7c1819e90d82e90a7e74b26360p-1L,
126390c8400Smartynas 	0x1.fd3c22b8f71f10975ba4b32bd006p-1L,
127390c8400Smartynas 	0x1.0000000000000000000000000000p+0L,
128390c8400Smartynas 	0x1.0163da9fb33356d84a66ae336e98p+0L,
129390c8400Smartynas 	0x1.02c9a3e778060ee6f7caca4f7a18p+0L,
130390c8400Smartynas 	0x1.04315e86e7f84bd738f9a20da442p+0L,
131390c8400Smartynas 	0x1.059b0d31585743ae7c548eb68c6ap+0L,
132390c8400Smartynas 	0x1.0706b29ddf6ddc6dc403a9d87b1ep+0L,
133390c8400Smartynas 	0x1.0874518759bc808c35f25d942856p+0L,
134390c8400Smartynas 	0x1.09e3ecac6f3834521e060c584d5cp+0L,
135390c8400Smartynas 	0x1.0b5586cf9890f6298b92b7184200p+0L,
136390c8400Smartynas 	0x1.0cc922b7247f7407b705b893dbdep+0L,
137390c8400Smartynas 	0x1.0e3ec32d3d1a2020742e4f8af794p+0L,
138390c8400Smartynas 	0x1.0fb66affed31af232091dd8a169ep+0L,
139390c8400Smartynas 	0x1.11301d0125b50a4ebbf1aed9321cp+0L,
140390c8400Smartynas 	0x1.12abdc06c31cbfb92bad324d6f84p+0L,
141390c8400Smartynas 	0x1.1429aaea92ddfb34101943b2588ep+0L,
142390c8400Smartynas 	0x1.15a98c8a58e512480d573dd562aep+0L,
143390c8400Smartynas 	0x1.172b83c7d517adcdf7c8c50eb162p+0L,
144390c8400Smartynas 	0x1.18af9388c8de9bbbf70b9a3c269cp+0L,
145390c8400Smartynas 	0x1.1a35beb6fcb753cb698f692d2038p+0L,
146390c8400Smartynas 	0x1.1bbe084045cd39ab1e72b442810ep+0L,
147390c8400Smartynas 	0x1.1d4873168b9aa7805b8028990be8p+0L,
148390c8400Smartynas 	0x1.1ed5022fcd91cb8819ff61121fbep+0L,
149390c8400Smartynas 	0x1.2063b88628cd63b8eeb0295093f6p+0L,
150390c8400Smartynas 	0x1.21f49917ddc962552fd29294bc20p+0L,
151390c8400Smartynas 	0x1.2387a6e75623866c1fadb1c159c0p+0L,
152390c8400Smartynas 	0x1.251ce4fb2a63f3582ab7de9e9562p+0L,
153390c8400Smartynas 	0x1.26b4565e27cdd257a673281d3068p+0L,
154390c8400Smartynas 	0x1.284dfe1f5638096cf15cf03c9fa0p+0L,
155390c8400Smartynas 	0x1.29e9df51fdee12c25d15f5a25022p+0L,
156390c8400Smartynas 	0x1.2b87fd0dad98ffddea46538fca24p+0L,
157390c8400Smartynas 	0x1.2d285a6e4030b40091d536d0733ep+0L,
158390c8400Smartynas 	0x1.2ecafa93e2f5611ca0f45d5239a4p+0L,
159390c8400Smartynas 	0x1.306fe0a31b7152de8d5a463063bep+0L,
160390c8400Smartynas 	0x1.32170fc4cd8313539cf1c3009330p+0L,
161390c8400Smartynas 	0x1.33c08b26416ff4c9c8610d96680ep+0L,
162390c8400Smartynas 	0x1.356c55f929ff0c94623476373be4p+0L,
163390c8400Smartynas 	0x1.371a7373aa9caa7145502f45452ap+0L,
164390c8400Smartynas 	0x1.38cae6d05d86585a9cb0d9bed530p+0L,
165390c8400Smartynas 	0x1.3a7db34e59ff6ea1bc9299e0a1fep+0L,
166390c8400Smartynas 	0x1.3c32dc313a8e484001f228b58cf0p+0L,
167390c8400Smartynas 	0x1.3dea64c12342235b41223e13d7eep+0L,
168390c8400Smartynas 	0x1.3fa4504ac801ba0bf701aa417b9cp+0L,
169390c8400Smartynas 	0x1.4160a21f72e29f84325b8f3dbacap+0L,
170390c8400Smartynas 	0x1.431f5d950a896dc704439410b628p+0L,
171390c8400Smartynas 	0x1.44e086061892d03136f409df0724p+0L,
172390c8400Smartynas 	0x1.46a41ed1d005772512f459229f0ap+0L,
173390c8400Smartynas 	0x1.486a2b5c13cd013c1a3b69062f26p+0L,
174390c8400Smartynas 	0x1.4a32af0d7d3de672d8bcf46f99b4p+0L,
175390c8400Smartynas 	0x1.4bfdad5362a271d4397afec42e36p+0L,
176390c8400Smartynas 	0x1.4dcb299fddd0d63b36ef1a9e19dep+0L,
177390c8400Smartynas 	0x1.4f9b2769d2ca6ad33d8b69aa0b8cp+0L,
178390c8400Smartynas 	0x1.516daa2cf6641c112f52c84d6066p+0L,
179390c8400Smartynas 	0x1.5342b569d4f81df0a83c49d86bf4p+0L,
180390c8400Smartynas 	0x1.551a4ca5d920ec52ec620243540cp+0L,
181390c8400Smartynas 	0x1.56f4736b527da66ecb004764e61ep+0L,
182390c8400Smartynas 	0x1.58d12d497c7fd252bc2b7343d554p+0L,
183390c8400Smartynas 	0x1.5ab07dd48542958c93015191e9a8p+0L,
184390c8400Smartynas 	0x1.5c9268a5946b701c4b1b81697ed4p+0L,
185390c8400Smartynas 	0x1.5e76f15ad21486e9be4c20399d12p+0L,
186390c8400Smartynas 	0x1.605e1b976dc08b076f592a487066p+0L,
187390c8400Smartynas 	0x1.6247eb03a5584b1f0fa06fd2d9eap+0L,
188390c8400Smartynas 	0x1.6434634ccc31fc76f8714c4ee122p+0L,
189390c8400Smartynas 	0x1.66238825522249127d9e29b92ea2p+0L,
190390c8400Smartynas 	0x1.68155d44ca973081c57227b9f69ep+0L,
191390c8400Smartynas };
192390c8400Smartynas 
193390c8400Smartynas static const float eps[TBLSIZE] = {
194390c8400Smartynas 	-0x1.5c50p-101,
195390c8400Smartynas 	-0x1.5d00p-106,
196390c8400Smartynas 	 0x1.8e90p-102,
197390c8400Smartynas 	-0x1.5340p-103,
198390c8400Smartynas 	 0x1.1bd0p-102,
199390c8400Smartynas 	-0x1.4600p-105,
200390c8400Smartynas 	-0x1.7a40p-104,
201390c8400Smartynas 	 0x1.d590p-102,
202390c8400Smartynas 	-0x1.d590p-101,
203390c8400Smartynas 	 0x1.b100p-103,
204390c8400Smartynas 	-0x1.0d80p-105,
205390c8400Smartynas 	 0x1.6b00p-103,
206390c8400Smartynas 	-0x1.9f00p-105,
207390c8400Smartynas 	 0x1.c400p-103,
208390c8400Smartynas 	 0x1.e120p-103,
209390c8400Smartynas 	-0x1.c100p-104,
210390c8400Smartynas 	-0x1.9d20p-103,
211390c8400Smartynas 	 0x1.a800p-108,
212390c8400Smartynas 	 0x1.4c00p-106,
213390c8400Smartynas 	-0x1.9500p-106,
214390c8400Smartynas 	 0x1.6900p-105,
215390c8400Smartynas 	-0x1.29d0p-100,
216390c8400Smartynas 	 0x1.4c60p-103,
217390c8400Smartynas 	 0x1.13a0p-102,
218390c8400Smartynas 	-0x1.5b60p-103,
219390c8400Smartynas 	-0x1.1c40p-103,
220390c8400Smartynas 	 0x1.db80p-102,
221390c8400Smartynas 	 0x1.91a0p-102,
222390c8400Smartynas 	 0x1.dc00p-105,
223390c8400Smartynas 	 0x1.44c0p-104,
224390c8400Smartynas 	 0x1.9710p-102,
225390c8400Smartynas 	 0x1.8760p-103,
226390c8400Smartynas 	-0x1.a720p-103,
227390c8400Smartynas 	 0x1.ed20p-103,
228390c8400Smartynas 	-0x1.49c0p-102,
229390c8400Smartynas 	-0x1.e000p-111,
230390c8400Smartynas 	 0x1.86a0p-103,
231390c8400Smartynas 	 0x1.2b40p-103,
232390c8400Smartynas 	-0x1.b400p-108,
233390c8400Smartynas 	 0x1.1280p-99,
234390c8400Smartynas 	-0x1.02d8p-102,
235390c8400Smartynas 	-0x1.e3d0p-103,
236390c8400Smartynas 	-0x1.b080p-105,
237390c8400Smartynas 	-0x1.f100p-107,
238390c8400Smartynas 	-0x1.16c0p-105,
239390c8400Smartynas 	-0x1.1190p-103,
240390c8400Smartynas 	-0x1.a7d2p-100,
241390c8400Smartynas 	 0x1.3450p-103,
242390c8400Smartynas 	-0x1.67c0p-105,
243390c8400Smartynas 	 0x1.4b80p-104,
244390c8400Smartynas 	-0x1.c4e0p-103,
245390c8400Smartynas 	 0x1.6000p-108,
246390c8400Smartynas 	-0x1.3f60p-105,
247390c8400Smartynas 	 0x1.93f0p-104,
248390c8400Smartynas 	 0x1.5fe0p-105,
249390c8400Smartynas 	 0x1.6f80p-107,
250390c8400Smartynas 	-0x1.7600p-106,
251390c8400Smartynas 	 0x1.21e0p-106,
252390c8400Smartynas 	-0x1.3a40p-106,
253390c8400Smartynas 	-0x1.40c0p-104,
254390c8400Smartynas 	-0x1.9860p-105,
255390c8400Smartynas 	-0x1.5d40p-108,
256390c8400Smartynas 	-0x1.1d70p-106,
257390c8400Smartynas 	 0x1.2760p-105,
258390c8400Smartynas 	 0x0.0000p+0,
259390c8400Smartynas 	 0x1.21e2p-104,
260390c8400Smartynas 	-0x1.9520p-108,
261390c8400Smartynas 	-0x1.5720p-106,
262390c8400Smartynas 	-0x1.4810p-106,
263390c8400Smartynas 	-0x1.be00p-109,
264390c8400Smartynas 	 0x1.0080p-105,
265390c8400Smartynas 	-0x1.5780p-108,
266390c8400Smartynas 	-0x1.d460p-105,
267390c8400Smartynas 	-0x1.6140p-105,
268390c8400Smartynas 	 0x1.4630p-104,
269390c8400Smartynas 	 0x1.ad50p-103,
270390c8400Smartynas 	 0x1.82e0p-105,
271390c8400Smartynas 	 0x1.1d3cp-101,
272390c8400Smartynas 	 0x1.6100p-107,
273390c8400Smartynas 	 0x1.ec30p-104,
274390c8400Smartynas 	 0x1.f200p-108,
275390c8400Smartynas 	 0x1.0b40p-103,
276390c8400Smartynas 	 0x1.3660p-102,
277390c8400Smartynas 	 0x1.d9d0p-103,
278390c8400Smartynas 	-0x1.02d0p-102,
279390c8400Smartynas 	 0x1.b070p-103,
280390c8400Smartynas 	 0x1.b9c0p-104,
281390c8400Smartynas 	-0x1.01c0p-103,
282390c8400Smartynas 	-0x1.dfe0p-103,
283390c8400Smartynas 	 0x1.1b60p-104,
284390c8400Smartynas 	-0x1.ae94p-101,
285390c8400Smartynas 	-0x1.3340p-104,
286390c8400Smartynas 	 0x1.b3d8p-102,
287390c8400Smartynas 	-0x1.6e40p-105,
288390c8400Smartynas 	-0x1.3670p-103,
289390c8400Smartynas 	 0x1.c140p-104,
290390c8400Smartynas 	 0x1.1840p-101,
291390c8400Smartynas 	 0x1.1ab0p-102,
292390c8400Smartynas 	-0x1.a400p-104,
293390c8400Smartynas 	 0x1.1f00p-104,
294390c8400Smartynas 	-0x1.7180p-103,
295390c8400Smartynas 	 0x1.4ce0p-102,
296390c8400Smartynas 	 0x1.9200p-107,
297390c8400Smartynas 	-0x1.54c0p-103,
298390c8400Smartynas 	 0x1.1b80p-105,
299390c8400Smartynas 	-0x1.1828p-101,
300390c8400Smartynas 	 0x1.5720p-102,
301390c8400Smartynas 	-0x1.a060p-100,
302390c8400Smartynas 	 0x1.9160p-102,
303390c8400Smartynas 	 0x1.a280p-104,
304390c8400Smartynas 	 0x1.3400p-107,
305390c8400Smartynas 	 0x1.2b20p-102,
306390c8400Smartynas 	 0x1.7800p-108,
307390c8400Smartynas 	 0x1.cfd0p-101,
308390c8400Smartynas 	 0x1.2ef0p-102,
309390c8400Smartynas 	-0x1.2760p-99,
310390c8400Smartynas 	 0x1.b380p-104,
311390c8400Smartynas 	 0x1.0048p-101,
312390c8400Smartynas 	-0x1.60b0p-102,
313390c8400Smartynas 	 0x1.a1ccp-100,
314390c8400Smartynas 	-0x1.a640p-104,
315390c8400Smartynas 	-0x1.08a0p-101,
316390c8400Smartynas 	 0x1.7e60p-102,
317390c8400Smartynas 	 0x1.22c0p-103,
318390c8400Smartynas 	-0x1.7200p-106,
319390c8400Smartynas 	 0x1.f0f0p-102,
320390c8400Smartynas 	 0x1.eb4ep-99,
321390c8400Smartynas 	 0x1.c6e0p-103,
322390c8400Smartynas };
323390c8400Smartynas 
324390c8400Smartynas /*
325390c8400Smartynas  * exp2l(x): compute the base 2 exponential of x
326390c8400Smartynas  *
327390c8400Smartynas  * Accuracy: Peak error < 0.502 ulp.
328390c8400Smartynas  *
329390c8400Smartynas  * Method: (accurate tables)
330390c8400Smartynas  *
331390c8400Smartynas  *   Reduce x:
332390c8400Smartynas  *     x = 2**k + y, for integer k and |y| <= 1/2.
333390c8400Smartynas  *     Thus we have exp2(x) = 2**k * exp2(y).
334390c8400Smartynas  *
335390c8400Smartynas  *   Reduce y:
336390c8400Smartynas  *     y = i/TBLSIZE + z - eps[i] for integer i near y * TBLSIZE.
337390c8400Smartynas  *     Thus we have exp2(y) = exp2(i/TBLSIZE) * exp2(z - eps[i]),
338390c8400Smartynas  *     with |z - eps[i]| <= 2**-8 + 2**-98 for the table used.
339390c8400Smartynas  *
340390c8400Smartynas  *   We compute exp2(i/TBLSIZE) via table lookup and exp2(z - eps[i]) via
341390c8400Smartynas  *   a degree-10 minimax polynomial with maximum error under 2**-120.
342390c8400Smartynas  *   The values in exp2t[] and eps[] are chosen such that
343390c8400Smartynas  *   exp2t[i] = exp2(i/TBLSIZE + eps[i]), and eps[i] is a small offset such
344390c8400Smartynas  *   that exp2t[i] is accurate to 2**-122.
345390c8400Smartynas  *
346390c8400Smartynas  *   Note that the range of i is +-TBLSIZE/2, so we actually index the tables
347390c8400Smartynas  *   by i0 = i + TBLSIZE/2.
348390c8400Smartynas  *
349390c8400Smartynas  *   This method is due to Gal, with many details due to Gal and Bachelis:
350390c8400Smartynas  *
351390c8400Smartynas  *	Gal, S. and Bachelis, B.  An Accurate Elementary Mathematical Library
352390c8400Smartynas  *	for the IEEE Floating Point Standard.  TOMS 17(1), 26-46 (1991).
353390c8400Smartynas  */
354390c8400Smartynas long double
exp2l(long double x)355390c8400Smartynas exp2l(long double x)
356390c8400Smartynas {
357390c8400Smartynas 	union {
358390c8400Smartynas 		long double e;
359390c8400Smartynas 		struct ieee_ext bits;
360390c8400Smartynas 	} u, v;
361390c8400Smartynas 	long double r, t, twopk, twopkp10000, z;
362390c8400Smartynas 	uint32_t es, hx, ix, i0;
363390c8400Smartynas 	int k;
364390c8400Smartynas 
365390c8400Smartynas 	u.e = x;
366390c8400Smartynas 
367390c8400Smartynas 	/* Filter out exceptional cases. */
368390c8400Smartynas 	hx = (u.bits.ext_sign << 15) | u.bits.ext_exp;
369390c8400Smartynas 	ix = hx & EXPMASK;
370390c8400Smartynas 	if (ix >= BIAS + 14) {		/* |x| >= 16384 */
371390c8400Smartynas 		if (ix == BIAS + LDBL_MAX_EXP) {
372390c8400Smartynas 			if (u.bits.ext_frach != 0
373390c8400Smartynas 			    || u.bits.ext_frachm != 0
374390c8400Smartynas 			    || u.bits.ext_fraclm != 0
375390c8400Smartynas 			    || u.bits.ext_fracl != 0
376390c8400Smartynas 			    || (hx & 0x8000) == 0)
377390c8400Smartynas 				return (x + x);	/* x is NaN or +Inf */
378390c8400Smartynas 			else
379390c8400Smartynas 				return (0.0);	/* x is -Inf */
380390c8400Smartynas 		}
381390c8400Smartynas 		if (x >= 16384)
382390c8400Smartynas 			return (huge * huge); /* overflow */
383390c8400Smartynas 		if (x <= -16495)
384390c8400Smartynas 			return (twom10000 * twom10000); /* underflow */
385390c8400Smartynas 	} else if (ix <= BIAS - 115) {		/* |x| < 0x1p-115 */
386390c8400Smartynas 		return (1.0 + x);
387390c8400Smartynas 	}
388390c8400Smartynas 
389390c8400Smartynas 	/*
390390c8400Smartynas 	 * Reduce x, computing z, i0, and k. The low bits of x + redux
391390c8400Smartynas 	 * contain the 16-bit integer part of the exponent (k) followed by
392390c8400Smartynas 	 * TBLBITS fractional bits (i0). We use bit tricks to extract these
393390c8400Smartynas 	 * as integers, then set z to the remainder.
394390c8400Smartynas 	 *
395390c8400Smartynas 	 * Example: Suppose x is 0xabc.123456p0 and TBLBITS is 8.
396390c8400Smartynas 	 * Then the low-order word of x + redux is 0x000abc12,
397390c8400Smartynas 	 * We split this into k = 0xabc and i0 = 0x12 (adjusted to
398390c8400Smartynas 	 * index into the table), then we compute z = 0x0.003456p0.
399390c8400Smartynas 	 *
400390c8400Smartynas 	 * XXX If the exponent is negative, the computation of k depends on
401390c8400Smartynas 	 *     '>>' doing sign extension.
402390c8400Smartynas 	 */
403390c8400Smartynas 	u.e = x + redux;
404390c8400Smartynas 	i0 = ((((uint64_t)u.bits.ext_fraclm << EXT_FRACLBITS)
405390c8400Smartynas 		| u.bits.ext_fracl) & 0xffffffff) + TBLSIZE / 2;
406390c8400Smartynas 	k = (int)i0 >> TBLBITS;
407390c8400Smartynas 	i0 = i0 & (TBLSIZE - 1);
408390c8400Smartynas 	u.e -= redux;
409390c8400Smartynas 	z = x - u.e;
410390c8400Smartynas 	v.bits.ext_frach = 0;
411390c8400Smartynas 	v.bits.ext_frachm = 0;
412390c8400Smartynas 	v.bits.ext_fraclm = 0;
413390c8400Smartynas 	v.bits.ext_fracl = 0;
414390c8400Smartynas 	if (k >= LDBL_MIN_EXP) {
415390c8400Smartynas 		es = LDBL_MAX_EXP - 1 + k;
416390c8400Smartynas 		v.bits.ext_exp = es & 0x7fffffff;
417390c8400Smartynas 		v.bits.ext_sign = u.bits.ext_sign >> 15;
418390c8400Smartynas 		twopk = v.e;
419390c8400Smartynas 	} else {
420390c8400Smartynas 		es = LDBL_MAX_EXP - 1 + k + 10000;
421390c8400Smartynas 		v.bits.ext_exp = es & 0x7fffffff;
422390c8400Smartynas 		v.bits.ext_sign = u.bits.ext_sign >> 15;
423390c8400Smartynas 		twopkp10000 = v.e;
424390c8400Smartynas 	}
425390c8400Smartynas 
426390c8400Smartynas 	/* Compute r = exp2(y) = exp2t[i0] * p(z - eps[i]). */
427390c8400Smartynas 	t = tbl[i0];		/* exp2t[i0] */
428390c8400Smartynas 	z -= eps[i0];		/* eps[i0]   */
429390c8400Smartynas 	r = t + t * z * (P1 + z * (P2 + z * (P3 + z * (P4 + z * (P5 + z * (P6
430390c8400Smartynas 	    + z * (P7 + z * (P8 + z * (P9 + z * P10)))))))));
431390c8400Smartynas 
432390c8400Smartynas 	/* Scale by 2**k. */
433390c8400Smartynas 	if(k >= LDBL_MIN_EXP) {
434390c8400Smartynas 		if (k == LDBL_MAX_EXP)
435390c8400Smartynas 			return (r * 2.0 * 0x1p16383L);
436390c8400Smartynas 		return (r * twopk);
437390c8400Smartynas 	} else {
438390c8400Smartynas 		return (r * twopkp10000 * twom10000);
439390c8400Smartynas 	}
440390c8400Smartynas }
441