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