1 /* IEEE754 floating point arithmetic
2 * double precision: common utilities
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 "ieee754dp.h"
30 int ieee754dp_class(ieee754dp x)
37 int ieee754dp_isnan(ieee754dp x)
39 return ieee754dp_class(x) >= IEEE754_CLASS_SNAN;
42 int ieee754dp_issnan(ieee754dp x)
44 assert(ieee754dp_isnan(x));
45 return ((DPMANT(x) & DP_MBIT(DP_MBITS-1)) == DP_MBIT(DP_MBITS-1));
49 ieee754dp ieee754dp_xcpt(ieee754dp r, const char *op, ...)
51 struct ieee754xctx ax;
56 ax.rt = IEEE754_RT_DP;
64 ieee754dp ieee754dp_nanxcpt(ieee754dp r, const char *op, ...)
66 struct ieee754xctx ax;
68 assert(ieee754dp_isnan(r));
70 if (!ieee754dp_issnan(r)) /* QNAN does not cause invalid op !! */
73 if (!SETANDTESTCX(IEEE754_INVALID_OPERATION)) {
74 /* not enabled convert to a quiet NaN */
75 DPMANT(r) &= (~DP_MBIT(DP_MBITS-1));
76 if (ieee754dp_isnan(r))
79 return ieee754dp_indef();
91 ieee754dp ieee754dp_bestnan(ieee754dp x, ieee754dp y)
93 assert(ieee754dp_isnan(x));
94 assert(ieee754dp_isnan(y));
96 if (DPMANT(x) > DPMANT(y))
103 static u64 get_rounding(int sn, u64 xm)
105 /* inexact must round of 3 bits
107 if (xm & (DP_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 ieee754dp ieee754dp_format(int sn, int xe, u64 xm)
136 assert(xm); /* we don't gen exact zeros (probably should) */
138 assert((xm >> (DP_MBITS + 1 + 3)) == 0); /* no execess */
139 assert(xm & (DP_HIDDEN_BIT << 3));
142 /* strip lower bits */
143 int es = DP_EMIN - xe;
145 if (ieee754_csr.nod) {
146 SETCX(IEEE754_UNDERFLOW);
147 SETCX(IEEE754_INEXACT);
149 switch(ieee754_csr.rm) {
152 return ieee754dp_zero(sn);
153 case IEEE754_RU: /* toward +Infinity */
155 return ieee754dp_min(0);
157 return ieee754dp_zero(1);
158 case IEEE754_RD: /* toward -Infinity */
160 return ieee754dp_zero(0);
162 return ieee754dp_min(1);
166 if (xe == DP_EMIN - 1
167 && get_rounding(sn, xm) >> (DP_MBITS + 1 + 3))
169 /* Not tiny after rounding */
170 SETCX(IEEE754_INEXACT);
171 xm = get_rounding(sn, xm);
174 xm &= ~(DP_MBIT(3) - 1);
178 /* sticky right shift es bits
182 assert((xm & (DP_HIDDEN_BIT << 3)) == 0);
183 assert(xe == DP_EMIN);
186 if (xm & (DP_MBIT(3) - 1)) {
187 SETCX(IEEE754_INEXACT);
188 if ((xm & (DP_HIDDEN_BIT << 3)) == 0) {
189 SETCX(IEEE754_UNDERFLOW);
192 /* inexact must round of 3 bits
194 xm = get_rounding(sn, xm);
195 /* adjust exponent for rounding add overflowing
197 if (xm >> (DP_MBITS + 3 + 1)) {
198 /* add causes mantissa overflow */
206 assert((xm >> (DP_MBITS + 1)) == 0); /* no execess */
207 assert(xe >= DP_EMIN);
210 SETCX(IEEE754_OVERFLOW);
211 SETCX(IEEE754_INEXACT);
212 /* -O can be table indexed by (rm,sn) */
213 switch (ieee754_csr.rm) {
215 return ieee754dp_inf(sn);
217 return ieee754dp_max(sn);
218 case IEEE754_RU: /* toward +Infinity */
220 return ieee754dp_inf(0);
222 return ieee754dp_max(1);
223 case IEEE754_RD: /* toward -Infinity */
225 return ieee754dp_max(0);
227 return ieee754dp_inf(1);
230 /* gen norm/denorm/zero */
232 if ((xm & DP_HIDDEN_BIT) == 0) {
233 /* we underflow (tiny/zero) */
234 assert(xe == DP_EMIN);
235 if (ieee754_csr.mx & IEEE754_UNDERFLOW)
236 SETCX(IEEE754_UNDERFLOW);
237 return builddp(sn, DP_EMIN - 1 + DP_EBIAS, xm);
239 assert((xm >> (DP_MBITS + 1)) == 0); /* no execess */
240 assert(xm & DP_HIDDEN_BIT);
242 return builddp(sn, xe + DP_EBIAS, xm & ~DP_HIDDEN_BIT);