1 //===-- lib/mulsf3.c - Single-precision multiplication ------------*- C -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements single-precision soft-float multiplication
11 // with the IEEE-754 default rounding (to nearest, ties to even).
13 //===----------------------------------------------------------------------===//
16 #define SINGLE_PRECISION
19 ARM_EABI_FNALIAS(fmul, mulsf3);
22 __mulsf3(fp_t a, fp_t b) {
24 const unsigned int aExponent = toRep(a) >> significandBits & maxExponent;
25 const unsigned int bExponent = toRep(b) >> significandBits & maxExponent;
26 const rep_t productSign = (toRep(a) ^ toRep(b)) & signBit;
28 rep_t aSignificand = toRep(a) & significandMask;
29 rep_t bSignificand = toRep(b) & significandMask;
32 // Detect if a or b is zero, denormal, infinity, or NaN.
33 if (aExponent-1U >= maxExponent-1U || bExponent-1U >= maxExponent-1U) {
35 const rep_t aAbs = toRep(a) & absMask;
36 const rep_t bAbs = toRep(b) & absMask;
38 // NaN * anything = qNaN
39 if (aAbs > infRep) return fromRep(toRep(a) | quietBit);
40 // anything * NaN = qNaN
41 if (bAbs > infRep) return fromRep(toRep(b) | quietBit);
44 // infinity * non-zero = +/- infinity
45 if (bAbs) return fromRep(aAbs | productSign);
46 // infinity * zero = NaN
47 else return fromRep(qnanRep);
51 // non-zero * infinity = +/- infinity
52 if (aAbs) return fromRep(bAbs | productSign);
53 // zero * infinity = NaN
54 else return fromRep(qnanRep);
57 // zero * anything = +/- zero
58 if (!aAbs) return fromRep(productSign);
59 // anything * zero = +/- zero
60 if (!bAbs) return fromRep(productSign);
62 // one or both of a or b is denormal, the other (if applicable) is a
63 // normal number. Renormalize one or both of a and b, and set scale to
64 // include the necessary exponent adjustment.
65 if (aAbs < implicitBit) scale += normalize(&aSignificand);
66 if (bAbs < implicitBit) scale += normalize(&bSignificand);
69 // Or in the implicit significand bit. (If we fell through from the
70 // denormal path it was already set by normalize( ), but setting it twice
71 // won't hurt anything.)
72 aSignificand |= implicitBit;
73 bSignificand |= implicitBit;
75 // Get the significand of a*b. Before multiplying the significands, shift
76 // one of them left to left-align it in the field. Thus, the product will
77 // have (exponentBits + 2) integral digits, all but two of which must be
78 // zero. Normalizing this result is just a conditional left-shift by one
79 // and bumping the exponent accordingly.
80 rep_t productHi, productLo;
81 wideMultiply(aSignificand, bSignificand << exponentBits,
82 &productHi, &productLo);
84 int productExponent = aExponent + bExponent - exponentBias + scale;
86 // Normalize the significand, adjust exponent if needed.
87 if (productHi & implicitBit) productExponent++;
88 else wideLeftShift(&productHi, &productLo, 1);
90 // If we have overflowed the type, return +/- infinity.
91 if (productExponent >= maxExponent) return fromRep(infRep | productSign);
93 if (productExponent <= 0) {
94 // Result is denormal before rounding, the exponent is zero and we
95 // need to shift the significand.
96 wideRightShiftWithSticky(&productHi, &productLo, 1 - productExponent);
100 // Result is normal before rounding; insert the exponent.
101 productHi &= significandMask;
102 productHi |= (rep_t)productExponent << significandBits;
105 // Insert the sign of the result:
106 productHi |= productSign;
108 // Final rounding. The final result may overflow to infinity, or underflow
109 // to zero, but those are the correct results in those cases.
110 if (productLo > signBit) productHi++;
111 if (productLo == signBit) productHi += productHi & 1;
112 return fromRep(productHi);