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 * ########################################################################
26 #include <linux/compiler.h>
28 #include "ieee754sp.h"
30 int ieee754sp_class(union ieee754sp x)
37 int ieee754sp_isnan(union ieee754sp x)
39 return ieee754sp_class(x) >= IEEE754_CLASS_SNAN;
42 static inline int ieee754sp_issnan(union ieee754sp x)
44 assert(ieee754sp_isnan(x));
45 return (SPMANT(x) & SP_MBIT(SP_FBITS-1));
49 union ieee754sp __cold ieee754sp_nanxcpt(union ieee754sp r)
51 assert(ieee754sp_isnan(r));
53 if (!ieee754sp_issnan(r)) /* QNAN does not cause invalid op !! */
56 if (!ieee754_setandtestcx(IEEE754_INVALID_OPERATION)) {
57 /* not enabled convert to a quiet NaN */
58 SPMANT(r) &= (~SP_MBIT(SP_FBITS-1));
59 if (ieee754sp_isnan(r))
62 return ieee754sp_indef();
68 static unsigned ieee754sp_get_rounding(int sn, unsigned xm)
70 /* inexact must round of 3 bits
72 if (xm & (SP_MBIT(3) - 1)) {
73 switch (ieee754_csr.rm) {
77 xm += 0x3 + ((xm >> 3) & 1);
78 /* xm += (xm&0x8)?0x4:0x3 */
80 case IEEE754_RU: /* toward +Infinity */
84 case IEEE754_RD: /* toward -Infinity */
94 /* generate a normal/denormal number with over,under handling
96 * xe is an unbiased exponent
97 * xm is 3bit extended precision value.
99 union ieee754sp ieee754sp_format(int sn, int xe, unsigned xm)
101 assert(xm); /* we don't gen exact zeros (probably should) */
103 assert((xm >> (SP_FBITS + 1 + 3)) == 0); /* no execess */
104 assert(xm & (SP_HIDDEN_BIT << 3));
107 /* strip lower bits */
108 int es = SP_EMIN - xe;
110 if (ieee754_csr.nod) {
111 ieee754_setcx(IEEE754_UNDERFLOW);
112 ieee754_setcx(IEEE754_INEXACT);
114 switch(ieee754_csr.rm) {
117 return ieee754sp_zero(sn);
118 case IEEE754_RU: /* toward +Infinity */
120 return ieee754sp_min(0);
122 return ieee754sp_zero(1);
123 case IEEE754_RD: /* toward -Infinity */
125 return ieee754sp_zero(0);
127 return ieee754sp_min(1);
131 if (xe == SP_EMIN - 1 &&
132 ieee754sp_get_rounding(sn, xm) >> (SP_FBITS + 1 + 3))
134 /* Not tiny after rounding */
135 ieee754_setcx(IEEE754_INEXACT);
136 xm = ieee754sp_get_rounding(sn, xm);
139 xm &= ~(SP_MBIT(3) - 1);
142 /* sticky right shift es bits
145 assert((xm & (SP_HIDDEN_BIT << 3)) == 0);
146 assert(xe == SP_EMIN);
149 if (xm & (SP_MBIT(3) - 1)) {
150 ieee754_setcx(IEEE754_INEXACT);
151 if ((xm & (SP_HIDDEN_BIT << 3)) == 0) {
152 ieee754_setcx(IEEE754_UNDERFLOW);
155 /* inexact must round of 3 bits
157 xm = ieee754sp_get_rounding(sn, xm);
158 /* adjust exponent for rounding add overflowing
160 if (xm >> (SP_FBITS + 1 + 3)) {
161 /* add causes mantissa overflow */
169 assert((xm >> (SP_FBITS + 1)) == 0); /* no execess */
170 assert(xe >= SP_EMIN);
173 ieee754_setcx(IEEE754_OVERFLOW);
174 ieee754_setcx(IEEE754_INEXACT);
175 /* -O can be table indexed by (rm,sn) */
176 switch (ieee754_csr.rm) {
178 return ieee754sp_inf(sn);
180 return ieee754sp_max(sn);
181 case IEEE754_RU: /* toward +Infinity */
183 return ieee754sp_inf(0);
185 return ieee754sp_max(1);
186 case IEEE754_RD: /* toward -Infinity */
188 return ieee754sp_max(0);
190 return ieee754sp_inf(1);
193 /* gen norm/denorm/zero */
195 if ((xm & SP_HIDDEN_BIT) == 0) {
196 /* we underflow (tiny/zero) */
197 assert(xe == SP_EMIN);
198 if (ieee754_csr.mx & IEEE754_UNDERFLOW)
199 ieee754_setcx(IEEE754_UNDERFLOW);
200 return buildsp(sn, SP_EMIN - 1 + SP_EBIAS, xm);
202 assert((xm >> (SP_FBITS + 1)) == 0); /* no execess */
203 assert(xm & SP_HIDDEN_BIT);
205 return buildsp(sn, xe + SP_EBIAS, xm & ~SP_HIDDEN_BIT);