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