2 * IEEE754 floating point arithmetic
3 * single 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 "ieee754sp.h"
18 static union ieee754sp
_sp_maddf(union ieee754sp z
, union ieee754sp x
,
19 union ieee754sp y
, enum maddf_flags flags
)
43 * Handle the cases when at least one of x, y or z is a NaN.
44 * Order of precedence is sNaN, qNaN and z, x, y.
46 if (zc
== IEEE754_CLASS_SNAN
)
47 return ieee754sp_nanxcpt(z
);
48 if (xc
== IEEE754_CLASS_SNAN
)
49 return ieee754sp_nanxcpt(x
);
50 if (yc
== IEEE754_CLASS_SNAN
)
51 return ieee754sp_nanxcpt(y
);
52 if (zc
== IEEE754_CLASS_QNAN
)
54 if (xc
== IEEE754_CLASS_QNAN
)
56 if (yc
== IEEE754_CLASS_QNAN
)
59 if (zc
== IEEE754_CLASS_DNORM
)
61 /* ZERO z cases are handled separately below */
63 switch (CLPAIR(xc
, yc
)) {
69 case CLPAIR(IEEE754_CLASS_INF
, IEEE754_CLASS_ZERO
):
70 case CLPAIR(IEEE754_CLASS_ZERO
, IEEE754_CLASS_INF
):
71 ieee754_setcx(IEEE754_INVALID_OPERATION
);
72 return ieee754sp_indef();
74 case CLPAIR(IEEE754_CLASS_NORM
, IEEE754_CLASS_INF
):
75 case CLPAIR(IEEE754_CLASS_DNORM
, IEEE754_CLASS_INF
):
76 case CLPAIR(IEEE754_CLASS_INF
, IEEE754_CLASS_NORM
):
77 case CLPAIR(IEEE754_CLASS_INF
, IEEE754_CLASS_DNORM
):
78 case CLPAIR(IEEE754_CLASS_INF
, IEEE754_CLASS_INF
):
79 if ((zc
== IEEE754_CLASS_INF
) &&
80 ((!(flags
& MADDF_NEGATE_PRODUCT
) && (zs
!= (xs
^ ys
))) ||
81 ((flags
& MADDF_NEGATE_PRODUCT
) && (zs
== (xs
^ ys
))))) {
83 * Cases of addition of infinities with opposite signs
84 * or subtraction of infinities with same signs.
86 ieee754_setcx(IEEE754_INVALID_OPERATION
);
87 return ieee754sp_indef();
90 * z is here either not an infinity, or an infinity having the
91 * same sign as product (x*y) (in case of MADDF.D instruction)
92 * or product -(x*y) (in MSUBF.D case). The result must be an
93 * infinity, and its sign is determined only by the value of
94 * (flags & MADDF_NEGATE_PRODUCT) and the signs of x and y.
96 if (flags
& MADDF_NEGATE_PRODUCT
)
97 return ieee754sp_inf(1 ^ (xs
^ ys
));
99 return ieee754sp_inf(xs
^ ys
);
101 case CLPAIR(IEEE754_CLASS_ZERO
, IEEE754_CLASS_ZERO
):
102 case CLPAIR(IEEE754_CLASS_ZERO
, IEEE754_CLASS_NORM
):
103 case CLPAIR(IEEE754_CLASS_ZERO
, IEEE754_CLASS_DNORM
):
104 case CLPAIR(IEEE754_CLASS_NORM
, IEEE754_CLASS_ZERO
):
105 case CLPAIR(IEEE754_CLASS_DNORM
, IEEE754_CLASS_ZERO
):
106 if (zc
== IEEE754_CLASS_INF
)
107 return ieee754sp_inf(zs
);
108 if (zc
== IEEE754_CLASS_ZERO
) {
109 /* Handle cases +0 + (-0) and similar ones. */
110 if ((!(flags
& MADDF_NEGATE_PRODUCT
)
111 && (zs
== (xs
^ ys
))) ||
112 ((flags
& MADDF_NEGATE_PRODUCT
)
113 && (zs
!= (xs
^ ys
))))
115 * Cases of addition of zeros of equal signs
116 * or subtraction of zeroes of opposite signs.
117 * The sign of the resulting zero is in any
118 * such case determined only by the sign of z.
122 return ieee754sp_zero(ieee754_csr
.rm
== FPU_CSR_RD
);
124 /* x*y is here 0, and z is not 0, so just return z */
127 case CLPAIR(IEEE754_CLASS_DNORM
, IEEE754_CLASS_DNORM
):
131 case CLPAIR(IEEE754_CLASS_NORM
, IEEE754_CLASS_DNORM
):
132 if (zc
== IEEE754_CLASS_INF
)
133 return ieee754sp_inf(zs
);
137 case CLPAIR(IEEE754_CLASS_DNORM
, IEEE754_CLASS_NORM
):
138 if (zc
== IEEE754_CLASS_INF
)
139 return ieee754sp_inf(zs
);
143 case CLPAIR(IEEE754_CLASS_NORM
, IEEE754_CLASS_NORM
):
144 if (zc
== IEEE754_CLASS_INF
)
145 return ieee754sp_inf(zs
);
146 /* continue to real computations */
149 /* Finally get to do some computation */
152 * Do the multiplication bit first
154 * rm = xm * ym, re = xe + ye basically
156 * At this point xm and ym should have been normalized.
159 /* rm = xm * ym, re = xe+ye basically */
160 assert(xm
& SP_HIDDEN_BIT
);
161 assert(ym
& SP_HIDDEN_BIT
);
165 if (flags
& MADDF_NEGATE_PRODUCT
)
168 /* Multiple 24 bit xm and ym to give 48 bit results */
169 rm64
= (uint64_t)xm
* ym
;
171 /* Shunt to top of word */
174 /* Put explicit bit at bit 62 if necessary */
175 if ((int64_t) rm64
< 0) {
180 assert(rm64
& (1 << 62));
182 if (zc
== IEEE754_CLASS_ZERO
) {
184 * Move explicit bit from bit 62 to bit 26 since the
185 * ieee754sp_format code expects the mantissa to be
186 * 27 bits wide (24 + 3 rounding bits).
188 rm
= XSPSRS64(rm64
, (62 - 26));
189 return ieee754sp_format(rs
, re
, rm
);
192 /* Move explicit bit from bit 23 to bit 62 */
193 zm64
= (uint64_t)zm
<< (62 - 23);
194 assert(zm64
& (1 << 62));
196 /* Make the exponents the same */
199 * Have to shift r fraction right to align.
202 rm64
= XSPSRS64(rm64
, s
);
204 } else if (re
> ze
) {
206 * Have to shift z fraction right to align.
209 zm64
= XSPSRS64(zm64
, s
);
213 assert(ze
<= SP_EMAX
);
215 /* Do the addition */
218 * Generate 64 bit result by adding two 63 bit numbers
219 * leaving result in zm64, zs and ze.
222 if ((int64_t)zm64
< 0) { /* carry out */
223 zm64
= XSPSRS1(zm64
);
234 return ieee754sp_zero(ieee754_csr
.rm
== FPU_CSR_RD
);
237 * Put explicit bit at bit 62 if necessary.
239 while ((zm64
>> 62) == 0) {
246 * Move explicit bit from bit 62 to bit 26 since the
247 * ieee754sp_format code expects the mantissa to be
248 * 27 bits wide (24 + 3 rounding bits).
250 zm
= XSPSRS64(zm64
, (62 - 26));
252 return ieee754sp_format(zs
, ze
, zm
);
255 union ieee754sp
ieee754sp_maddf(union ieee754sp z
, union ieee754sp x
,
258 return _sp_maddf(z
, x
, y
, 0);
261 union ieee754sp
ieee754sp_msubf(union ieee754sp z
, union ieee754sp x
,
264 return _sp_maddf(z
, x
, y
, MADDF_NEGATE_PRODUCT
);