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