15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//===-- lib/mulsf3.c - Single-precision multiplication ------------*- C -*-===//
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//                     The LLVM Compiler Infrastructure
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// This file is dual licensed under the MIT and the University of Illinois Open
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Source Licenses. See LICENSE.TXT for details.
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//===----------------------------------------------------------------------===//
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// This file implements single-precision soft-float multiplication
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// with the IEEE-754 default rounding (to nearest, ties to even).
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//===----------------------------------------------------------------------===//
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define SINGLE_PRECISION
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "fp_lib.h"
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)ARM_EABI_FNALIAS(fmul, mulsf3)
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)COMPILER_RT_ABI fp_t
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)__mulsf3(fp_t a, fp_t b) {
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const unsigned int aExponent = toRep(a) >> significandBits & maxExponent;
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const unsigned int bExponent = toRep(b) >> significandBits & maxExponent;
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const rep_t productSign = (toRep(a) ^ toRep(b)) & signBit;
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    rep_t aSignificand = toRep(a) & significandMask;
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    rep_t bSignificand = toRep(b) & significandMask;
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    int scale = 0;
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Detect if a or b is zero, denormal, infinity, or NaN.
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (aExponent-1U >= maxExponent-1U || bExponent-1U >= maxExponent-1U) {
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        const rep_t aAbs = toRep(a) & absMask;
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        const rep_t bAbs = toRep(b) & absMask;
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        // NaN * anything = qNaN
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        if (aAbs > infRep) return fromRep(toRep(a) | quietBit);
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        // anything * NaN = qNaN
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        if (bAbs > infRep) return fromRep(toRep(b) | quietBit);
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        if (aAbs == infRep) {
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            // infinity * non-zero = +/- infinity
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            if (bAbs) return fromRep(aAbs | productSign);
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            // infinity * zero = NaN
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            else return fromRep(qnanRep);
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        }
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        if (bAbs == infRep) {
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            // non-zero * infinity = +/- infinity
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            if (aAbs) return fromRep(bAbs | productSign);
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            // zero * infinity = NaN
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            else return fromRep(qnanRep);
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        }
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        // zero * anything = +/- zero
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        if (!aAbs) return fromRep(productSign);
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        // anything * zero = +/- zero
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        if (!bAbs) return fromRep(productSign);
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
61        // one or both of a or b is denormal, the other (if applicable) is a
62        // normal number.  Renormalize one or both of a and b, and set scale to
63        // include the necessary exponent adjustment.
64        if (aAbs < implicitBit) scale += normalize(&aSignificand);
65        if (bAbs < implicitBit) scale += normalize(&bSignificand);
66    }
67
68    // Or in the implicit significand bit.  (If we fell through from the
69    // denormal path it was already set by normalize( ), but setting it twice
70    // won't hurt anything.)
71    aSignificand |= implicitBit;
72    bSignificand |= implicitBit;
73
74    // Get the significand of a*b.  Before multiplying the significands, shift
75    // one of them left to left-align it in the field.  Thus, the product will
76    // have (exponentBits + 2) integral digits, all but two of which must be
77    // zero.  Normalizing this result is just a conditional left-shift by one
78    // and bumping the exponent accordingly.
79    rep_t productHi, productLo;
80    wideMultiply(aSignificand, bSignificand << exponentBits,
81                 &productHi, &productLo);
82
83    int productExponent = aExponent + bExponent - exponentBias + scale;
84
85    // Normalize the significand, adjust exponent if needed.
86    if (productHi & implicitBit) productExponent++;
87    else wideLeftShift(&productHi, &productLo, 1);
88
89    // If we have overflowed the type, return +/- infinity.
90    if (productExponent >= maxExponent) return fromRep(infRep | productSign);
91
92    if (productExponent <= 0) {
93        // Result is denormal before rounding, the exponent is zero and we
94        // need to shift the significand.
95        wideRightShiftWithSticky(&productHi, &productLo, 1U - (unsigned)productExponent);
96    }
97
98    else {
99        // Result is normal before rounding; insert the exponent.
100        productHi &= significandMask;
101        productHi |= (rep_t)productExponent << significandBits;
102    }
103
104    // Insert the sign of the result:
105    productHi |= productSign;
106
107    // Final rounding.  The final result may overflow to infinity, or underflow
108    // to zero, but those are the correct results in those cases.
109    if (productLo > signBit) productHi++;
110    if (productLo == signBit) productHi += productHi & 1;
111    return fromRep(productHi);
112}
113