1 //===-- lib/addsf3.c - Single-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 single-precision soft-float addition with the IEEE-754
11 // default rounding (to nearest, ties to even).
13 //===----------------------------------------------------------------------===//
17 #define SINGLE_PRECISION
20 ARM_EABI_FNALIAS(fadd, addsf3);
22 fp_t __addsf3(fp_t a, fp_t b) {
24 rep_t aRep = toRep(a);
25 rep_t bRep = toRep(b);
26 const rep_t aAbs = aRep & absMask;
27 const rep_t bAbs = bRep & absMask;
29 // Detect if a or b is zero, infinity, or NaN.
30 if (aAbs - 1U >= infRep - 1U || bAbs - 1U >= infRep - 1U) {
32 // NaN + anything = qNaN
33 if (aAbs > infRep) return fromRep(toRep(a) | quietBit);
34 // anything + NaN = qNaN
35 if (bAbs > infRep) return fromRep(toRep(b) | quietBit);
38 // +/-infinity + -/+infinity = qNaN
39 if ((toRep(a) ^ toRep(b)) == signBit) return fromRep(qnanRep);
40 // +/-infinity + anything remaining = +/- infinity
44 // anything remaining + +/-infinity = +/-infinity
45 if (bAbs == infRep) return b;
47 // zero + anything = anything
49 // but we need to get the sign right for zero + zero
50 if (!bAbs) return fromRep(toRep(a) & toRep(b));
54 // anything + zero = anything
58 // Swap a and b if necessary so that a has the larger absolute value.
60 const rep_t temp = aRep;
65 // Extract the exponent and significand from the (possibly swapped) a and b.
66 int aExponent = aRep >> significandBits & maxExponent;
67 int bExponent = bRep >> significandBits & maxExponent;
68 rep_t aSignificand = aRep & significandMask;
69 rep_t bSignificand = bRep & significandMask;
71 // Normalize any denormals, and adjust the exponent accordingly.
72 if (aExponent == 0) aExponent = normalize(&aSignificand);
73 if (bExponent == 0) bExponent = normalize(&bSignificand);
75 // The sign of the result is the sign of the larger operand, a. If they
76 // have opposite signs, we are performing a subtraction; otherwise addition.
77 const rep_t resultSign = aRep & signBit;
78 const bool subtraction = (aRep ^ bRep) & signBit;
80 // Shift the significands to give us round, guard and sticky, and or in the
81 // implicit significand bit. (If we fell through from the denormal path it
82 // was already set by normalize( ), but setting it twice won't hurt
84 aSignificand = (aSignificand | implicitBit) << 3;
85 bSignificand = (bSignificand | implicitBit) << 3;
87 // Shift the significand of b by the difference in exponents, with a sticky
88 // bottom bit to get rounding correct.
89 const int align = aExponent - bExponent;
91 if (align < typeWidth) {
92 const bool sticky = bSignificand << (typeWidth - align);
93 bSignificand = bSignificand >> align | sticky;
95 bSignificand = 1; // sticky; b is known to be non-zero.
100 aSignificand -= bSignificand;
102 // If a == -b, return +zero.
103 if (aSignificand == 0) return fromRep(0);
105 // If partial cancellation occured, we need to left-shift the result
106 // and adjust the exponent:
107 if (aSignificand < implicitBit << 3) {
108 const int shift = rep_clz(aSignificand) - rep_clz(implicitBit << 3);
109 aSignificand <<= shift;
114 else /* addition */ {
115 aSignificand += bSignificand;
117 // If the addition carried up, we need to right-shift the result and
118 // adjust the exponent:
119 if (aSignificand & implicitBit << 4) {
120 const bool sticky = aSignificand & 1;
121 aSignificand = aSignificand >> 1 | sticky;
126 // If we have overflowed the type, return +/- infinity:
127 if (aExponent >= maxExponent) return fromRep(infRep | resultSign);
129 if (aExponent <= 0) {
130 // Result is denormal before rounding; the exponent is zero and we
131 // need to shift the significand.
132 const int shift = 1 - aExponent;
133 const bool sticky = aSignificand << (typeWidth - shift);
134 aSignificand = aSignificand >> shift | sticky;
138 // Low three bits are round, guard, and sticky.
139 const int roundGuardSticky = aSignificand & 0x7;
141 // Shift the significand into place, and mask off the implicit bit.
142 rep_t result = aSignificand >> 3 & significandMask;
144 // Insert the exponent and sign.
145 result |= (rep_t)aExponent << significandBits;
146 result |= resultSign;
148 // Final rounding. The result may overflow to infinity, but that is the
149 // correct result in that case.
150 if (roundGuardSticky > 0x4) result++;
151 if (roundGuardSticky == 0x4) result += result & 1;
152 return fromRep(result);