blob: 85672e584436bf69ad62cd5fec4ed6098e13e0ae [file] [log] [blame]
Stephen Canon5c6d2ec2010-07-01 17:58:24 +00001//===-- lib/muldf3.c - Double-precision multiplication ------------*- C -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
Howard Hinnant9ad441f2010-11-16 22:13:33 +00005// This file is dual licensed under the MIT and the University of Illinois Open
6// Source Licenses. See LICENSE.TXT for details.
Stephen Canon5c6d2ec2010-07-01 17:58:24 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements double-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 DOUBLE_PRECISION
16#include "fp_lib.h"
17
Stephen Canone5086322010-07-01 15:52:42 +000018fp_t __muldf3(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
91 //
92 // If the result is so small that it just underflows to zero, return
93 // a zero of the appropriate sign. Mathematically there is no need to
94 // handle this case separately, but we make it a special case to
95 // simplify the shift logic.
96 const int shift = 1 - productExponent;
97 if (shift >= typeWidth) return fromRep(productSign);
98
99 // Otherwise, shift the significand of the result so that the round
100 // bit is the high bit of productLo.
101 wideRightShiftWithSticky(&productHi, &productLo, shift);
102 }
103
104 else {
105 // Result is normal before rounding; insert the exponent.
106 productHi &= significandMask;
107 productHi |= (rep_t)productExponent << significandBits;
108 }
109
110 // Insert the sign of the result:
111 productHi |= productSign;
112
113 // Final rounding. The final result may overflow to infinity, or underflow
114 // to zero, but those are the correct results in those cases. We use the
115 // default IEEE-754 round-to-nearest, ties-to-even rounding mode.
116 if (productLo > signBit) productHi++;
117 if (productLo == signBit) productHi += productHi & 1;
118 return fromRep(productHi);
119}