blob: d9ceb6bd3536ae132a4567ec1026eafdf63d6c2d [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 Baechle1eaf1982014-04-20 01:20:08 +020026#include <stdarg.h>
Ralf Baechlecae55062014-04-16 00:47:59 +020027#include <linux/compiler.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
29#include "ieee754dp.h"
30
Ralf Baechle2209bcb2014-04-16 01:31:11 +020031int ieee754dp_class(union ieee754dp x)
Linus Torvalds1da177e2005-04-16 15:20:36 -070032{
33 COMPXDP;
34 EXPLODEXDP;
35 return xc;
36}
37
Ralf Baechle2209bcb2014-04-16 01:31:11 +020038int ieee754dp_isnan(union ieee754dp x)
Linus Torvalds1da177e2005-04-16 15:20:36 -070039{
40 return ieee754dp_class(x) >= IEEE754_CLASS_SNAN;
41}
42
Ralf Baechlef71baa12014-04-25 00:40:42 +020043static inline int ieee754dp_issnan(union ieee754dp x)
Linus Torvalds1da177e2005-04-16 15:20:36 -070044{
45 assert(ieee754dp_isnan(x));
Ralf Baechlead8fb552014-04-22 15:51:55 +020046 return ((DPMANT(x) & DP_MBIT(DP_FBITS-1)) == DP_MBIT(DP_FBITS-1));
Linus Torvalds1da177e2005-04-16 15:20:36 -070047}
48
49
Ralf Baechle2209bcb2014-04-16 01:31:11 +020050union ieee754dp __cold ieee754dp_xcpt(union ieee754dp r, const char *op, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -070051{
52 struct ieee754xctx ax;
Ralf Baechle9e8bad12014-04-19 00:36:32 +020053 if (!ieee754_tstx())
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 return r;
55
56 ax.op = op;
57 ax.rt = IEEE754_RT_DP;
58 ax.rv.dp = 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.dp;
63}
64
Ralf Baechle2209bcb2014-04-16 01:31:11 +020065union ieee754dp __cold ieee754dp_nanxcpt(union ieee754dp r, const char *op, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -070066{
67 struct ieee754xctx ax;
68
69 assert(ieee754dp_isnan(r));
70
71 if (!ieee754dp_issnan(r)) /* QNAN does not cause invalid op !! */
72 return r;
73
Ralf Baechle9e8bad12014-04-19 00:36:32 +020074 if (!ieee754_setandtestcx(IEEE754_INVALID_OPERATION)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 /* not enabled convert to a quiet NaN */
Ralf Baechlead8fb552014-04-22 15:51:55 +020076 DPMANT(r) &= (~DP_MBIT(DP_FBITS-1));
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 if (ieee754dp_isnan(r))
78 return r;
79 else
80 return ieee754dp_indef();
81 }
82
83 ax.op = op;
84 ax.rt = 0;
85 ax.rv.dp = 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.dp;
90}
91
Linus Torvalds1da177e2005-04-16 15:20:36 -070092static u64 get_rounding(int sn, u64 xm)
93{
94 /* inexact must round of 3 bits
95 */
96 if (xm & (DP_MBIT(3) - 1)) {
97 switch (ieee754_csr.rm) {
98 case IEEE754_RZ:
99 break;
100 case IEEE754_RN:
101 xm += 0x3 + ((xm >> 3) & 1);
102 /* xm += (xm&0x8)?0x4:0x3 */
103 break;
104 case IEEE754_RU: /* toward +Infinity */
105 if (!sn) /* ?? */
106 xm += 0x8;
107 break;
108 case IEEE754_RD: /* toward -Infinity */
Ralf Baechle70342282013-01-22 12:59:30 +0100109 if (sn) /* ?? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 xm += 0x8;
111 break;
112 }
113 }
114 return xm;
115}
116
117
118/* generate a normal/denormal number with over,under handling
119 * sn is sign
120 * xe is an unbiased exponent
121 * xm is 3bit extended precision value.
122 */
Ralf Baechle2209bcb2014-04-16 01:31:11 +0200123union ieee754dp ieee754dp_format(int sn, int xe, u64 xm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124{
125 assert(xm); /* we don't gen exact zeros (probably should) */
126
Ralf Baechlead8fb552014-04-22 15:51:55 +0200127 assert((xm >> (DP_FBITS + 1 + 3)) == 0); /* no execess */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 assert(xm & (DP_HIDDEN_BIT << 3));
129
130 if (xe < DP_EMIN) {
131 /* strip lower bits */
132 int es = DP_EMIN - xe;
133
134 if (ieee754_csr.nod) {
Ralf Baechle9e8bad12014-04-19 00:36:32 +0200135 ieee754_setcx(IEEE754_UNDERFLOW);
136 ieee754_setcx(IEEE754_INEXACT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
138 switch(ieee754_csr.rm) {
139 case IEEE754_RN:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 case IEEE754_RZ:
141 return ieee754dp_zero(sn);
142 case IEEE754_RU: /* toward +Infinity */
Ralf Baechle47fa0c02014-04-16 11:00:12 +0200143 if (sn == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 return ieee754dp_min(0);
145 else
146 return ieee754dp_zero(1);
147 case IEEE754_RD: /* toward -Infinity */
Ralf Baechle47fa0c02014-04-16 11:00:12 +0200148 if (sn == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 return ieee754dp_zero(0);
150 else
151 return ieee754dp_min(1);
152 }
153 }
154
155 if (xe == DP_EMIN - 1
Ralf Baechlead8fb552014-04-22 15:51:55 +0200156 && get_rounding(sn, xm) >> (DP_FBITS + 1 + 3))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 {
158 /* Not tiny after rounding */
Ralf Baechle9e8bad12014-04-19 00:36:32 +0200159 ieee754_setcx(IEEE754_INEXACT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 xm = get_rounding(sn, xm);
161 xm >>= 1;
162 /* Clear grs bits */
163 xm &= ~(DP_MBIT(3) - 1);
164 xe++;
165 }
166 else {
167 /* sticky right shift es bits
168 */
169 xm = XDPSRS(xm, es);
170 xe += es;
171 assert((xm & (DP_HIDDEN_BIT << 3)) == 0);
172 assert(xe == DP_EMIN);
173 }
174 }
175 if (xm & (DP_MBIT(3) - 1)) {
Ralf Baechle9e8bad12014-04-19 00:36:32 +0200176 ieee754_setcx(IEEE754_INEXACT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 if ((xm & (DP_HIDDEN_BIT << 3)) == 0) {
Ralf Baechle9e8bad12014-04-19 00:36:32 +0200178 ieee754_setcx(IEEE754_UNDERFLOW);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 }
180
181 /* inexact must round of 3 bits
182 */
183 xm = get_rounding(sn, xm);
184 /* adjust exponent for rounding add overflowing
185 */
Ralf Baechlead8fb552014-04-22 15:51:55 +0200186 if (xm >> (DP_FBITS + 3 + 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 /* add causes mantissa overflow */
188 xm >>= 1;
189 xe++;
190 }
191 }
192 /* strip grs bits */
193 xm >>= 3;
194
Ralf Baechlead8fb552014-04-22 15:51:55 +0200195 assert((xm >> (DP_FBITS + 1)) == 0); /* no execess */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 assert(xe >= DP_EMIN);
197
198 if (xe > DP_EMAX) {
Ralf Baechle9e8bad12014-04-19 00:36:32 +0200199 ieee754_setcx(IEEE754_OVERFLOW);
200 ieee754_setcx(IEEE754_INEXACT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 /* -O can be table indexed by (rm,sn) */
202 switch (ieee754_csr.rm) {
203 case IEEE754_RN:
204 return ieee754dp_inf(sn);
205 case IEEE754_RZ:
206 return ieee754dp_max(sn);
207 case IEEE754_RU: /* toward +Infinity */
208 if (sn == 0)
209 return ieee754dp_inf(0);
210 else
211 return ieee754dp_max(1);
212 case IEEE754_RD: /* toward -Infinity */
213 if (sn == 0)
214 return ieee754dp_max(0);
215 else
216 return ieee754dp_inf(1);
217 }
218 }
219 /* gen norm/denorm/zero */
220
221 if ((xm & DP_HIDDEN_BIT) == 0) {
222 /* we underflow (tiny/zero) */
223 assert(xe == DP_EMIN);
224 if (ieee754_csr.mx & IEEE754_UNDERFLOW)
Ralf Baechle9e8bad12014-04-19 00:36:32 +0200225 ieee754_setcx(IEEE754_UNDERFLOW);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 return builddp(sn, DP_EMIN - 1 + DP_EBIAS, xm);
227 } else {
Ralf Baechlead8fb552014-04-22 15:51:55 +0200228 assert((xm >> (DP_FBITS + 1)) == 0); /* no execess */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 assert(xm & DP_HIDDEN_BIT);
230
231 return builddp(sn, xe + DP_EBIAS, xm & ~DP_HIDDEN_BIT);
232 }
233}