1 /* @(#)e_fmod.c 1.3 95/01/18 */
2 /*-
3  * ====================================================
4  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
5  *
6  * Developed at SunSoft, a Sun Microsystems, Inc. business.
7  * Permission to use, copy, modify, and distribute this
8  * software is freely granted, provided that this notice
9  * is preserved.
10  * ====================================================
11  */
12 
13 
14 
15 
16 #define	BIAS (LDBL_MAX_EXP - 1)
17 
18 /*
19  * These macros add and remove an explicit integer bit in front of the
20  * fractional mantissa, if the architecture doesn't have such a bit by
21  * default already.
22  */
23 #ifdef LDBL_IMPLICIT_NBIT
24 #define	LDBL_NBIT	0
25 #define	SET_NBIT(hx)	((hx) | (1ULL << LDBL_MANH_SIZE))
26 #define	HFRAC_BITS	EXT_FRACHBITS
27 #else
28 #define	LDBL_NBIT	0x80000000
29 #define	SET_NBIT(hx)	(hx)
30 #define	HFRAC_BITS	(EXT_FRACHBITS - 1)
31 #endif
32 
33 #define	MANL_SHIFT	(EXT_FRACLBITS - 1)
34 
35 static const long double Zero[] = {0.0L, -0.0L};
36 
37 /*
38  * Return the IEEE remainder and set *quo to the last n bits of the
39  * quotient, rounded to the nearest integer.  We choose n=31 because
40  * we wind up computing all the integer bits of the quotient anyway as
41  * a side-effect of computing the remainder by the shift and subtract
42  * method.  In practice, this is far more bits than are needed to use
43  * remquo in reduction algorithms.
44  *
45  * Assumptions:
46  * - The low part of the mantissa fits in a manl_t exactly.
47  * - The high part of the mantissa fits in an int64_t with enough room
48  *   for an explicit integer bit in front of the fractional bits.
49  */
50 long double
remquol(long double x,long double y,int * quo)51 remquol(long double x, long double y, int *quo)
52 {
53 	int64_t hx,hz;	/* We need a carry bit even if LDBL_MANH_SIZE is 32. */
54 	uint32_t hy;
55 	uint32_t lx,ly,lz;
56 	uint32_t esx, esy;
57 	int ix,iy,n,q,sx,sxy;
58 
59 	GET_LDOUBLE_WORDS(esx,hx,lx,x);
60 	GET_LDOUBLE_WORDS(esy,hy,ly,y);
61 	sx = esx & 0x8000;
62 	sxy = sx ^ (esy & 0x8000);
63 	esx &= 0x7fff;				/* |x| */
64 	esy &= 0x7fff;				/* |y| */
65 	SET_LDOUBLE_EXP(x,esx);
66 	SET_LDOUBLE_EXP(y,esy);
67 
68     /* purge off exception values */
69 	if((esy|hy|ly)==0 ||			/* y=0 */
70 	   (esx == BIAS + LDBL_MAX_EXP) ||	/* or x not finite */
71 	   (esy == BIAS + LDBL_MAX_EXP &&
72 	    ((hy&~LDBL_NBIT)|ly)!=0)) {		/* or y is NaN */
73             *quo = 0;
74 	    return (x*y)/(x*y);
75         }
76 	if(esx<=esy) {
77 	    if((esx<esy) ||
78 	       (hx<=hy &&
79 		(hx<hy ||
80 		 lx<ly))) {
81 		q = 0;
82 		goto fixup;			/* |x|<|y| return x or x-y */
83 	    }
84 	    if(hx==hy && lx==ly) {
85 		*quo = 1;
86 		return Zero[sx!=0];		/* |x|=|y| return x*0*/
87 	    }
88 	}
89 
90     /* determine ix = ilogb(x) */
91 	if(esx == 0) {				/* subnormal x */
92 	    x *= 0x1.0p512L;
93 	    GET_LDOUBLE_WORDS(esx,hx,lx,x);
94 	    ix = esx - (BIAS + 512);
95 	} else {
96 	    ix = esx - BIAS;
97 	}
98 
99     /* determine iy = ilogb(y) */
100 	if(esy == 0) {				/* subnormal y */
101 	    y *= 0x1.0p512L;
102 	    GET_LDOUBLE_WORDS(esy,hy,ly,y);
103 	    iy = esy - (BIAS + 512);
104 	} else {
105 	    iy = esy - BIAS;
106 	}
107 
108     /* set up {hx,lx}, {hy,ly} and align y to x */
109 	hx = SET_NBIT(hx);
110 	lx = SET_NBIT(lx);
111 
112     /* fix point fmod */
113 	n = ix - iy;
114 	q = 0;
115 
116 	while(n--) {
117 	    hz=hx-hy;lz=lx-ly; if(lx<ly) hz -= 1;
118 	    if(hz<0){hx = hx+hx+(lx>>MANL_SHIFT); lx = lx+lx;}
119 	    else {hx = hz+hz+(lz>>MANL_SHIFT); lx = lz+lz; q++;}
120 	    q <<= 1;
121 	}
122 	hz=hx-hy;lz=lx-ly; if(lx<ly) hz -= 1;
123 	if(hz>=0) {hx=hz;lx=lz;q++;}
124 
125     /* convert back to floating value and restore the sign */
126 	if((hx|lx)==0) {			/* return sign(x)*0 */
127 	    *quo = (sxy ? -q : q);
128 	    return Zero[sx!=0];
129 	}
130 	while(hx<(1LL<<HFRAC_BITS)) {	/* normalize x */
131 	    hx = hx+hx+(lx>>MANL_SHIFT); lx = lx+lx;
132 	    iy -= 1;
133 	}
134 	if (iy < LDBL_MIN_EXP) {
135 	    esx = (iy + BIAS + 512) & 0x7fff;
136 	    SET_LDOUBLE_WORDS(x,esx,hx,lx);
137 	    x *= 0x1p-512L;
138 	    GET_LDOUBLE_WORDS(esx,hx,lx,x);
139 	} else {
140 	    esx = (iy + BIAS) & 0x7fff;
141 	}
142 	SET_LDOUBLE_WORDS(x,esx,hx,lx);
143 fixup:
144 	y = fabsl(y);
145 	if (y < LDBL_MIN * 2) {
146 	    if (x+x>y || (x+x==y && (q & 1))) {
147 		q++;
148 		x-=y;
149 	    }
150 	} else if (x>0.5L*y || (x==0.5L*y && (q & 1))) {
151 	    q++;
152 	    x-=y;
153 	}
154 
155 	GET_LDOUBLE_EXP(esx,x);
156 	esx ^= sx;
157 	SET_LDOUBLE_EXP(x,esx);
158 
159 	q &= 0x7fffffff;
160 	*quo = (sxy ? -q : q);
161 	return x;
162 }
163