xref: /onnv-gate/usr/src/lib/libbc/libc/gen/common/fmod.c (revision 722:636b850d4ee9)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
22*722Smuffin /*
23*722Smuffin  * Copyright 1988 Sun Microsystems, Inc.  All rights reserved.
24*722Smuffin  * Use is subject to license terms.
25*722Smuffin  */
260Sstevel@tonic-gate 
27*722Smuffin #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate /* Special version adapted from libm for use in libc. */
300Sstevel@tonic-gate 
31*722Smuffin static int	n0 = 0, n1 = 1;
320Sstevel@tonic-gate 
330Sstevel@tonic-gate static double   two52 = 4.503599627370496000E+15;
340Sstevel@tonic-gate static double   twom52 = 2.220446049250313081E-16;
350Sstevel@tonic-gate 
360Sstevel@tonic-gate static double
setexception(int n,double x)37*722Smuffin setexception(int n, double x)
380Sstevel@tonic-gate {
39*722Smuffin 	return (0.0);
400Sstevel@tonic-gate }
410Sstevel@tonic-gate 
420Sstevel@tonic-gate double
copysign(double x,double y)43*722Smuffin copysign(double x, double y)
440Sstevel@tonic-gate {
450Sstevel@tonic-gate 	long           *px = (long *) &x;
460Sstevel@tonic-gate 	long           *py = (long *) &y;
470Sstevel@tonic-gate 	px[n0] = (px[n0] & 0x7fffffff) | (py[n0] & 0x80000000);
48*722Smuffin 	return (x);
490Sstevel@tonic-gate }
500Sstevel@tonic-gate 
510Sstevel@tonic-gate static double
fabs(double x)52*722Smuffin fabs(double x)
530Sstevel@tonic-gate {
540Sstevel@tonic-gate 	long           *px = (long *) &x;
550Sstevel@tonic-gate 	px[0] &= 0x7fffffff;
56*722Smuffin 
57*722Smuffin 	return (x);
580Sstevel@tonic-gate }
590Sstevel@tonic-gate 
600Sstevel@tonic-gate static int
finite(double x)61*722Smuffin finite(double x)
620Sstevel@tonic-gate {
630Sstevel@tonic-gate 	long           *px = (long *) &x;
640Sstevel@tonic-gate 	return ((px[n0] & 0x7ff00000) != 0x7ff00000);
650Sstevel@tonic-gate }
660Sstevel@tonic-gate 
670Sstevel@tonic-gate static int
ilogb(double x)68*722Smuffin ilogb(double x)
690Sstevel@tonic-gate {
700Sstevel@tonic-gate 	long           *px = (long *) &x, k;
710Sstevel@tonic-gate 	k = px[n0] & 0x7ff00000;
720Sstevel@tonic-gate 	if (k == 0) {
730Sstevel@tonic-gate 		if ((px[n1] | (px[n0] & 0x7fffffff)) == 0)
74*722Smuffin 			return (0x80000001);
750Sstevel@tonic-gate 		else {
760Sstevel@tonic-gate 			x *= two52;
770Sstevel@tonic-gate 			return ((px[n0] & 0x7ff00000) >> 20) - 1075;
780Sstevel@tonic-gate 		}
790Sstevel@tonic-gate 	} else if (k != 0x7ff00000)
800Sstevel@tonic-gate 		return (k >> 20) - 1023;
810Sstevel@tonic-gate 	else
82*722Smuffin 		return (0x7fffffff);
830Sstevel@tonic-gate }
840Sstevel@tonic-gate 
850Sstevel@tonic-gate static double
scalbn(double x,int n)86*722Smuffin scalbn(double x, int n)
870Sstevel@tonic-gate {
880Sstevel@tonic-gate 	long           *px = (long *) &x, k;
890Sstevel@tonic-gate 	double          twom54 = twom52 * 0.25;
900Sstevel@tonic-gate 	k = (px[n0] & 0x7ff00000) >> 20;
910Sstevel@tonic-gate 	if (k == 0x7ff)
92*722Smuffin 		return (x + x);
930Sstevel@tonic-gate 	if ((px[n1] | (px[n0] & 0x7fffffff)) == 0)
94*722Smuffin 		return (x);
950Sstevel@tonic-gate 	if (k == 0) {
960Sstevel@tonic-gate 		x *= two52;
970Sstevel@tonic-gate 		k = ((px[n0] & 0x7ff00000) >> 20) - 52;
980Sstevel@tonic-gate 	}
990Sstevel@tonic-gate 	k = k + n;
1000Sstevel@tonic-gate 	if (n > 5000)
101*722Smuffin 		return (setexception(2, x));
1020Sstevel@tonic-gate 	if (n < -5000)
103*722Smuffin 		return (setexception(1, x));
1040Sstevel@tonic-gate 	if (k > 0x7fe)
105*722Smuffin 		return (setexception(2, x));
1060Sstevel@tonic-gate 	if (k <= -54)
107*722Smuffin 		return (setexception(1, x));
1080Sstevel@tonic-gate 	if (k > 0) {
1090Sstevel@tonic-gate 		px[n0] = (px[n0] & 0x800fffff) | (k << 20);
110*722Smuffin 		return (x);
1110Sstevel@tonic-gate 	}
1120Sstevel@tonic-gate 	k += 54;
1130Sstevel@tonic-gate 	px[n0] = (px[n0] & 0x800fffff) | (k << 20);
114*722Smuffin 	return (x * twom54);
1150Sstevel@tonic-gate }
1160Sstevel@tonic-gate 
1170Sstevel@tonic-gate double
fmod(double x,double y)118*722Smuffin fmod(double x, double y)
1190Sstevel@tonic-gate {
1200Sstevel@tonic-gate 	int             ny, nr;
1210Sstevel@tonic-gate 	double          r, z, w;
122*722Smuffin 
123*722Smuffin 	int finite(), ilogb();
124*722Smuffin 	double fabs(), scalbn(), copysign();
1250Sstevel@tonic-gate 
1260Sstevel@tonic-gate 	/* purge off exception values */
1270Sstevel@tonic-gate 	if (!finite(x) || y != y || y == 0.0) {
128*722Smuffin 		return ((x * y) / (x * y));
1290Sstevel@tonic-gate 	}
1300Sstevel@tonic-gate 	/* scale and subtract to get the remainder */
1310Sstevel@tonic-gate 	r = fabs(x);
1320Sstevel@tonic-gate 	y = fabs(y);
1330Sstevel@tonic-gate 	ny = ilogb(y);
1340Sstevel@tonic-gate 	while (r >= y) {
1350Sstevel@tonic-gate 		nr = ilogb(r);
1360Sstevel@tonic-gate 		if (nr == ny)
1370Sstevel@tonic-gate 			w = y;
1380Sstevel@tonic-gate 		else {
1390Sstevel@tonic-gate 			z = scalbn(y, nr - ny - 1);
1400Sstevel@tonic-gate 			w = z + z;
1410Sstevel@tonic-gate 		}
1420Sstevel@tonic-gate 		if (r >= w)
1430Sstevel@tonic-gate 			r -= w;
1440Sstevel@tonic-gate 		else
1450Sstevel@tonic-gate 			r -= z;
1460Sstevel@tonic-gate 	}
1470Sstevel@tonic-gate 
1480Sstevel@tonic-gate 	/* restore sign */
149*722Smuffin 	return (copysign(r, x));
1500Sstevel@tonic-gate }
151