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 <linux/compiler.h>
29 #include "ieee754sp.h"
31 int ieee754sp_class(union ieee754sp x)
38 int ieee754sp_isnan(union ieee754sp x)
40 return ieee754sp_class(x) >= IEEE754_CLASS_SNAN;
43 static inline int ieee754sp_issnan(union ieee754sp x)
45 assert(ieee754sp_isnan(x));
46 return (SPMANT(x) & SP_MBIT(SP_FBITS-1));
50 union ieee754sp __cold ieee754sp_xcpt(union ieee754sp r, const char *op, ...)
52 struct ieee754xctx ax;
58 ax.rt = IEEE754_RT_SP;
66 union ieee754sp __cold ieee754sp_nanxcpt(union ieee754sp r, const char *op, ...)
68 struct ieee754xctx ax;
70 assert(ieee754sp_isnan(r));
72 if (!ieee754sp_issnan(r)) /* QNAN does not cause invalid op !! */
75 if (!ieee754_setandtestcx(IEEE754_INVALID_OPERATION)) {
76 /* not enabled convert to a quiet NaN */
77 SPMANT(r) &= (~SP_MBIT(SP_FBITS-1));
78 if (ieee754sp_isnan(r))
81 return ieee754sp_indef();
93 static unsigned get_rounding(int sn, unsigned xm)
95 /* inexact must round of 3 bits
97 if (xm & (SP_MBIT(3) - 1)) {
98 switch (ieee754_csr.rm) {
102 xm += 0x3 + ((xm >> 3) & 1);
103 /* xm += (xm&0x8)?0x4:0x3 */
105 case IEEE754_RU: /* toward +Infinity */
109 case IEEE754_RD: /* toward -Infinity */
119 /* generate a normal/denormal number with over,under handling
121 * xe is an unbiased exponent
122 * xm is 3bit extended precision value.
124 union ieee754sp ieee754sp_format(int sn, int xe, unsigned xm)
126 assert(xm); /* we don't gen exact zeros (probably should) */
128 assert((xm >> (SP_FBITS + 1 + 3)) == 0); /* no execess */
129 assert(xm & (SP_HIDDEN_BIT << 3));
132 /* strip lower bits */
133 int es = SP_EMIN - xe;
135 if (ieee754_csr.nod) {
136 ieee754_setcx(IEEE754_UNDERFLOW);
137 ieee754_setcx(IEEE754_INEXACT);
139 switch(ieee754_csr.rm) {
142 return ieee754sp_zero(sn);
143 case IEEE754_RU: /* toward +Infinity */
145 return ieee754sp_min(0);
147 return ieee754sp_zero(1);
148 case IEEE754_RD: /* toward -Infinity */
150 return ieee754sp_zero(0);
152 return ieee754sp_min(1);
156 if (xe == SP_EMIN - 1
157 && get_rounding(sn, xm) >> (SP_FBITS + 1 + 3))
159 /* Not tiny after rounding */
160 ieee754_setcx(IEEE754_INEXACT);
161 xm = get_rounding(sn, xm);
164 xm &= ~(SP_MBIT(3) - 1);
167 /* sticky right shift es bits
170 assert((xm & (SP_HIDDEN_BIT << 3)) == 0);
171 assert(xe == SP_EMIN);
174 if (xm & (SP_MBIT(3) - 1)) {
175 ieee754_setcx(IEEE754_INEXACT);
176 if ((xm & (SP_HIDDEN_BIT << 3)) == 0) {
177 ieee754_setcx(IEEE754_UNDERFLOW);
180 /* inexact must round of 3 bits
182 xm = get_rounding(sn, xm);
183 /* adjust exponent for rounding add overflowing
185 if (xm >> (SP_FBITS + 1 + 3)) {
186 /* add causes mantissa overflow */
194 assert((xm >> (SP_FBITS + 1)) == 0); /* no execess */
195 assert(xe >= SP_EMIN);
198 ieee754_setcx(IEEE754_OVERFLOW);
199 ieee754_setcx(IEEE754_INEXACT);
200 /* -O can be table indexed by (rm,sn) */
201 switch (ieee754_csr.rm) {
203 return ieee754sp_inf(sn);
205 return ieee754sp_max(sn);
206 case IEEE754_RU: /* toward +Infinity */
208 return ieee754sp_inf(0);
210 return ieee754sp_max(1);
211 case IEEE754_RD: /* toward -Infinity */
213 return ieee754sp_max(0);
215 return ieee754sp_inf(1);
218 /* gen norm/denorm/zero */
220 if ((xm & SP_HIDDEN_BIT) == 0) {
221 /* we underflow (tiny/zero) */
222 assert(xe == SP_EMIN);
223 if (ieee754_csr.mx & IEEE754_UNDERFLOW)
224 ieee754_setcx(IEEE754_UNDERFLOW);
225 return buildsp(sn, SP_EMIN - 1 + SP_EBIAS, xm);
227 assert((xm >> (SP_FBITS + 1)) == 0); /* no execess */
228 assert(xm & SP_HIDDEN_BIT);
230 return buildsp(sn, xe + SP_EBIAS, xm & ~SP_HIDDEN_BIT);