1 /* IEEE754 floating point arithmetic
2 * double precision: common utilities
5 * MIPS floating point support
6 * Copyright (C) 1994-2000 Algorithmics Ltd.
8 * ########################################################################
10 * This program is free software; you can distribute it and/or modify it
11 * under the terms of the GNU General Public License (Version 2) as
12 * published by the Free Software Foundation.
14 * This program is distributed in the hope it will be useful, but WITHOUT
15 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
23 * ########################################################################
27 #include <linux/compiler.h>
29 #include "ieee754dp.h"
31 int ieee754dp_class(union ieee754dp x
)
38 int ieee754dp_isnan(union ieee754dp x
)
40 return ieee754dp_class(x
) >= IEEE754_CLASS_SNAN
;
43 int ieee754dp_issnan(union ieee754dp x
)
45 assert(ieee754dp_isnan(x
));
46 return ((DPMANT(x
) & DP_MBIT(DP_FBITS
-1)) == DP_MBIT(DP_FBITS
-1));
50 union ieee754dp __cold
ieee754dp_xcpt(union ieee754dp r
, const char *op
, ...)
52 struct ieee754xctx ax
;
57 ax
.rt
= IEEE754_RT_DP
;
65 union ieee754dp __cold
ieee754dp_nanxcpt(union ieee754dp r
, const char *op
, ...)
67 struct ieee754xctx ax
;
69 assert(ieee754dp_isnan(r
));
71 if (!ieee754dp_issnan(r
)) /* QNAN does not cause invalid op !! */
74 if (!ieee754_setandtestcx(IEEE754_INVALID_OPERATION
)) {
75 /* not enabled convert to a quiet NaN */
76 DPMANT(r
) &= (~DP_MBIT(DP_FBITS
-1));
77 if (ieee754dp_isnan(r
))
80 return ieee754dp_indef();
92 union ieee754dp
ieee754dp_bestnan(union ieee754dp x
, union ieee754dp y
)
94 assert(ieee754dp_isnan(x
));
95 assert(ieee754dp_isnan(y
));
97 if (DPMANT(x
) > DPMANT(y
))
104 static u64
get_rounding(int sn
, u64 xm
)
106 /* inexact must round of 3 bits
108 if (xm
& (DP_MBIT(3) - 1)) {
109 switch (ieee754_csr
.rm
) {
113 xm
+= 0x3 + ((xm
>> 3) & 1);
114 /* xm += (xm&0x8)?0x4:0x3 */
116 case IEEE754_RU
: /* toward +Infinity */
120 case IEEE754_RD
: /* toward -Infinity */
130 /* generate a normal/denormal number with over,under handling
132 * xe is an unbiased exponent
133 * xm is 3bit extended precision value.
135 union ieee754dp
ieee754dp_format(int sn
, int xe
, u64 xm
)
137 assert(xm
); /* we don't gen exact zeros (probably should) */
139 assert((xm
>> (DP_FBITS
+ 1 + 3)) == 0); /* no execess */
140 assert(xm
& (DP_HIDDEN_BIT
<< 3));
143 /* strip lower bits */
144 int es
= DP_EMIN
- xe
;
146 if (ieee754_csr
.nod
) {
147 ieee754_setcx(IEEE754_UNDERFLOW
);
148 ieee754_setcx(IEEE754_INEXACT
);
150 switch(ieee754_csr
.rm
) {
153 return ieee754dp_zero(sn
);
154 case IEEE754_RU
: /* toward +Infinity */
156 return ieee754dp_min(0);
158 return ieee754dp_zero(1);
159 case IEEE754_RD
: /* toward -Infinity */
161 return ieee754dp_zero(0);
163 return ieee754dp_min(1);
167 if (xe
== DP_EMIN
- 1
168 && get_rounding(sn
, xm
) >> (DP_FBITS
+ 1 + 3))
170 /* Not tiny after rounding */
171 ieee754_setcx(IEEE754_INEXACT
);
172 xm
= get_rounding(sn
, xm
);
175 xm
&= ~(DP_MBIT(3) - 1);
179 /* sticky right shift es bits
183 assert((xm
& (DP_HIDDEN_BIT
<< 3)) == 0);
184 assert(xe
== DP_EMIN
);
187 if (xm
& (DP_MBIT(3) - 1)) {
188 ieee754_setcx(IEEE754_INEXACT
);
189 if ((xm
& (DP_HIDDEN_BIT
<< 3)) == 0) {
190 ieee754_setcx(IEEE754_UNDERFLOW
);
193 /* inexact must round of 3 bits
195 xm
= get_rounding(sn
, xm
);
196 /* adjust exponent for rounding add overflowing
198 if (xm
>> (DP_FBITS
+ 3 + 1)) {
199 /* add causes mantissa overflow */
207 assert((xm
>> (DP_FBITS
+ 1)) == 0); /* no execess */
208 assert(xe
>= DP_EMIN
);
211 ieee754_setcx(IEEE754_OVERFLOW
);
212 ieee754_setcx(IEEE754_INEXACT
);
213 /* -O can be table indexed by (rm,sn) */
214 switch (ieee754_csr
.rm
) {
216 return ieee754dp_inf(sn
);
218 return ieee754dp_max(sn
);
219 case IEEE754_RU
: /* toward +Infinity */
221 return ieee754dp_inf(0);
223 return ieee754dp_max(1);
224 case IEEE754_RD
: /* toward -Infinity */
226 return ieee754dp_max(0);
228 return ieee754dp_inf(1);
231 /* gen norm/denorm/zero */
233 if ((xm
& DP_HIDDEN_BIT
) == 0) {
234 /* we underflow (tiny/zero) */
235 assert(xe
== DP_EMIN
);
236 if (ieee754_csr
.mx
& IEEE754_UNDERFLOW
)
237 ieee754_setcx(IEEE754_UNDERFLOW
);
238 return builddp(sn
, DP_EMIN
- 1 + DP_EBIAS
, xm
);
240 assert((xm
>> (DP_FBITS
+ 1)) == 0); /* no execess */
241 assert(xm
& DP_HIDDEN_BIT
);
243 return builddp(sn
, xe
+ DP_EBIAS
, xm
& ~DP_HIDDEN_BIT
);