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 static u64
get_rounding(int sn
, u64 xm
)
94 /* inexact must round of 3 bits
96 if (xm
& (DP_MBIT(3) - 1)) {
97 switch (ieee754_csr
.rm
) {
101 xm
+= 0x3 + ((xm
>> 3) & 1);
102 /* xm += (xm&0x8)?0x4:0x3 */
104 case IEEE754_RU
: /* toward +Infinity */
108 case IEEE754_RD
: /* toward -Infinity */
118 /* generate a normal/denormal number with over,under handling
120 * xe is an unbiased exponent
121 * xm is 3bit extended precision value.
123 union ieee754dp
ieee754dp_format(int sn
, int xe
, u64 xm
)
125 assert(xm
); /* we don't gen exact zeros (probably should) */
127 assert((xm
>> (DP_FBITS
+ 1 + 3)) == 0); /* no execess */
128 assert(xm
& (DP_HIDDEN_BIT
<< 3));
131 /* strip lower bits */
132 int es
= DP_EMIN
- xe
;
134 if (ieee754_csr
.nod
) {
135 ieee754_setcx(IEEE754_UNDERFLOW
);
136 ieee754_setcx(IEEE754_INEXACT
);
138 switch(ieee754_csr
.rm
) {
141 return ieee754dp_zero(sn
);
142 case IEEE754_RU
: /* toward +Infinity */
144 return ieee754dp_min(0);
146 return ieee754dp_zero(1);
147 case IEEE754_RD
: /* toward -Infinity */
149 return ieee754dp_zero(0);
151 return ieee754dp_min(1);
155 if (xe
== DP_EMIN
- 1
156 && get_rounding(sn
, xm
) >> (DP_FBITS
+ 1 + 3))
158 /* Not tiny after rounding */
159 ieee754_setcx(IEEE754_INEXACT
);
160 xm
= get_rounding(sn
, xm
);
163 xm
&= ~(DP_MBIT(3) - 1);
167 /* sticky right shift es bits
171 assert((xm
& (DP_HIDDEN_BIT
<< 3)) == 0);
172 assert(xe
== DP_EMIN
);
175 if (xm
& (DP_MBIT(3) - 1)) {
176 ieee754_setcx(IEEE754_INEXACT
);
177 if ((xm
& (DP_HIDDEN_BIT
<< 3)) == 0) {
178 ieee754_setcx(IEEE754_UNDERFLOW
);
181 /* inexact must round of 3 bits
183 xm
= get_rounding(sn
, xm
);
184 /* adjust exponent for rounding add overflowing
186 if (xm
>> (DP_FBITS
+ 3 + 1)) {
187 /* add causes mantissa overflow */
195 assert((xm
>> (DP_FBITS
+ 1)) == 0); /* no execess */
196 assert(xe
>= DP_EMIN
);
199 ieee754_setcx(IEEE754_OVERFLOW
);
200 ieee754_setcx(IEEE754_INEXACT
);
201 /* -O can be table indexed by (rm,sn) */
202 switch (ieee754_csr
.rm
) {
204 return ieee754dp_inf(sn
);
206 return ieee754dp_max(sn
);
207 case IEEE754_RU
: /* toward +Infinity */
209 return ieee754dp_inf(0);
211 return ieee754dp_max(1);
212 case IEEE754_RD
: /* toward -Infinity */
214 return ieee754dp_max(0);
216 return ieee754dp_inf(1);
219 /* gen norm/denorm/zero */
221 if ((xm
& DP_HIDDEN_BIT
) == 0) {
222 /* we underflow (tiny/zero) */
223 assert(xe
== DP_EMIN
);
224 if (ieee754_csr
.mx
& IEEE754_UNDERFLOW
)
225 ieee754_setcx(IEEE754_UNDERFLOW
);
226 return builddp(sn
, DP_EMIN
- 1 + DP_EBIAS
, xm
);
228 assert((xm
>> (DP_FBITS
+ 1)) == 0); /* no execess */
229 assert(xm
& DP_HIDDEN_BIT
);
231 return builddp(sn
, xe
+ DP_EBIAS
, xm
& ~DP_HIDDEN_BIT
);