1 /* IEEE754 floating point arithmetic
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 "ieee754sp.h"
29 int ieee754sp_class(ieee754sp x
)
36 int ieee754sp_isnan(ieee754sp x
)
38 return ieee754sp_class(x
) >= IEEE754_CLASS_SNAN
;
41 int ieee754sp_issnan(ieee754sp x
)
43 assert(ieee754sp_isnan(x
));
44 return (SPMANT(x
) & SP_MBIT(SP_MBITS
-1));
48 ieee754sp
ieee754sp_xcpt(ieee754sp r
, const char *op
, ...)
50 struct ieee754xctx ax
;
56 ax
.rt
= IEEE754_RT_SP
;
64 ieee754sp
ieee754sp_nanxcpt(ieee754sp r
, const char *op
, ...)
66 struct ieee754xctx ax
;
68 assert(ieee754sp_isnan(r
));
70 if (!ieee754sp_issnan(r
)) /* QNAN does not cause invalid op !! */
73 if (!SETANDTESTCX(IEEE754_INVALID_OPERATION
)) {
74 /* not enabled convert to a quiet NaN */
75 SPMANT(r
) &= (~SP_MBIT(SP_MBITS
-1));
76 if (ieee754sp_isnan(r
))
79 return ieee754sp_indef();
91 ieee754sp
ieee754sp_bestnan(ieee754sp x
, ieee754sp y
)
93 assert(ieee754sp_isnan(x
));
94 assert(ieee754sp_isnan(y
));
96 if (SPMANT(x
) > SPMANT(y
))
103 static unsigned get_rounding(int sn
, unsigned xm
)
105 /* inexact must round of 3 bits
107 if (xm
& (SP_MBIT(3) - 1)) {
108 switch (ieee754_csr
.rm
) {
112 xm
+= 0x3 + ((xm
>> 3) & 1);
113 /* xm += (xm&0x8)?0x4:0x3 */
115 case IEEE754_RU
: /* toward +Infinity */
119 case IEEE754_RD
: /* toward -Infinity */
129 /* generate a normal/denormal number with over,under handling
131 * xe is an unbiased exponent
132 * xm is 3bit extended precision value.
134 ieee754sp
ieee754sp_format(int sn
, int xe
, unsigned xm
)
136 assert(xm
); /* we don't gen exact zeros (probably should) */
138 assert((xm
>> (SP_MBITS
+ 1 + 3)) == 0); /* no execess */
139 assert(xm
& (SP_HIDDEN_BIT
<< 3));
142 /* strip lower bits */
143 int es
= SP_EMIN
- xe
;
145 if (ieee754_csr
.nod
) {
146 SETCX(IEEE754_UNDERFLOW
);
147 SETCX(IEEE754_INEXACT
);
149 switch(ieee754_csr
.rm
) {
152 return ieee754sp_zero(sn
);
153 case IEEE754_RU
: /* toward +Infinity */
155 return ieee754sp_min(0);
157 return ieee754sp_zero(1);
158 case IEEE754_RD
: /* toward -Infinity */
160 return ieee754sp_zero(0);
162 return ieee754sp_min(1);
166 if (xe
== SP_EMIN
- 1
167 && get_rounding(sn
, xm
) >> (SP_MBITS
+ 1 + 3))
169 /* Not tiny after rounding */
170 SETCX(IEEE754_INEXACT
);
171 xm
= get_rounding(sn
, xm
);
174 xm
&= ~(SP_MBIT(3) - 1);
178 /* sticky right shift es bits
181 assert((xm
& (SP_HIDDEN_BIT
<< 3)) == 0);
182 assert(xe
== SP_EMIN
);
185 if (xm
& (SP_MBIT(3) - 1)) {
186 SETCX(IEEE754_INEXACT
);
187 if ((xm
& (SP_HIDDEN_BIT
<< 3)) == 0) {
188 SETCX(IEEE754_UNDERFLOW
);
191 /* inexact must round of 3 bits
193 xm
= get_rounding(sn
, xm
);
194 /* adjust exponent for rounding add overflowing
196 if (xm
>> (SP_MBITS
+ 1 + 3)) {
197 /* add causes mantissa overflow */
205 assert((xm
>> (SP_MBITS
+ 1)) == 0); /* no execess */
206 assert(xe
>= SP_EMIN
);
209 SETCX(IEEE754_OVERFLOW
);
210 SETCX(IEEE754_INEXACT
);
211 /* -O can be table indexed by (rm,sn) */
212 switch (ieee754_csr
.rm
) {
214 return ieee754sp_inf(sn
);
216 return ieee754sp_max(sn
);
217 case IEEE754_RU
: /* toward +Infinity */
219 return ieee754sp_inf(0);
221 return ieee754sp_max(1);
222 case IEEE754_RD
: /* toward -Infinity */
224 return ieee754sp_max(0);
226 return ieee754sp_inf(1);
229 /* gen norm/denorm/zero */
231 if ((xm
& SP_HIDDEN_BIT
) == 0) {
232 /* we underflow (tiny/zero) */
233 assert(xe
== SP_EMIN
);
234 if (ieee754_csr
.mx
& IEEE754_UNDERFLOW
)
235 SETCX(IEEE754_UNDERFLOW
);
236 return buildsp(sn
, SP_EMIN
- 1 + SP_EBIAS
, xm
);
238 assert((xm
>> (SP_MBITS
+ 1)) == 0); /* no execess */
239 assert(xm
& SP_HIDDEN_BIT
);
241 return buildsp(sn
, xe
+ SP_EBIAS
, xm
& ~SP_HIDDEN_BIT
);