Path: blob/master/src/java.base/share/native/libfdlibm/e_sqrt.c
41152 views
/*1* Copyright (c) 1998, 2001, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425/* __ieee754_sqrt(x)26* Return correctly rounded sqrt.27* ------------------------------------------28* | Use the hardware sqrt if you have one |29* ------------------------------------------30* Method:31* Bit by bit method using integer arithmetic. (Slow, but portable)32* 1. Normalization33* Scale x to y in [1,4) with even powers of 2:34* find an integer k such that 1 <= (y=x*2^(2k)) < 4, then35* sqrt(x) = 2^k * sqrt(y)36* 2. Bit by bit computation37* Let q = sqrt(y) truncated to i bit after binary point (q = 1),38* i 039* i+1 240* s = 2*q , and y = 2 * ( y - q ). (1)41* i i i i42*43* To compute q from q , one checks whether44* i+1 i45*46* -(i+1) 247* (q + 2 ) <= y. (2)48* i49* -(i+1)50* If (2) is false, then q = q ; otherwise q = q + 2 .51* i+1 i i+1 i52*53* With some algebric manipulation, it is not difficult to see54* that (2) is equivalent to55* -(i+1)56* s + 2 <= y (3)57* i i58*59* The advantage of (3) is that s and y can be computed by60* i i61* the following recurrence formula:62* if (3) is false63*64* s = s , y = y ; (4)65* i+1 i i+1 i66*67* otherwise,68* -i -(i+1)69* s = s + 2 , y = y - s - 2 (5)70* i+1 i i+1 i i71*72* One may easily use induction to prove (4) and (5).73* Note. Since the left hand side of (3) contain only i+2 bits,74* it does not necessary to do a full (53-bit) comparison75* in (3).76* 3. Final rounding77* After generating the 53 bits result, we compute one more bit.78* Together with the remainder, we can decide whether the79* result is exact, bigger than 1/2ulp, or less than 1/2ulp80* (it will never equal to 1/2ulp).81* The rounding mode can be detected by checking whether82* huge + tiny is equal to huge, and whether huge - tiny is83* equal to huge for some floating point number "huge" and "tiny".84*85* Special cases:86* sqrt(+-0) = +-0 ... exact87* sqrt(inf) = inf88* sqrt(-ve) = NaN ... with invalid signal89* sqrt(NaN) = NaN ... with invalid signal for signaling NaN90*91* Other methods : see the appended file at the end of the program below.92*---------------93*/9495#include "fdlibm.h"9697#ifdef __STDC__98static const double one = 1.0, tiny=1.0e-300;99#else100static double one = 1.0, tiny=1.0e-300;101#endif102103#ifdef __STDC__104double __ieee754_sqrt(double x)105#else106double __ieee754_sqrt(x)107double x;108#endif109{110double z;111int sign = (int)0x80000000;112unsigned r,t1,s1,ix1,q1;113int ix0,s0,q,m,t,i;114115ix0 = __HI(x); /* high word of x */116ix1 = __LO(x); /* low word of x */117118/* take care of Inf and NaN */119if((ix0&0x7ff00000)==0x7ff00000) {120return x*x+x; /* sqrt(NaN)=NaN, sqrt(+inf)=+inf121sqrt(-inf)=sNaN */122}123/* take care of zero */124if(ix0<=0) {125if(((ix0&(~sign))|ix1)==0) return x;/* sqrt(+-0) = +-0 */126else if(ix0<0)127return (x-x)/(x-x); /* sqrt(-ve) = sNaN */128}129/* normalize x */130m = (ix0>>20);131if(m==0) { /* subnormal x */132while(ix0==0) {133m -= 21;134ix0 |= (ix1>>11); ix1 <<= 21;135}136for(i=0;(ix0&0x00100000)==0;i++) ix0<<=1;137m -= i-1;138ix0 |= (ix1>>(32-i));139ix1 <<= i;140}141m -= 1023; /* unbias exponent */142ix0 = (ix0&0x000fffff)|0x00100000;143if(m&1){ /* odd m, double x to make it even */144ix0 += ix0 + ((ix1&sign)>>31);145ix1 += ix1;146}147m >>= 1; /* m = [m/2] */148149/* generate sqrt(x) bit by bit */150ix0 += ix0 + ((ix1&sign)>>31);151ix1 += ix1;152q = q1 = s0 = s1 = 0; /* [q,q1] = sqrt(x) */153r = 0x00200000; /* r = moving bit from right to left */154155while(r!=0) {156t = s0+r;157if(t<=ix0) {158s0 = t+r;159ix0 -= t;160q += r;161}162ix0 += ix0 + ((ix1&sign)>>31);163ix1 += ix1;164r>>=1;165}166167r = sign;168while(r!=0) {169t1 = s1+r;170t = s0;171if((t<ix0)||((t==ix0)&&(t1<=ix1))) {172s1 = t1+r;173if(((t1&sign)==sign)&&(s1&sign)==0) s0 += 1;174ix0 -= t;175if (ix1 < t1) ix0 -= 1;176ix1 -= t1;177q1 += r;178}179ix0 += ix0 + ((ix1&sign)>>31);180ix1 += ix1;181r>>=1;182}183184/* use floating add to find out rounding direction */185if((ix0|ix1)!=0) {186z = one-tiny; /* trigger inexact flag */187if (z>=one) {188z = one+tiny;189if (q1==(unsigned)0xffffffff) { q1=0; q += 1;}190else if (z>one) {191if (q1==(unsigned)0xfffffffe) q+=1;192q1+=2;193} else194q1 += (q1&1);195}196}197ix0 = (q>>1)+0x3fe00000;198ix1 = q1>>1;199if ((q&1)==1) ix1 |= sign;200ix0 += (m <<20);201__HI(z) = ix0;202__LO(z) = ix1;203return z;204}205206/*207Other methods (use floating-point arithmetic)208-------------209(This is a copy of a drafted paper by Prof W. Kahan210and K.C. Ng, written in May, 1986)211212Two algorithms are given here to implement sqrt(x)213(IEEE double precision arithmetic) in software.214Both supply sqrt(x) correctly rounded. The first algorithm (in215Section A) uses newton iterations and involves four divisions.216The second one uses reciproot iterations to avoid division, but217requires more multiplications. Both algorithms need the ability218to chop results of arithmetic operations instead of round them,219and the INEXACT flag to indicate when an arithmetic operation220is executed exactly with no roundoff error, all part of the221standard (IEEE 754-1985). The ability to perform shift, add,222subtract and logical AND operations upon 32-bit words is needed223too, though not part of the standard.224225A. sqrt(x) by Newton Iteration226227(1) Initial approximation228229Let x0 and x1 be the leading and the trailing 32-bit words of230a floating point number x (in IEEE double format) respectively2312321 11 52 ...widths233------------------------------------------------------234x: |s| e | f |235------------------------------------------------------236msb lsb msb lsb ...order237238239------------------------ ------------------------240x0: |s| e | f1 | x1: | f2 |241------------------------ ------------------------242243By performing shifts and subtracts on x0 and x1 (both regarded244as integers), we obtain an 8-bit approximation of sqrt(x) as245follows.246247k := (x0>>1) + 0x1ff80000;248y0 := k - T1[31&(k>>15)]. ... y ~ sqrt(x) to 8 bits249Here k is a 32-bit integer and T1[] is an integer array containing250correction terms. Now magically the floating value of y (y's251leading 32-bit word is y0, the value of its trailing word is 0)252approximates sqrt(x) to almost 8-bit.253254Value of T1:255static int T1[32]= {2560, 1024, 3062, 5746, 9193, 13348, 18162, 23592,25729598, 36145, 43202, 50740, 58733, 67158, 75992, 85215,25883599, 71378, 60428, 50647, 41945, 34246, 27478, 21581,25916499, 12183, 8588, 5674, 3403, 1742, 661, 130,};260261(2) Iterative refinement262263Apply Heron's rule three times to y, we have y approximates264sqrt(x) to within 1 ulp (Unit in the Last Place):265266y := (y+x/y)/2 ... almost 17 sig. bits267y := (y+x/y)/2 ... almost 35 sig. bits268y := y-(y-x/y)/2 ... within 1 ulp269270271Remark 1.272Another way to improve y to within 1 ulp is:273274y := (y+x/y) ... almost 17 sig. bits to 2*sqrt(x)275y := y - 0x00100006 ... almost 18 sig. bits to sqrt(x)2762772278(x-y )*y279y := y + 2* ---------- ...within 1 ulp28022813y + x282283284This formula has one division fewer than the one above; however,285it requires more multiplications and additions. Also x must be286scaled in advance to avoid spurious overflow in evaluating the287expression 3y*y+x. Hence it is not recommended uless division288is slow. If division is very slow, then one should use the289reciproot algorithm given in section B.290291(3) Final adjustment292293By twiddling y's last bit it is possible to force y to be294correctly rounded according to the prevailing rounding mode295as follows. Let r and i be copies of the rounding mode and296inexact flag before entering the square root program. Also we297use the expression y+-ulp for the next representable floating298numbers (up and down) of y. Note that y+-ulp = either fixed299point y+-1, or multiply y by nextafter(1,+-inf) in chopped300mode.301302I := FALSE; ... reset INEXACT flag I303R := RZ; ... set rounding mode to round-toward-zero304z := x/y; ... chopped quotient, possibly inexact305If(not I) then { ... if the quotient is exact306if(z=y) {307I := i; ... restore inexact flag308R := r; ... restore rounded mode309return sqrt(x):=y.310} else {311z := z - ulp; ... special rounding312}313}314i := TRUE; ... sqrt(x) is inexact315If (r=RN) then z=z+ulp ... rounded-to-nearest316If (r=RP) then { ... round-toward-+inf317y = y+ulp; z=z+ulp;318}319y := y+z; ... chopped sum320y0:=y0-0x00100000; ... y := y/2 is correctly rounded.321I := i; ... restore inexact flag322R := r; ... restore rounded mode323return sqrt(x):=y.324325(4) Special cases326327Square root of +inf, +-0, or NaN is itself;328Square root of a negative number is NaN with invalid signal.329330331B. sqrt(x) by Reciproot Iteration332333(1) Initial approximation334335Let x0 and x1 be the leading and the trailing 32-bit words of336a floating point number x (in IEEE double format) respectively337(see section A). By performing shifs and subtracts on x0 and y0,338we obtain a 7.8-bit approximation of 1/sqrt(x) as follows.339340k := 0x5fe80000 - (x0>>1);341y0:= k - T2[63&(k>>14)]. ... y ~ 1/sqrt(x) to 7.8 bits342343Here k is a 32-bit integer and T2[] is an integer array344containing correction terms. Now magically the floating345value of y (y's leading 32-bit word is y0, the value of346its trailing word y1 is set to zero) approximates 1/sqrt(x)347to almost 7.8-bit.348349Value of T2:350static int T2[64]= {3510x1500, 0x2ef8, 0x4d67, 0x6b02, 0x87be, 0xa395, 0xbe7a, 0xd866,3520xf14a, 0x1091b,0x11fcd,0x13552,0x14999,0x15c98,0x16e34,0x17e5f,3530x18d03,0x19a01,0x1a545,0x1ae8a,0x1b5c4,0x1bb01,0x1bfde,0x1c28d,3540x1c2de,0x1c0db,0x1ba73,0x1b11c,0x1a4b5,0x1953d,0x18266,0x16be0,3550x1683e,0x179d8,0x18a4d,0x19992,0x1a789,0x1b445,0x1bf61,0x1c989,3560x1d16d,0x1d77b,0x1dddf,0x1e2ad,0x1e5bf,0x1e6e8,0x1e654,0x1e3cd,3570x1df2a,0x1d635,0x1cb16,0x1be2c,0x1ae4e,0x19bde,0x1868e,0x16e2e,3580x1527f,0x1334a,0x11051,0xe951, 0xbe01, 0x8e0d, 0x5924, 0x1edd,};359360(2) Iterative refinement361362Apply Reciproot iteration three times to y and multiply the363result by x to get an approximation z that matches sqrt(x)364to about 1 ulp. To be exact, we will have365-1ulp < sqrt(x)-z<1.0625ulp.366367... set rounding mode to Round-to-nearest368y := y*(1.5-0.5*x*y*y) ... almost 15 sig. bits to 1/sqrt(x)369y := y*((1.5-2^-30)+0.5*x*y*y)... about 29 sig. bits to 1/sqrt(x)370... special arrangement for better accuracy371z := x*y ... 29 bits to sqrt(x), with z*y<1372z := z + 0.5*z*(1-z*y) ... about 1 ulp to sqrt(x)373374Remark 2. The constant 1.5-2^-30 is chosen to bias the error so that375(a) the term z*y in the final iteration is always less than 1;376(b) the error in the final result is biased upward so that377-1 ulp < sqrt(x) - z < 1.0625 ulp378instead of |sqrt(x)-z|<1.03125ulp.379380(3) Final adjustment381382By twiddling y's last bit it is possible to force y to be383correctly rounded according to the prevailing rounding mode384as follows. Let r and i be copies of the rounding mode and385inexact flag before entering the square root program. Also we386use the expression y+-ulp for the next representable floating387numbers (up and down) of y. Note that y+-ulp = either fixed388point y+-1, or multiply y by nextafter(1,+-inf) in chopped389mode.390391R := RZ; ... set rounding mode to round-toward-zero392switch(r) {393case RN: ... round-to-nearest394if(x<= z*(z-ulp)...chopped) z = z - ulp; else395if(x<= z*(z+ulp)...chopped) z = z; else z = z+ulp;396break;397case RZ:case RM: ... round-to-zero or round-to--inf398R:=RP; ... reset rounding mod to round-to-+inf399if(x<z*z ... rounded up) z = z - ulp; else400if(x>=(z+ulp)*(z+ulp) ...rounded up) z = z+ulp;401break;402case RP: ... round-to-+inf403if(x>(z+ulp)*(z+ulp)...chopped) z = z+2*ulp; else404if(x>z*z ...chopped) z = z+ulp;405break;406}407408Remark 3. The above comparisons can be done in fixed point. For409example, to compare x and w=z*z chopped, it suffices to compare410x1 and w1 (the trailing parts of x and w), regarding them as411two's complement integers.412413...Is z an exact square root?414To determine whether z is an exact square root of x, let z1 be the415trailing part of z, and also let x0 and x1 be the leading and416trailing parts of x.417418If ((z1&0x03ffffff)!=0) ... not exact if trailing 26 bits of z!=0419I := 1; ... Raise Inexact flag: z is not exact420else {421j := 1 - [(x0>>20)&1] ... j = logb(x) mod 2422k := z1 >> 26; ... get z's 25-th and 26-th423fraction bits424I := i or (k&j) or ((k&(j+j+1))!=(x1&3));425}426R:= r ... restore rounded mode427return sqrt(x):=z.428429If multiplication is cheaper then the foregoing red tape, the430Inexact flag can be evaluated by431432I := i;433I := (z*z!=x) or I.434435Note that z*z can overwrite I; this value must be sensed if it is436True.437438Remark 4. If z*z = x exactly, then bit 25 to bit 0 of z1 must be439zero.440441--------------------442z1: | f2 |443--------------------444bit 31 bit 0445446Further more, bit 27 and 26 of z1, bit 0 and 1 of x1, and the odd447or even of logb(x) have the following relations:448449-------------------------------------------------450bit 27,26 of z1 bit 1,0 of x1 logb(x)451-------------------------------------------------45200 00 odd and even45301 01 even45410 10 odd45510 00 even45611 01 even457-------------------------------------------------458459(4) Special cases (see (4) of Section A).460461*/462463464