blob: e6d13208429fd53e7fd6f6ab194ede37b164342f [file] [log] [blame]
Stephen Canone5086322010-07-01 15:52:42 +00001/*
2 * The LLVM Compiler Infrastructure
3 *
4 * This file is distributed under the University of Illinois Open Source
5 * License. See LICENSE.TXT for details.
6 */
7
8#define SINGLE_PRECISION
9#include "fp_lib.h"
10
11// This file implements single-precision soft-float addition and subtraction
12// with the IEEE-754 default rounding (to nearest, ties to even).
13
14fp_t __addsf3(fp_t a, fp_t b) {
15
16 rep_t aRep = toRep(a);
17 rep_t bRep = toRep(b);
18 const rep_t aAbs = aRep & absMask;
19 const rep_t bAbs = bRep & absMask;
20
21 // Detect if a or b is zero, infinity, or NaN.
22 if (aAbs - 1U >= infRep - 1U || bAbs - 1U >= infRep - 1U) {
23
24 // NaN + anything = qNaN
25 if (aAbs > infRep) return fromRep(toRep(a) | quietBit);
26 // anything + NaN = qNaN
27 if (bAbs > infRep) return fromRep(toRep(b) | quietBit);
28
29 if (aAbs == infRep) {
30 // +/-infinity + -/+infinity = qNaN
31 if ((toRep(a) ^ toRep(b)) == signBit) return fromRep(qnanRep);
32 // +/-infinity + anything remaining = +/- infinity
33 else return a;
34 }
35
36 // anything remaining + +/-infinity = +/-infinity
37 if (bAbs == infRep) return b;
38
39 // zero + anything = anything
40 if (!aAbs) {
41 // but we need to get the sign right for zero + zero
42 if (!bAbs) return fromRep(toRep(a) & toRep(b));
43 else return b;
44 }
45
46 // anything + zero = anything
47 if (!bAbs) return a;
48 }
49
50 // Swap a and b if necessary so that a has the larger absolute value.
51 if (bAbs > aAbs) {
52 const rep_t temp = aRep;
53 aRep = bRep;
54 bRep = temp;
55 }
56
57 // Extract the exponent and significand from the (possibly swapped) a and b.
58 int aExponent = aRep >> significandBits & maxExponent;
59 int bExponent = bRep >> significandBits & maxExponent;
60 rep_t aSignificand = aRep & significandMask;
61 rep_t bSignificand = bRep & significandMask;
62
63 // Normalize any denormals, and adjust the exponent accordingly.
64 if (aExponent == 0) aExponent = normalize(&aSignificand);
65 if (bExponent == 0) bExponent = normalize(&bSignificand);
66
67 // The sign of the result is the sign of the larger operand, a. If they
68 // have opposite signs, we are performing a subtraction; otherwise addition.
69 const rep_t resultSign = aRep & signBit;
70 const bool subtraction = (aRep ^ bRep) & signBit;
71
72 // Shift the significands to give us round, guard and sticky, and or in the
73 // implicit significand bit. (If we fell through from the denormal path it
74 // was already set by normalize( ), but setting it twice won't hurt
75 // anything.)
76 aSignificand = (aSignificand | implicitBit) << 3;
77 bSignificand = (bSignificand | implicitBit) << 3;
78
79 // Shift the significand of b by the difference in exponents, with a sticky
80 // bottom bit to get rounding correct.
81 const int align = aExponent - bExponent;
82 if (align) {
83 if (align < typeWidth) {
84 const bool sticky = bSignificand << (typeWidth - align);
85 bSignificand = bSignificand >> align | sticky;
86 } else {
87 bSignificand = 1; // sticky; b is known to be non-zero.
88 }
89 }
90
91 if (subtraction) {
92 aSignificand -= bSignificand;
93
94 // If a == -b, return +zero.
95 if (aSignificand == 0) return fromRep(0);
96
97 // If partial cancellation occured, we need to left-shift the result
98 // and adjust the exponent:
99 if (aSignificand < implicitBit << 3) {
100 const int shift = rep_clz(aSignificand) - rep_clz(implicitBit << 3);
101 aSignificand <<= shift;
102 aExponent -= shift;
103 }
104 }
105
106 else /* addition */ {
107 aSignificand += bSignificand;
108
109 // If the addition carried up, we need to right-shift the result and
110 // adjust the exponent:
111 if (aSignificand & implicitBit << 4) {
112 const bool sticky = aSignificand & 1;
113 aSignificand = aSignificand >> 1 | sticky;
114 aExponent += 1;
115 }
116 }
117
118 // If we have overflowed the type, return +/- infinity:
119 if (aExponent >= maxExponent) return fromRep(infRep | resultSign);
120
121 if (aExponent <= 0) {
122 // Result is denormal before rounding; the exponent is zero and we
123 // need to shift the significand.
124 const int shift = 1 - aExponent;
125 const bool sticky = aSignificand << (typeWidth - shift);
126 aSignificand = aSignificand >> shift | sticky;
127 aExponent = 0;
128 }
129
130 // Low three bits are round, guard, and sticky.
131 const int roundGuardSticky = aSignificand & 0x7;
132
133 // Shift the significand into place, and mask off the implicit bit.
134 rep_t result = aSignificand >> 3 & significandMask;
135
136 // Insert the exponent and sign.
137 result |= (rep_t)aExponent << significandBits;
138 result |= resultSign;
139
140 // Final rounding. The result may overflow to infinity, but that is the
141 // correct result in that case.
142 if (roundGuardSticky > 0x4) result++;
143 if (roundGuardSticky == 0x4) result += result & 1;
144 return fromRep(result);
145}
146
147// Subtraction; flip the sign bit of b and add.
148fp_t __subsf3(fp_t a, fp_t b) {
149 return __addsf3(a, fromRep(toRep(b) ^ signBit));
150}
151
152
153
154
155
156
157
158
159
160