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 if (flags & MADDF_NEGATE_PRODUCT)
42 if (flags & MADDF_NEGATE_ADDITION)
46 * Handle the cases when at least one of x, y or z is a NaN.
47 * Order of precedence is sNaN, qNaN and z, x, y.
49 if (zc == IEEE754_CLASS_SNAN)
50 return ieee754sp_nanxcpt(z);
51 if (xc == IEEE754_CLASS_SNAN)
52 return ieee754sp_nanxcpt(x);
53 if (yc == IEEE754_CLASS_SNAN)
54 return ieee754sp_nanxcpt(y);
55 if (zc == IEEE754_CLASS_QNAN)
57 if (xc == IEEE754_CLASS_QNAN)
59 if (yc == IEEE754_CLASS_QNAN)
62 if (zc == IEEE754_CLASS_DNORM)
64 /* ZERO z cases are handled separately below */
66 switch (CLPAIR(xc, yc)) {
72 case CLPAIR(IEEE754_CLASS_INF, IEEE754_CLASS_ZERO):
73 case CLPAIR(IEEE754_CLASS_ZERO, IEEE754_CLASS_INF):
74 ieee754_setcx(IEEE754_INVALID_OPERATION);
75 return ieee754sp_indef();
77 case CLPAIR(IEEE754_CLASS_NORM, IEEE754_CLASS_INF):
78 case CLPAIR(IEEE754_CLASS_DNORM, IEEE754_CLASS_INF):
79 case CLPAIR(IEEE754_CLASS_INF, IEEE754_CLASS_NORM):
80 case CLPAIR(IEEE754_CLASS_INF, IEEE754_CLASS_DNORM):
81 case CLPAIR(IEEE754_CLASS_INF, IEEE754_CLASS_INF):
82 if ((zc == IEEE754_CLASS_INF) && (zs != rs)) {
84 * Cases of addition of infinities with opposite signs
85 * or subtraction of infinities with same signs.
87 ieee754_setcx(IEEE754_INVALID_OPERATION);
88 return ieee754sp_indef();
91 * z is here either not an infinity, or an infinity having the
92 * same sign as product (x*y). The result must be an infinity,
93 * and its sign is determined only by the sign of product (x*y).
95 return ieee754sp_inf(rs);
97 case CLPAIR(IEEE754_CLASS_ZERO, IEEE754_CLASS_ZERO):
98 case CLPAIR(IEEE754_CLASS_ZERO, IEEE754_CLASS_NORM):
99 case CLPAIR(IEEE754_CLASS_ZERO, IEEE754_CLASS_DNORM):
100 case CLPAIR(IEEE754_CLASS_NORM, IEEE754_CLASS_ZERO):
101 case CLPAIR(IEEE754_CLASS_DNORM, IEEE754_CLASS_ZERO):
102 if (zc == IEEE754_CLASS_INF)
103 return ieee754sp_inf(zs);
104 if (zc == IEEE754_CLASS_ZERO) {
105 /* Handle cases +0 + (-0) and similar ones. */
108 * Cases of addition of zeros of equal signs
109 * or subtraction of zeroes of opposite signs.
110 * The sign of the resulting zero is in any
111 * such case determined only by the sign of z.
115 return ieee754sp_zero(ieee754_csr.rm == FPU_CSR_RD);
117 /* x*y is here 0, and z is not 0, so just return z */
120 case CLPAIR(IEEE754_CLASS_DNORM, IEEE754_CLASS_DNORM):
123 case CLPAIR(IEEE754_CLASS_NORM, IEEE754_CLASS_DNORM):
124 if (zc == IEEE754_CLASS_INF)
125 return ieee754sp_inf(zs);
129 case CLPAIR(IEEE754_CLASS_DNORM, IEEE754_CLASS_NORM):
130 if (zc == IEEE754_CLASS_INF)
131 return ieee754sp_inf(zs);
135 case CLPAIR(IEEE754_CLASS_NORM, IEEE754_CLASS_NORM):
136 if (zc == IEEE754_CLASS_INF)
137 return ieee754sp_inf(zs);
138 /* continue to real computations */
141 /* Finally get to do some computation */
144 * Do the multiplication bit first
146 * rm = xm * ym, re = xe + ye basically
148 * At this point xm and ym should have been normalized.
151 /* rm = xm * ym, re = xe+ye basically */
152 assert(xm & SP_HIDDEN_BIT);
153 assert(ym & SP_HIDDEN_BIT);
157 /* Multiple 24 bit xm and ym to give 48 bit results */
158 rm64 = (uint64_t)xm * ym;
160 /* Shunt to top of word */
163 /* Put explicit bit at bit 62 if necessary */
164 if ((int64_t) rm64 < 0) {
169 assert(rm64 & (1 << 62));
171 if (zc == IEEE754_CLASS_ZERO) {
173 * Move explicit bit from bit 62 to bit 26 since the
174 * ieee754sp_format code expects the mantissa to be
175 * 27 bits wide (24 + 3 rounding bits).
177 rm = XSPSRS64(rm64, (62 - 26));
178 return ieee754sp_format(rs, re, rm);
181 /* Move explicit bit from bit 23 to bit 62 */
182 zm64 = (uint64_t)zm << (62 - 23);
183 assert(zm64 & (1 << 62));
185 /* Make the exponents the same */
188 * Have to shift r fraction right to align.
191 rm64 = XSPSRS64(rm64, s);
193 } else if (re > ze) {
195 * Have to shift z fraction right to align.
198 zm64 = XSPSRS64(zm64, s);
202 assert(ze <= SP_EMAX);
204 /* Do the addition */
207 * Generate 64 bit result by adding two 63 bit numbers
208 * leaving result in zm64, zs and ze.
211 if ((int64_t)zm64 < 0) { /* carry out */
212 zm64 = XSPSRS1(zm64);
223 return ieee754sp_zero(ieee754_csr.rm == FPU_CSR_RD);
226 * Put explicit bit at bit 62 if necessary.
228 while ((zm64 >> 62) == 0) {
235 * Move explicit bit from bit 62 to bit 26 since the
236 * ieee754sp_format code expects the mantissa to be
237 * 27 bits wide (24 + 3 rounding bits).
239 zm = XSPSRS64(zm64, (62 - 26));
241 return ieee754sp_format(zs, ze, zm);
244 union ieee754sp ieee754sp_maddf(union ieee754sp z, union ieee754sp x,
247 return _sp_maddf(z, x, y, 0);
250 union ieee754sp ieee754sp_msubf(union ieee754sp z, union ieee754sp x,
253 return _sp_maddf(z, x, y, MADDF_NEGATE_PRODUCT);
256 union ieee754sp ieee754sp_madd(union ieee754sp z, union ieee754sp x,
259 return _sp_maddf(z, x, y, 0);
262 union ieee754sp ieee754sp_msub(union ieee754sp z, union ieee754sp x,
265 return _sp_maddf(z, x, y, MADDF_NEGATE_ADDITION);
268 union ieee754sp ieee754sp_nmadd(union ieee754sp z, union ieee754sp x,
271 return _sp_maddf(z, x, y, MADDF_NEGATE_PRODUCT|MADDF_NEGATE_ADDITION);
274 union ieee754sp ieee754sp_nmsub(union ieee754sp z, union ieee754sp x,
277 return _sp_maddf(z, x, y, MADDF_NEGATE_PRODUCT);