1 | /* Adapted for Newlib, 2009. (Allow for int < 32 bits; return *quo=0 during |
---|
2 | * errors to make test scripts easier.) */ |
---|
3 | /* @(#)e_fmod.c 1.3 95/01/18 */ |
---|
4 | /*- |
---|
5 | * ==================================================== |
---|
6 | * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. |
---|
7 | * |
---|
8 | * Developed at SunSoft, a Sun Microsystems, Inc. business. |
---|
9 | * Permission to use, copy, modify, and distribute this |
---|
10 | * software is freely granted, provided that this notice |
---|
11 | * is preserved. |
---|
12 | * ==================================================== |
---|
13 | */ |
---|
14 | |
---|
15 | #include <math.h> |
---|
16 | #include "fdlibm.h" |
---|
17 | |
---|
18 | /* For quotient, return either all 31 bits that can from calculation (using |
---|
19 | * int32_t), or as many as can fit into an int that is smaller than 32 bits. */ |
---|
20 | #if INT_MAX > 0x7FFFFFFFL |
---|
21 | #define QUO_MASK 0x7FFFFFFF |
---|
22 | # else |
---|
23 | #define QUO_MASK INT_MAX |
---|
24 | #endif |
---|
25 | |
---|
26 | static const float Zero[] = {0.0, -0.0,}; |
---|
27 | |
---|
28 | /* |
---|
29 | * Return the IEEE remainder and set *quo to the last n bits of the |
---|
30 | * quotient, rounded to the nearest integer. We choose n=31--if that many fit-- |
---|
31 | * we wind up computing all the integer bits of the quotient anyway as |
---|
32 | * a side-effect of computing the remainder by the shift and subtract |
---|
33 | * method. In practice, this is far more bits than are needed to use |
---|
34 | * remquo in reduction algorithms. |
---|
35 | */ |
---|
36 | float |
---|
37 | remquof(float x, float y, int *quo) |
---|
38 | { |
---|
39 | __int32_t n,hx,hy,hz,ix,iy,sx,i; |
---|
40 | __uint32_t q,sxy; |
---|
41 | |
---|
42 | GET_FLOAT_WORD(hx,x); |
---|
43 | GET_FLOAT_WORD(hy,y); |
---|
44 | sxy = (hx ^ hy) & 0x80000000; |
---|
45 | sx = hx&0x80000000; /* sign of x */ |
---|
46 | hx ^=sx; /* |x| */ |
---|
47 | hy &= 0x7fffffff; /* |y| */ |
---|
48 | |
---|
49 | /* purge off exception values */ |
---|
50 | if(hy==0||hx>=0x7f800000||hy>0x7f800000) { /* y=0,NaN;or x not finite */ |
---|
51 | *quo = 0; /* Not necessary, but return consistent value */ |
---|
52 | return (x*y)/(x*y); |
---|
53 | } |
---|
54 | if(hx<hy) { |
---|
55 | q = 0; |
---|
56 | goto fixup; /* |x|<|y| return x or x-y */ |
---|
57 | } else if(hx==hy) { |
---|
58 | *quo = 1; |
---|
59 | return Zero[(__uint32_t)sx>>31]; /* |x|=|y| return x*0*/ |
---|
60 | } |
---|
61 | |
---|
62 | /* determine ix = ilogb(x) */ |
---|
63 | if(hx<0x00800000) { /* subnormal x */ |
---|
64 | for (ix = -126,i=(hx<<8); i>0; i<<=1) ix -=1; |
---|
65 | } else ix = (hx>>23)-127; |
---|
66 | |
---|
67 | /* determine iy = ilogb(y) */ |
---|
68 | if(hy<0x00800000) { /* subnormal y */ |
---|
69 | for (iy = -126,i=(hy<<8); i>0; i<<=1) iy -=1; |
---|
70 | } else iy = (hy>>23)-127; |
---|
71 | |
---|
72 | /* set up {hx,lx}, {hy,ly} and align y to x */ |
---|
73 | if(ix >= -126) |
---|
74 | hx = 0x00800000|(0x007fffff&hx); |
---|
75 | else { /* subnormal x, shift x to normal */ |
---|
76 | n = -126-ix; |
---|
77 | hx <<= n; |
---|
78 | } |
---|
79 | if(iy >= -126) |
---|
80 | hy = 0x00800000|(0x007fffff&hy); |
---|
81 | else { /* subnormal y, shift y to normal */ |
---|
82 | n = -126-iy; |
---|
83 | hy <<= n; |
---|
84 | } |
---|
85 | |
---|
86 | /* fix point fmod */ |
---|
87 | n = ix - iy; |
---|
88 | q = 0; |
---|
89 | while(n--) { |
---|
90 | hz=hx-hy; |
---|
91 | if(hz<0) hx = hx << 1; |
---|
92 | else {hx = hz << 1; q++;} |
---|
93 | q <<= 1; |
---|
94 | } |
---|
95 | hz=hx-hy; |
---|
96 | if(hz>=0) {hx=hz;q++;} |
---|
97 | |
---|
98 | /* convert back to floating value and restore the sign */ |
---|
99 | if(hx==0) { /* return sign(x)*0 */ |
---|
100 | *quo = (sxy ? -q : q); |
---|
101 | return Zero[(__uint32_t)sx>>31]; |
---|
102 | } |
---|
103 | while(hx<0x00800000) { /* normalize x */ |
---|
104 | hx <<= 1; |
---|
105 | iy -= 1; |
---|
106 | } |
---|
107 | if(iy>= -126) { /* normalize output */ |
---|
108 | hx = ((hx-0x00800000)|((iy+127)<<23)); |
---|
109 | } else { /* subnormal output */ |
---|
110 | n = -126 - iy; |
---|
111 | hx >>= n; |
---|
112 | } |
---|
113 | fixup: |
---|
114 | SET_FLOAT_WORD(x,hx); |
---|
115 | y = fabsf(y); |
---|
116 | if (y < 0x1p-125f) { |
---|
117 | if (x+x>y || (x+x==y && (q & 1))) { |
---|
118 | q++; |
---|
119 | x-=y; |
---|
120 | } |
---|
121 | } else if (x>0.5f*y || (x==0.5f*y && (q & 1))) { |
---|
122 | q++; |
---|
123 | x-=y; |
---|
124 | } |
---|
125 | GET_FLOAT_WORD(hx,x); |
---|
126 | SET_FLOAT_WORD(x,hx^sx); |
---|
127 | q &= 0x7fffffff; |
---|
128 | *quo = (sxy ? -q : q); |
---|
129 | return x; |
---|
130 | } |
---|