1 /* Adapted for Newlib, 2009.  (Allow for int < 32 bits; return *quo=0 during
2  * errors to make test scripts easier.)  */
3 /* @(#)e_fmod.c 1.3 95/01/18 */
4 /*-
5  * ====================================================
6  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
7  *
8  * Developed at SunSoft, a Sun Microsystems, Inc. business.
9  * Permission to use, copy, modify, and distribute this
10  * software is freely granted, provided that this notice
11  * is preserved.
12  * ====================================================
13  */
14 /*
15 FUNCTION
16 <<remquo>>, <<remquof>>---remainder and part of quotient
17 INDEX
18 	remquo
19 INDEX
20 	remquof
21 
22 SYNOPSIS
23 	#include <math.h>
24 	double remquo(double <[x]>, double <[y]>, int *<[quo]>);
25 	float remquof(float <[x]>, float <[y]>, int *<[quo]>);
26 
27 DESCRIPTION
28 The <<remquo>> functions compute the same remainder as the <<remainder>>
29 functions; this value is in the range -<[y]>/2 ... +<[y]>/2.  In the object
30 pointed to by <<quo>> they store a value whose sign is the sign of <<x>>/<<y>>
31 and whose magnitude is congruent modulo 2**n to the magnitude of the integral
32 quotient of <<x>>/<<y>>.  (That is, <<quo>> is given the n lsbs of the
33 quotient, not counting the sign.)  This implementation uses n=31 if int is 32
34 bits or more, otherwise, n is 1 less than the width of int.
35 
36 For example:
37 .	remquo(-29.0, 3.0, &<[quo]>)
38 returns -1.0 and sets <[quo]>=10, and
39 .	remquo(-98307.0, 3.0, &<[quo]>)
40 returns -0.0 and sets <[quo]>=-32769, although for 16-bit int, <[quo]>=-1.  In
41 the latter case, the actual quotient of -(32769=0x8001) is reduced to -1
42 because of the 15-bit limitation for the quotient.
43 
44 RETURNS
45 When either argument is NaN, NaN is returned.  If <[y]> is 0 or <[x]> is
46 infinite (and neither is NaN), a domain error occurs (i.e. the "invalid"
47 floating point exception is raised or errno is set to EDOM), and NaN is
48 returned.
49 Otherwise, the <<remquo>> functions return <[x]> REM <[y]>.
50 
51 BUGS
52 IEEE754-2008 calls for <<remquo>>(subnormal, inf) to cause the "underflow"
53 floating-point exception.  This implementation does not.
54 
55 PORTABILITY
56 C99, POSIX.
57 
58 */
59 
60 #include "fdlibm.h"
61 
62 #ifdef _NEED_FLOAT64
63 
64 #include <limits.h>
65 #include <math.h>
66 
67 /* For quotient, return either all 31 bits that can from calculation (using
68  * int32_t), or as many as can fit into an int that is smaller than 32 bits.  */
69 #if INT_MAX > 0x7FFFFFFFL
70   #define QUO_MASK 0x7FFFFFFF
71 # else
72   #define QUO_MASK INT_MAX
73 #endif
74 
75 static const __float64 Zero[] = {_F_64(0.0), _F_64(-0.0),};
76 
77 /*
78  * Return the IEEE remainder and set *quo to the last n bits of the
79  * quotient, rounded to the nearest integer.  We choose n=31--if that many fit--
80  * because we wind up computing all the integer bits of the quotient anyway as
81  * a side-effect of computing the remainder by the shift and subtract
82  * method.  In practice, this is far more bits than are needed to use
83  * remquo in reduction algorithms.
84  */
85 __float64
remquo64(__float64 x,__float64 y,int * quo)86 remquo64(__float64 x, __float64 y, int *quo)
87 {
88 	__int32_t n,hx,hy,hz,ix,iy,sx,i;
89 	__uint32_t lx,ly,lz,q,sxy;
90 
91 	EXTRACT_WORDS(hx,lx,x);
92 	EXTRACT_WORDS(hy,ly,y);
93 	sxy = (hx ^ hy) & 0x80000000;
94 	sx = hx&0x80000000;		/* sign of x */
95 	hx ^=sx;		/* |x| */
96 	hy &= 0x7fffffff;	/* |y| */
97 
98     /* purge off exception values */
99 	if((hy|ly)==0||(hx>=0x7ff00000)||	/* y=0,or x not finite */
100 	  ((hy|((ly|-ly)>>31))>0x7ff00000))  {	/* or y is NaN */
101 	    *quo = 0;	/* Not necessary, but return consistent value */
102 	    return (x*y)/(x*y);
103 	}
104 	if(hx<=hy) {
105 	    if((hx<hy)||(lx<ly)) {
106 		q = 0;
107 		goto fixup;	/* |x|<|y| return x or x-y */
108 	    }
109 	    if(lx==ly) {
110 		*quo = (sxy ? -1 : 1);
111 		return Zero[(__uint32_t)sx>>31];	/* |x|=|y| return x*0 */
112 	    }
113 	}
114 
115     /* determine ix = ilogb(x) */
116 	if(hx<0x00100000) {	/* subnormal x */
117 	    if(hx==0) {
118 		for (ix = -1043, i=lx; i>0; i<<=1) ix -=1;
119 	    } else {
120 		for (ix = -1022,i=(hx<<11); i>0; i<<=1) ix -=1;
121 	    }
122 	} else ix = (hx>>20)-1023;
123 
124     /* determine iy = ilogb(y) */
125 	if(hy<0x00100000) {	/* subnormal y */
126 	    if(hy==0) {
127 		for (iy = -1043, i=ly; i>0; i<<=1) iy -=1;
128 	    } else {
129 		for (iy = -1022,i=(hy<<11); i>0; i<<=1) iy -=1;
130 	    }
131 	} else iy = (hy>>20)-1023;
132 
133     /* set up {hx,lx}, {hy,ly} and align y to x */
134 	if(ix >= -1022)
135 	    hx = 0x00100000|(0x000fffff&hx);
136 	else {		/* subnormal x, shift x to normal */
137 	    n = -1022-ix;
138 	    if(n<=31) {
139 	        hx = (hx<<n)|(lx>>(32-n));
140 	        lx <<= n;
141 	    } else {
142 		hx = lx<<(n-32);
143 		lx = 0;
144 	    }
145 	}
146 	if(iy >= -1022)
147 	    hy = 0x00100000|(0x000fffff&hy);
148 	else {		/* subnormal y, shift y to normal */
149 	    n = -1022-iy;
150 	    if(n<=31) {
151 	        hy = (hy<<n)|(ly>>(32-n));
152 	        ly <<= n;
153 	    } else {
154 		hy = ly<<(n-32);
155 		ly = 0;
156 	    }
157 	}
158 
159     /* fix point fmod */
160 	n = ix - iy;
161 	q = 0;
162 	while(n--) {
163 	    hz=hx-hy;lz=lx-ly; if(lx<ly) hz -= 1;
164 	    if(hz<0){hx = hx+hx+(lx>>31); lx = lx+lx;}
165 	    else {hx = hz+hz+(lz>>31); lx = lz+lz; q++;}
166 	    q <<= 1;
167 	}
168 	hz=hx-hy;lz=lx-ly; if(lx<ly) hz -= 1;
169 	if(hz>=0) {hx=hz;lx=lz;q++;}
170 
171     /* convert back to floating value and restore the sign */
172 	if((hx|lx)==0) {			/* return sign(x)*0 */
173 	    q &= QUO_MASK;
174 	    *quo = (sxy ? -q : q);
175 	    return Zero[(__uint32_t)sx>>31];
176 	}
177 	while(hx<0x00100000) {		/* normalize x */
178 	    hx = hx+hx+(lx>>31); lx = lx+lx;
179 	    iy -= 1;
180 	}
181 	if(iy>= -1022) {	/* normalize output */
182 	    hx = ((hx-0x00100000)|((iy+1023)<<20));
183 	} else {		/* subnormal output */
184 	    n = -1022 - iy;
185 	    if(n<=20) {
186 		lx = (lx>>n)|((__uint32_t)hx<<(32-n));
187 		hx >>= n;
188 	    } else if (n<=31) {
189 		lx = (hx<<(32-n))|(lx>>n); hx = sx;
190 	    } else {
191 		lx = hx>>(n-32); hx = sx;
192 	    }
193 	}
194 fixup:
195 	INSERT_WORDS(x,hx,lx);
196 	y = fabs64(y);
197 	if (y < _F_64(0x1p-1021)) {
198 	    if (x+x>y || (x+x==y && (q & 1))) {
199 		q++;
200 		x-=y;
201 	    }
202 	} else if (x>_F_64(0.5)*y || (x==_F_64(0.5)*y && (q & 1))) {
203 	    q++;
204 	    x-=y;
205 	}
206 	GET_HIGH_WORD(hx,x);
207 	SET_HIGH_WORD(x,hx^sx);
208 	q &= QUO_MASK;
209 	*quo = (sxy ? -q : q);
210 	return x;
211 }
212 
213 _MATH_ALIAS_d_ddI(remquo)
214 
215 #endif /* _NEED_FLOAT64 */
216