Cygwin: mmap: allow remapping part of an existing anonymous mapping
[newlib-cygwin.git] / newlib / libm / mathfp / s_fmod.c
blob4197ea8322575336168bc1dfe50ee16c1a94f627
2 /* @(#)z_fmod.c 1.0 98/08/13 */
3 /*
4 * ====================================================
5 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
7 * Developed at SunPro, a Sun Microsystems, Inc. business.
8 * Permission to use, copy, modify, and distribute this
9 * software is freely granted, provided that this notice
10 * is preserved.
11 * ====================================================
15 FUNCTION
16 <<fmod>>, <<fmodf>>---floating-point remainder (modulo)
18 INDEX
19 fmod
20 INDEX
21 fmodf
23 SYNOPSIS
24 #include <math.h>
25 double fmod(double <[x]>, double <[y]>);
26 float fmodf(float <[x]>, float <[y]>);
28 DESCRIPTION
29 The <<fmod>> and <<fmodf>> functions compute the floating-point
30 remainder of <[x]>/<[y]> (<[x]> modulo <[y]>).
32 RETURNS
33 The <<fmod>> function returns the value
34 @ifnottex
35 <[x]>-<[i]>*<[y]>,
36 @end ifnottex
37 @tex
38 $x-i\times y$,
39 @end tex
40 for the largest integer <[i]> such that, if <[y]> is nonzero, the
41 result has the same sign as <[x]> and magnitude less than the
42 magnitude of <[y]>.
44 <<fmod(<[x]>,0)>> returns NaN, and sets <<errno>> to <<EDOM>>.
46 PORTABILITY
47 <<fmod>> is ANSI C. <<fmodf>> is an extension.
50 /*
51 * fmod(x,y)
52 * Return x mod y in exact arithmetic
53 * Method: shift and subtract
56 #include "fdlibm.h"
57 #include "zmath.h"
59 #ifndef _DOUBLE_IS_32BITS
61 #ifdef __STDC__
62 static const double one = 1.0, Zero[] = {0.0, -0.0,};
63 #else
64 static double one = 1.0, Zero[] = {0.0, -0.0,};
65 #endif
67 #ifdef __STDC__
68 double fmod(double x, double y)
69 #else
70 double fmod(x,y)
71 double x,y ;
72 #endif
74 __int32_t n,hx,hy,hz,ix,iy,sx,i;
75 __uint32_t lx,ly,lz;
77 EXTRACT_WORDS(hx,lx,x);
78 EXTRACT_WORDS(hy,ly,y);
79 sx = hx&0x80000000; /* sign of x */
80 hx ^=sx; /* |x| */
81 hy &= 0x7fffffff; /* |y| */
83 /* purge off exception values */
84 if((hy|ly)==0||(hx>=0x7ff00000)|| /* y=0,or x not finite */
85 ((hy|((ly|-ly)>>31))>0x7ff00000)) /* or y is NaN */
86 return (x*y)/(x*y);
87 if(hx<=hy) {
88 if((hx<hy)||(lx<ly)) return x; /* |x|<|y| return x */
89 if(lx==ly)
90 return Zero[(__uint32_t)sx>>31]; /* |x|=|y| return x*0*/
93 /* determine ix = ilogb(x) */
94 if(hx<0x00100000) { /* subnormal x */
95 if(hx==0) {
96 for (ix = -1043, i=lx; i>0; i<<=1) ix -=1;
97 } else {
98 for (ix = -1022,i=(hx<<11); i>0; i<<=1) ix -=1;
100 } else ix = (hx>>20)-1023;
102 /* determine iy = ilogb(y) */
103 if(hy<0x00100000) { /* subnormal y */
104 if(hy==0) {
105 for (iy = -1043, i=ly; i>0; i<<=1) iy -=1;
106 } else {
107 for (iy = -1022,i=(hy<<11); i>0; i<<=1) iy -=1;
109 } else iy = (hy>>20)-1023;
111 /* set up {hx,lx}, {hy,ly} and align y to x */
112 if(ix >= -1022)
113 hx = 0x00100000|(0x000fffff&hx);
114 else { /* subnormal x, shift x to normal */
115 n = -1022-ix;
116 if(n<=31) {
117 hx = (hx<<n)|(lx>>(32-n));
118 lx <<= n;
119 } else {
120 hx = lx<<(n-32);
121 lx = 0;
124 if(iy >= -1022)
125 hy = 0x00100000|(0x000fffff&hy);
126 else { /* subnormal y, shift y to normal */
127 n = -1022-iy;
128 if(n<=31) {
129 hy = (hy<<n)|(ly>>(32-n));
130 ly <<= n;
131 } else {
132 hy = ly<<(n-32);
133 ly = 0;
137 /* fix point fmod */
138 n = ix - iy;
139 while(n--) {
140 hz=hx-hy;lz=lx-ly; if(lx<ly) hz -= 1;
141 if(hz<0){hx = hx+hx+(lx>>31); lx = lx+lx;}
142 else {
143 if((hz|lz)==0) /* return sign(x)*0 */
144 return Zero[(__uint32_t)sx>>31];
145 hx = hz+hz+(lz>>31); lx = lz+lz;
148 hz=hx-hy;lz=lx-ly; if(lx<ly) hz -= 1;
149 if(hz>=0) {hx=hz;lx=lz;}
151 /* convert back to floating value and restore the sign */
152 if((hx|lx)==0) /* return sign(x)*0 */
153 return Zero[(__uint32_t)sx>>31];
154 while(hx<0x00100000) { /* normalize x */
155 hx = hx+hx+(lx>>31); lx = lx+lx;
156 iy -= 1;
158 if(iy>= -1022) { /* normalize output */
159 hx = ((hx-0x00100000)|((iy+1023)<<20));
160 INSERT_WORDS(x,hx|sx,lx);
161 } else { /* subnormal output */
162 n = -1022 - iy;
163 if(n<=20) {
164 lx = (lx>>n)|((__uint32_t)hx<<(32-n));
165 hx >>= n;
166 } else if (n<=31) {
167 lx = (hx<<(32-n))|(lx>>n); hx = sx;
168 } else {
169 lx = hx>>(n-32); hx = sx;
171 INSERT_WORDS(x,hx|sx,lx);
172 x *= one; /* create necessary signal */
174 return x; /* exact output */
177 #endif /* defined(_DOUBLE_IS_32BITS) */