1*03a88e3dSMark Murray /*-
2*03a88e3dSMark Murray * SPDX-License-Identifier: BSD-3-Clause
3*03a88e3dSMark Murray *
4*03a88e3dSMark Murray * Copyright (c) 1992, 1993
5*03a88e3dSMark Murray * The Regents of the University of California. All rights reserved.
6*03a88e3dSMark Murray *
7*03a88e3dSMark Murray * Redistribution and use in source and binary forms, with or without
8*03a88e3dSMark Murray * modification, are permitted provided that the following conditions
9*03a88e3dSMark Murray * are met:
10*03a88e3dSMark Murray * 1. Redistributions of source code must retain the above copyright
11*03a88e3dSMark Murray * notice, this list of conditions and the following disclaimer.
12*03a88e3dSMark Murray * 2. Redistributions in binary form must reproduce the above copyright
13*03a88e3dSMark Murray * notice, this list of conditions and the following disclaimer in the
14*03a88e3dSMark Murray * documentation and/or other materials provided with the distribution.
15*03a88e3dSMark Murray * 3. Neither the name of the University nor the names of its contributors
16*03a88e3dSMark Murray * may be used to endorse or promote products derived from this software
17*03a88e3dSMark Murray * without specific prior written permission.
18*03a88e3dSMark Murray *
19*03a88e3dSMark Murray * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20*03a88e3dSMark Murray * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21*03a88e3dSMark Murray * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22*03a88e3dSMark Murray * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23*03a88e3dSMark Murray * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24*03a88e3dSMark Murray * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25*03a88e3dSMark Murray * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26*03a88e3dSMark Murray * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27*03a88e3dSMark Murray * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28*03a88e3dSMark Murray * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29*03a88e3dSMark Murray * SUCH DAMAGE.
30*03a88e3dSMark Murray */
31*03a88e3dSMark Murray
32*03a88e3dSMark Murray /*
33*03a88e3dSMark Murray * See bsdsrc/b_log.c for implementation details.
34*03a88e3dSMark Murray *
35*03a88e3dSMark Murray * bsdrc/b_log.c converted to long double by Steven G. Kargl.
36*03a88e3dSMark Murray */
37*03a88e3dSMark Murray
38*03a88e3dSMark Murray #define N 128
39*03a88e3dSMark Murray
40*03a88e3dSMark Murray /*
41*03a88e3dSMark Murray * Coefficients in the polynomial approximation of log(1+f/F).
42*03a88e3dSMark Murray * Domain of x is [0,1./256] with 2**(-84.48) precision.
43*03a88e3dSMark Murray */
44*03a88e3dSMark Murray static const union IEEEl2bits
45*03a88e3dSMark Murray a1u = LD80C(0xaaaaaaaaaaaaaaab, -4, 8.33333333333333333356e-02L),
46*03a88e3dSMark Murray a2u = LD80C(0xcccccccccccccd29, -7, 1.25000000000000000781e-02L),
47*03a88e3dSMark Murray a3u = LD80C(0x9249249241ed3764, -9, 2.23214285711721994134e-03L),
48*03a88e3dSMark Murray a4u = LD80C(0xe38e959e1e7e01cf, -12, 4.34030476540000360640e-04L);
49*03a88e3dSMark Murray #define A1 (a1u.e)
50*03a88e3dSMark Murray #define A2 (a2u.e)
51*03a88e3dSMark Murray #define A3 (a3u.e)
52*03a88e3dSMark Murray #define A4 (a4u.e)
53*03a88e3dSMark Murray
54*03a88e3dSMark Murray /*
55*03a88e3dSMark Murray * Table of log(Fj) = logF_head[j] + logF_tail[j], for Fj = 1+j/128.
56*03a88e3dSMark Murray * Used for generation of extend precision logarithms.
57*03a88e3dSMark Murray * The constant 35184372088832 is 2^45, so the divide is exact.
58*03a88e3dSMark Murray * It ensures correct reading of logF_head, even for inaccurate
59*03a88e3dSMark Murray * decimal-to-binary conversion routines. (Everybody gets the
60*03a88e3dSMark Murray * right answer for integers less than 2^53.)
61*03a88e3dSMark Murray * Values for log(F) were generated using error < 10^-57 absolute
62*03a88e3dSMark Murray * with the bc -l package.
63*03a88e3dSMark Murray */
64*03a88e3dSMark Murray
65*03a88e3dSMark Murray static double logF_head[N+1] = {
66*03a88e3dSMark Murray 0.,
67*03a88e3dSMark Murray .007782140442060381246,
68*03a88e3dSMark Murray .015504186535963526694,
69*03a88e3dSMark Murray .023167059281547608406,
70*03a88e3dSMark Murray .030771658666765233647,
71*03a88e3dSMark Murray .038318864302141264488,
72*03a88e3dSMark Murray .045809536031242714670,
73*03a88e3dSMark Murray .053244514518837604555,
74*03a88e3dSMark Murray .060624621816486978786,
75*03a88e3dSMark Murray .067950661908525944454,
76*03a88e3dSMark Murray .075223421237524235039,
77*03a88e3dSMark Murray .082443669210988446138,
78*03a88e3dSMark Murray .089612158689760690322,
79*03a88e3dSMark Murray .096729626458454731618,
80*03a88e3dSMark Murray .103796793681567578460,
81*03a88e3dSMark Murray .110814366340264314203,
82*03a88e3dSMark Murray .117783035656430001836,
83*03a88e3dSMark Murray .124703478501032805070,
84*03a88e3dSMark Murray .131576357788617315236,
85*03a88e3dSMark Murray .138402322859292326029,
86*03a88e3dSMark Murray .145182009844575077295,
87*03a88e3dSMark Murray .151916042025732167530,
88*03a88e3dSMark Murray .158605030176659056451,
89*03a88e3dSMark Murray .165249572895390883786,
90*03a88e3dSMark Murray .171850256926518341060,
91*03a88e3dSMark Murray .178407657472689606947,
92*03a88e3dSMark Murray .184922338493834104156,
93*03a88e3dSMark Murray .191394852999565046047,
94*03a88e3dSMark Murray .197825743329758552135,
95*03a88e3dSMark Murray .204215541428766300668,
96*03a88e3dSMark Murray .210564769107350002741,
97*03a88e3dSMark Murray .216873938300523150246,
98*03a88e3dSMark Murray .223143551314024080056,
99*03a88e3dSMark Murray .229374101064877322642,
100*03a88e3dSMark Murray .235566071312860003672,
101*03a88e3dSMark Murray .241719936886966024758,
102*03a88e3dSMark Murray .247836163904594286577,
103*03a88e3dSMark Murray .253915209980732470285,
104*03a88e3dSMark Murray .259957524436686071567,
105*03a88e3dSMark Murray .265963548496984003577,
106*03a88e3dSMark Murray .271933715484010463114,
107*03a88e3dSMark Murray .277868451003087102435,
108*03a88e3dSMark Murray .283768173130738432519,
109*03a88e3dSMark Murray .289633292582948342896,
110*03a88e3dSMark Murray .295464212893421063199,
111*03a88e3dSMark Murray .301261330578199704177,
112*03a88e3dSMark Murray .307025035294827830512,
113*03a88e3dSMark Murray .312755710004239517729,
114*03a88e3dSMark Murray .318453731118097493890,
115*03a88e3dSMark Murray .324119468654316733591,
116*03a88e3dSMark Murray .329753286372579168528,
117*03a88e3dSMark Murray .335355541920762334484,
118*03a88e3dSMark Murray .340926586970454081892,
119*03a88e3dSMark Murray .346466767346100823488,
120*03a88e3dSMark Murray .351976423156884266063,
121*03a88e3dSMark Murray .357455888922231679316,
122*03a88e3dSMark Murray .362905493689140712376,
123*03a88e3dSMark Murray .368325561158599157352,
124*03a88e3dSMark Murray .373716409793814818840,
125*03a88e3dSMark Murray .379078352934811846353,
126*03a88e3dSMark Murray .384411698910298582632,
127*03a88e3dSMark Murray .389716751140440464951,
128*03a88e3dSMark Murray .394993808240542421117,
129*03a88e3dSMark Murray .400243164127459749579,
130*03a88e3dSMark Murray .405465108107819105498,
131*03a88e3dSMark Murray .410659924985338875558,
132*03a88e3dSMark Murray .415827895143593195825,
133*03a88e3dSMark Murray .420969294644237379543,
134*03a88e3dSMark Murray .426084395310681429691,
135*03a88e3dSMark Murray .431173464818130014464,
136*03a88e3dSMark Murray .436236766774527495726,
137*03a88e3dSMark Murray .441274560805140936281,
138*03a88e3dSMark Murray .446287102628048160113,
139*03a88e3dSMark Murray .451274644139630254358,
140*03a88e3dSMark Murray .456237433481874177232,
141*03a88e3dSMark Murray .461175715122408291790,
142*03a88e3dSMark Murray .466089729924533457960,
143*03a88e3dSMark Murray .470979715219073113985,
144*03a88e3dSMark Murray .475845904869856894947,
145*03a88e3dSMark Murray .480688529345570714212,
146*03a88e3dSMark Murray .485507815781602403149,
147*03a88e3dSMark Murray .490303988045525329653,
148*03a88e3dSMark Murray .495077266798034543171,
149*03a88e3dSMark Murray .499827869556611403822,
150*03a88e3dSMark Murray .504556010751912253908,
151*03a88e3dSMark Murray .509261901790523552335,
152*03a88e3dSMark Murray .513945751101346104405,
153*03a88e3dSMark Murray .518607764208354637958,
154*03a88e3dSMark Murray .523248143765158602036,
155*03a88e3dSMark Murray .527867089620485785417,
156*03a88e3dSMark Murray .532464798869114019908,
157*03a88e3dSMark Murray .537041465897345915436,
158*03a88e3dSMark Murray .541597282432121573947,
159*03a88e3dSMark Murray .546132437597407260909,
160*03a88e3dSMark Murray .550647117952394182793,
161*03a88e3dSMark Murray .555141507540611200965,
162*03a88e3dSMark Murray .559615787935399566777,
163*03a88e3dSMark Murray .564070138285387656651,
164*03a88e3dSMark Murray .568504735352689749561,
165*03a88e3dSMark Murray .572919753562018740922,
166*03a88e3dSMark Murray .577315365035246941260,
167*03a88e3dSMark Murray .581691739635061821900,
168*03a88e3dSMark Murray .586049045003164792433,
169*03a88e3dSMark Murray .590387446602107957005,
170*03a88e3dSMark Murray .594707107746216934174,
171*03a88e3dSMark Murray .599008189645246602594,
172*03a88e3dSMark Murray .603290851438941899687,
173*03a88e3dSMark Murray .607555250224322662688,
174*03a88e3dSMark Murray .611801541106615331955,
175*03a88e3dSMark Murray .616029877215623855590,
176*03a88e3dSMark Murray .620240409751204424537,
177*03a88e3dSMark Murray .624433288012369303032,
178*03a88e3dSMark Murray .628608659422752680256,
179*03a88e3dSMark Murray .632766669570628437213,
180*03a88e3dSMark Murray .636907462236194987781,
181*03a88e3dSMark Murray .641031179420679109171,
182*03a88e3dSMark Murray .645137961373620782978,
183*03a88e3dSMark Murray .649227946625615004450,
184*03a88e3dSMark Murray .653301272011958644725,
185*03a88e3dSMark Murray .657358072709030238911,
186*03a88e3dSMark Murray .661398482245203922502,
187*03a88e3dSMark Murray .665422632544505177065,
188*03a88e3dSMark Murray .669430653942981734871,
189*03a88e3dSMark Murray .673422675212350441142,
190*03a88e3dSMark Murray .677398823590920073911,
191*03a88e3dSMark Murray .681359224807238206267,
192*03a88e3dSMark Murray .685304003098281100392,
193*03a88e3dSMark Murray .689233281238557538017,
194*03a88e3dSMark Murray .693147180560117703862
195*03a88e3dSMark Murray };
196*03a88e3dSMark Murray
197*03a88e3dSMark Murray static double logF_tail[N+1] = {
198*03a88e3dSMark Murray 0.,
199*03a88e3dSMark Murray -.00000000000000543229938420049,
200*03a88e3dSMark Murray .00000000000000172745674997061,
201*03a88e3dSMark Murray -.00000000000001323017818229233,
202*03a88e3dSMark Murray -.00000000000001154527628289872,
203*03a88e3dSMark Murray -.00000000000000466529469958300,
204*03a88e3dSMark Murray .00000000000005148849572685810,
205*03a88e3dSMark Murray -.00000000000002532168943117445,
206*03a88e3dSMark Murray -.00000000000005213620639136504,
207*03a88e3dSMark Murray -.00000000000001819506003016881,
208*03a88e3dSMark Murray .00000000000006329065958724544,
209*03a88e3dSMark Murray .00000000000008614512936087814,
210*03a88e3dSMark Murray -.00000000000007355770219435028,
211*03a88e3dSMark Murray .00000000000009638067658552277,
212*03a88e3dSMark Murray .00000000000007598636597194141,
213*03a88e3dSMark Murray .00000000000002579999128306990,
214*03a88e3dSMark Murray -.00000000000004654729747598444,
215*03a88e3dSMark Murray -.00000000000007556920687451336,
216*03a88e3dSMark Murray .00000000000010195735223708472,
217*03a88e3dSMark Murray -.00000000000017319034406422306,
218*03a88e3dSMark Murray -.00000000000007718001336828098,
219*03a88e3dSMark Murray .00000000000010980754099855238,
220*03a88e3dSMark Murray -.00000000000002047235780046195,
221*03a88e3dSMark Murray -.00000000000008372091099235912,
222*03a88e3dSMark Murray .00000000000014088127937111135,
223*03a88e3dSMark Murray .00000000000012869017157588257,
224*03a88e3dSMark Murray .00000000000017788850778198106,
225*03a88e3dSMark Murray .00000000000006440856150696891,
226*03a88e3dSMark Murray .00000000000016132822667240822,
227*03a88e3dSMark Murray -.00000000000007540916511956188,
228*03a88e3dSMark Murray -.00000000000000036507188831790,
229*03a88e3dSMark Murray .00000000000009120937249914984,
230*03a88e3dSMark Murray .00000000000018567570959796010,
231*03a88e3dSMark Murray -.00000000000003149265065191483,
232*03a88e3dSMark Murray -.00000000000009309459495196889,
233*03a88e3dSMark Murray .00000000000017914338601329117,
234*03a88e3dSMark Murray -.00000000000001302979717330866,
235*03a88e3dSMark Murray .00000000000023097385217586939,
236*03a88e3dSMark Murray .00000000000023999540484211737,
237*03a88e3dSMark Murray .00000000000015393776174455408,
238*03a88e3dSMark Murray -.00000000000036870428315837678,
239*03a88e3dSMark Murray .00000000000036920375082080089,
240*03a88e3dSMark Murray -.00000000000009383417223663699,
241*03a88e3dSMark Murray .00000000000009433398189512690,
242*03a88e3dSMark Murray .00000000000041481318704258568,
243*03a88e3dSMark Murray -.00000000000003792316480209314,
244*03a88e3dSMark Murray .00000000000008403156304792424,
245*03a88e3dSMark Murray -.00000000000034262934348285429,
246*03a88e3dSMark Murray .00000000000043712191957429145,
247*03a88e3dSMark Murray -.00000000000010475750058776541,
248*03a88e3dSMark Murray -.00000000000011118671389559323,
249*03a88e3dSMark Murray .00000000000037549577257259853,
250*03a88e3dSMark Murray .00000000000013912841212197565,
251*03a88e3dSMark Murray .00000000000010775743037572640,
252*03a88e3dSMark Murray .00000000000029391859187648000,
253*03a88e3dSMark Murray -.00000000000042790509060060774,
254*03a88e3dSMark Murray .00000000000022774076114039555,
255*03a88e3dSMark Murray .00000000000010849569622967912,
256*03a88e3dSMark Murray -.00000000000023073801945705758,
257*03a88e3dSMark Murray .00000000000015761203773969435,
258*03a88e3dSMark Murray .00000000000003345710269544082,
259*03a88e3dSMark Murray -.00000000000041525158063436123,
260*03a88e3dSMark Murray .00000000000032655698896907146,
261*03a88e3dSMark Murray -.00000000000044704265010452446,
262*03a88e3dSMark Murray .00000000000034527647952039772,
263*03a88e3dSMark Murray -.00000000000007048962392109746,
264*03a88e3dSMark Murray .00000000000011776978751369214,
265*03a88e3dSMark Murray -.00000000000010774341461609578,
266*03a88e3dSMark Murray .00000000000021863343293215910,
267*03a88e3dSMark Murray .00000000000024132639491333131,
268*03a88e3dSMark Murray .00000000000039057462209830700,
269*03a88e3dSMark Murray -.00000000000026570679203560751,
270*03a88e3dSMark Murray .00000000000037135141919592021,
271*03a88e3dSMark Murray -.00000000000017166921336082431,
272*03a88e3dSMark Murray -.00000000000028658285157914353,
273*03a88e3dSMark Murray -.00000000000023812542263446809,
274*03a88e3dSMark Murray .00000000000006576659768580062,
275*03a88e3dSMark Murray -.00000000000028210143846181267,
276*03a88e3dSMark Murray .00000000000010701931762114254,
277*03a88e3dSMark Murray .00000000000018119346366441110,
278*03a88e3dSMark Murray .00000000000009840465278232627,
279*03a88e3dSMark Murray -.00000000000033149150282752542,
280*03a88e3dSMark Murray -.00000000000018302857356041668,
281*03a88e3dSMark Murray -.00000000000016207400156744949,
282*03a88e3dSMark Murray .00000000000048303314949553201,
283*03a88e3dSMark Murray -.00000000000071560553172382115,
284*03a88e3dSMark Murray .00000000000088821239518571855,
285*03a88e3dSMark Murray -.00000000000030900580513238244,
286*03a88e3dSMark Murray -.00000000000061076551972851496,
287*03a88e3dSMark Murray .00000000000035659969663347830,
288*03a88e3dSMark Murray .00000000000035782396591276383,
289*03a88e3dSMark Murray -.00000000000046226087001544578,
290*03a88e3dSMark Murray .00000000000062279762917225156,
291*03a88e3dSMark Murray .00000000000072838947272065741,
292*03a88e3dSMark Murray .00000000000026809646615211673,
293*03a88e3dSMark Murray -.00000000000010960825046059278,
294*03a88e3dSMark Murray .00000000000002311949383800537,
295*03a88e3dSMark Murray -.00000000000058469058005299247,
296*03a88e3dSMark Murray -.00000000000002103748251144494,
297*03a88e3dSMark Murray -.00000000000023323182945587408,
298*03a88e3dSMark Murray -.00000000000042333694288141916,
299*03a88e3dSMark Murray -.00000000000043933937969737844,
300*03a88e3dSMark Murray .00000000000041341647073835565,
301*03a88e3dSMark Murray .00000000000006841763641591466,
302*03a88e3dSMark Murray .00000000000047585534004430641,
303*03a88e3dSMark Murray .00000000000083679678674757695,
304*03a88e3dSMark Murray -.00000000000085763734646658640,
305*03a88e3dSMark Murray .00000000000021913281229340092,
306*03a88e3dSMark Murray -.00000000000062242842536431148,
307*03a88e3dSMark Murray -.00000000000010983594325438430,
308*03a88e3dSMark Murray .00000000000065310431377633651,
309*03a88e3dSMark Murray -.00000000000047580199021710769,
310*03a88e3dSMark Murray -.00000000000037854251265457040,
311*03a88e3dSMark Murray .00000000000040939233218678664,
312*03a88e3dSMark Murray .00000000000087424383914858291,
313*03a88e3dSMark Murray .00000000000025218188456842882,
314*03a88e3dSMark Murray -.00000000000003608131360422557,
315*03a88e3dSMark Murray -.00000000000050518555924280902,
316*03a88e3dSMark Murray .00000000000078699403323355317,
317*03a88e3dSMark Murray -.00000000000067020876961949060,
318*03a88e3dSMark Murray .00000000000016108575753932458,
319*03a88e3dSMark Murray .00000000000058527188436251509,
320*03a88e3dSMark Murray -.00000000000035246757297904791,
321*03a88e3dSMark Murray -.00000000000018372084495629058,
322*03a88e3dSMark Murray .00000000000088606689813494916,
323*03a88e3dSMark Murray .00000000000066486268071468700,
324*03a88e3dSMark Murray .00000000000063831615170646519,
325*03a88e3dSMark Murray .00000000000025144230728376072,
326*03a88e3dSMark Murray -.00000000000017239444525614834
327*03a88e3dSMark Murray };
328*03a88e3dSMark Murray /*
329*03a88e3dSMark Murray * Extra precision variant, returning struct {double a, b;};
330*03a88e3dSMark Murray * log(x) = a + b to 63 bits, with 'a' rounded to 24 bits.
331*03a88e3dSMark Murray */
332*03a88e3dSMark Murray static struct Double
__log__D(long double x)333*03a88e3dSMark Murray __log__D(long double x)
334*03a88e3dSMark Murray {
335*03a88e3dSMark Murray int m, j;
336*03a88e3dSMark Murray long double F, f, g, q, u, v, u1, u2;
337*03a88e3dSMark Murray struct Double r;
338*03a88e3dSMark Murray
339*03a88e3dSMark Murray /*
340*03a88e3dSMark Murray * Argument reduction: 1 <= g < 2; x/2^m = g;
341*03a88e3dSMark Murray * y = F*(1 + f/F) for |f| <= 2^-8
342*03a88e3dSMark Murray */
343*03a88e3dSMark Murray g = frexpl(x, &m);
344*03a88e3dSMark Murray g *= 2;
345*03a88e3dSMark Murray m--;
346*03a88e3dSMark Murray if (m == DBL_MIN_EXP - 1) {
347*03a88e3dSMark Murray j = ilogbl(g);
348*03a88e3dSMark Murray m += j;
349*03a88e3dSMark Murray g = ldexpl(g, -j);
350*03a88e3dSMark Murray }
351*03a88e3dSMark Murray j = N * (g - 1) + 0.5L;
352*03a88e3dSMark Murray F = (1.L / N) * j + 1;
353*03a88e3dSMark Murray f = g - F;
354*03a88e3dSMark Murray
355*03a88e3dSMark Murray g = 1 / (2 * F + f);
356*03a88e3dSMark Murray u = 2 * f * g;
357*03a88e3dSMark Murray v = u * u;
358*03a88e3dSMark Murray q = u * v * (A1 + v * (A2 + v * (A3 + v * A4)));
359*03a88e3dSMark Murray if (m | j) {
360*03a88e3dSMark Murray u1 = u + 513;
361*03a88e3dSMark Murray u1 -= 513;
362*03a88e3dSMark Murray } else {
363*03a88e3dSMark Murray u1 = (float)u;
364*03a88e3dSMark Murray }
365*03a88e3dSMark Murray u2 = (2 * (f - F * u1) - u1 * f) * g;
366*03a88e3dSMark Murray
367*03a88e3dSMark Murray u1 += m * (long double)logF_head[N] + logF_head[j];
368*03a88e3dSMark Murray
369*03a88e3dSMark Murray u2 += logF_tail[j];
370*03a88e3dSMark Murray u2 += q;
371*03a88e3dSMark Murray u2 += logF_tail[N] * m;
372*03a88e3dSMark Murray r.a = (float)(u1 + u2); /* Only difference is here. */
373*03a88e3dSMark Murray r.b = (u1 - r.a) + u2;
374*03a88e3dSMark Murray return (r);
375*03a88e3dSMark Murray }
376