1 //===-- lib/adddf3.c - Double-precision addition ------------------*- 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 double-precision soft-float addition with the IEEE-754
11 // default rounding (to nearest, ties to even).
13 //===----------------------------------------------------------------------===//
17 #define DOUBLE_PRECISION
20 ARM_EABI_FNALIAS(dadd, adddf3);
23 __adddf3(fp_t a, fp_t b) {
25 rep_t aRep = toRep(a);
26 rep_t bRep = toRep(b);
27 const rep_t aAbs = aRep & absMask;
28 const rep_t bAbs = bRep & absMask;
30 // Detect if a or b is zero, infinity, or NaN.
31 if (aAbs - 1U >= infRep - 1U || bAbs - 1U >= infRep - 1U) {
33 // NaN + anything = qNaN
34 if (aAbs > infRep) return fromRep(toRep(a) | quietBit);
35 // anything + NaN = qNaN
36 if (bAbs > infRep) return fromRep(toRep(b) | quietBit);
39 // +/-infinity + -/+infinity = qNaN
40 if ((toRep(a) ^ toRep(b)) == signBit) return fromRep(qnanRep);
41 // +/-infinity + anything remaining = +/- infinity
45 // anything remaining + +/-infinity = +/-infinity
46 if (bAbs == infRep) return b;
48 // zero + anything = anything
50 // but we need to get the sign right for zero + zero
51 if (!bAbs) return fromRep(toRep(a) & toRep(b));
55 // anything + zero = anything
59 // Swap a and b if necessary so that a has the larger absolute value.
61 const rep_t temp = aRep;
66 // Extract the exponent and significand from the (possibly swapped) a and b.
67 int aExponent = aRep >> significandBits & maxExponent;
68 int bExponent = bRep >> significandBits & maxExponent;
69 rep_t aSignificand = aRep & significandMask;
70 rep_t bSignificand = bRep & significandMask;
72 // Normalize any denormals, and adjust the exponent accordingly.
73 if (aExponent == 0) aExponent = normalize(&aSignificand);
74 if (bExponent == 0) bExponent = normalize(&bSignificand);
76 // The sign of the result is the sign of the larger operand, a. If they
77 // have opposite signs, we are performing a subtraction; otherwise addition.
78 const rep_t resultSign = aRep & signBit;
79 const bool subtraction = (aRep ^ bRep) & signBit;
81 // Shift the significands to give us round, guard and sticky, and or in the
82 // implicit significand bit. (If we fell through from the denormal path it
83 // was already set by normalize( ), but setting it twice won't hurt
85 aSignificand = (aSignificand | implicitBit) << 3;
86 bSignificand = (bSignificand | implicitBit) << 3;
88 // Shift the significand of b by the difference in exponents, with a sticky
89 // bottom bit to get rounding correct.
90 const int align = aExponent - bExponent;
92 if (align < typeWidth) {
93 const bool sticky = bSignificand << (typeWidth - align);
94 bSignificand = bSignificand >> align | sticky;
96 bSignificand = 1; // sticky; b is known to be non-zero.
101 aSignificand -= bSignificand;
103 // If a == -b, return +zero.
104 if (aSignificand == 0) return fromRep(0);
106 // If partial cancellation occured, we need to left-shift the result
107 // and adjust the exponent:
108 if (aSignificand < implicitBit << 3) {
109 const int shift = rep_clz(aSignificand) - rep_clz(implicitBit << 3);
110 aSignificand <<= shift;
115 else /* addition */ {
116 aSignificand += bSignificand;
118 // If the addition carried up, we need to right-shift the result and
119 // adjust the exponent:
120 if (aSignificand & implicitBit << 4) {
121 const bool sticky = aSignificand & 1;
122 aSignificand = aSignificand >> 1 | sticky;
127 // If we have overflowed the type, return +/- infinity:
128 if (aExponent >= maxExponent) return fromRep(infRep | resultSign);
130 if (aExponent <= 0) {
131 // Result is denormal before rounding; the exponent is zero and we
132 // need to shift the significand.
133 const int shift = 1 - aExponent;
134 const bool sticky = aSignificand << (typeWidth - shift);
135 aSignificand = aSignificand >> shift | sticky;
139 // Low three bits are round, guard, and sticky.
140 const int roundGuardSticky = aSignificand & 0x7;
142 // Shift the significand into place, and mask off the implicit bit.
143 rep_t result = aSignificand >> 3 & significandMask;
145 // Insert the exponent and sign.
146 result |= (rep_t)aExponent << significandBits;
147 result |= resultSign;
149 // Final rounding. The result may overflow to infinity, but that is the
150 // correct result in that case.
151 if (roundGuardSticky > 0x4) result++;
152 if (roundGuardSticky == 0x4) result += result & 1;
153 return fromRep(result);