1222656Sed//===-- lib/adddf3.c - Double-precision addition ------------------*- C -*-===// 2214152Sed// 3214152Sed// The LLVM Compiler Infrastructure 4214152Sed// 5222656Sed// This file is dual licensed under the MIT and the University of Illinois Open 6222656Sed// Source Licenses. See LICENSE.TXT for details. 7214152Sed// 8214152Sed//===----------------------------------------------------------------------===// 9214152Sed// 10222656Sed// This file implements double-precision soft-float addition with the IEEE-754 11222656Sed// default rounding (to nearest, ties to even). 12214152Sed// 13214152Sed//===----------------------------------------------------------------------===// 14214152Sed 15214152Sed#define DOUBLE_PRECISION 16214152Sed#include "fp_lib.h" 17214152Sed 18263560SdimARM_EABI_FNALIAS(dadd, adddf3) 19222656Sed 20222656SedCOMPILER_RT_ABI fp_t 21222656Sed__adddf3(fp_t a, fp_t b) { 22214152Sed 23214152Sed rep_t aRep = toRep(a); 24214152Sed rep_t bRep = toRep(b); 25214152Sed const rep_t aAbs = aRep & absMask; 26214152Sed const rep_t bAbs = bRep & absMask; 27214152Sed 28214152Sed // Detect if a or b is zero, infinity, or NaN. 29214152Sed if (aAbs - 1U >= infRep - 1U || bAbs - 1U >= infRep - 1U) { 30214152Sed 31214152Sed // NaN + anything = qNaN 32214152Sed if (aAbs > infRep) return fromRep(toRep(a) | quietBit); 33214152Sed // anything + NaN = qNaN 34214152Sed if (bAbs > infRep) return fromRep(toRep(b) | quietBit); 35214152Sed 36214152Sed if (aAbs == infRep) { 37214152Sed // +/-infinity + -/+infinity = qNaN 38214152Sed if ((toRep(a) ^ toRep(b)) == signBit) return fromRep(qnanRep); 39214152Sed // +/-infinity + anything remaining = +/- infinity 40214152Sed else return a; 41214152Sed } 42214152Sed 43214152Sed // anything remaining + +/-infinity = +/-infinity 44214152Sed if (bAbs == infRep) return b; 45214152Sed 46214152Sed // zero + anything = anything 47214152Sed if (!aAbs) { 48214152Sed // but we need to get the sign right for zero + zero 49214152Sed if (!bAbs) return fromRep(toRep(a) & toRep(b)); 50214152Sed else return b; 51214152Sed } 52214152Sed 53214152Sed // anything + zero = anything 54214152Sed if (!bAbs) return a; 55214152Sed } 56214152Sed 57214152Sed // Swap a and b if necessary so that a has the larger absolute value. 58214152Sed if (bAbs > aAbs) { 59214152Sed const rep_t temp = aRep; 60214152Sed aRep = bRep; 61214152Sed bRep = temp; 62214152Sed } 63214152Sed 64214152Sed // Extract the exponent and significand from the (possibly swapped) a and b. 65214152Sed int aExponent = aRep >> significandBits & maxExponent; 66214152Sed int bExponent = bRep >> significandBits & maxExponent; 67214152Sed rep_t aSignificand = aRep & significandMask; 68214152Sed rep_t bSignificand = bRep & significandMask; 69214152Sed 70214152Sed // Normalize any denormals, and adjust the exponent accordingly. 71214152Sed if (aExponent == 0) aExponent = normalize(&aSignificand); 72214152Sed if (bExponent == 0) bExponent = normalize(&bSignificand); 73214152Sed 74214152Sed // The sign of the result is the sign of the larger operand, a. If they 75214152Sed // have opposite signs, we are performing a subtraction; otherwise addition. 76214152Sed const rep_t resultSign = aRep & signBit; 77214152Sed const bool subtraction = (aRep ^ bRep) & signBit; 78214152Sed 79214152Sed // Shift the significands to give us round, guard and sticky, and or in the 80214152Sed // implicit significand bit. (If we fell through from the denormal path it 81214152Sed // was already set by normalize( ), but setting it twice won't hurt 82214152Sed // anything.) 83214152Sed aSignificand = (aSignificand | implicitBit) << 3; 84214152Sed bSignificand = (bSignificand | implicitBit) << 3; 85214152Sed 86214152Sed // Shift the significand of b by the difference in exponents, with a sticky 87214152Sed // bottom bit to get rounding correct. 88263560Sdim const unsigned int align = aExponent - bExponent; 89214152Sed if (align) { 90214152Sed if (align < typeWidth) { 91214152Sed const bool sticky = bSignificand << (typeWidth - align); 92214152Sed bSignificand = bSignificand >> align | sticky; 93214152Sed } else { 94214152Sed bSignificand = 1; // sticky; b is known to be non-zero. 95214152Sed } 96214152Sed } 97214152Sed 98214152Sed if (subtraction) { 99214152Sed aSignificand -= bSignificand; 100214152Sed 101214152Sed // If a == -b, return +zero. 102214152Sed if (aSignificand == 0) return fromRep(0); 103214152Sed 104214152Sed // If partial cancellation occured, we need to left-shift the result 105214152Sed // and adjust the exponent: 106214152Sed if (aSignificand < implicitBit << 3) { 107214152Sed const int shift = rep_clz(aSignificand) - rep_clz(implicitBit << 3); 108214152Sed aSignificand <<= shift; 109214152Sed aExponent -= shift; 110214152Sed } 111214152Sed } 112214152Sed 113214152Sed else /* addition */ { 114214152Sed aSignificand += bSignificand; 115214152Sed 116214152Sed // If the addition carried up, we need to right-shift the result and 117214152Sed // adjust the exponent: 118214152Sed if (aSignificand & implicitBit << 4) { 119214152Sed const bool sticky = aSignificand & 1; 120214152Sed aSignificand = aSignificand >> 1 | sticky; 121214152Sed aExponent += 1; 122214152Sed } 123214152Sed } 124214152Sed 125214152Sed // If we have overflowed the type, return +/- infinity: 126214152Sed if (aExponent >= maxExponent) return fromRep(infRep | resultSign); 127214152Sed 128214152Sed if (aExponent <= 0) { 129214152Sed // Result is denormal before rounding; the exponent is zero and we 130214152Sed // need to shift the significand. 131214152Sed const int shift = 1 - aExponent; 132214152Sed const bool sticky = aSignificand << (typeWidth - shift); 133214152Sed aSignificand = aSignificand >> shift | sticky; 134214152Sed aExponent = 0; 135214152Sed } 136214152Sed 137214152Sed // Low three bits are round, guard, and sticky. 138214152Sed const int roundGuardSticky = aSignificand & 0x7; 139214152Sed 140214152Sed // Shift the significand into place, and mask off the implicit bit. 141214152Sed rep_t result = aSignificand >> 3 & significandMask; 142214152Sed 143214152Sed // Insert the exponent and sign. 144214152Sed result |= (rep_t)aExponent << significandBits; 145214152Sed result |= resultSign; 146214152Sed 147214152Sed // Final rounding. The result may overflow to infinity, but that is the 148214152Sed // correct result in that case. 149214152Sed if (roundGuardSticky > 0x4) result++; 150214152Sed if (roundGuardSticky == 0x4) result += result & 1; 151214152Sed return fromRep(result); 152214152Sed} 153