source: trunk/libs/newlib/src/newlib/libm/mathfp/s_fmod.c @ 471

Last change on this file since 471 was 444, checked in by satin@…, 6 years ago

add newlib,libalmos-mkh, restructure shared_syscalls.h and mini-libc

File size: 4.1 KB
Line 
1
2/* @(#)z_fmod.c 1.0 98/08/13 */
3/*
4 * ====================================================
5 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
6 *
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 * ====================================================
12 */
13
14/*
15FUNCTION
16<<fmod>>, <<fmodf>>---floating-point remainder (modulo)
17
18INDEX
19fmod
20INDEX
21fmodf
22
23SYNOPSIS
24#include <math.h>
25double fmod(double <[x]>, double <[y]>);
26float fmodf(float <[x]>, float <[y]>);
27
28DESCRIPTION
29The <<fmod>> and <<fmodf>> functions compute the floating-point
30remainder of <[x]>/<[y]> (<[x]> modulo <[y]>).
31
32RETURNS
33The <<fmod>> function returns the value
34@ifnottex
35<[x]>-<[i]>*<[y]>,
36@end ifnottex
37@tex
38$x-i\times y$,
39@end tex
40for the largest integer <[i]> such that, if <[y]> is nonzero, the
41result has the same sign as <[x]> and magnitude less than the
42magnitude of <[y]>.
43
44<<fmod(<[x]>,0)>> returns NaN, and sets <<errno>> to <<EDOM>>.
45
46You can modify error treatment for these functions using <<matherr>>.
47
48PORTABILITY
49<<fmod>> is ANSI C. <<fmodf>> is an extension.
50*/
51
52/*
53 * fmod(x,y)
54 * Return x mod y in exact arithmetic
55 * Method: shift and subtract
56 */
57
58#include "fdlibm.h"
59#include "zmath.h"
60
61#ifndef _DOUBLE_IS_32BITS
62
63#ifdef __STDC__
64static const double one = 1.0, Zero[] = {0.0, -0.0,};
65#else
66static double one = 1.0, Zero[] = {0.0, -0.0,};
67#endif
68
69#ifdef __STDC__
70        double fmod(double x, double y)
71#else
72        double fmod(x,y)
73        double x,y ;
74#endif
75{
76        __int32_t n,hx,hy,hz,ix,iy,sx,i;
77        __uint32_t lx,ly,lz;
78
79        EXTRACT_WORDS(hx,lx,x);
80        EXTRACT_WORDS(hy,ly,y);
81        sx = hx&0x80000000;             /* sign of x */
82        hx ^=sx;                /* |x| */
83        hy &= 0x7fffffff;       /* |y| */
84
85    /* purge off exception values */
86        if((hy|ly)==0||(hx>=0x7ff00000)||       /* y=0,or x not finite */
87          ((hy|((ly|-ly)>>31))>0x7ff00000))     /* or y is NaN */
88            return (x*y)/(x*y);
89        if(hx<=hy) {
90            if((hx<hy)||(lx<ly)) return x;      /* |x|<|y| return x */
91            if(lx==ly) 
92                return Zero[(__uint32_t)sx>>31];        /* |x|=|y| return x*0*/
93        }
94
95    /* determine ix = ilogb(x) */
96        if(hx<0x00100000) {     /* subnormal x */
97            if(hx==0) {
98                for (ix = -1043, i=lx; i>0; i<<=1) ix -=1;
99            } else {
100                for (ix = -1022,i=(hx<<11); i>0; i<<=1) ix -=1;
101            }
102        } else ix = (hx>>20)-1023;
103
104    /* determine iy = ilogb(y) */
105        if(hy<0x00100000) {     /* subnormal y */
106            if(hy==0) {
107                for (iy = -1043, i=ly; i>0; i<<=1) iy -=1;
108            } else {
109                for (iy = -1022,i=(hy<<11); i>0; i<<=1) iy -=1;
110            }
111        } else iy = (hy>>20)-1023;
112
113    /* set up {hx,lx}, {hy,ly} and align y to x */
114        if(ix >= -1022) 
115            hx = 0x00100000|(0x000fffff&hx);
116        else {          /* subnormal x, shift x to normal */
117            n = -1022-ix;
118            if(n<=31) {
119                hx = (hx<<n)|(lx>>(32-n));
120                lx <<= n;
121            } else {
122                hx = lx<<(n-32);
123                lx = 0;
124            }
125        }
126        if(iy >= -1022) 
127            hy = 0x00100000|(0x000fffff&hy);
128        else {          /* subnormal y, shift y to normal */
129            n = -1022-iy;
130            if(n<=31) {
131                hy = (hy<<n)|(ly>>(32-n));
132                ly <<= n;
133            } else {
134                hy = ly<<(n-32);
135                ly = 0;
136            }
137        }
138
139    /* fix point fmod */
140        n = ix - iy;
141        while(n--) {
142            hz=hx-hy;lz=lx-ly; if(lx<ly) hz -= 1;
143            if(hz<0){hx = hx+hx+(lx>>31); lx = lx+lx;}
144            else {
145                if((hz|lz)==0)          /* return sign(x)*0 */
146                    return Zero[(__uint32_t)sx>>31];
147                hx = hz+hz+(lz>>31); lx = lz+lz;
148            }
149        }
150        hz=hx-hy;lz=lx-ly; if(lx<ly) hz -= 1;
151        if(hz>=0) {hx=hz;lx=lz;}
152
153    /* convert back to floating value and restore the sign */
154        if((hx|lx)==0)                  /* return sign(x)*0 */
155            return Zero[(__uint32_t)sx>>31];   
156        while(hx<0x00100000) {          /* normalize x */
157            hx = hx+hx+(lx>>31); lx = lx+lx;
158            iy -= 1;
159        }
160        if(iy>= -1022) {        /* normalize output */
161            hx = ((hx-0x00100000)|((iy+1023)<<20));
162            INSERT_WORDS(x,hx|sx,lx);
163        } else {                /* subnormal output */
164            n = -1022 - iy;
165            if(n<=20) {
166                lx = (lx>>n)|((__uint32_t)hx<<(32-n));
167                hx >>= n;
168            } else if (n<=31) {
169                lx = (hx<<(32-n))|(lx>>n); hx = sx;
170            } else {
171                lx = hx>>(n-32); hx = sx;
172            }
173            INSERT_WORDS(x,hx|sx,lx);
174            x *= one;           /* create necessary signal */
175        }
176        return x;               /* exact output */
177}
178
179#endif /* defined(_DOUBLE_IS_32BITS) */
Note: See TracBrowser for help on using the repository browser.