2014-05-28 23:08:05 +08:00
|
|
|
//===---- lib/fp_mul_impl.inc - floating point multiplication -----*- C -*-===//
|
|
|
|
//
|
2019-01-19 18:56:40 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2014-05-28 23:08:05 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements soft-float multiplication with the IEEE-754 default
|
|
|
|
// rounding (to nearest, ties to even).
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "fp_lib.h"
|
|
|
|
|
2015-10-11 05:21:28 +08:00
|
|
|
static __inline fp_t __mulXf3__(fp_t a, fp_t b) {
|
2019-04-29 05:53:32 +08:00
|
|
|
const unsigned int aExponent = toRep(a) >> significandBits & maxExponent;
|
|
|
|
const unsigned int bExponent = toRep(b) >> significandBits & maxExponent;
|
|
|
|
const rep_t productSign = (toRep(a) ^ toRep(b)) & signBit;
|
|
|
|
|
|
|
|
rep_t aSignificand = toRep(a) & significandMask;
|
|
|
|
rep_t bSignificand = toRep(b) & significandMask;
|
|
|
|
int scale = 0;
|
|
|
|
|
|
|
|
// Detect if a or b is zero, denormal, infinity, or NaN.
|
|
|
|
if (aExponent - 1U >= maxExponent - 1U ||
|
|
|
|
bExponent - 1U >= maxExponent - 1U) {
|
|
|
|
|
|
|
|
const rep_t aAbs = toRep(a) & absMask;
|
|
|
|
const rep_t bAbs = toRep(b) & absMask;
|
|
|
|
|
|
|
|
// NaN * anything = qNaN
|
|
|
|
if (aAbs > infRep)
|
|
|
|
return fromRep(toRep(a) | quietBit);
|
|
|
|
// anything * NaN = qNaN
|
|
|
|
if (bAbs > infRep)
|
|
|
|
return fromRep(toRep(b) | quietBit);
|
|
|
|
|
|
|
|
if (aAbs == infRep) {
|
|
|
|
// infinity * non-zero = +/- infinity
|
|
|
|
if (bAbs)
|
|
|
|
return fromRep(aAbs | productSign);
|
|
|
|
// infinity * zero = NaN
|
|
|
|
else
|
|
|
|
return fromRep(qnanRep);
|
2014-05-28 23:08:05 +08:00
|
|
|
}
|
|
|
|
|
2019-04-29 05:53:32 +08:00
|
|
|
if (bAbs == infRep) {
|
2019-05-10 06:48:30 +08:00
|
|
|
// non-zero * infinity = +/- infinity
|
2019-04-29 05:53:32 +08:00
|
|
|
if (aAbs)
|
|
|
|
return fromRep(bAbs | productSign);
|
|
|
|
// zero * infinity = NaN
|
|
|
|
else
|
|
|
|
return fromRep(qnanRep);
|
2014-05-28 23:08:05 +08:00
|
|
|
}
|
|
|
|
|
2019-04-29 05:53:32 +08:00
|
|
|
// zero * anything = +/- zero
|
|
|
|
if (!aAbs)
|
|
|
|
return fromRep(productSign);
|
|
|
|
// anything * zero = +/- zero
|
|
|
|
if (!bAbs)
|
|
|
|
return fromRep(productSign);
|
|
|
|
|
2019-05-10 06:48:30 +08:00
|
|
|
// One or both of a or b is denormal. The other (if applicable) is a
|
2019-04-29 05:53:32 +08:00
|
|
|
// normal number. Renormalize one or both of a and b, and set scale to
|
|
|
|
// include the necessary exponent adjustment.
|
|
|
|
if (aAbs < implicitBit)
|
|
|
|
scale += normalize(&aSignificand);
|
|
|
|
if (bAbs < implicitBit)
|
|
|
|
scale += normalize(&bSignificand);
|
|
|
|
}
|
|
|
|
|
2019-05-10 06:48:30 +08:00
|
|
|
// Set the implicit significand bit. If we fell through from the
|
2019-04-29 05:53:32 +08:00
|
|
|
// denormal path it was already set by normalize( ), but setting it twice
|
2019-05-10 06:48:30 +08:00
|
|
|
// won't hurt anything.
|
2019-04-29 05:53:32 +08:00
|
|
|
aSignificand |= implicitBit;
|
|
|
|
bSignificand |= implicitBit;
|
|
|
|
|
2019-05-10 06:48:30 +08:00
|
|
|
// Perform a basic multiplication on the significands. One of them must be
|
|
|
|
// shifted beforehand to be aligned with the exponent.
|
2019-04-29 05:53:32 +08:00
|
|
|
rep_t productHi, productLo;
|
|
|
|
wideMultiply(aSignificand, bSignificand << exponentBits, &productHi,
|
|
|
|
&productLo);
|
|
|
|
|
|
|
|
int productExponent = aExponent + bExponent - exponentBias + scale;
|
|
|
|
|
2019-05-10 06:48:30 +08:00
|
|
|
// Normalize the significand and adjust the exponent if needed.
|
2019-04-29 05:53:32 +08:00
|
|
|
if (productHi & implicitBit)
|
|
|
|
productExponent++;
|
|
|
|
else
|
|
|
|
wideLeftShift(&productHi, &productLo, 1);
|
|
|
|
|
|
|
|
// If we have overflowed the type, return +/- infinity.
|
|
|
|
if (productExponent >= maxExponent)
|
|
|
|
return fromRep(infRep | productSign);
|
|
|
|
|
|
|
|
if (productExponent <= 0) {
|
2019-05-10 06:48:30 +08:00
|
|
|
// The result is denormal before rounding.
|
2019-04-29 05:53:32 +08:00
|
|
|
//
|
|
|
|
// If the result is so small that it just underflows to zero, return
|
2019-05-10 06:48:30 +08:00
|
|
|
// zero with the appropriate sign. Mathematically, there is no need to
|
2019-04-29 05:53:32 +08:00
|
|
|
// handle this case separately, but we make it a special case to
|
|
|
|
// simplify the shift logic.
|
|
|
|
const unsigned int shift = REP_C(1) - (unsigned int)productExponent;
|
|
|
|
if (shift >= typeWidth)
|
|
|
|
return fromRep(productSign);
|
|
|
|
|
|
|
|
// Otherwise, shift the significand of the result so that the round
|
|
|
|
// bit is the high bit of productLo.
|
|
|
|
wideRightShiftWithSticky(&productHi, &productLo, shift);
|
|
|
|
} else {
|
2019-05-10 06:48:30 +08:00
|
|
|
// The result is normal before rounding. Insert the exponent.
|
2019-04-29 05:53:32 +08:00
|
|
|
productHi &= significandMask;
|
|
|
|
productHi |= (rep_t)productExponent << significandBits;
|
|
|
|
}
|
|
|
|
|
2019-05-10 06:48:30 +08:00
|
|
|
// Insert the sign of the result.
|
2019-04-29 05:53:32 +08:00
|
|
|
productHi |= productSign;
|
|
|
|
|
2019-05-10 06:48:30 +08:00
|
|
|
// Perform the final rounding. The final result may overflow to infinity,
|
|
|
|
// or underflow to zero, but those are the correct results in those cases.
|
|
|
|
// We use the default IEEE-754 round-to-nearest, ties-to-even rounding mode.
|
2019-04-29 05:53:32 +08:00
|
|
|
if (productLo > signBit)
|
|
|
|
productHi++;
|
|
|
|
if (productLo == signBit)
|
|
|
|
productHi += productHi & 1;
|
|
|
|
return fromRep(productHi);
|
2014-05-28 23:08:05 +08:00
|
|
|
}
|