blob: 53dbb1cb52a932cd6de3cce141e9ca428c94bc84 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* IEEE754 floating point arithmetic
2 * double precision: common utilities
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 "ieee754dp.h"
29
Ralf Baechle2209bcb2014-04-16 01:31:11 +020030int ieee754dp_class(union ieee754dp x)
Linus Torvalds1da177e2005-04-16 15:20:36 -070031{
32 COMPXDP;
33 EXPLODEXDP;
34 return xc;
35}
36
Ralf Baechle2209bcb2014-04-16 01:31:11 +020037int ieee754dp_isnan(union ieee754dp x)
Linus Torvalds1da177e2005-04-16 15:20:36 -070038{
39 return ieee754dp_class(x) >= IEEE754_CLASS_SNAN;
40}
41
Ralf Baechle2209bcb2014-04-16 01:31:11 +020042int ieee754dp_issnan(union ieee754dp x)
Linus Torvalds1da177e2005-04-16 15:20:36 -070043{
44 assert(ieee754dp_isnan(x));
45 return ((DPMANT(x) & DP_MBIT(DP_MBITS-1)) == DP_MBIT(DP_MBITS-1));
46}
47
48
Ralf Baechle2209bcb2014-04-16 01:31:11 +020049union ieee754dp __cold ieee754dp_xcpt(union ieee754dp r, const char *op, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -070050{
51 struct ieee754xctx ax;
Ralf Baechle9e8bad12014-04-19 00:36:32 +020052 if (!ieee754_tstx())
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 return r;
54
55 ax.op = op;
56 ax.rt = IEEE754_RT_DP;
57 ax.rv.dp = r;
58 va_start(ax.ap, op);
59 ieee754_xcpt(&ax);
Richard Knutsson81422942007-11-24 22:22:19 +010060 va_end(ax.ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 return ax.rv.dp;
62}
63
Ralf Baechle2209bcb2014-04-16 01:31:11 +020064union ieee754dp __cold ieee754dp_nanxcpt(union ieee754dp r, const char *op, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -070065{
66 struct ieee754xctx ax;
67
68 assert(ieee754dp_isnan(r));
69
70 if (!ieee754dp_issnan(r)) /* QNAN does not cause invalid op !! */
71 return r;
72
Ralf Baechle9e8bad12014-04-19 00:36:32 +020073 if (!ieee754_setandtestcx(IEEE754_INVALID_OPERATION)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 /* not enabled convert to a quiet NaN */
75 DPMANT(r) &= (~DP_MBIT(DP_MBITS-1));
76 if (ieee754dp_isnan(r))
77 return r;
78 else
79 return ieee754dp_indef();
80 }
81
82 ax.op = op;
83 ax.rt = 0;
84 ax.rv.dp = r;
85 va_start(ax.ap, op);
86 ieee754_xcpt(&ax);
Richard Knutsson81422942007-11-24 22:22:19 +010087 va_end(ax.ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 return ax.rv.dp;
89}
90
Ralf Baechle2209bcb2014-04-16 01:31:11 +020091union ieee754dp ieee754dp_bestnan(union ieee754dp x, union ieee754dp y)
Linus Torvalds1da177e2005-04-16 15:20:36 -070092{
93 assert(ieee754dp_isnan(x));
94 assert(ieee754dp_isnan(y));
95
96 if (DPMANT(x) > DPMANT(y))
97 return x;
98 else
99 return y;
100}
101
102
103static u64 get_rounding(int sn, u64 xm)
104{
105 /* inexact must round of 3 bits
106 */
107 if (xm & (DP_MBIT(3) - 1)) {
108 switch (ieee754_csr.rm) {
109 case IEEE754_RZ:
110 break;
111 case IEEE754_RN:
112 xm += 0x3 + ((xm >> 3) & 1);
113 /* xm += (xm&0x8)?0x4:0x3 */
114 break;
115 case IEEE754_RU: /* toward +Infinity */
116 if (!sn) /* ?? */
117 xm += 0x8;
118 break;
119 case IEEE754_RD: /* toward -Infinity */
Ralf Baechle70342282013-01-22 12:59:30 +0100120 if (sn) /* ?? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 xm += 0x8;
122 break;
123 }
124 }
125 return xm;
126}
127
128
129/* generate a normal/denormal number with over,under handling
130 * sn is sign
131 * xe is an unbiased exponent
132 * xm is 3bit extended precision value.
133 */
Ralf Baechle2209bcb2014-04-16 01:31:11 +0200134union ieee754dp ieee754dp_format(int sn, int xe, u64 xm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135{
136 assert(xm); /* we don't gen exact zeros (probably should) */
137
138 assert((xm >> (DP_MBITS + 1 + 3)) == 0); /* no execess */
139 assert(xm & (DP_HIDDEN_BIT << 3));
140
141 if (xe < DP_EMIN) {
142 /* strip lower bits */
143 int es = DP_EMIN - xe;
144
145 if (ieee754_csr.nod) {
Ralf Baechle9e8bad12014-04-19 00:36:32 +0200146 ieee754_setcx(IEEE754_UNDERFLOW);
147 ieee754_setcx(IEEE754_INEXACT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
149 switch(ieee754_csr.rm) {
150 case IEEE754_RN:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 case IEEE754_RZ:
152 return ieee754dp_zero(sn);
153 case IEEE754_RU: /* toward +Infinity */
Ralf Baechle47fa0c02014-04-16 11:00:12 +0200154 if (sn == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 return ieee754dp_min(0);
156 else
157 return ieee754dp_zero(1);
158 case IEEE754_RD: /* toward -Infinity */
Ralf Baechle47fa0c02014-04-16 11:00:12 +0200159 if (sn == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 return ieee754dp_zero(0);
161 else
162 return ieee754dp_min(1);
163 }
164 }
165
166 if (xe == DP_EMIN - 1
167 && get_rounding(sn, xm) >> (DP_MBITS + 1 + 3))
168 {
169 /* Not tiny after rounding */
Ralf Baechle9e8bad12014-04-19 00:36:32 +0200170 ieee754_setcx(IEEE754_INEXACT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 xm = get_rounding(sn, xm);
172 xm >>= 1;
173 /* Clear grs bits */
174 xm &= ~(DP_MBIT(3) - 1);
175 xe++;
176 }
177 else {
178 /* sticky right shift es bits
179 */
180 xm = XDPSRS(xm, es);
181 xe += es;
182 assert((xm & (DP_HIDDEN_BIT << 3)) == 0);
183 assert(xe == DP_EMIN);
184 }
185 }
186 if (xm & (DP_MBIT(3) - 1)) {
Ralf Baechle9e8bad12014-04-19 00:36:32 +0200187 ieee754_setcx(IEEE754_INEXACT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 if ((xm & (DP_HIDDEN_BIT << 3)) == 0) {
Ralf Baechle9e8bad12014-04-19 00:36:32 +0200189 ieee754_setcx(IEEE754_UNDERFLOW);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 }
191
192 /* inexact must round of 3 bits
193 */
194 xm = get_rounding(sn, xm);
195 /* adjust exponent for rounding add overflowing
196 */
197 if (xm >> (DP_MBITS + 3 + 1)) {
198 /* add causes mantissa overflow */
199 xm >>= 1;
200 xe++;
201 }
202 }
203 /* strip grs bits */
204 xm >>= 3;
205
206 assert((xm >> (DP_MBITS + 1)) == 0); /* no execess */
207 assert(xe >= DP_EMIN);
208
209 if (xe > DP_EMAX) {
Ralf Baechle9e8bad12014-04-19 00:36:32 +0200210 ieee754_setcx(IEEE754_OVERFLOW);
211 ieee754_setcx(IEEE754_INEXACT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 /* -O can be table indexed by (rm,sn) */
213 switch (ieee754_csr.rm) {
214 case IEEE754_RN:
215 return ieee754dp_inf(sn);
216 case IEEE754_RZ:
217 return ieee754dp_max(sn);
218 case IEEE754_RU: /* toward +Infinity */
219 if (sn == 0)
220 return ieee754dp_inf(0);
221 else
222 return ieee754dp_max(1);
223 case IEEE754_RD: /* toward -Infinity */
224 if (sn == 0)
225 return ieee754dp_max(0);
226 else
227 return ieee754dp_inf(1);
228 }
229 }
230 /* gen norm/denorm/zero */
231
232 if ((xm & DP_HIDDEN_BIT) == 0) {
233 /* we underflow (tiny/zero) */
234 assert(xe == DP_EMIN);
235 if (ieee754_csr.mx & IEEE754_UNDERFLOW)
Ralf Baechle9e8bad12014-04-19 00:36:32 +0200236 ieee754_setcx(IEEE754_UNDERFLOW);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 return builddp(sn, DP_EMIN - 1 + DP_EBIAS, xm);
238 } else {
239 assert((xm >> (DP_MBITS + 1)) == 0); /* no execess */
240 assert(xm & DP_HIDDEN_BIT);
241
242 return builddp(sn, xe + DP_EBIAS, xm & ~DP_HIDDEN_BIT);
243 }
244}