Subversion Repositories HelenOS-historic

Rev

Rev 1657 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
731 cejka 1
/*
2
 * Copyright (C) 2005 Josef Cejka
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
7
 * are met:
8
 *
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.
16
 *
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.
27
 */
28
 
1740 jermar 29
/** @addtogroup softfloat  
1657 cejka 30
 * @{
31
 */
32
/** @file
33
 */
34
 
731 cejka 35
#include<sftypes.h>
36
#include<add.h>
37
#include<comparison.h>
38
 
39
/** Add two Float32 numbers with same signs
40
 */
41
float32 addFloat32(float32 a, float32 b)
42
{
43
    int expdiff;
1031 cejka 44
    uint32_t exp1, exp2,frac1, frac2;
731 cejka 45
 
732 cejka 46
    expdiff = a.parts.exp - b.parts.exp;
47
    if (expdiff < 0) {
731 cejka 48
        if (isFloat32NaN(b)) {
804 cejka 49
            /* TODO: fix SigNaN */
731 cejka 50
            if (isFloat32SigNaN(b)) {
51
            };
52
 
53
            return b;
54
        };
55
 
732 cejka 56
        if (b.parts.exp == FLOAT32_MAX_EXPONENT) {
731 cejka 57
            return b;
58
        }
59
 
804 cejka 60
        frac1 = b.parts.fraction;
732 cejka 61
        exp1 = b.parts.exp;
804 cejka 62
        frac2 = a.parts.fraction;
732 cejka 63
        exp2 = a.parts.exp;
64
        expdiff *= -1;
731 cejka 65
    } else {
804 cejka 66
        if ((isFloat32NaN(a)) || (isFloat32NaN(b))) {
67
            /* TODO: fix SigNaN */
732 cejka 68
            if (isFloat32SigNaN(a) || isFloat32SigNaN(b)) {
731 cejka 69
            };
804 cejka 70
            return (isFloat32NaN(a)?a:b);
731 cejka 71
        };
72
 
732 cejka 73
        if (a.parts.exp == FLOAT32_MAX_EXPONENT) {
731 cejka 74
            return a;
75
        }
76
 
804 cejka 77
        frac1 = a.parts.fraction;
732 cejka 78
        exp1 = a.parts.exp;
804 cejka 79
        frac2 = b.parts.fraction;
732 cejka 80
        exp2 = b.parts.exp;
731 cejka 81
    };
82
 
83
    if (exp1 == 0) {
84
        /* both are denormalized */
804 cejka 85
        frac1 += frac2;
86
        if (frac1 & FLOAT32_HIDDEN_BIT_MASK ) {
731 cejka 87
            /* result is not denormalized */
88
            a.parts.exp = 1;
89
        };
804 cejka 90
        a.parts.fraction = frac1;
731 cejka 91
        return a;
92
    };
93
 
804 cejka 94
    frac1 |= FLOAT32_HIDDEN_BIT_MASK; /* add hidden bit */
731 cejka 95
 
96
    if (exp2 == 0) {
97
        /* second operand is denormalized */
828 cejka 98
        --expdiff;
731 cejka 99
    } else {
100
        /* add hidden bit to second operand */
804 cejka 101
        frac2 |= FLOAT32_HIDDEN_BIT_MASK;
731 cejka 102
    };
103
 
104
    /* create some space for rounding */
804 cejka 105
    frac1 <<= 6;
106
    frac2 <<= 6;
731 cejka 107
 
804 cejka 108
    if (expdiff < (FLOAT32_FRACTION_SIZE + 2) ) {
109
        frac2 >>= expdiff;
110
        frac1 += frac2;
834 cejka 111
    } else {
112
        a.parts.exp = exp1;
113
        a.parts.fraction = (frac1 >> 6) & (~(FLOAT32_HIDDEN_BIT_MASK));
114
        return a;
115
    }
731 cejka 116
 
804 cejka 117
    if (frac1 & (FLOAT32_HIDDEN_BIT_MASK << 7) ) {
731 cejka 118
        ++exp1;
804 cejka 119
        frac1 >>= 1;
731 cejka 120
    };
121
 
804 cejka 122
    /* rounding - if first bit after fraction is set then round up */
123
    frac1 += (0x1 << 5);
731 cejka 124
 
804 cejka 125
    if (frac1 & (FLOAT32_HIDDEN_BIT_MASK << 7)) {
126
        /* rounding overflow */
731 cejka 127
        ++exp1;
804 cejka 128
        frac1 >>= 1;
731 cejka 129
    };
130
 
828 cejka 131
 
132
    if ((exp1 == FLOAT32_MAX_EXPONENT ) || (exp2 > exp1)) {
804 cejka 133
            /* overflow - set infinity as result */
134
            a.parts.exp = FLOAT32_MAX_EXPONENT;
135
            a.parts.fraction = 0;
136
            return a;
137
            }
138
 
731 cejka 139
    a.parts.exp = exp1;
140
 
141
    /*Clear hidden bit and shift */
804 cejka 142
    a.parts.fraction = ((frac1 >> 6) & (~FLOAT32_HIDDEN_BIT_MASK)) ;
731 cejka 143
    return a;
144
}
145
 
