blob: d4ee2c4c445335e4f5314c41bfb85b5088245182 [file] [log] [blame]
Daniel Dunbar0868ca62011-03-21 23:30:19 +00001//===-- lib/addsf3.c - Single-precision addition ------------------*- C -*-===//
Stephen Canon5c6d2ec2010-07-01 17:58:24 +00002//
3// The LLVM Compiler Infrastructure
4//
Howard Hinnant9ad441f2010-11-16 22:13:33 +00005// This file is dual licensed under the MIT and the University of Illinois Open
6// Source Licenses. See LICENSE.TXT for details.
Stephen Canon5c6d2ec2010-07-01 17:58:24 +00007//
8//===----------------------------------------------------------------------===//
9//
Daniel Dunbar0868ca62011-03-21 23:30:19 +000010// This file implements single-precision soft-float addition with the IEEE-754
11// default rounding (to nearest, ties to even).
Stephen Canon5c6d2ec2010-07-01 17:58:24 +000012//
13//===----------------------------------------------------------------------===//
Stephen Canone5086322010-07-01 15:52:42 +000014
15#define SINGLE_PRECISION
16#include "fp_lib.h"
17
Anton Korobeynikov37b97d12011-04-19 17:51:24 +000018#include "int_lib.h"
19
20ARM_EABI_FNALIAS(fadd, addsf3);
21
Stephen Canone5086322010-07-01 15:52:42 +000022fp_t __addsf3(fp_t a, fp_t b) {
23
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;
28
29 // Detect if a or b is zero, infinity, or NaN.
30 if (aAbs - 1U >= infRep - 1U || bAbs - 1U >= infRep - 1U) {
31
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);
36
37 if (aAbs == infRep) {
38 // +/-infinity + -/+infinity = qNaN
39 if ((toRep(a) ^ toRep(b)) == signBit) return fromRep(qnanRep);
40 // +/-infinity + anything remaining = +/- infinity
41 else return a;
42 }
43
44 // anything remaining + +/-infinity = +/-infinity
45 if (bAbs == infRep) return b;
46
47 // zero + anything = anything
48 if (!aAbs) {
49 // but we need to get the sign right for zero + zero
50 if (!bAbs) return fromRep(toRep(a) & toRep(b));
51 else return b;
52 }
53
54 // anything + zero = anything
55 if (!bAbs) return a;
56 }
57
58 // Swap a and b if necessary so that a has the larger absolute value.
59 if (bAbs > aAbs) {
60 const rep_t temp = aRep;
61 aRep = bRep;
62 bRep = temp;
63 }
64
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;
70
71 // Normalize any denormals, and adjust the exponent accordingly.
72 if (aExponent == 0) aExponent = normalize(&aSignificand);
73 if (bExponent == 0) bExponent = normalize(&bSignificand);
74
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;
79
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
83 // anything.)
84 aSignificand = (aSignificand | implicitBit) << 3;
85 bSignificand = (bSignificand | implicitBit) << 3;
86
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;
90 if (align) {
91 if (align < typeWidth) {
92 const bool sticky = bSignificand << (typeWidth - align);
93 bSignificand = bSignificand >> align | sticky;
94 } else {
95 bSignificand = 1; // sticky; b is known to be non-zero.
96 }
97 }
98
99 if (subtraction) {
100 aSignificand -= bSignificand;
101
102 // If a == -b, return +zero.
103 if (aSignificand == 0) return fromRep(0);
104
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;
110 aExponent -= shift;
111 }
112 }
113
114 else /* addition */ {
115 aSignificand += bSignificand;
116
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;
122 aExponent += 1;
123 }
124 }
125
126 // If we have overflowed the type, return +/- infinity:
127 if (aExponent >= maxExponent) return fromRep(infRep | resultSign);
128
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;
135 aExponent = 0;
136 }
137
138 // Low three bits are round, guard, and sticky.
139 const int roundGuardSticky = aSignificand & 0x7;
140
141 // Shift the significand into place, and mask off the implicit bit.
142 rep_t result = aSignificand >> 3 & significandMask;
143
144 // Insert the exponent and sign.
145 result |= (rep_t)aExponent << significandBits;
146 result |= resultSign;
147
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);
153}