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