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