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