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