1*4887Schin #include "FEATURE/uwin" 2*4887Schin 3*4887Schin #if !_UWIN || (_lib__copysign||_lib_copysign) && _lib_logb && (_lib__finite||_lib_finite) && (_lib_drem||_lib_remainder) && _lib_sqrt && _lib_ilogb && (_lib__scalb||_lib_scalb) 4*4887Schin 5*4887Schin void _STUB_support(){} 6*4887Schin 7*4887Schin #else 8*4887Schin 9*4887Schin /* 10*4887Schin * Copyright (c) 1985, 1993 11*4887Schin * The Regents of the University of California. All rights reserved. 12*4887Schin * 13*4887Schin * Redistribution and use in source and binary forms, with or without 14*4887Schin * modification, are permitted provided that the following conditions 15*4887Schin * are met: 16*4887Schin * 1. Redistributions of source code must retain the above copyright 17*4887Schin * notice, this list of conditions and the following disclaimer. 18*4887Schin * 2. Redistributions in binary form must reproduce the above copyright 19*4887Schin * notice, this list of conditions and the following disclaimer in the 20*4887Schin * documentation and/or other materials provided with the distribution. 21*4887Schin * 3. Neither the name of the University nor the names of its contributors 22*4887Schin * may be used to endorse or promote products derived from this software 23*4887Schin * without specific prior written permission. 24*4887Schin * 25*4887Schin * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 26*4887Schin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 27*4887Schin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 28*4887Schin * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 29*4887Schin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 30*4887Schin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 31*4887Schin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 32*4887Schin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 33*4887Schin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 34*4887Schin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 35*4887Schin * SUCH DAMAGE. 36*4887Schin */ 37*4887Schin 38*4887Schin #ifndef lint 39*4887Schin static char sccsid[] = "@(#)support.c 8.1 (Berkeley) 6/4/93"; 40*4887Schin #endif /* not lint */ 41*4887Schin 42*4887Schin /* 43*4887Schin * Some IEEE standard 754 recommended functions and remainder and sqrt for 44*4887Schin * supporting the C elementary functions. 45*4887Schin ****************************************************************************** 46*4887Schin * WARNING: 47*4887Schin * These codes are developed (in double) to support the C elementary 48*4887Schin * functions temporarily. They are not universal, and some of them are very 49*4887Schin * slow (in particular, drem and sqrt is extremely inefficient). Each 50*4887Schin * computer system should have its implementation of these functions using 51*4887Schin * its own assembler. 52*4887Schin ****************************************************************************** 53*4887Schin * 54*4887Schin * IEEE 754 required operations: 55*4887Schin * drem(x,p) 56*4887Schin * returns x REM y = x - [x/y]*y , where [x/y] is the integer 57*4887Schin * nearest x/y; in half way case, choose the even one. 58*4887Schin * sqrt(x) 59*4887Schin * returns the square root of x correctly rounded according to 60*4887Schin * the rounding mod. 61*4887Schin * 62*4887Schin * IEEE 754 recommended functions: 63*4887Schin * (a) copysign(x,y) 64*4887Schin * returns x with the sign of y. 65*4887Schin * (b) scalb(x,N) 66*4887Schin * returns x * (2**N), for integer values N. 67*4887Schin * (c) logb(x) 68*4887Schin * returns the unbiased exponent of x, a signed integer in 69*4887Schin * double precision, except that logb(0) is -INF, logb(INF) 70*4887Schin * is +INF, and logb(NAN) is that NAN. 71*4887Schin * (d) finite(x) 72*4887Schin * returns the value TRUE if -INF < x < +INF and returns 73*4887Schin * FALSE otherwise. 74*4887Schin * 75*4887Schin * 76*4887Schin * CODED IN C BY K.C. NG, 11/25/84; 77*4887Schin * REVISED BY K.C. NG on 1/22/85, 2/13/85, 3/24/85. 78*4887Schin */ 79*4887Schin 80*4887Schin #include "mathimpl.h" 81*4887Schin 82*4887Schin #if defined(vax)||defined(tahoe) /* VAX D format */ 83*4887Schin #include <errno.h> 84*4887Schin static const unsigned short msign=0x7fff , mexp =0x7f80 ; 85*4887Schin static const short prep1=57, gap=7, bias=129 ; 86*4887Schin static const double novf=1.7E38, nunf=3.0E-39, zero=0.0 ; 87*4887Schin #else /* defined(vax)||defined(tahoe) */ 88*4887Schin static const unsigned short msign=0x7fff, mexp =0x7ff0 ; 89*4887Schin static const short prep1=54, gap=4, bias=1023 ; 90*4887Schin static const double novf=1.7E308, nunf=3.0E-308,zero=0.0; 91*4887Schin #endif /* defined(vax)||defined(tahoe) */ 92*4887Schin 93*4887Schin #if !_lib__scalb || !_lib_scalb 94*4887Schin 95*4887Schin extern double _scalb(x,N) 96*4887Schin double x; double N; 97*4887Schin { 98*4887Schin int k; 99*4887Schin 100*4887Schin #ifdef national 101*4887Schin unsigned short *px=(unsigned short *) &x + 3; 102*4887Schin #else /* national */ 103*4887Schin unsigned short *px=(unsigned short *) &x; 104*4887Schin #endif /* national */ 105*4887Schin 106*4887Schin if( x == zero ) return(x); 107*4887Schin 108*4887Schin #if defined(vax)||defined(tahoe) 109*4887Schin if( (k= *px & mexp ) != ~msign ) { 110*4887Schin if (N < -260) 111*4887Schin return(nunf*nunf); 112*4887Schin else if (N > 260) { 113*4887Schin return(copysign(infnan(ERANGE),x)); 114*4887Schin } 115*4887Schin #else /* defined(vax)||defined(tahoe) */ 116*4887Schin if( (k= *px & mexp ) != mexp ) { 117*4887Schin if( N<-2100) return(nunf*nunf); else if(N>2100) return(novf+novf); 118*4887Schin if( k == 0 ) { 119*4887Schin x *= scalb(1.0,prep1); N -= prep1; return(scalb(x,N));} 120*4887Schin #endif /* defined(vax)||defined(tahoe) */ 121*4887Schin 122*4887Schin if((k = (k>>gap)+ N) > 0 ) 123*4887Schin if( k < (mexp>>gap) ) *px = (*px&~mexp) | (k<<gap); 124*4887Schin else x=novf+novf; /* overflow */ 125*4887Schin else 126*4887Schin if( k > -prep1 ) 127*4887Schin /* gradual underflow */ 128*4887Schin {*px=(*px&~mexp)|(short)(1<<gap); x *= scalb(1.0,k-1);} 129*4887Schin else 130*4887Schin return(nunf*nunf); 131*4887Schin } 132*4887Schin return(x); 133*4887Schin } 134*4887Schin 135*4887Schin #endif 136*4887Schin 137*4887Schin #if !_lib_scalb 138*4887Schin 139*4887Schin extern double scalb(x,N) 140*4887Schin double x; double N; 141*4887Schin { 142*4887Schin return _scalb(x, N); 143*4887Schin } 144*4887Schin 145*4887Schin #endif 146*4887Schin 147*4887Schin #if !_lib__copysign 148*4887Schin 149*4887Schin extern double _copysign(x,y) 150*4887Schin double x,y; 151*4887Schin { 152*4887Schin #ifdef national 153*4887Schin unsigned short *px=(unsigned short *) &x+3, 154*4887Schin *py=(unsigned short *) &y+3; 155*4887Schin #else /* national */ 156*4887Schin unsigned short *px=(unsigned short *) &x, 157*4887Schin *py=(unsigned short *) &y; 158*4887Schin #endif /* national */ 159*4887Schin 160*4887Schin #if defined(vax)||defined(tahoe) 161*4887Schin if ( (*px & mexp) == 0 ) return(x); 162*4887Schin #endif /* defined(vax)||defined(tahoe) */ 163*4887Schin 164*4887Schin *px = ( *px & msign ) | ( *py & ~msign ); 165*4887Schin return(x); 166*4887Schin } 167*4887Schin 168*4887Schin #endif 169*4887Schin 170*4887Schin #if !_lib_copysign 171*4887Schin 172*4887Schin extern double copysign(x,y) 173*4887Schin double x,y; 174*4887Schin { 175*4887Schin return _copysign(x,y); 176*4887Schin } 177*4887Schin 178*4887Schin #endif 179*4887Schin 180*4887Schin #if !_lib_logb 181*4887Schin 182*4887Schin extern double logb(x) 183*4887Schin double x; 184*4887Schin { 185*4887Schin 186*4887Schin #ifdef national 187*4887Schin short *px=(short *) &x+3, k; 188*4887Schin #else /* national */ 189*4887Schin short *px=(short *) &x, k; 190*4887Schin #endif /* national */ 191*4887Schin 192*4887Schin #if defined(vax)||defined(tahoe) 193*4887Schin return (int)(((*px&mexp)>>gap)-bias); 194*4887Schin #else /* defined(vax)||defined(tahoe) */ 195*4887Schin if( (k= *px & mexp ) != mexp ) 196*4887Schin if ( k != 0 ) 197*4887Schin return ( (k>>gap) - bias ); 198*4887Schin else if( x != zero) 199*4887Schin return ( -1022.0 ); 200*4887Schin else 201*4887Schin return(-(1.0/zero)); 202*4887Schin else if(x != x) 203*4887Schin return(x); 204*4887Schin else 205*4887Schin {*px &= msign; return(x);} 206*4887Schin #endif /* defined(vax)||defined(tahoe) */ 207*4887Schin } 208*4887Schin 209*4887Schin #endif 210*4887Schin 211*4887Schin #if !_lib__finite 212*4887Schin 213*4887Schin extern int _finite(x) 214*4887Schin double x; 215*4887Schin { 216*4887Schin #if defined(vax)||defined(tahoe) 217*4887Schin return(1); 218*4887Schin #else /* defined(vax)||defined(tahoe) */ 219*4887Schin #ifdef national 220*4887Schin return( (*((short *) &x+3 ) & mexp ) != mexp ); 221*4887Schin #else /* national */ 222*4887Schin return( (*((short *) &x ) & mexp ) != mexp ); 223*4887Schin #endif /* national */ 224*4887Schin #endif /* defined(vax)||defined(tahoe) */ 225*4887Schin } 226*4887Schin 227*4887Schin #endif 228*4887Schin 229*4887Schin #if !_lib_finite 230*4887Schin 231*4887Schin extern int finite(x) 232*4887Schin double x; 233*4887Schin { 234*4887Schin return _finite(x); 235*4887Schin } 236*4887Schin 237*4887Schin #endif 238*4887Schin 239*4887Schin #if !_lib_drem 240*4887Schin 241*4887Schin extern double drem(x,p) 242*4887Schin double x,p; 243*4887Schin { 244*4887Schin #if _lib_remainder 245*4887Schin return remainder(x,p); 246*4887Schin #else 247*4887Schin short sign; 248*4887Schin double hp,dp,tmp; 249*4887Schin unsigned short k; 250*4887Schin #ifdef national 251*4887Schin unsigned short 252*4887Schin *px=(unsigned short *) &x +3, 253*4887Schin *pp=(unsigned short *) &p +3, 254*4887Schin *pd=(unsigned short *) &dp +3, 255*4887Schin *pt=(unsigned short *) &tmp+3; 256*4887Schin #else /* national */ 257*4887Schin unsigned short 258*4887Schin *px=(unsigned short *) &x , 259*4887Schin *pp=(unsigned short *) &p , 260*4887Schin *pd=(unsigned short *) &dp , 261*4887Schin *pt=(unsigned short *) &tmp; 262*4887Schin #endif /* national */ 263*4887Schin 264*4887Schin *pp &= msign ; 265*4887Schin 266*4887Schin #if defined(vax)||defined(tahoe) 267*4887Schin if( ( *px & mexp ) == ~msign ) /* is x a reserved operand? */ 268*4887Schin #else /* defined(vax)||defined(tahoe) */ 269*4887Schin if( ( *px & mexp ) == mexp ) 270*4887Schin #endif /* defined(vax)||defined(tahoe) */ 271*4887Schin return (x-p)-(x-p); /* create nan if x is inf */ 272*4887Schin if (p == zero) { 273*4887Schin #if defined(vax)||defined(tahoe) 274*4887Schin return(infnan(EDOM)); 275*4887Schin #else /* defined(vax)||defined(tahoe) */ 276*4887Schin return zero/zero; 277*4887Schin #endif /* defined(vax)||defined(tahoe) */ 278*4887Schin } 279*4887Schin 280*4887Schin #if defined(vax)||defined(tahoe) 281*4887Schin if( ( *pp & mexp ) == ~msign ) /* is p a reserved operand? */ 282*4887Schin #else /* defined(vax)||defined(tahoe) */ 283*4887Schin if( ( *pp & mexp ) == mexp ) 284*4887Schin #endif /* defined(vax)||defined(tahoe) */ 285*4887Schin { if (p != p) return p; else return x;} 286*4887Schin 287*4887Schin else if ( ((*pp & mexp)>>gap) <= 1 ) 288*4887Schin /* subnormal p, or almost subnormal p */ 289*4887Schin { double b; b=scalb(1.0,(int)prep1); 290*4887Schin p *= b; x = drem(x,p); x *= b; return(drem(x,p)/b);} 291*4887Schin else if ( p >= novf/2) 292*4887Schin { p /= 2 ; x /= 2; return(drem(x,p)*2);} 293*4887Schin else 294*4887Schin { 295*4887Schin dp=p+p; hp=p/2; 296*4887Schin sign= *px & ~msign ; 297*4887Schin *px &= msign ; 298*4887Schin while ( x > dp ) 299*4887Schin { 300*4887Schin k=(*px & mexp) - (*pd & mexp) ; 301*4887Schin tmp = dp ; 302*4887Schin *pt += k ; 303*4887Schin 304*4887Schin #if defined(vax)||defined(tahoe) 305*4887Schin if( x < tmp ) *pt -= 128 ; 306*4887Schin #else /* defined(vax)||defined(tahoe) */ 307*4887Schin if( x < tmp ) *pt -= 16 ; 308*4887Schin #endif /* defined(vax)||defined(tahoe) */ 309*4887Schin 310*4887Schin x -= tmp ; 311*4887Schin } 312*4887Schin if ( x > hp ) 313*4887Schin { x -= p ; if ( x >= hp ) x -= p ; } 314*4887Schin 315*4887Schin #if defined(vax)||defined(tahoe) 316*4887Schin if (x) 317*4887Schin #endif /* defined(vax)||defined(tahoe) */ 318*4887Schin *px ^= sign; 319*4887Schin return( x); 320*4887Schin 321*4887Schin } 322*4887Schin #endif 323*4887Schin } 324*4887Schin 325*4887Schin #endif 326*4887Schin 327*4887Schin #if !_lib_remainder 328*4887Schin 329*4887Schin extern double remainder(x,p) 330*4887Schin double x,p; 331*4887Schin { 332*4887Schin return drem(x,p); 333*4887Schin } 334*4887Schin 335*4887Schin #endif 336*4887Schin 337*4887Schin #if !_lib_sqrt 338*4887Schin 339*4887Schin extern double sqrt(x) 340*4887Schin double x; 341*4887Schin { 342*4887Schin double q,s,b,r; 343*4887Schin double t; 344*4887Schin double const zero=0.0; 345*4887Schin int m,n,i; 346*4887Schin #if defined(vax)||defined(tahoe) 347*4887Schin int k=54; 348*4887Schin #else /* defined(vax)||defined(tahoe) */ 349*4887Schin int k=51; 350*4887Schin #endif /* defined(vax)||defined(tahoe) */ 351*4887Schin 352*4887Schin /* sqrt(NaN) is NaN, sqrt(+-0) = +-0 */ 353*4887Schin if(x!=x||x==zero) return(x); 354*4887Schin 355*4887Schin /* sqrt(negative) is invalid */ 356*4887Schin if(x<zero) { 357*4887Schin #if defined(vax)||defined(tahoe) 358*4887Schin return (infnan(EDOM)); /* NaN */ 359*4887Schin #else /* defined(vax)||defined(tahoe) */ 360*4887Schin return(zero/zero); 361*4887Schin #endif /* defined(vax)||defined(tahoe) */ 362*4887Schin } 363*4887Schin 364*4887Schin /* sqrt(INF) is INF */ 365*4887Schin if(!finite(x)) return(x); 366*4887Schin 367*4887Schin /* scale x to [1,4) */ 368*4887Schin n=logb(x); 369*4887Schin x=scalb(x,-n); 370*4887Schin if((m=logb(x))!=0) x=scalb(x,-m); /* subnormal number */ 371*4887Schin m += n; 372*4887Schin n = m/2; 373*4887Schin if((n+n)!=m) {x *= 2; m -=1; n=m/2;} 374*4887Schin 375*4887Schin /* generate sqrt(x) bit by bit (accumulating in q) */ 376*4887Schin q=1.0; s=4.0; x -= 1.0; r=1; 377*4887Schin for(i=1;i<=k;i++) { 378*4887Schin t=s+1; x *= 4; r /= 2; 379*4887Schin if(t<=x) { 380*4887Schin s=t+t+2, x -= t; q += r;} 381*4887Schin else 382*4887Schin s *= 2; 383*4887Schin } 384*4887Schin 385*4887Schin /* generate the last bit and determine the final rounding */ 386*4887Schin r/=2; x *= 4; 387*4887Schin if(x==zero) goto end; 100+r; /* trigger inexact flag */ 388*4887Schin if(s<x) { 389*4887Schin q+=r; x -=s; s += 2; s *= 2; x *= 4; 390*4887Schin t = (x-s)-5; 391*4887Schin b=1.0+3*r/4; if(b==1.0) goto end; /* b==1 : Round-to-zero */ 392*4887Schin b=1.0+r/4; if(b>1.0) t=1; /* b>1 : Round-to-(+INF) */ 393*4887Schin if(t>=0) q+=r; } /* else: Round-to-nearest */ 394*4887Schin else { 395*4887Schin s *= 2; x *= 4; 396*4887Schin t = (x-s)-1; 397*4887Schin b=1.0+3*r/4; if(b==1.0) goto end; 398*4887Schin b=1.0+r/4; if(b>1.0) t=1; 399*4887Schin if(t>=0) q+=r; } 400*4887Schin 401*4887Schin end: return(scalb(q,n)); 402*4887Schin } 403*4887Schin 404*4887Schin #endif 405*4887Schin 406*4887Schin #if 0 407*4887Schin /* DREM(X,Y) 408*4887Schin * RETURN X REM Y =X-N*Y, N=[X/Y] ROUNDED (ROUNDED TO EVEN IN THE HALF WAY CASE) 409*4887Schin * DOUBLE PRECISION (VAX D format 56 bits, IEEE DOUBLE 53 BITS) 410*4887Schin * INTENDED FOR ASSEMBLY LANGUAGE 411*4887Schin * CODED IN C BY K.C. NG, 3/23/85, 4/8/85. 412*4887Schin * 413*4887Schin * Warning: this code should not get compiled in unless ALL of 414*4887Schin * the following machine-dependent routines are supplied. 415*4887Schin * 416*4887Schin * Required machine dependent functions (not on a VAX): 417*4887Schin * swapINX(i): save inexact flag and reset it to "i" 418*4887Schin * swapENI(e): save inexact enable and reset it to "e" 419*4887Schin */ 420*4887Schin 421*4887Schin extern double drem(x,y) 422*4887Schin double x,y; 423*4887Schin { 424*4887Schin 425*4887Schin #ifdef national /* order of words in floating point number */ 426*4887Schin static const n0=3,n1=2,n2=1,n3=0; 427*4887Schin #else /* VAX, SUN, ZILOG, TAHOE */ 428*4887Schin static const n0=0,n1=1,n2=2,n3=3; 429*4887Schin #endif 430*4887Schin 431*4887Schin static const unsigned short mexp =0x7ff0, m25 =0x0190, m57 =0x0390; 432*4887Schin static const double zero=0.0; 433*4887Schin double hy,y1,t,t1; 434*4887Schin short k; 435*4887Schin long n; 436*4887Schin int i,e; 437*4887Schin unsigned short xexp,yexp, *px =(unsigned short *) &x , 438*4887Schin nx,nf, *py =(unsigned short *) &y , 439*4887Schin sign, *pt =(unsigned short *) &t , 440*4887Schin *pt1 =(unsigned short *) &t1 ; 441*4887Schin 442*4887Schin xexp = px[n0] & mexp ; /* exponent of x */ 443*4887Schin yexp = py[n0] & mexp ; /* exponent of y */ 444*4887Schin sign = px[n0] &0x8000; /* sign of x */ 445*4887Schin 446*4887Schin /* return NaN if x is NaN, or y is NaN, or x is INF, or y is zero */ 447*4887Schin if(x!=x) return(x); if(y!=y) return(y); /* x or y is NaN */ 448*4887Schin if( xexp == mexp ) return(zero/zero); /* x is INF */ 449*4887Schin if(y==zero) return(y/y); 450*4887Schin 451*4887Schin /* save the inexact flag and inexact enable in i and e respectively 452*4887Schin * and reset them to zero 453*4887Schin */ 454*4887Schin i=swapINX(0); e=swapENI(0); 455*4887Schin 456*4887Schin /* subnormal number */ 457*4887Schin nx=0; 458*4887Schin if(yexp==0) {t=1.0,pt[n0]+=m57; y*=t; nx=m57;} 459*4887Schin 460*4887Schin /* if y is tiny (biased exponent <= 57), scale up y to y*2**57 */ 461*4887Schin if( yexp <= m57 ) {py[n0]+=m57; nx+=m57; yexp+=m57;} 462*4887Schin 463*4887Schin nf=nx; 464*4887Schin py[n0] &= 0x7fff; 465*4887Schin px[n0] &= 0x7fff; 466*4887Schin 467*4887Schin /* mask off the least significant 27 bits of y */ 468*4887Schin t=y; pt[n3]=0; pt[n2]&=0xf800; y1=t; 469*4887Schin 470*4887Schin /* LOOP: argument reduction on x whenever x > y */ 471*4887Schin loop: 472*4887Schin while ( x > y ) 473*4887Schin { 474*4887Schin t=y; 475*4887Schin t1=y1; 476*4887Schin xexp=px[n0]&mexp; /* exponent of x */ 477*4887Schin k=xexp-yexp-m25; 478*4887Schin if(k>0) /* if x/y >= 2**26, scale up y so that x/y < 2**26 */ 479*4887Schin {pt[n0]+=k;pt1[n0]+=k;} 480*4887Schin n=x/t; x=(x-n*t1)-n*(t-t1); 481*4887Schin } 482*4887Schin /* end while (x > y) */ 483*4887Schin 484*4887Schin if(nx!=0) {t=1.0; pt[n0]+=nx; x*=t; nx=0; goto loop;} 485*4887Schin 486*4887Schin /* final adjustment */ 487*4887Schin 488*4887Schin hy=y/2.0; 489*4887Schin if(x>hy||((x==hy)&&n%2==1)) x-=y; 490*4887Schin px[n0] ^= sign; 491*4887Schin if(nf!=0) { t=1.0; pt[n0]-=nf; x*=t;} 492*4887Schin 493*4887Schin /* restore inexact flag and inexact enable */ 494*4887Schin swapINX(i); swapENI(e); 495*4887Schin 496*4887Schin return(x); 497*4887Schin } 498*4887Schin #endif 499*4887Schin 500*4887Schin #if 0 501*4887Schin /* SQRT 502*4887Schin * RETURN CORRECTLY ROUNDED (ACCORDING TO THE ROUNDING MODE) SQRT 503*4887Schin * FOR IEEE DOUBLE PRECISION ONLY, INTENDED FOR ASSEMBLY LANGUAGE 504*4887Schin * CODED IN C BY K.C. NG, 3/22/85. 505*4887Schin * 506*4887Schin * Warning: this code should not get compiled in unless ALL of 507*4887Schin * the following machine-dependent routines are supplied. 508*4887Schin * 509*4887Schin * Required machine dependent functions: 510*4887Schin * swapINX(i) ...return the status of INEXACT flag and reset it to "i" 511*4887Schin * swapRM(r) ...return the current Rounding Mode and reset it to "r" 512*4887Schin * swapENI(e) ...return the status of inexact enable and reset it to "e" 513*4887Schin * addc(t) ...perform t=t+1 regarding t as a 64 bit unsigned integer 514*4887Schin * subc(t) ...perform t=t-1 regarding t as a 64 bit unsigned integer 515*4887Schin */ 516*4887Schin 517*4887Schin static const unsigned long table[] = { 518*4887Schin 0, 1204, 3062, 5746, 9193, 13348, 18162, 23592, 29598, 36145, 43202, 50740, 519*4887Schin 58733, 67158, 75992, 85215, 83599, 71378, 60428, 50647, 41945, 34246, 27478, 520*4887Schin 21581, 16499, 12183, 8588, 5674, 3403, 1742, 661, 130, }; 521*4887Schin 522*4887Schin extern double newsqrt(x) 523*4887Schin double x; 524*4887Schin { 525*4887Schin double y,z,t,addc(),subc() 526*4887Schin double const b54=134217728.*134217728.; /* b54=2**54 */ 527*4887Schin long mx,scalx; 528*4887Schin long const mexp=0x7ff00000; 529*4887Schin int i,j,r,e,swapINX(),swapRM(),swapENI(); 530*4887Schin unsigned long *py=(unsigned long *) &y , 531*4887Schin *pt=(unsigned long *) &t , 532*4887Schin *px=(unsigned long *) &x ; 533*4887Schin #ifdef national /* ordering of word in a floating point number */ 534*4887Schin const int n0=1, n1=0; 535*4887Schin #else 536*4887Schin const int n0=0, n1=1; 537*4887Schin #endif 538*4887Schin /* Rounding Mode: RN ...round-to-nearest 539*4887Schin * RZ ...round-towards 0 540*4887Schin * RP ...round-towards +INF 541*4887Schin * RM ...round-towards -INF 542*4887Schin */ 543*4887Schin const int RN=0,RZ=1,RP=2,RM=3; 544*4887Schin /* machine dependent: work on a Zilog Z8070 545*4887Schin * and a National 32081 & 16081 546*4887Schin */ 547*4887Schin 548*4887Schin /* exceptions */ 549*4887Schin if(x!=x||x==0.0) return(x); /* sqrt(NaN) is NaN, sqrt(+-0) = +-0 */ 550*4887Schin if(x<0) return((x-x)/(x-x)); /* sqrt(negative) is invalid */ 551*4887Schin if((mx=px[n0]&mexp)==mexp) return(x); /* sqrt(+INF) is +INF */ 552*4887Schin 553*4887Schin /* save, reset, initialize */ 554*4887Schin e=swapENI(0); /* ...save and reset the inexact enable */ 555*4887Schin i=swapINX(0); /* ...save INEXACT flag */ 556*4887Schin r=swapRM(RN); /* ...save and reset the Rounding Mode to RN */ 557*4887Schin scalx=0; 558*4887Schin 559*4887Schin /* subnormal number, scale up x to x*2**54 */ 560*4887Schin if(mx==0) {x *= b54 ; scalx-=0x01b00000;} 561*4887Schin 562*4887Schin /* scale x to avoid intermediate over/underflow: 563*4887Schin * if (x > 2**512) x=x/2**512; if (x < 2**-512) x=x*2**512 */ 564*4887Schin if(mx>0x5ff00000) {px[n0] -= 0x20000000; scalx+= 0x10000000;} 565*4887Schin if(mx<0x1ff00000) {px[n0] += 0x20000000; scalx-= 0x10000000;} 566*4887Schin 567*4887Schin /* magic initial approximation to almost 8 sig. bits */ 568*4887Schin py[n0]=(px[n0]>>1)+0x1ff80000; 569*4887Schin py[n0]=py[n0]-table[(py[n0]>>15)&31]; 570*4887Schin 571*4887Schin /* Heron's rule once with correction to improve y to almost 18 sig. bits */ 572*4887Schin t=x/y; y=y+t; py[n0]=py[n0]-0x00100006; py[n1]=0; 573*4887Schin 574*4887Schin /* triple to almost 56 sig. bits; now y approx. sqrt(x) to within 1 ulp */ 575*4887Schin t=y*y; z=t; pt[n0]+=0x00100000; t+=z; z=(x-z)*y; 576*4887Schin t=z/(t+x) ; pt[n0]+=0x00100000; y+=t; 577*4887Schin 578*4887Schin /* twiddle last bit to force y correctly rounded */ 579*4887Schin swapRM(RZ); /* ...set Rounding Mode to round-toward-zero */ 580*4887Schin swapINX(0); /* ...clear INEXACT flag */ 581*4887Schin swapENI(e); /* ...restore inexact enable status */ 582*4887Schin t=x/y; /* ...chopped quotient, possibly inexact */ 583*4887Schin j=swapINX(i); /* ...read and restore inexact flag */ 584*4887Schin if(j==0) { if(t==y) goto end; else t=subc(t); } /* ...t=t-ulp */ 585*4887Schin b54+0.1; /* ..trigger inexact flag, sqrt(x) is inexact */ 586*4887Schin if(r==RN) t=addc(t); /* ...t=t+ulp */ 587*4887Schin else if(r==RP) { t=addc(t);y=addc(y);}/* ...t=t+ulp;y=y+ulp; */ 588*4887Schin y=y+t; /* ...chopped sum */ 589*4887Schin py[n0]=py[n0]-0x00100000; /* ...correctly rounded sqrt(x) */ 590*4887Schin end: py[n0]=py[n0]+scalx; /* ...scale back y */ 591*4887Schin swapRM(r); /* ...restore Rounding Mode */ 592*4887Schin return(y); 593*4887Schin } 594*4887Schin #endif 595*4887Schin 596*4887Schin #if !_lib_ilogb 597*4887Schin 598*4887Schin extern int ilogb(double x) 599*4887Schin { 600*4887Schin return((int)logb(x)); 601*4887Schin } 602*4887Schin 603*4887Schin #endif 604*4887Schin 605*4887Schin #endif 606