2 * IEEE754 floating point arithmetic
3 * double precision: MSUB.f (Fused Multiply Subtract)
4 * MSUBF.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_msubf(union ieee754dp z
, union ieee754dp x
,
36 u64 zm
; int ze
; int zs __maybe_unused
; int zc
;
40 EXPLODEDP(z
, zc
, zs
, ze
, zm
)
44 FLUSHDP(z
, zc
, zs
, ze
, zm
);
49 case IEEE754_CLASS_SNAN
:
50 ieee754_setcx(IEEE754_INVALID_OPERATION
);
51 return ieee754dp_nanxcpt(z
);
52 case IEEE754_CLASS_DNORM
:
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
):
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
):
90 case CLPAIR(IEEE754_CLASS_INF
, IEEE754_CLASS_ZERO
):
91 case CLPAIR(IEEE754_CLASS_ZERO
, IEEE754_CLASS_INF
):
92 if (zc
== IEEE754_CLASS_QNAN
)
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
)
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 */
116 case CLPAIR(IEEE754_CLASS_DNORM
, IEEE754_CLASS_DNORM
):
119 case CLPAIR(IEEE754_CLASS_NORM
, IEEE754_CLASS_DNORM
):
120 if (zc
== IEEE754_CLASS_QNAN
)
122 else if (zc
== IEEE754_CLASS_INF
)
123 return ieee754dp_inf(zs
);
127 case CLPAIR(IEEE754_CLASS_DNORM
, IEEE754_CLASS_NORM
):
128 if (zc
== IEEE754_CLASS_QNAN
)
130 else if (zc
== IEEE754_CLASS_INF
)
131 return ieee754dp_inf(zs
);
135 case CLPAIR(IEEE754_CLASS_NORM
, IEEE754_CLASS_NORM
):
136 if (zc
== IEEE754_CLASS_QNAN
)
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
);
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.
167 #define DPXMULT(x, y) ((u64)(x) * (u64)y)
174 lrm
= DPXMULT(lxm
, lym
);
175 hrm
= DPXMULT(hxm
, hym
);
177 t
= DPXMULT(lxm
, hym
);
179 at
= lrm
+ (t
<< 32);
183 hrm
= hrm
+ (t
>> 32);
185 t
= DPXMULT(hxm
, lym
);
187 at
= lrm
+ (t
<< 32);
191 hrm
= hrm
+ (t
>> 32);
193 rm
= hrm
| (lrm
!= 0);
196 * Sticky shift down to normal rounding precision.
199 rm
= (rm
>> (64 - (DP_FBITS
+ 1 + 3))) |
200 ((rm
<< (DP_FBITS
+ 1 + 3)) != 0);
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 subtraction */
210 /* flip sign of r and handle as add */
213 assert(zm
& DP_HIDDEN_BIT
);
216 * Provide guard,round and stick bit space.
222 * Have to shift y fraction right to align.
227 } else if (re
> ze
) {
229 * Have to shift x fraction right to align.
236 assert(ze
<= DP_EMAX
);
240 * Generate 28 bit result of adding two 27 bit numbers
241 * leaving result in xm, xs and xe.
245 if (zm
>> (DP_FBITS
+ 1 + 3)) { /* carry out */
257 return ieee754dp_zero(ieee754_csr
.rm
== FPU_CSR_RD
);
260 * Normalize to rounding precision.
262 while ((zm
>> (DP_FBITS
+ 3)) == 0) {
268 return ieee754dp_format(zs
, ze
, zm
);