1 //===----- lib/fp_add_impl.inc - floaing point addition -----------*- C -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file implements soft-float addition with the IEEE-754 default rounding
10 // (to nearest, ties to even).
12 //===----------------------------------------------------------------------===//
16 static __inline fp_t __addXf3__(fp_t a, fp_t b) {
17 rep_t aRep = toRep(a);
18 rep_t bRep = toRep(b);
19 const rep_t aAbs = aRep & absMask;
20 const rep_t bAbs = bRep & absMask;
22 // Detect if a or b is zero, infinity, or NaN.
23 if (aAbs - REP_C(1) >= infRep - REP_C(1) ||
24 bAbs - REP_C(1) >= infRep - REP_C(1)) {
25 // NaN + anything = qNaN
27 return fromRep(toRep(a) | quietBit);
28 // anything + NaN = qNaN
30 return fromRep(toRep(b) | quietBit);
33 // +/-infinity + -/+infinity = qNaN
34 if ((toRep(a) ^ toRep(b)) == signBit)
35 return fromRep(qnanRep);
36 // +/-infinity + anything remaining = +/- infinity
41 // anything remaining + +/-infinity = +/-infinity
45 // zero + anything = anything
47 // We need to get the sign right for zero + zero.
49 return fromRep(toRep(a) & toRep(b));
54 // 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.
74 aExponent = normalize(&aSignificand);
76 bExponent = normalize(&bSignificand);
78 // The sign of the result is the sign of the larger operand, a. If they
79 // have opposite signs, we are performing a subtraction. Otherwise, we
81 const rep_t resultSign = aRep & signBit;
82 const bool subtraction = (aRep ^ bRep) & signBit;
84 // Shift the significands to give us round, guard and sticky, and set the
85 // implicit significand bit. If we fell through from the denormal path it
86 // was already set by normalize( ), but setting it twice won't hurt
88 aSignificand = (aSignificand | implicitBit) << 3;
89 bSignificand = (bSignificand | implicitBit) << 3;
91 // Shift the significand of b by the difference in exponents, with a sticky
92 // bottom bit to get rounding correct.
93 const unsigned int align = aExponent - bExponent;
95 if (align < typeWidth) {
96 const bool sticky = bSignificand << (typeWidth - align);
97 bSignificand = bSignificand >> align | sticky;
99 bSignificand = 1; // Set the sticky bit. b is known to be non-zero.
103 aSignificand -= bSignificand;
104 // If a == -b, return +zero.
105 if (aSignificand == 0)
108 // If partial cancellation occured, we need to left-shift the result
109 // and adjust the exponent.
110 if (aSignificand < implicitBit << 3) {
111 const int shift = rep_clz(aSignificand) - rep_clz(implicitBit << 3);
112 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)
129 return fromRep(infRep | resultSign);
131 if (aExponent <= 0) {
132 // The result is denormal before rounding. The exponent is zero and we
133 // need to shift the significand.
134 const int shift = 1 - aExponent;
135 const bool sticky = aSignificand << (typeWidth - shift);
136 aSignificand = aSignificand >> shift | sticky;
140 // Low three bits are round, guard, and sticky.
141 const int roundGuardSticky = aSignificand & 0x7;
143 // Shift the significand into place, and mask off the implicit bit.
144 rep_t result = aSignificand >> 3 & significandMask;
146 // Insert the exponent and sign.
147 result |= (rep_t)aExponent << significandBits;
148 result |= resultSign;
150 // Perform the final rounding. The result may overflow to infinity, but
151 // that is the correct result in that case.
152 if (roundGuardSticky > 0x4)
154 if (roundGuardSticky == 0x4)
155 result += result & 1;
156 return fromRep(result);