of: MSI: Simplify irqdomain lookup
[linux/fpc-iii.git] / arch / mips / math-emu / dp_maddf.c
blob119eda9fa1ea8f353fded2fe03beb9bece359154
1 /*
2 * IEEE754 floating point arithmetic
3 * double precision: MADDF.f (Fused Multiply Add)
4 * MADDF.fmt: FPR[fd] = FPR[fd] + (FPR[fs] x FPR[ft])
6 * MIPS floating point support
7 * Copyright (C) 2015 Imagination Technologies, Ltd.
8 * Author: Markos Chandras <markos.chandras@imgtec.com>
10 * This program is free software; you can distribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the
12 * Free Software Foundation; version 2 of the License.
15 #include "ieee754dp.h"
17 union ieee754dp ieee754dp_maddf(union ieee754dp z, union ieee754dp x,
18 union ieee754dp y)
20 int re;
21 int rs;
22 u64 rm;
23 unsigned lxm;
24 unsigned hxm;
25 unsigned lym;
26 unsigned hym;
27 u64 lrm;
28 u64 hrm;
29 u64 t;
30 u64 at;
31 int s;
33 COMPXDP;
34 COMPYDP;
36 u64 zm; int ze; int zs __maybe_unused; int zc;
38 EXPLODEXDP;
39 EXPLODEYDP;
40 EXPLODEDP(z, zc, zs, ze, zm)
42 FLUSHXDP;
43 FLUSHYDP;
44 FLUSHDP(z, zc, zs, ze, zm);
46 ieee754_clearcx();
48 switch (zc) {
49 case IEEE754_CLASS_SNAN:
50 ieee754_setcx(IEEE754_INVALID_OPERATION);
51 return ieee754dp_nanxcpt(z);
52 case IEEE754_CLASS_DNORM:
53 DPDNORMx(zm, ze);
54 /* QNAN is handled separately below */
57 switch (CLPAIR(xc, yc)) {
58 case CLPAIR(IEEE754_CLASS_QNAN, IEEE754_CLASS_SNAN):
59 case CLPAIR(IEEE754_CLASS_ZERO, IEEE754_CLASS_SNAN):
60 case CLPAIR(IEEE754_CLASS_NORM, IEEE754_CLASS_SNAN):
61 case CLPAIR(IEEE754_CLASS_DNORM, IEEE754_CLASS_SNAN):
62 case CLPAIR(IEEE754_CLASS_INF, IEEE754_CLASS_SNAN):
63 return ieee754dp_nanxcpt(y);
65 case CLPAIR(IEEE754_CLASS_SNAN, IEEE754_CLASS_SNAN):
66 case CLPAIR(IEEE754_CLASS_SNAN, IEEE754_CLASS_QNAN):
67 case CLPAIR(IEEE754_CLASS_SNAN, IEEE754_CLASS_ZERO):
68 case CLPAIR(IEEE754_CLASS_SNAN, IEEE754_CLASS_NORM):
69 case CLPAIR(IEEE754_CLASS_SNAN, IEEE754_CLASS_DNORM):
70 case CLPAIR(IEEE754_CLASS_SNAN, IEEE754_CLASS_INF):
71 return ieee754dp_nanxcpt(x);
73 case CLPAIR(IEEE754_CLASS_ZERO, IEEE754_CLASS_QNAN):
74 case CLPAIR(IEEE754_CLASS_NORM, IEEE754_CLASS_QNAN):
75 case CLPAIR(IEEE754_CLASS_DNORM, IEEE754_CLASS_QNAN):
76 case CLPAIR(IEEE754_CLASS_INF, IEEE754_CLASS_QNAN):
77 return y;
79 case CLPAIR(IEEE754_CLASS_QNAN, IEEE754_CLASS_QNAN):
80 case CLPAIR(IEEE754_CLASS_QNAN, IEEE754_CLASS_ZERO):
81 case CLPAIR(IEEE754_CLASS_QNAN, IEEE754_CLASS_NORM):
82 case CLPAIR(IEEE754_CLASS_QNAN, IEEE754_CLASS_DNORM):
83 case CLPAIR(IEEE754_CLASS_QNAN, IEEE754_CLASS_INF):
84 return x;
88 * Infinity handling
90 case CLPAIR(IEEE754_CLASS_INF, IEEE754_CLASS_ZERO):
91 case CLPAIR(IEEE754_CLASS_ZERO, IEEE754_CLASS_INF):
92 if (zc == IEEE754_CLASS_QNAN)
93 return z;
94 ieee754_setcx(IEEE754_INVALID_OPERATION);
95 return ieee754dp_indef();
97 case CLPAIR(IEEE754_CLASS_NORM, IEEE754_CLASS_INF):
98 case CLPAIR(IEEE754_CLASS_DNORM, IEEE754_CLASS_INF):
99 case CLPAIR(IEEE754_CLASS_INF, IEEE754_CLASS_NORM):
100 case CLPAIR(IEEE754_CLASS_INF, IEEE754_CLASS_DNORM):
101 case CLPAIR(IEEE754_CLASS_INF, IEEE754_CLASS_INF):
102 if (zc == IEEE754_CLASS_QNAN)
103 return z;
104 return ieee754dp_inf(xs ^ ys);
106 case CLPAIR(IEEE754_CLASS_ZERO, IEEE754_CLASS_ZERO):
107 case CLPAIR(IEEE754_CLASS_ZERO, IEEE754_CLASS_NORM):
108 case CLPAIR(IEEE754_CLASS_ZERO, IEEE754_CLASS_DNORM):
109 case CLPAIR(IEEE754_CLASS_NORM, IEEE754_CLASS_ZERO):
110 case CLPAIR(IEEE754_CLASS_DNORM, IEEE754_CLASS_ZERO):
111 if (zc == IEEE754_CLASS_INF)
112 return ieee754dp_inf(zs);
113 /* Multiplication is 0 so just return z */
114 return z;
116 case CLPAIR(IEEE754_CLASS_DNORM, IEEE754_CLASS_DNORM):
117 DPDNORMX;
119 case CLPAIR(IEEE754_CLASS_NORM, IEEE754_CLASS_DNORM):
120 if (zc == IEEE754_CLASS_QNAN)
121 return z;
122 else if (zc == IEEE754_CLASS_INF)
123 return ieee754dp_inf(zs);
124 DPDNORMY;
125 break;
127 case CLPAIR(IEEE754_CLASS_DNORM, IEEE754_CLASS_NORM):
128 if (zc == IEEE754_CLASS_QNAN)
129 return z;
130 else if (zc == IEEE754_CLASS_INF)
131 return ieee754dp_inf(zs);
132 DPDNORMX;
133 break;
135 case CLPAIR(IEEE754_CLASS_NORM, IEEE754_CLASS_NORM):
136 if (zc == IEEE754_CLASS_QNAN)
137 return z;
138 else if (zc == IEEE754_CLASS_INF)
139 return ieee754dp_inf(zs);
140 /* fall through to real computations */
143 /* Finally get to do some computation */
146 * Do the multiplication bit first
148 * rm = xm * ym, re = xe + ye basically
150 * At this point xm and ym should have been normalized.
152 assert(xm & DP_HIDDEN_BIT);
153 assert(ym & DP_HIDDEN_BIT);
155 re = xe + ye;
156 rs = xs ^ ys;
158 /* shunt to top of word */
159 xm <<= 64 - (DP_FBITS + 1);
160 ym <<= 64 - (DP_FBITS + 1);
163 * Multiply 32 bits xm, ym to give high 32 bits rm with stickness.
166 /* 32 * 32 => 64 */
167 #define DPXMULT(x, y) ((u64)(x) * (u64)y)
169 lxm = xm;
170 hxm = xm >> 32;
171 lym = ym;
172 hym = ym >> 32;
174 lrm = DPXMULT(lxm, lym);
175 hrm = DPXMULT(hxm, hym);
177 t = DPXMULT(lxm, hym);
179 at = lrm + (t << 32);
180 hrm += at < lrm;
181 lrm = at;
183 hrm = hrm + (t >> 32);
185 t = DPXMULT(hxm, lym);
187 at = lrm + (t << 32);
188 hrm += at < lrm;
189 lrm = at;
191 hrm = hrm + (t >> 32);
193 rm = hrm | (lrm != 0);
196 * Sticky shift down to normal rounding precision.
198 if ((s64) rm < 0) {
199 rm = (rm >> (64 - (DP_FBITS + 1 + 3))) |
200 ((rm << (DP_FBITS + 1 + 3)) != 0);
201 re++;
202 } else {
203 rm = (rm >> (64 - (DP_FBITS + 1 + 3 + 1))) |
204 ((rm << (DP_FBITS + 1 + 3 + 1)) != 0);
206 assert(rm & (DP_HIDDEN_BIT << 3));
208 /* And now the addition */
209 assert(zm & DP_HIDDEN_BIT);
212 * Provide guard,round and stick bit space.
214 zm <<= 3;
216 if (ze > re) {
218 * Have to shift y fraction right to align.
220 s = ze - re;
221 rm = XDPSRS(rm, s);
222 re += s;
223 } else if (re > ze) {
225 * Have to shift x fraction right to align.
227 s = re - ze;
228 zm = XDPSRS(zm, s);
229 ze += s;
231 assert(ze == re);
232 assert(ze <= DP_EMAX);
234 if (zs == rs) {
236 * Generate 28 bit result of adding two 27 bit numbers
237 * leaving result in xm, xs and xe.
239 zm = zm + rm;
241 if (zm >> (DP_FBITS + 1 + 3)) { /* carry out */
242 zm = XDPSRS1(zm);
243 ze++;
245 } else {
246 if (zm >= rm) {
247 zm = zm - rm;
248 } else {
249 zm = rm - zm;
250 zs = rs;
252 if (zm == 0)
253 return ieee754dp_zero(ieee754_csr.rm == FPU_CSR_RD);
256 * Normalize to rounding precision.
258 while ((zm >> (DP_FBITS + 3)) == 0) {
259 zm <<= 1;
260 ze--;
264 return ieee754dp_format(zs, ze, zm);