1*05a0b428SJohn Marino /* @(#)e_sqrt.c 5.1 93/09/24 */
2*05a0b428SJohn Marino /*
3*05a0b428SJohn Marino * ====================================================
4*05a0b428SJohn Marino * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
5*05a0b428SJohn Marino *
6*05a0b428SJohn Marino * Developed at SunPro, a Sun Microsystems, Inc. business.
7*05a0b428SJohn Marino * Permission to use, copy, modify, and distribute this
8*05a0b428SJohn Marino * software is freely granted, provided that this notice
9*05a0b428SJohn Marino * is preserved.
10*05a0b428SJohn Marino * ====================================================
11*05a0b428SJohn Marino */
12*05a0b428SJohn Marino
13*05a0b428SJohn Marino /* sqrt(x)
14*05a0b428SJohn Marino * Return correctly rounded sqrt.
15*05a0b428SJohn Marino * ------------------------------------------
16*05a0b428SJohn Marino * | Use the hardware sqrt if you have one |
17*05a0b428SJohn Marino * ------------------------------------------
18*05a0b428SJohn Marino * Method:
19*05a0b428SJohn Marino * Bit by bit method using integer arithmetic. (Slow, but portable)
20*05a0b428SJohn Marino * 1. Normalization
21*05a0b428SJohn Marino * Scale x to y in [1,4) with even powers of 2:
22*05a0b428SJohn Marino * find an integer k such that 1 <= (y=x*2^(2k)) < 4, then
23*05a0b428SJohn Marino * sqrt(x) = 2^k * sqrt(y)
24*05a0b428SJohn Marino * 2. Bit by bit computation
25*05a0b428SJohn Marino * Let q = sqrt(y) truncated to i bit after binary point (q = 1),
26*05a0b428SJohn Marino * i 0
27*05a0b428SJohn Marino * i+1 2
28*05a0b428SJohn Marino * s = 2*q , and y = 2 * ( y - q ). (1)
29*05a0b428SJohn Marino * i i i i
30*05a0b428SJohn Marino *
31*05a0b428SJohn Marino * To compute q from q , one checks whether
32*05a0b428SJohn Marino * i+1 i
33*05a0b428SJohn Marino *
34*05a0b428SJohn Marino * -(i+1) 2
35*05a0b428SJohn Marino * (q + 2 ) <= y. (2)
36*05a0b428SJohn Marino * i
37*05a0b428SJohn Marino * -(i+1)
38*05a0b428SJohn Marino * If (2) is false, then q = q ; otherwise q = q + 2 .
39*05a0b428SJohn Marino * i+1 i i+1 i
40*05a0b428SJohn Marino *
41*05a0b428SJohn Marino * With some algebric manipulation, it is not difficult to see
42*05a0b428SJohn Marino * that (2) is equivalent to
43*05a0b428SJohn Marino * -(i+1)
44*05a0b428SJohn Marino * s + 2 <= y (3)
45*05a0b428SJohn Marino * i i
46*05a0b428SJohn Marino *
47*05a0b428SJohn Marino * The advantage of (3) is that s and y can be computed by
48*05a0b428SJohn Marino * i i
49*05a0b428SJohn Marino * the following recurrence formula:
50*05a0b428SJohn Marino * if (3) is false
51*05a0b428SJohn Marino *
52*05a0b428SJohn Marino * s = s , y = y ; (4)
53*05a0b428SJohn Marino * i+1 i i+1 i
54*05a0b428SJohn Marino *
55*05a0b428SJohn Marino * otherwise,
56*05a0b428SJohn Marino * -i -(i+1)
57*05a0b428SJohn Marino * s = s + 2 , y = y - s - 2 (5)
58*05a0b428SJohn Marino * i+1 i i+1 i i
59*05a0b428SJohn Marino *
60*05a0b428SJohn Marino * One may easily use induction to prove (4) and (5).
61*05a0b428SJohn Marino * Note. Since the left hand side of (3) contain only i+2 bits,
62*05a0b428SJohn Marino * it does not necessary to do a full (53-bit) comparison
63*05a0b428SJohn Marino * in (3).
64*05a0b428SJohn Marino * 3. Final rounding
65*05a0b428SJohn Marino * After generating the 53 bits result, we compute one more bit.
66*05a0b428SJohn Marino * Together with the remainder, we can decide whether the
67*05a0b428SJohn Marino * result is exact, bigger than 1/2ulp, or less than 1/2ulp
68*05a0b428SJohn Marino * (it will never equal to 1/2ulp).
69*05a0b428SJohn Marino * The rounding mode can be detected by checking whether
70*05a0b428SJohn Marino * huge + tiny is equal to huge, and whether huge - tiny is
71*05a0b428SJohn Marino * equal to huge for some floating point number "huge" and "tiny".
72*05a0b428SJohn Marino *
73*05a0b428SJohn Marino * Special cases:
74*05a0b428SJohn Marino * sqrt(+-0) = +-0 ... exact
75*05a0b428SJohn Marino * sqrt(inf) = inf
76*05a0b428SJohn Marino * sqrt(-ve) = NaN ... with invalid signal
77*05a0b428SJohn Marino * sqrt(NaN) = NaN ... with invalid signal for signaling NaN
78*05a0b428SJohn Marino *
79*05a0b428SJohn Marino * Other methods : see the appended file at the end of the program below.
80*05a0b428SJohn Marino *---------------
81*05a0b428SJohn Marino */
82*05a0b428SJohn Marino
83*05a0b428SJohn Marino #include <float.h>
84*05a0b428SJohn Marino #include <math.h>
85*05a0b428SJohn Marino
86*05a0b428SJohn Marino #include "math_private.h"
87*05a0b428SJohn Marino
88*05a0b428SJohn Marino static const double one = 1.0, tiny=1.0e-300;
89*05a0b428SJohn Marino
90*05a0b428SJohn Marino double
sqrt(double x)91*05a0b428SJohn Marino sqrt(double x)
92*05a0b428SJohn Marino {
93*05a0b428SJohn Marino double z;
94*05a0b428SJohn Marino int32_t sign = (int)0x80000000;
95*05a0b428SJohn Marino int32_t ix0,s0,q,m,t,i;
96*05a0b428SJohn Marino u_int32_t r,t1,s1,ix1,q1;
97*05a0b428SJohn Marino
98*05a0b428SJohn Marino EXTRACT_WORDS(ix0,ix1,x);
99*05a0b428SJohn Marino
100*05a0b428SJohn Marino /* take care of Inf and NaN */
101*05a0b428SJohn Marino if((ix0&0x7ff00000)==0x7ff00000) {
102*05a0b428SJohn Marino return x*x+x; /* sqrt(NaN)=NaN, sqrt(+inf)=+inf
103*05a0b428SJohn Marino sqrt(-inf)=sNaN */
104*05a0b428SJohn Marino }
105*05a0b428SJohn Marino /* take care of zero */
106*05a0b428SJohn Marino if(ix0<=0) {
107*05a0b428SJohn Marino if(((ix0&(~sign))|ix1)==0) return x;/* sqrt(+-0) = +-0 */
108*05a0b428SJohn Marino else if(ix0<0)
109*05a0b428SJohn Marino return (x-x)/(x-x); /* sqrt(-ve) = sNaN */
110*05a0b428SJohn Marino }
111*05a0b428SJohn Marino /* normalize x */
112*05a0b428SJohn Marino m = (ix0>>20);
113*05a0b428SJohn Marino if(m==0) { /* subnormal x */
114*05a0b428SJohn Marino while(ix0==0) {
115*05a0b428SJohn Marino m -= 21;
116*05a0b428SJohn Marino ix0 |= (ix1>>11); ix1 <<= 21;
117*05a0b428SJohn Marino }
118*05a0b428SJohn Marino for(i=0;(ix0&0x00100000)==0;i++) ix0<<=1;
119*05a0b428SJohn Marino m -= i-1;
120*05a0b428SJohn Marino ix0 |= (ix1>>(32-i));
121*05a0b428SJohn Marino ix1 <<= i;
122*05a0b428SJohn Marino }
123*05a0b428SJohn Marino m -= 1023; /* unbias exponent */
124*05a0b428SJohn Marino ix0 = (ix0&0x000fffff)|0x00100000;
125*05a0b428SJohn Marino if(m&1){ /* odd m, double x to make it even */
126*05a0b428SJohn Marino ix0 += ix0 + ((ix1&sign)>>31);
127*05a0b428SJohn Marino ix1 += ix1;
128*05a0b428SJohn Marino }
129*05a0b428SJohn Marino m >>= 1; /* m = [m/2] */
130*05a0b428SJohn Marino
131*05a0b428SJohn Marino /* generate sqrt(x) bit by bit */
132*05a0b428SJohn Marino ix0 += ix0 + ((ix1&sign)>>31);
133*05a0b428SJohn Marino ix1 += ix1;
134*05a0b428SJohn Marino q = q1 = s0 = s1 = 0; /* [q,q1] = sqrt(x) */
135*05a0b428SJohn Marino r = 0x00200000; /* r = moving bit from right to left */
136*05a0b428SJohn Marino
137*05a0b428SJohn Marino while(r!=0) {
138*05a0b428SJohn Marino t = s0+r;
139*05a0b428SJohn Marino if(t<=ix0) {
140*05a0b428SJohn Marino s0 = t+r;
141*05a0b428SJohn Marino ix0 -= t;
142*05a0b428SJohn Marino q += r;
143*05a0b428SJohn Marino }
144*05a0b428SJohn Marino ix0 += ix0 + ((ix1&sign)>>31);
145*05a0b428SJohn Marino ix1 += ix1;
146*05a0b428SJohn Marino r>>=1;
147*05a0b428SJohn Marino }
148*05a0b428SJohn Marino
149*05a0b428SJohn Marino r = sign;
150*05a0b428SJohn Marino while(r!=0) {
151*05a0b428SJohn Marino t1 = s1+r;
152*05a0b428SJohn Marino t = s0;
153*05a0b428SJohn Marino if((t<ix0)||((t==ix0)&&(t1<=ix1))) {
154*05a0b428SJohn Marino s1 = t1+r;
155*05a0b428SJohn Marino if(((t1&sign)==sign)&&(s1&sign)==0) s0 += 1;
156*05a0b428SJohn Marino ix0 -= t;
157*05a0b428SJohn Marino if (ix1 < t1) ix0 -= 1;
158*05a0b428SJohn Marino ix1 -= t1;
159*05a0b428SJohn Marino q1 += r;
160*05a0b428SJohn Marino }
161*05a0b428SJohn Marino ix0 += ix0 + ((ix1&sign)>>31);
162*05a0b428SJohn Marino ix1 += ix1;
163*05a0b428SJohn Marino r>>=1;
164*05a0b428SJohn Marino }
165*05a0b428SJohn Marino
166*05a0b428SJohn Marino /* use floating add to find out rounding direction */
167*05a0b428SJohn Marino if((ix0|ix1)!=0) {
168*05a0b428SJohn Marino z = one-tiny; /* trigger inexact flag */
169*05a0b428SJohn Marino if (z>=one) {
170*05a0b428SJohn Marino z = one+tiny;
171*05a0b428SJohn Marino if (q1==(u_int32_t)0xffffffff) { q1=0; q += 1;}
172*05a0b428SJohn Marino else if (z>one) {
173*05a0b428SJohn Marino if (q1==(u_int32_t)0xfffffffe) q+=1;
174*05a0b428SJohn Marino q1+=2;
175*05a0b428SJohn Marino } else
176*05a0b428SJohn Marino q1 += (q1&1);
177*05a0b428SJohn Marino }
178*05a0b428SJohn Marino }
179*05a0b428SJohn Marino ix0 = (q>>1)+0x3fe00000;
180*05a0b428SJohn Marino ix1 = q1>>1;
181*05a0b428SJohn Marino if ((q&1)==1) ix1 |= sign;
182*05a0b428SJohn Marino ix0 += (m <<20);
183*05a0b428SJohn Marino INSERT_WORDS(z,ix0,ix1);
184*05a0b428SJohn Marino return z;
185*05a0b428SJohn Marino }
186*05a0b428SJohn Marino
187*05a0b428SJohn Marino /*
188*05a0b428SJohn Marino Other methods (use floating-point arithmetic)
189*05a0b428SJohn Marino -------------
190*05a0b428SJohn Marino (This is a copy of a drafted paper by Prof W. Kahan
191*05a0b428SJohn Marino and K.C. Ng, written in May, 1986)
192*05a0b428SJohn Marino
193*05a0b428SJohn Marino Two algorithms are given here to implement sqrt(x)
194*05a0b428SJohn Marino (IEEE double precision arithmetic) in software.
195*05a0b428SJohn Marino Both supply sqrt(x) correctly rounded. The first algorithm (in
196*05a0b428SJohn Marino Section A) uses newton iterations and involves four divisions.
197*05a0b428SJohn Marino The second one uses reciproot iterations to avoid division, but
198*05a0b428SJohn Marino requires more multiplications. Both algorithms need the ability
199*05a0b428SJohn Marino to chop results of arithmetic operations instead of round them,
200*05a0b428SJohn Marino and the INEXACT flag to indicate when an arithmetic operation
201*05a0b428SJohn Marino is executed exactly with no roundoff error, all part of the
202*05a0b428SJohn Marino standard (IEEE 754-1985). The ability to perform shift, add,
203*05a0b428SJohn Marino subtract and logical AND operations upon 32-bit words is needed
204*05a0b428SJohn Marino too, though not part of the standard.
205*05a0b428SJohn Marino
206*05a0b428SJohn Marino A. sqrt(x) by Newton Iteration
207*05a0b428SJohn Marino
208*05a0b428SJohn Marino (1) Initial approximation
209*05a0b428SJohn Marino
210*05a0b428SJohn Marino Let x0 and x1 be the leading and the trailing 32-bit words of
211*05a0b428SJohn Marino a floating point number x (in IEEE double format) respectively
212*05a0b428SJohn Marino
213*05a0b428SJohn Marino 1 11 52 ...widths
214*05a0b428SJohn Marino ------------------------------------------------------
215*05a0b428SJohn Marino x: |s| e | f |
216*05a0b428SJohn Marino ------------------------------------------------------
217*05a0b428SJohn Marino msb lsb msb lsb ...order
218*05a0b428SJohn Marino
219*05a0b428SJohn Marino
220*05a0b428SJohn Marino ------------------------ ------------------------
221*05a0b428SJohn Marino x0: |s| e | f1 | x1: | f2 |
222*05a0b428SJohn Marino ------------------------ ------------------------
223*05a0b428SJohn Marino
224*05a0b428SJohn Marino By performing shifts and subtracts on x0 and x1 (both regarded
225*05a0b428SJohn Marino as integers), we obtain an 8-bit approximation of sqrt(x) as
226*05a0b428SJohn Marino follows.
227*05a0b428SJohn Marino
228*05a0b428SJohn Marino k := (x0>>1) + 0x1ff80000;
229*05a0b428SJohn Marino y0 := k - T1[31&(k>>15)]. ... y ~ sqrt(x) to 8 bits
230*05a0b428SJohn Marino Here k is a 32-bit integer and T1[] is an integer array containing
231*05a0b428SJohn Marino correction terms. Now magically the floating value of y (y's
232*05a0b428SJohn Marino leading 32-bit word is y0, the value of its trailing word is 0)
233*05a0b428SJohn Marino approximates sqrt(x) to almost 8-bit.
234*05a0b428SJohn Marino
235*05a0b428SJohn Marino Value of T1:
236*05a0b428SJohn Marino static int T1[32]= {
237*05a0b428SJohn Marino 0, 1024, 3062, 5746, 9193, 13348, 18162, 23592,
238*05a0b428SJohn Marino 29598, 36145, 43202, 50740, 58733, 67158, 75992, 85215,
239*05a0b428SJohn Marino 83599, 71378, 60428, 50647, 41945, 34246, 27478, 21581,
240*05a0b428SJohn Marino 16499, 12183, 8588, 5674, 3403, 1742, 661, 130,};
241*05a0b428SJohn Marino
242*05a0b428SJohn Marino (2) Iterative refinement
243*05a0b428SJohn Marino
244*05a0b428SJohn Marino Apply Heron's rule three times to y, we have y approximates
245*05a0b428SJohn Marino sqrt(x) to within 1 ulp (Unit in the Last Place):
246*05a0b428SJohn Marino
247*05a0b428SJohn Marino y := (y+x/y)/2 ... almost 17 sig. bits
248*05a0b428SJohn Marino y := (y+x/y)/2 ... almost 35 sig. bits
249*05a0b428SJohn Marino y := y-(y-x/y)/2 ... within 1 ulp
250*05a0b428SJohn Marino
251*05a0b428SJohn Marino
252*05a0b428SJohn Marino Remark 1.
253*05a0b428SJohn Marino Another way to improve y to within 1 ulp is:
254*05a0b428SJohn Marino
255*05a0b428SJohn Marino y := (y+x/y) ... almost 17 sig. bits to 2*sqrt(x)
256*05a0b428SJohn Marino y := y - 0x00100006 ... almost 18 sig. bits to sqrt(x)
257*05a0b428SJohn Marino
258*05a0b428SJohn Marino 2
259*05a0b428SJohn Marino (x-y )*y
260*05a0b428SJohn Marino y := y + 2* ---------- ...within 1 ulp
261*05a0b428SJohn Marino 2
262*05a0b428SJohn Marino 3y + x
263*05a0b428SJohn Marino
264*05a0b428SJohn Marino
265*05a0b428SJohn Marino This formula has one division fewer than the one above; however,
266*05a0b428SJohn Marino it requires more multiplications and additions. Also x must be
267*05a0b428SJohn Marino scaled in advance to avoid spurious overflow in evaluating the
268*05a0b428SJohn Marino expression 3y*y+x. Hence it is not recommended uless division
269*05a0b428SJohn Marino is slow. If division is very slow, then one should use the
270*05a0b428SJohn Marino reciproot algorithm given in section B.
271*05a0b428SJohn Marino
272*05a0b428SJohn Marino (3) Final adjustment
273*05a0b428SJohn Marino
274*05a0b428SJohn Marino By twiddling y's last bit it is possible to force y to be
275*05a0b428SJohn Marino correctly rounded according to the prevailing rounding mode
276*05a0b428SJohn Marino as follows. Let r and i be copies of the rounding mode and
277*05a0b428SJohn Marino inexact flag before entering the square root program. Also we
278*05a0b428SJohn Marino use the expression y+-ulp for the next representable floating
279*05a0b428SJohn Marino numbers (up and down) of y. Note that y+-ulp = either fixed
280*05a0b428SJohn Marino point y+-1, or multiply y by nextafter(1,+-inf) in chopped
281*05a0b428SJohn Marino mode.
282*05a0b428SJohn Marino
283*05a0b428SJohn Marino I := FALSE; ... reset INEXACT flag I
284*05a0b428SJohn Marino R := RZ; ... set rounding mode to round-toward-zero
285*05a0b428SJohn Marino z := x/y; ... chopped quotient, possibly inexact
286*05a0b428SJohn Marino If(not I) then { ... if the quotient is exact
287*05a0b428SJohn Marino if(z=y) {
288*05a0b428SJohn Marino I := i; ... restore inexact flag
289*05a0b428SJohn Marino R := r; ... restore rounded mode
290*05a0b428SJohn Marino return sqrt(x):=y.
291*05a0b428SJohn Marino } else {
292*05a0b428SJohn Marino z := z - ulp; ... special rounding
293*05a0b428SJohn Marino }
294*05a0b428SJohn Marino }
295*05a0b428SJohn Marino i := TRUE; ... sqrt(x) is inexact
296*05a0b428SJohn Marino If (r=RN) then z=z+ulp ... rounded-to-nearest
297*05a0b428SJohn Marino If (r=RP) then { ... round-toward-+inf
298*05a0b428SJohn Marino y = y+ulp; z=z+ulp;
299*05a0b428SJohn Marino }
300*05a0b428SJohn Marino y := y+z; ... chopped sum
301*05a0b428SJohn Marino y0:=y0-0x00100000; ... y := y/2 is correctly rounded.
302*05a0b428SJohn Marino I := i; ... restore inexact flag
303*05a0b428SJohn Marino R := r; ... restore rounded mode
304*05a0b428SJohn Marino return sqrt(x):=y.
305*05a0b428SJohn Marino
306*05a0b428SJohn Marino (4) Special cases
307*05a0b428SJohn Marino
308*05a0b428SJohn Marino Square root of +inf, +-0, or NaN is itself;
309*05a0b428SJohn Marino Square root of a negative number is NaN with invalid signal.
310*05a0b428SJohn Marino
311*05a0b428SJohn Marino
312*05a0b428SJohn Marino B. sqrt(x) by Reciproot Iteration
313*05a0b428SJohn Marino
314*05a0b428SJohn Marino (1) Initial approximation
315*05a0b428SJohn Marino
316*05a0b428SJohn Marino Let x0 and x1 be the leading and the trailing 32-bit words of
317*05a0b428SJohn Marino a floating point number x (in IEEE double format) respectively
318*05a0b428SJohn Marino (see section A). By performing shifs and subtracts on x0 and y0,
319*05a0b428SJohn Marino we obtain a 7.8-bit approximation of 1/sqrt(x) as follows.
320*05a0b428SJohn Marino
321*05a0b428SJohn Marino k := 0x5fe80000 - (x0>>1);
322*05a0b428SJohn Marino y0:= k - T2[63&(k>>14)]. ... y ~ 1/sqrt(x) to 7.8 bits
323*05a0b428SJohn Marino
324*05a0b428SJohn Marino Here k is a 32-bit integer and T2[] is an integer array
325*05a0b428SJohn Marino containing correction terms. Now magically the floating
326*05a0b428SJohn Marino value of y (y's leading 32-bit word is y0, the value of
327*05a0b428SJohn Marino its trailing word y1 is set to zero) approximates 1/sqrt(x)
328*05a0b428SJohn Marino to almost 7.8-bit.
329*05a0b428SJohn Marino
330*05a0b428SJohn Marino Value of T2:
331*05a0b428SJohn Marino static int T2[64]= {
332*05a0b428SJohn Marino 0x1500, 0x2ef8, 0x4d67, 0x6b02, 0x87be, 0xa395, 0xbe7a, 0xd866,
333*05a0b428SJohn Marino 0xf14a, 0x1091b,0x11fcd,0x13552,0x14999,0x15c98,0x16e34,0x17e5f,
334*05a0b428SJohn Marino 0x18d03,0x19a01,0x1a545,0x1ae8a,0x1b5c4,0x1bb01,0x1bfde,0x1c28d,
335*05a0b428SJohn Marino 0x1c2de,0x1c0db,0x1ba73,0x1b11c,0x1a4b5,0x1953d,0x18266,0x16be0,
336*05a0b428SJohn Marino 0x1683e,0x179d8,0x18a4d,0x19992,0x1a789,0x1b445,0x1bf61,0x1c989,
337*05a0b428SJohn Marino 0x1d16d,0x1d77b,0x1dddf,0x1e2ad,0x1e5bf,0x1e6e8,0x1e654,0x1e3cd,
338*05a0b428SJohn Marino 0x1df2a,0x1d635,0x1cb16,0x1be2c,0x1ae4e,0x19bde,0x1868e,0x16e2e,
339*05a0b428SJohn Marino 0x1527f,0x1334a,0x11051,0xe951, 0xbe01, 0x8e0d, 0x5924, 0x1edd,};
340*05a0b428SJohn Marino
341*05a0b428SJohn Marino (2) Iterative refinement
342*05a0b428SJohn Marino
343*05a0b428SJohn Marino Apply Reciproot iteration three times to y and multiply the
344*05a0b428SJohn Marino result by x to get an approximation z that matches sqrt(x)
345*05a0b428SJohn Marino to about 1 ulp. To be exact, we will have
346*05a0b428SJohn Marino -1ulp < sqrt(x)-z<1.0625ulp.
347*05a0b428SJohn Marino
348*05a0b428SJohn Marino ... set rounding mode to Round-to-nearest
349*05a0b428SJohn Marino y := y*(1.5-0.5*x*y*y) ... almost 15 sig. bits to 1/sqrt(x)
350*05a0b428SJohn Marino y := y*((1.5-2^-30)+0.5*x*y*y)... about 29 sig. bits to 1/sqrt(x)
351*05a0b428SJohn Marino ... special arrangement for better accuracy
352*05a0b428SJohn Marino z := x*y ... 29 bits to sqrt(x), with z*y<1
353*05a0b428SJohn Marino z := z + 0.5*z*(1-z*y) ... about 1 ulp to sqrt(x)
354*05a0b428SJohn Marino
355*05a0b428SJohn Marino Remark 2. The constant 1.5-2^-30 is chosen to bias the error so that
356*05a0b428SJohn Marino (a) the term z*y in the final iteration is always less than 1;
357*05a0b428SJohn Marino (b) the error in the final result is biased upward so that
358*05a0b428SJohn Marino -1 ulp < sqrt(x) - z < 1.0625 ulp
359*05a0b428SJohn Marino instead of |sqrt(x)-z|<1.03125ulp.
360*05a0b428SJohn Marino
361*05a0b428SJohn Marino (3) Final adjustment
362*05a0b428SJohn Marino
363*05a0b428SJohn Marino By twiddling y's last bit it is possible to force y to be
364*05a0b428SJohn Marino correctly rounded according to the prevailing rounding mode
365*05a0b428SJohn Marino as follows. Let r and i be copies of the rounding mode and
366*05a0b428SJohn Marino inexact flag before entering the square root program. Also we
367*05a0b428SJohn Marino use the expression y+-ulp for the next representable floating
368*05a0b428SJohn Marino numbers (up and down) of y. Note that y+-ulp = either fixed
369*05a0b428SJohn Marino point y+-1, or multiply y by nextafter(1,+-inf) in chopped
370*05a0b428SJohn Marino mode.
371*05a0b428SJohn Marino
372*05a0b428SJohn Marino R := RZ; ... set rounding mode to round-toward-zero
373*05a0b428SJohn Marino switch(r) {
374*05a0b428SJohn Marino case RN: ... round-to-nearest
375*05a0b428SJohn Marino if(x<= z*(z-ulp)...chopped) z = z - ulp; else
376*05a0b428SJohn Marino if(x<= z*(z+ulp)...chopped) z = z; else z = z+ulp;
377*05a0b428SJohn Marino break;
378*05a0b428SJohn Marino case RZ:case RM: ... round-to-zero or round-to--inf
379*05a0b428SJohn Marino R:=RP; ... reset rounding mod to round-to-+inf
380*05a0b428SJohn Marino if(x<z*z ... rounded up) z = z - ulp; else
381*05a0b428SJohn Marino if(x>=(z+ulp)*(z+ulp) ...rounded up) z = z+ulp;
382*05a0b428SJohn Marino break;
383*05a0b428SJohn Marino case RP: ... round-to-+inf
384*05a0b428SJohn Marino if(x>(z+ulp)*(z+ulp)...chopped) z = z+2*ulp; else
385*05a0b428SJohn Marino if(x>z*z ...chopped) z = z+ulp;
386*05a0b428SJohn Marino break;
387*05a0b428SJohn Marino }
388*05a0b428SJohn Marino
389*05a0b428SJohn Marino Remark 3. The above comparisons can be done in fixed point. For
390*05a0b428SJohn Marino example, to compare x and w=z*z chopped, it suffices to compare
391*05a0b428SJohn Marino x1 and w1 (the trailing parts of x and w), regarding them as
392*05a0b428SJohn Marino two's complement integers.
393*05a0b428SJohn Marino
394*05a0b428SJohn Marino ...Is z an exact square root?
395*05a0b428SJohn Marino To determine whether z is an exact square root of x, let z1 be the
396*05a0b428SJohn Marino trailing part of z, and also let x0 and x1 be the leading and
397*05a0b428SJohn Marino trailing parts of x.
398*05a0b428SJohn Marino
399*05a0b428SJohn Marino If ((z1&0x03ffffff)!=0) ... not exact if trailing 26 bits of z!=0
400*05a0b428SJohn Marino I := 1; ... Raise Inexact flag: z is not exact
401*05a0b428SJohn Marino else {
402*05a0b428SJohn Marino j := 1 - [(x0>>20)&1] ... j = logb(x) mod 2
403*05a0b428SJohn Marino k := z1 >> 26; ... get z's 25-th and 26-th
404*05a0b428SJohn Marino fraction bits
405*05a0b428SJohn Marino I := i or (k&j) or ((k&(j+j+1))!=(x1&3));
406*05a0b428SJohn Marino }
407*05a0b428SJohn Marino R:= r ... restore rounded mode
408*05a0b428SJohn Marino return sqrt(x):=z.
409*05a0b428SJohn Marino
410*05a0b428SJohn Marino If multiplication is cheaper then the foregoing red tape, the
411*05a0b428SJohn Marino Inexact flag can be evaluated by
412*05a0b428SJohn Marino
413*05a0b428SJohn Marino I := i;
414*05a0b428SJohn Marino I := (z*z!=x) or I.
415*05a0b428SJohn Marino
416*05a0b428SJohn Marino Note that z*z can overwrite I; this value must be sensed if it is
417*05a0b428SJohn Marino True.
418*05a0b428SJohn Marino
419*05a0b428SJohn Marino Remark 4. If z*z = x exactly, then bit 25 to bit 0 of z1 must be
420*05a0b428SJohn Marino zero.
421*05a0b428SJohn Marino
422*05a0b428SJohn Marino --------------------
423*05a0b428SJohn Marino z1: | f2 |
424*05a0b428SJohn Marino --------------------
425*05a0b428SJohn Marino bit 31 bit 0
426*05a0b428SJohn Marino
427*05a0b428SJohn Marino Further more, bit 27 and 26 of z1, bit 0 and 1 of x1, and the odd
428*05a0b428SJohn Marino or even of logb(x) have the following relations:
429*05a0b428SJohn Marino
430*05a0b428SJohn Marino -------------------------------------------------
431*05a0b428SJohn Marino bit 27,26 of z1 bit 1,0 of x1 logb(x)
432*05a0b428SJohn Marino -------------------------------------------------
433*05a0b428SJohn Marino 00 00 odd and even
434*05a0b428SJohn Marino 01 01 even
435*05a0b428SJohn Marino 10 10 odd
436*05a0b428SJohn Marino 10 00 even
437*05a0b428SJohn Marino 11 01 even
438*05a0b428SJohn Marino -------------------------------------------------
439*05a0b428SJohn Marino
440*05a0b428SJohn Marino (4) Special cases (see (4) of Section A).
441*05a0b428SJohn Marino
442*05a0b428SJohn Marino */
443*05a0b428SJohn Marino
444*05a0b428SJohn Marino #if LDBL_MANT_DIG == DBL_MANT_DIG
445*05a0b428SJohn Marino __strong_alias(sqrtl, sqrt);
446*05a0b428SJohn Marino #endif /* LDBL_MANT_DIG == DBL_MANT_DIG */
447