xref: /openbsd-src/lib/libm/src/ld80/e_fmodl.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/e_fmodl.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 one = 1.0, Zero[] = {0.0, -0.0,};
47 
48 /*
49  * fmodl(x,y)
50  * Return x mod y in exact arithmetic
51  * Method: shift and subtract
52  *
53  * Assumptions:
54  * - The low part of the mantissa fits in a manl_t exactly.
55  * - The high part of the mantissa fits in an int64_t with enough room
56  *   for an explicit integer bit in front of the fractional bits.
57  */
58 long double
59 fmodl(long double x, long double y)
60 {
61 	union {
62 		long double e;
63 		struct ieee_ext bits;
64 	} ux, uy;
65 	int64_t hx,hz;	/* We need a carry bit even if LDBL_MANH_SIZE is 32. */
66 	uint32_t hy;
67 	uint32_t lx,ly,lz;
68 	int ix,iy,n,sx;
69 
70 	ux.e = x;
71 	uy.e = y;
72 	sx = ux.bits.ext_sign;
73 
74     /* purge off exception values */
75 	if((uy.bits.ext_exp|uy.bits.ext_frach|uy.bits.ext_fracl)==0 || /* y=0 */
76 	   (ux.bits.ext_exp == BIAS + LDBL_MAX_EXP) ||	 /* or x not finite */
77 	   (uy.bits.ext_exp == BIAS + LDBL_MAX_EXP &&
78 	    ((uy.bits.ext_frach&~LDBL_NBIT)|uy.bits.ext_fracl)!=0)) /* or y is NaN */
79 	    return (x*y)/(x*y);
80 	if(ux.bits.ext_exp<=uy.bits.ext_exp) {
81 	    if((ux.bits.ext_exp<uy.bits.ext_exp) ||
82 	       (ux.bits.ext_frach<=uy.bits.ext_frach &&
83 		(ux.bits.ext_frach<uy.bits.ext_frach ||
84 		 ux.bits.ext_fracl<uy.bits.ext_fracl))) {
85 		return x;		/* |x|<|y| return x or x-y */
86 	    }
87 	    if(ux.bits.ext_frach==uy.bits.ext_frach &&
88 		ux.bits.ext_fracl==uy.bits.ext_fracl) {
89 		return Zero[sx];	/* |x|=|y| return x*0*/
90 	    }
91 	}
92 
93     /* determine ix = ilogb(x) */
94 	if(ux.bits.ext_exp == 0) {	/* subnormal x */
95 	    ux.e *= 0x1.0p512;
96 	    ix = ux.bits.ext_exp - (BIAS + 512);
97 	} else {
98 	    ix = ux.bits.ext_exp - BIAS;
99 	}
100 
101     /* determine iy = ilogb(y) */
102 	if(uy.bits.ext_exp == 0) {	/* subnormal y */
103 	    uy.e *= 0x1.0p512;
104 	    iy = uy.bits.ext_exp - (BIAS + 512);
105 	} else {
106 	    iy = uy.bits.ext_exp - BIAS;
107 	}
108 
109     /* set up {hx,lx}, {hy,ly} and align y to x */
110 	hx = SET_NBIT(ux.bits.ext_frach);
111 	hy = SET_NBIT(uy.bits.ext_frach);
112 	lx = ux.bits.ext_fracl;
113 	ly = uy.bits.ext_fracl;
114 
115     /* fix point fmod */
116 	n = ix - iy;
117 
118 	while(n--) {
119 	    hz=hx-hy;lz=lx-ly; if(lx<ly) hz -= 1;
120 	    if(hz<0){hx = hx+hx+(lx>>MANL_SHIFT); lx = lx+lx;}
121 	    else {
122 		if ((hz|lz)==0)		/* return sign(x)*0 */
123 		    return Zero[sx];
124 		hx = hz+hz+(lz>>MANL_SHIFT); lx = lz+lz;
125 	    }
126 	}
127 	hz=hx-hy;lz=lx-ly; if(lx<ly) hz -= 1;
128 	if(hz>=0) {hx=hz;lx=lz;}
129 
130     /* convert back to floating value and restore the sign */
131 	if((hx|lx)==0)			/* return sign(x)*0 */
132 	    return Zero[sx];
133 	while(hx<(1ULL<<HFRAC_BITS)) {	/* normalize x */
134 	    hx = hx+hx+(lx>>MANL_SHIFT); lx = lx+lx;
135 	    iy -= 1;
136 	}
137 	ux.bits.ext_frach = hx; /* The mantissa is truncated here if needed. */
138 	ux.bits.ext_fracl = lx;
139 	if (iy < LDBL_MIN_EXP) {
140 	    ux.bits.ext_exp = iy + (BIAS + 512);
141 	    ux.e *= 0x1p-512;
142 	} else {
143 	    ux.bits.ext_exp = iy + BIAS;
144 	}
145 	x = ux.e * one;		/* create necessary signal */
146 	return x;		/* exact output */
147 }
148