146
 
147
/** Add two Float64 numbers with same signs
148
 */
149
float64 addFloat64(float64 a, float64 b)
150
{
151
    int expdiff;
1031 cejka 152
    uint32_t exp1, exp2;
153
    uint64_t frac1, frac2;
731 cejka 154
 
834 cejka 155
    expdiff = ((int )a.parts.exp) - b.parts.exp;
732 cejka 156
    if (expdiff < 0) {
731 cejka 157
        if (isFloat64NaN(b)) {
804 cejka 158
            /* TODO: fix SigNaN */
731 cejka 159
            if (isFloat64SigNaN(b)) {
160
            };
161
 
162
            return b;
163
        };
164
 
165
        /* b is infinity and a not */  
732 cejka 166
        if (b.parts.exp == FLOAT64_MAX_EXPONENT ) {
731 cejka 167
            return b;
168
        }
169
 
804 cejka 170
        frac1 = b.parts.fraction;
732 cejka 171
        exp1 = b.parts.exp;
804 cejka 172
        frac2 = a.parts.fraction;
732 cejka 173
        exp2 = a.parts.exp;
174
        expdiff *= -1;
731 cejka 175
    } else {
176
        if (isFloat64NaN(a)) {
804 cejka 177
            /* TODO: fix SigNaN */
732 cejka 178
            if (isFloat64SigNaN(a) || isFloat64SigNaN(b)) {
731 cejka 179
            };
180
            return a;
181
        };
182
 
183
        /* a is infinity and b not */
732 cejka 184
        if (a.parts.exp == FLOAT64_MAX_EXPONENT ) {
731 cejka 185
            return a;
186
        }
187
 
804 cejka 188
        frac1 = a.parts.fraction;
731 cejka 189
        exp1 = a.parts.exp;
804 cejka 190
        frac2 = b.parts.fraction;
731 cejka 191
        exp2 = b.parts.exp;
192
    };
193
 
194
    if (exp1 == 0) {
195
        /* both are denormalized */
804 cejka 196
        frac1 += frac2;
197
        if (frac1 & FLOAT64_HIDDEN_BIT_MASK) {
731 cejka 198
            /* result is not denormalized */
199
            a.parts.exp = 1;
200
        };
804 cejka 201
        a.parts.fraction = frac1;
731 cejka 202
        return a;
203
    };
204
 
804 cejka 205
    /* add hidden bit - frac1 is sure not denormalized */
206
    frac1 |= FLOAT64_HIDDEN_BIT_MASK;
731 cejka 207
 
208
    /* second operand ... */
209
    if (exp2 == 0) {
210
        /* ... is denormalized */
211
        --expdiff; 
212
    } else {
213
        /* is not denormalized */
804 cejka 214
        frac2 |= FLOAT64_HIDDEN_BIT_MASK;
731 cejka 215
    };
216
 
217
    /* create some space for rounding */
804 cejka 218
    frac1 <<= 6;
219
    frac2 <<= 6;
731 cejka 220
 
804 cejka 221
    if (expdiff < (FLOAT64_FRACTION_SIZE + 2) ) {
222
        frac2 >>= expdiff;
223
        frac1 += frac2;
834 cejka 224
    } else {
225
        a.parts.exp = exp1;
226
        a.parts.fraction = (frac1 >> 6) & (~(FLOAT64_HIDDEN_BIT_MASK));
227
        return a;
228
    }
731 cejka 229
 
804 cejka 230
    if (frac1 & (FLOAT64_HIDDEN_BIT_MASK << 7) ) {
731 cejka 231
        ++exp1;
804 cejka 232
        frac1 >>= 1;
731 cejka 233
    };
234
 
804 cejka 235
    /* rounding - if first bit after fraction is set then round up */
236
    frac1 += (0x1 << 5);
731 cejka 237
 
804 cejka 238
    if (frac1 & (FLOAT64_HIDDEN_BIT_MASK << 7)) {
239
        /* rounding overflow */
731 cejka 240
        ++exp1;
804 cejka 241
        frac1 >>= 1;
731 cejka 242
    };
243
 
834 cejka 244
    if ((exp1 == FLOAT64_MAX_EXPONENT ) || (exp2 > exp1)) {
804 cejka 245
            /* overflow - set infinity as result */
246
            a.parts.exp = FLOAT64_MAX_EXPONENT;
247
            a.parts.fraction = 0;
248
            return a;
249
            }
250
 
731 cejka 251
    a.parts.exp = exp1;
252
    /*Clear hidden bit and shift */
804 cejka 253
    a.parts.fraction = ( (frac1 >> 6 ) & (~FLOAT64_HIDDEN_BIT_MASK));
834 cejka 254
 
731 cejka 255
    return a;
256
}
257
 
1740 jermar 258
/** @}
1657 cejka 259
 */