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