1 // SPDX-License-Identifier: GPL-2.0-only
3 * IEEE754 floating point arithmetic
4 * single precision: MADDF.f (Fused Multiply Add)
5 * MADDF.fmt: FPR[fd] = FPR[fd] + (FPR[fs] x FPR[ft])
7 * MIPS floating point support
8 * Copyright (C) 2015 Imagination Technologies, Ltd.
9 * Author: Markos Chandras <markos.chandras@imgtec.com>
12 #include "ieee754sp.h"
15 static union ieee754sp _sp_maddf(union ieee754sp z, union ieee754sp x,
16 union ieee754sp y, enum maddf_flags flags)
40 * Handle the cases when at least one of x, y or z is a NaN.
41 * Order of precedence is sNaN, qNaN and z, x, y.
43 if (zc == IEEE754_CLASS_SNAN)
44 return ieee754sp_nanxcpt(z);
45 if (xc == IEEE754_CLASS_SNAN)
46 return ieee754sp_nanxcpt(x);
47 if (yc == IEEE754_CLASS_SNAN)
48 return ieee754sp_nanxcpt(y);
49 if (zc == IEEE754_CLASS_QNAN)
51 if (xc == IEEE754_CLASS_QNAN)
53 if (yc == IEEE754_CLASS_QNAN)
56 if (zc == IEEE754_CLASS_DNORM)
58 /* ZERO z cases are handled separately below */
60 switch (CLPAIR(xc, yc)) {
66 case CLPAIR(IEEE754_CLASS_INF, IEEE754_CLASS_ZERO):
67 case CLPAIR(IEEE754_CLASS_ZERO, IEEE754_CLASS_INF):
68 ieee754_setcx(IEEE754_INVALID_OPERATION);
69 return ieee754sp_indef();
71 case CLPAIR(IEEE754_CLASS_NORM, IEEE754_CLASS_INF):
72 case CLPAIR(IEEE754_CLASS_DNORM, IEEE754_CLASS_INF):
73 case CLPAIR(IEEE754_CLASS_INF, IEEE754_CLASS_NORM):
74 case CLPAIR(IEEE754_CLASS_INF, IEEE754_CLASS_DNORM):
75 case CLPAIR(IEEE754_CLASS_INF, IEEE754_CLASS_INF):
76 if ((zc == IEEE754_CLASS_INF) &&
77 ((!(flags & MADDF_NEGATE_PRODUCT) && (zs != (xs ^ ys))) ||
78 ((flags & MADDF_NEGATE_PRODUCT) && (zs == (xs ^ ys))))) {
80 * Cases of addition of infinities with opposite signs
81 * or subtraction of infinities with same signs.
83 ieee754_setcx(IEEE754_INVALID_OPERATION);
84 return ieee754sp_indef();
87 * z is here either not an infinity, or an infinity having the
88 * same sign as product (x*y) (in case of MADDF.D instruction)
89 * or product -(x*y) (in MSUBF.D case). The result must be an
90 * infinity, and its sign is determined only by the value of
91 * (flags & MADDF_NEGATE_PRODUCT) and the signs of x and y.
93 if (flags & MADDF_NEGATE_PRODUCT)
94 return ieee754sp_inf(1 ^ (xs ^ ys));
96 return ieee754sp_inf(xs ^ ys);
98 case CLPAIR(IEEE754_CLASS_ZERO, IEEE754_CLASS_ZERO):
99 case CLPAIR(IEEE754_CLASS_ZERO, IEEE754_CLASS_NORM):
100 case CLPAIR(IEEE754_CLASS_ZERO, IEEE754_CLASS_DNORM):
101 case CLPAIR(IEEE754_CLASS_NORM, IEEE754_CLASS_ZERO):
102 case CLPAIR(IEEE754_CLASS_DNORM, IEEE754_CLASS_ZERO):
103 if (zc == IEEE754_CLASS_INF)
104 return ieee754sp_inf(zs);
105 if (zc == IEEE754_CLASS_ZERO) {
106 /* Handle cases +0 + (-0) and similar ones. */
107 if ((!(flags & MADDF_NEGATE_PRODUCT)
108 && (zs == (xs ^ ys))) ||
109 ((flags & MADDF_NEGATE_PRODUCT)
110 && (zs != (xs ^ ys))))
112 * Cases of addition of zeros of equal signs
113 * or subtraction of zeroes of opposite signs.
114 * The sign of the resulting zero is in any
115 * such case determined only by the sign of z.
119 return ieee754sp_zero(ieee754_csr.rm == FPU_CSR_RD);
121 /* x*y is here 0, and z is not 0, so just return z */
124 case CLPAIR(IEEE754_CLASS_DNORM, IEEE754_CLASS_DNORM):
128 case CLPAIR(IEEE754_CLASS_NORM, IEEE754_CLASS_DNORM):
129 if (zc == IEEE754_CLASS_INF)
130 return ieee754sp_inf(zs);
134 case CLPAIR(IEEE754_CLASS_DNORM, IEEE754_CLASS_NORM):
135 if (zc == IEEE754_CLASS_INF)
136 return ieee754sp_inf(zs);
140 case CLPAIR(IEEE754_CLASS_NORM, IEEE754_CLASS_NORM):
141 if (zc == IEEE754_CLASS_INF)
142 return ieee754sp_inf(zs);
143 /* continue to real computations */
146 /* Finally get to do some computation */
149 * Do the multiplication bit first
151 * rm = xm * ym, re = xe + ye basically
153 * At this point xm and ym should have been normalized.
156 /* rm = xm * ym, re = xe+ye basically */
157 assert(xm & SP_HIDDEN_BIT);
158 assert(ym & SP_HIDDEN_BIT);
162 if (flags & MADDF_NEGATE_PRODUCT)
165 /* Multiple 24 bit xm and ym to give 48 bit results */
166 rm64 = (uint64_t)xm * ym;
168 /* Shunt to top of word */
171 /* Put explicit bit at bit 62 if necessary */
172 if ((int64_t) rm64 < 0) {
177 assert(rm64 & (1 << 62));
179 if (zc == IEEE754_CLASS_ZERO) {
181 * Move explicit bit from bit 62 to bit 26 since the
182 * ieee754sp_format code expects the mantissa to be
183 * 27 bits wide (24 + 3 rounding bits).
185 rm = XSPSRS64(rm64, (62 - 26));
186 return ieee754sp_format(rs, re, rm);
189 /* Move explicit bit from bit 23 to bit 62 */
190 zm64 = (uint64_t)zm << (62 - 23);
191 assert(zm64 & (1 << 62));
193 /* Make the exponents the same */
196 * Have to shift r fraction right to align.
199 rm64 = XSPSRS64(rm64, s);
201 } else if (re > ze) {
203 * Have to shift z fraction right to align.
206 zm64 = XSPSRS64(zm64, s);
210 assert(ze <= SP_EMAX);
212 /* Do the addition */
215 * Generate 64 bit result by adding two 63 bit numbers
216 * leaving result in zm64, zs and ze.
219 if ((int64_t)zm64 < 0) { /* carry out */
220 zm64 = XSPSRS1(zm64);
231 return ieee754sp_zero(ieee754_csr.rm == FPU_CSR_RD);
234 * Put explicit bit at bit 62 if necessary.
236 while ((zm64 >> 62) == 0) {
243 * Move explicit bit from bit 62 to bit 26 since the
244 * ieee754sp_format code expects the mantissa to be
245 * 27 bits wide (24 + 3 rounding bits).
247 zm = XSPSRS64(zm64, (62 - 26));
249 return ieee754sp_format(zs, ze, zm);
252 union ieee754sp ieee754sp_maddf(union ieee754sp z, union ieee754sp x,
255 return _sp_maddf(z, x, y, 0);
258 union ieee754sp ieee754sp_msubf(union ieee754sp z, union ieee754sp x,
261 return _sp_maddf(z, x, y, MADDF_NEGATE_PRODUCT);