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