blob: 56a1ee36ab2bf06c6a3b657cfc7a05d300353210 [file] [log] [blame]
Stephen Canon5c6d2ec2010-07-01 17:58:24 +00001//===-- 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//===----------------------------------------------------------------------===//
Stephen Canone5086322010-07-01 15:52:42 +000014
15#define SINGLE_PRECISION
16#include "fp_lib.h"
17
Stephen Canone5086322010-07-01 15:52:42 +000018// 32x32 --> 64 bit multiply
19static inline void wideMultiply(rep_t a, rep_t b, rep_t *hi, rep_t *lo) {
20 const uint64_t product = (uint64_t)a*b;
21 *hi = product >> 32;
22 *lo = product;
23}
24
25fp_t __mulsf3(fp_t a, fp_t b) {
26
27 const unsigned int aExponent = toRep(a) >> significandBits & maxExponent;
28 const unsigned int bExponent = toRep(b) >> significandBits & maxExponent;
29 const rep_t productSign = (toRep(a) ^ toRep(b)) & signBit;
30
31 rep_t aSignificand = toRep(a) & significandMask;
32 rep_t bSignificand = toRep(b) & significandMask;
33 int scale = 0;
34
35 // Detect if a or b is zero, denormal, infinity, or NaN.
36 if (aExponent-1U >= maxExponent-1U || bExponent-1U >= maxExponent-1U) {
37
38 const rep_t aAbs = toRep(a) & absMask;
39 const rep_t bAbs = toRep(b) & absMask;
40
41 // NaN * anything = qNaN
42 if (aAbs > infRep) return fromRep(toRep(a) | quietBit);
43 // anything * NaN = qNaN
44 if (bAbs > infRep) return fromRep(toRep(b) | quietBit);
45
46 if (aAbs == infRep) {
47 // infinity * non-zero = +/- infinity
48 if (bAbs) return fromRep(aAbs | productSign);
49 // infinity * zero = NaN
50 else return fromRep(qnanRep);
51 }
52
53 if (bAbs == infRep) {
54 // non-zero * infinity = +/- infinity
55 if (aAbs) return fromRep(bAbs | productSign);
56 // zero * infinity = NaN
57 else return fromRep(qnanRep);
58 }
59
60 // zero * anything = +/- zero
61 if (!aAbs) return fromRep(productSign);
62 // anything * zero = +/- zero
63 if (!bAbs) return fromRep(productSign);
64
65 // one or both of a or b is denormal, the other (if applicable) is a
66 // normal number. Renormalize one or both of a and b, and set scale to
67 // include the necessary exponent adjustment.
68 if (aAbs < implicitBit) scale += normalize(&aSignificand);
69 if (bAbs < implicitBit) scale += normalize(&bSignificand);
70 }
71
72 // Or in the implicit significand bit. (If we fell through from the
73 // denormal path it was already set by normalize( ), but setting it twice
74 // won't hurt anything.)
75 aSignificand |= implicitBit;
76 bSignificand |= implicitBit;
77
78 // Get the significand of a*b. Before multiplying the significands, shift
79 // one of them left to left-align it in the field. Thus, the product will
80 // have (exponentBits + 2) integral digits, all but two of which must be
81 // zero. Normalizing this result is just a conditional left-shift by one
82 // and bumping the exponent accordingly.
83 rep_t productHi, productLo;
84 wideMultiply(aSignificand, bSignificand << exponentBits,
85 &productHi, &productLo);
86
87 int productExponent = aExponent + bExponent - exponentBias + scale;
88
89 // Normalize the significand, adjust exponent if needed.
90 if (productHi & implicitBit) productExponent++;
91 else wideLeftShift(&productHi, &productLo, 1);
92
93 // If we have overflowed the type, return +/- infinity.
94 if (productExponent >= maxExponent) return fromRep(infRep | productSign);
95
96 if (productExponent <= 0) {
97 // Result is denormal before rounding, the exponent is zero and we
98 // need to shift the significand.
99 wideRightShiftWithSticky(&productHi, &productLo, 1 - productExponent);
100 }
101
102 else {
103 // Result is normal before rounding; insert the exponent.
104 productHi &= significandMask;
105 productHi |= (rep_t)productExponent << significandBits;
106 }
107
108 // Insert the sign of the result:
109 productHi |= productSign;
110
111 // Final rounding. The final result may overflow to infinity, or underflow
112 // to zero, but those are the correct results in those cases.
113 if (productLo > signBit) productHi++;
114 if (productLo == signBit) productHi += productHi & 1;
115 return fromRep(productHi);
116}