2
* Copyright (c) 2005 Josef Cejka
5
* Redistribution and use in source and binary forms, with or without
6
* modification, are permitted provided that the following conditions
9
* - Redistributions of source code must retain the above copyright
10
* notice, this list of conditions and the following disclaimer.
11
* - Redistributions in binary form must reproduce the above copyright
12
* notice, this list of conditions and the following disclaimer in the
13
* documentation and/or other materials provided with the distribution.
14
* - The name of the author may not be used to endorse or promote products
15
* derived from this software without specific prior written permission.
17
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
/** @addtogroup softfloat
37
#include<comparison.h>
39
/** Add two Float32 numbers with same signs
41
float32 addFloat32(float32 a, float32 b)
44
uint32_t exp1, exp2,frac1, frac2;
46
expdiff = a.parts.exp - b.parts.exp;
48
if (isFloat32NaN(b)) {
49
/* TODO: fix SigNaN */
50
if (isFloat32SigNaN(b)) {
56
if (b.parts.exp == FLOAT32_MAX_EXPONENT) {
60
frac1 = b.parts.fraction;
62
frac2 = a.parts.fraction;
66
if ((isFloat32NaN(a)) || (isFloat32NaN(b))) {
67
/* TODO: fix SigNaN */
68
if (isFloat32SigNaN(a) || isFloat32SigNaN(b)) {
70
return (isFloat32NaN(a)?a:b);
73
if (a.parts.exp == FLOAT32_MAX_EXPONENT) {
77
frac1 = a.parts.fraction;
79
frac2 = b.parts.fraction;
84
/* both are denormalized */
86
if (frac1 & FLOAT32_HIDDEN_BIT_MASK ) {
87
/* result is not denormalized */
90
a.parts.fraction = frac1;
94
frac1 |= FLOAT32_HIDDEN_BIT_MASK; /* add hidden bit */
97
/* second operand is denormalized */
100
/* add hidden bit to second operand */
101
frac2 |= FLOAT32_HIDDEN_BIT_MASK;
104
/* create some space for rounding */
108
if (expdiff < (FLOAT32_FRACTION_SIZE + 2) ) {
113
a.parts.fraction = (frac1 >> 6) & (~(FLOAT32_HIDDEN_BIT_MASK));
117
if (frac1 & (FLOAT32_HIDDEN_BIT_MASK << 7) ) {
122
/* rounding - if first bit after fraction is set then round up */
125
if (frac1 & (FLOAT32_HIDDEN_BIT_MASK << 7)) {
126
/* rounding overflow */
132
if ((exp1 == FLOAT32_MAX_EXPONENT ) || (exp2 > exp1)) {
133
/* overflow - set infinity as result */
134
a.parts.exp = FLOAT32_MAX_EXPONENT;
135
a.parts.fraction = 0;
141
/*Clear hidden bit and shift */
142
a.parts.fraction = ((frac1 >> 6) & (~FLOAT32_HIDDEN_BIT_MASK)) ;
147
/** Add two Float64 numbers with same signs
149
float64 addFloat64(float64 a, float64 b)
153
uint64_t frac1, frac2;
155
expdiff = ((int )a.parts.exp) - b.parts.exp;
157
if (isFloat64NaN(b)) {
158
/* TODO: fix SigNaN */
159
if (isFloat64SigNaN(b)) {
165
/* b is infinity and a not */
166
if (b.parts.exp == FLOAT64_MAX_EXPONENT ) {
170
frac1 = b.parts.fraction;
172
frac2 = a.parts.fraction;
176
if (isFloat64NaN(a)) {
177
/* TODO: fix SigNaN */
178
if (isFloat64SigNaN(a) || isFloat64SigNaN(b)) {
183
/* a is infinity and b not */
184
if (a.parts.exp == FLOAT64_MAX_EXPONENT ) {
188
frac1 = a.parts.fraction;
190
frac2 = b.parts.fraction;
195
/* both are denormalized */
197
if (frac1 & FLOAT64_HIDDEN_BIT_MASK) {
198
/* result is not denormalized */
201
a.parts.fraction = frac1;
205
/* add hidden bit - frac1 is sure not denormalized */
206
frac1 |= FLOAT64_HIDDEN_BIT_MASK;
208
/* second operand ... */
210
/* ... is denormalized */
213
/* is not denormalized */
214
frac2 |= FLOAT64_HIDDEN_BIT_MASK;
217
/* create some space for rounding */
221
if (expdiff < (FLOAT64_FRACTION_SIZE + 2) ) {
226
a.parts.fraction = (frac1 >> 6) & (~(FLOAT64_HIDDEN_BIT_MASK));
230
if (frac1 & (FLOAT64_HIDDEN_BIT_MASK << 7) ) {
235
/* rounding - if first bit after fraction is set then round up */
238
if (frac1 & (FLOAT64_HIDDEN_BIT_MASK << 7)) {
239
/* rounding overflow */
244
if ((exp1 == FLOAT64_MAX_EXPONENT ) || (exp2 > exp1)) {
245
/* overflow - set infinity as result */
246
a.parts.exp = FLOAT64_MAX_EXPONENT;
247
a.parts.fraction = 0;
252
/*Clear hidden bit and shift */
253
a.parts.fraction = ( (frac1 >> 6 ) & (~FLOAT64_HIDDEN_BIT_MASK));