blob: a15d2123e05e784a783217747852924f2d587bee [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* IEEE754 floating point arithmetic
2 * single precision
3 */
4/*
5 * MIPS floating point support
6 * Copyright (C) 1994-2000 Algorithmics Ltd.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
8 * ########################################################################
9 *
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.
13 *
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
17 * for more details.
18 *
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.
22 *
23 * ########################################################################
24 */
25
Ralf Baechlecae55062014-04-16 00:47:59 +020026#include <linux/compiler.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
28#include "ieee754sp.h"
29
Ralf Baechle2209bcb2014-04-16 01:31:11 +020030int ieee754sp_class(union ieee754sp x)
Linus Torvalds1da177e2005-04-16 15:20:36 -070031{
32 COMPXSP;
33 EXPLODEXSP;
34 return xc;
35}
36
Ralf Baechle2209bcb2014-04-16 01:31:11 +020037int ieee754sp_isnan(union ieee754sp x)
Linus Torvalds1da177e2005-04-16 15:20:36 -070038{
39 return ieee754sp_class(x) >= IEEE754_CLASS_SNAN;
40}
41
Ralf Baechle2209bcb2014-04-16 01:31:11 +020042int ieee754sp_issnan(union ieee754sp x)
Linus Torvalds1da177e2005-04-16 15:20:36 -070043{
44 assert(ieee754sp_isnan(x));
45 return (SPMANT(x) & SP_MBIT(SP_MBITS-1));
46}
47
48
Ralf Baechle2209bcb2014-04-16 01:31:11 +020049union ieee754sp __cold ieee754sp_xcpt(union ieee754sp r, const char *op, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -070050{
51 struct ieee754xctx ax;
52
53 if (!TSTX())
54 return r;
55
56 ax.op = op;
57 ax.rt = IEEE754_RT_SP;
58 ax.rv.sp = r;
59 va_start(ax.ap, op);
60 ieee754_xcpt(&ax);
Richard Knutsson81422942007-11-24 22:22:19 +010061 va_end(ax.ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 return ax.rv.sp;
63}
64
Ralf Baechle2209bcb2014-04-16 01:31:11 +020065union ieee754sp __cold ieee754sp_nanxcpt(union ieee754sp r, const char *op, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -070066{
67 struct ieee754xctx ax;
68
69 assert(ieee754sp_isnan(r));
70
71 if (!ieee754sp_issnan(r)) /* QNAN does not cause invalid op !! */
72 return r;
73
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))
78 return r;
79 else
80 return ieee754sp_indef();
81 }
82
83 ax.op = op;
84 ax.rt = 0;
85 ax.rv.sp = r;
86 va_start(ax.ap, op);
87 ieee754_xcpt(&ax);
Richard Knutsson81422942007-11-24 22:22:19 +010088 va_end(ax.ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 return ax.rv.sp;
90}
91
Ralf Baechle2209bcb2014-04-16 01:31:11 +020092union ieee754sp ieee754sp_bestnan(union ieee754sp x, union ieee754sp y)
Linus Torvalds1da177e2005-04-16 15:20:36 -070093{
94 assert(ieee754sp_isnan(x));
95 assert(ieee754sp_isnan(y));
96
97 if (SPMANT(x) > SPMANT(y))
98 return x;
99 else
100 return y;
101}
102
103
104static unsigned get_rounding(int sn, unsigned xm)
105{
106 /* inexact must round of 3 bits
107 */
108 if (xm & (SP_MBIT(3) - 1)) {
109 switch (ieee754_csr.rm) {
110 case IEEE754_RZ:
111 break;
112 case IEEE754_RN:
113 xm += 0x3 + ((xm >> 3) & 1);
114 /* xm += (xm&0x8)?0x4:0x3 */
115 break;
116 case IEEE754_RU: /* toward +Infinity */
117 if (!sn) /* ?? */
118 xm += 0x8;
119 break;
120 case IEEE754_RD: /* toward -Infinity */
Ralf Baechle70342282013-01-22 12:59:30 +0100121 if (sn) /* ?? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 xm += 0x8;
123 break;
124 }
125 }
126 return xm;
127}
128
129
130/* generate a normal/denormal number with over,under handling
131 * sn is sign
132 * xe is an unbiased exponent
133 * xm is 3bit extended precision value.
134 */
Ralf Baechle2209bcb2014-04-16 01:31:11 +0200135union ieee754sp ieee754sp_format(int sn, int xe, unsigned xm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136{
137 assert(xm); /* we don't gen exact zeros (probably should) */
138
139 assert((xm >> (SP_MBITS + 1 + 3)) == 0); /* no execess */
140 assert(xm & (SP_HIDDEN_BIT << 3));
141
142 if (xe < SP_EMIN) {
143 /* strip lower bits */
144 int es = SP_EMIN - xe;
145
146 if (ieee754_csr.nod) {
147 SETCX(IEEE754_UNDERFLOW);
148 SETCX(IEEE754_INEXACT);
149
150 switch(ieee754_csr.rm) {
151 case IEEE754_RN:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 case IEEE754_RZ:
153 return ieee754sp_zero(sn);
154 case IEEE754_RU: /* toward +Infinity */
Ralf Baechle47fa0c02014-04-16 11:00:12 +0200155 if (sn == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 return ieee754sp_min(0);
157 else
158 return ieee754sp_zero(1);
159 case IEEE754_RD: /* toward -Infinity */
Ralf Baechle47fa0c02014-04-16 11:00:12 +0200160 if (sn == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 return ieee754sp_zero(0);
162 else
163 return ieee754sp_min(1);
164 }
165 }
166
167 if (xe == SP_EMIN - 1
168 && get_rounding(sn, xm) >> (SP_MBITS + 1 + 3))
169 {
170 /* Not tiny after rounding */
171 SETCX(IEEE754_INEXACT);
172 xm = get_rounding(sn, xm);
173 xm >>= 1;
174 /* Clear grs bits */
175 xm &= ~(SP_MBIT(3) - 1);
176 xe++;
Ralf Baechle47fa0c02014-04-16 11:00:12 +0200177 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 /* sticky right shift es bits
179 */
180 SPXSRSXn(es);
181 assert((xm & (SP_HIDDEN_BIT << 3)) == 0);
182 assert(xe == SP_EMIN);
183 }
184 }
185 if (xm & (SP_MBIT(3) - 1)) {
186 SETCX(IEEE754_INEXACT);
187 if ((xm & (SP_HIDDEN_BIT << 3)) == 0) {
188 SETCX(IEEE754_UNDERFLOW);
189 }
190
191 /* inexact must round of 3 bits
192 */
193 xm = get_rounding(sn, xm);
194 /* adjust exponent for rounding add overflowing
195 */
196 if (xm >> (SP_MBITS + 1 + 3)) {
197 /* add causes mantissa overflow */
198 xm >>= 1;
199 xe++;
200 }
201 }
202 /* strip grs bits */
203 xm >>= 3;
204
205 assert((xm >> (SP_MBITS + 1)) == 0); /* no execess */
206 assert(xe >= SP_EMIN);
207
208 if (xe > SP_EMAX) {
209 SETCX(IEEE754_OVERFLOW);
210 SETCX(IEEE754_INEXACT);
211 /* -O can be table indexed by (rm,sn) */
212 switch (ieee754_csr.rm) {
213 case IEEE754_RN:
214 return ieee754sp_inf(sn);
215 case IEEE754_RZ:
216 return ieee754sp_max(sn);
217 case IEEE754_RU: /* toward +Infinity */
218 if (sn == 0)
219 return ieee754sp_inf(0);
220 else
221 return ieee754sp_max(1);
222 case IEEE754_RD: /* toward -Infinity */
223 if (sn == 0)
224 return ieee754sp_max(0);
225 else
226 return ieee754sp_inf(1);
227 }
228 }
229 /* gen norm/denorm/zero */
230
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);
237 } else {
238 assert((xm >> (SP_MBITS + 1)) == 0); /* no execess */
239 assert(xm & SP_HIDDEN_BIT);
240
241 return buildsp(sn, xe + SP_EBIAS, xm & ~SP_HIDDEN_BIT);
242 }
243}