1 /* IEEE754 floating point arithmetic
5 * MIPS floating point support
6 * Copyright (C) 1994-2000 Algorithmics Ltd.
7 * http://www.algor.co.uk
9 * ########################################################################
11 * This program is free software; you can distribute it and/or modify it
12 * under the terms of the GNU General Public License (Version 2) as
13 * published by the Free Software Foundation.
15 * This program is distributed in the hope it will be useful, but WITHOUT
16 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
24 * ########################################################################
28 #include "ieee754sp.h"
30 int ieee754sp_class(ieee754sp x
)
37 int ieee754sp_isnan(ieee754sp x
)
39 return ieee754sp_class(x
) >= IEEE754_CLASS_SNAN
;
42 int ieee754sp_issnan(ieee754sp x
)
44 assert(ieee754sp_isnan(x
));
45 return (SPMANT(x
) & SP_MBIT(SP_MBITS
-1));
49 ieee754sp
ieee754sp_xcpt(ieee754sp r
, const char *op
, ...)
51 struct ieee754xctx ax
;
57 ax
.rt
= IEEE754_RT_SP
;
65 ieee754sp
ieee754sp_nanxcpt(ieee754sp r
, const char *op
, ...)
67 struct ieee754xctx ax
;
69 assert(ieee754sp_isnan(r
));
71 if (!ieee754sp_issnan(r
)) /* QNAN does not cause invalid op !! */
74 if (!SETANDTESTCX(IEEE754_INVALID_OPERATION
)) {
75 /* not enabled convert to a quiet NaN */
76 SPMANT(r
) &= (~SP_MBIT(SP_MBITS
-1));
77 if (ieee754sp_isnan(r
))
80 return ieee754sp_indef();
92 ieee754sp
ieee754sp_bestnan(ieee754sp x
, ieee754sp y
)
94 assert(ieee754sp_isnan(x
));
95 assert(ieee754sp_isnan(y
));
97 if (SPMANT(x
) > SPMANT(y
))
104 static unsigned get_rounding(int sn
, unsigned xm
)
106 /* inexact must round of 3 bits
108 if (xm
& (SP_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 ieee754sp
ieee754sp_format(int sn
, int xe
, unsigned xm
)
137 assert(xm
); /* we don't gen exact zeros (probably should) */
139 assert((xm
>> (SP_MBITS
+ 1 + 3)) == 0); /* no execess */
140 assert(xm
& (SP_HIDDEN_BIT
<< 3));
143 /* strip lower bits */
144 int es
= SP_EMIN
- xe
;
146 if (ieee754_csr
.nod
) {
147 SETCX(IEEE754_UNDERFLOW
);
148 SETCX(IEEE754_INEXACT
);
150 switch(ieee754_csr
.rm
) {
152 return ieee754sp_zero(sn
);
154 return ieee754sp_zero(sn
);
155 case IEEE754_RU
: /* toward +Infinity */
157 return ieee754sp_min(0);
159 return ieee754sp_zero(1);
160 case IEEE754_RD
: /* toward -Infinity */
162 return ieee754sp_zero(0);
164 return ieee754sp_min(1);
168 if (xe
== SP_EMIN
- 1
169 && get_rounding(sn
, xm
) >> (SP_MBITS
+ 1 + 3))
171 /* Not tiny after rounding */
172 SETCX(IEEE754_INEXACT
);
173 xm
= get_rounding(sn
, xm
);
176 xm
&= ~(SP_MBIT(3) - 1);
180 /* sticky right shift es bits
183 assert((xm
& (SP_HIDDEN_BIT
<< 3)) == 0);
184 assert(xe
== SP_EMIN
);
187 if (xm
& (SP_MBIT(3) - 1)) {
188 SETCX(IEEE754_INEXACT
);
189 if ((xm
& (SP_HIDDEN_BIT
<< 3)) == 0) {
190 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
>> (SP_MBITS
+ 1 + 3)) {
199 /* add causes mantissa overflow */
207 assert((xm
>> (SP_MBITS
+ 1)) == 0); /* no execess */
208 assert(xe
>= SP_EMIN
);
211 SETCX(IEEE754_OVERFLOW
);
212 SETCX(IEEE754_INEXACT
);
213 /* -O can be table indexed by (rm,sn) */
214 switch (ieee754_csr
.rm
) {
216 return ieee754sp_inf(sn
);
218 return ieee754sp_max(sn
);
219 case IEEE754_RU
: /* toward +Infinity */
221 return ieee754sp_inf(0);
223 return ieee754sp_max(1);
224 case IEEE754_RD
: /* toward -Infinity */
226 return ieee754sp_max(0);
228 return ieee754sp_inf(1);
231 /* gen norm/denorm/zero */
233 if ((xm
& SP_HIDDEN_BIT
) == 0) {
234 /* we underflow (tiny/zero) */
235 assert(xe
== SP_EMIN
);
236 if (ieee754_csr
.mx
& IEEE754_UNDERFLOW
)
237 SETCX(IEEE754_UNDERFLOW
);
238 return buildsp(sn
, SP_EMIN
- 1 + SP_EBIAS
, xm
);
240 assert((xm
>> (SP_MBITS
+ 1)) == 0); /* no execess */
241 assert(xm
& SP_HIDDEN_BIT
);
243 return buildsp(sn
, xe
+ SP_EBIAS
, xm
& ~SP_HIDDEN_BIT
);