Subversion Repositories HelenOS-historic

Rev

Rev 732 | Rev 804 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 732 Rev 734
1
/*
1
/*
2
 * Copyright (C) 2005 Josef Cejka
2
 * Copyright (C) 2005 Josef Cejka
3
 * All rights reserved.
3
 * All rights reserved.
4
 *
4
 *
5
 * Redistribution and use in source and binary forms, with or without
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
6
 * modification, are permitted provided that the following conditions
7
 * are met:
7
 * are met:
8
 *
8
 *
9
 * - Redistributions of source code must retain the above copyright
9
 * - Redistributions of source code must retain the above copyright
10
 *   notice, this list of conditions and the following disclaimer.
10
 *   notice, this list of conditions and the following disclaimer.
11
 * - Redistributions in binary form must reproduce the above copyright
11
 * - Redistributions in binary form must reproduce the above copyright
12
 *   notice, this list of conditions and the following disclaimer in the
12
 *   notice, this list of conditions and the following disclaimer in the
13
 *   documentation and/or other materials provided with the distribution.
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
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.
15
 *   derived from this software without specific prior written permission.
16
 *
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
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
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
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
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
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
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.
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 */
27
 */
28
 
28
 
29
#include<sftypes.h>
29
#include<sftypes.h>
30
#include<add.h>
30
#include<add.h>
31
#include<comparison.h>
31
#include<comparison.h>
32
 
32
 
33
/** Add two Float32 numbers with same signs
33
/** Add two Float32 numbers with same signs
34
 */
34
 */
35
float32 addFloat32(float32 a, float32 b)
35
float32 addFloat32(float32 a, float32 b)
36
{
36
{
37
    int expdiff;
37
    int expdiff;
38
    __u32 exp1, exp2,mant1, mant2;
38
    __u32 exp1, exp2,mant1, mant2;
39
   
39
   
40
    expdiff = a.parts.exp - b.parts.exp;
40
    expdiff = a.parts.exp - b.parts.exp;
41
    if (expdiff < 0) {
41
    if (expdiff < 0) {
42
        if (isFloat32NaN(b)) {
42
        if (isFloat32NaN(b)) {
43
            //TODO: fix SigNaN
43
            //TODO: fix SigNaN
44
            if (isFloat32SigNaN(b)) {
44
            if (isFloat32SigNaN(b)) {
45
            };
45
            };
46
 
46
 
47
            return b;
47
            return b;
48
        };
48
        };
49
       
49
       
50
        if (b.parts.exp == FLOAT32_MAX_EXPONENT) {
50
        if (b.parts.exp == FLOAT32_MAX_EXPONENT) {
51
            return b;
51
            return b;
52
        }
52
        }
53
       
53
       
54
        mant1 = b.parts.mantisa;
54
        mant1 = b.parts.mantisa;
55
        exp1 = b.parts.exp;
55
        exp1 = b.parts.exp;
56
        mant2 = a.parts.mantisa;
56
        mant2 = a.parts.mantisa;
57
        exp2 = a.parts.exp;
57
        exp2 = a.parts.exp;
58
        expdiff *= -1;
58
        expdiff *= -1;
59
    } else {
59
    } else {
60
        if (isFloat32NaN(a)) {
60
        if (isFloat32NaN(a)) {
61
            //TODO: fix SigNaN
61
            //TODO: fix SigNaN
62
            if (isFloat32SigNaN(a) || isFloat32SigNaN(b)) {
62
            if (isFloat32SigNaN(a) || isFloat32SigNaN(b)) {
63
            };
63
            };
64
            return a;
64
            return a;
65
        };
65
        };
66
       
66
       
67
        if (a.parts.exp == FLOAT32_MAX_EXPONENT) {
67
        if (a.parts.exp == FLOAT32_MAX_EXPONENT) {
68
            return a;
68
            return a;
69
        }
69
        }
70
       
70
       
71
        mant1 = a.parts.mantisa;
71
        mant1 = a.parts.mantisa;
72
        exp1 = a.parts.exp;
72
        exp1 = a.parts.exp;
73
        mant2 = b.parts.mantisa;
73
        mant2 = b.parts.mantisa;
74
        exp2 = b.parts.exp;
74
        exp2 = b.parts.exp;
75
    };
75
    };
76
   
76
   
77
    if (exp1 == 0) {
77
    if (exp1 == 0) {
78
        /* both are denormalized */
78
        /* both are denormalized */
79
        mant1 += mant2;
79
        mant1 += mant2;
80
        if (mant1 & FLOAT32_HIDDEN_BIT_MASK ) {
80
        if (mant1 & FLOAT32_HIDDEN_BIT_MASK ) {
81
            /* result is not denormalized */
81
            /* result is not denormalized */
82
            a.parts.exp = 1;
82
            a.parts.exp = 1;
83
        };
83
        };
84
        a.parts.mantisa = mant1;
84
        a.parts.mantisa = mant1;
85
        return a;
85
        return a;
86
    };
86
    };
87
   
87
   
88
    mant1 |= FLOAT32_HIDDEN_BIT_MASK; //add hidden bit
88
    mant1 |= FLOAT32_HIDDEN_BIT_MASK; //add hidden bit
89
 
89
 
90
    if (exp2 == 0) {
90
    if (exp2 == 0) {
91
        /* second operand is denormalized */
91
        /* second operand is denormalized */
92
        --expdiff; 
92
        --expdiff; 
93
    } else {
93
    } else {
94
        /* add hidden bit to second operand */
94
        /* add hidden bit to second operand */
95
        mant2 |= FLOAT32_HIDDEN_BIT_MASK;
95
        mant2 |= FLOAT32_HIDDEN_BIT_MASK;
96
    };
96
    };
97
   
97
   
98
    /* create some space for rounding */
98
    /* create some space for rounding */
99
    mant1 <<= 6;
99
    mant1 <<= 6;
100
    mant2 <<= 6;
100
    mant2 <<= 6;
101
   
101
   
102
    if (expdiff > (FLOAT32_MANTISA_SIZE + 1) ) {
102
    if (expdiff > (FLOAT32_MANTISA_SIZE + 1) ) {
103
         goto done;
103
         goto done;
104
         };
104
         };
105
   
105
   
106
    mant2 >>= expdiff;
106
    mant2 >>= expdiff;
107
    mant1 += mant2;
107
    mant1 += mant2;
108
done:
108
done:
109
    if (mant1 & (FLOAT32_HIDDEN_BIT_MASK << 7) ) {
109
    if (mant1 & (FLOAT32_HIDDEN_BIT_MASK << 7) ) {
110
        ++exp1;
110
        ++exp1;
111
        mant1 >>= 1;
111
        mant1 >>= 1;
112
    };
112
    };
113
   
113
   
114
    /* rounding - if first bit after mantisa is set then round up */
114
    /* rounding - if first bit after mantisa is set then round up */
115
    mant1 += (0x1 << 5);
115
    mant1 += (0x1 << 5);
116
   
116
   
117
    if (mant1 & (FLOAT32_HIDDEN_BIT_MASK << 7)) {
117
    if (mant1 & (FLOAT32_HIDDEN_BIT_MASK << 7)) {
118
        ++exp1;
118
        ++exp1;
119
        mant1 >>= 1;
119
        mant1 >>= 1;
120
    };
120
    };
121
   
121
   
122
    a.parts.exp = exp1;
122
    a.parts.exp = exp1;
123
   
123
   
124
    /*Clear hidden bit and shift */
124
    /*Clear hidden bit and shift */
125
    a.parts.mantisa = ((mant1 >> 6) & (~FLOAT32_HIDDEN_BIT_MASK)) ;
125
    a.parts.mantisa = ((mant1 >> 6) & (~FLOAT32_HIDDEN_BIT_MASK)) ;
126
    return a;
126
    return a;
127
}
127
}
128
 
128
 
129
 
129
 
130
/** Add two Float64 numbers with same signs
130
/** Add two Float64 numbers with same signs
131
 */
131
 */
132
float64 addFloat64(float64 a, float64 b)
132
float64 addFloat64(float64 a, float64 b)
133
{
133
{
134
    int expdiff;
134
    int expdiff;
-
 
135
    __u32 exp1, exp2;
135
    __u64 exp1, exp2, mant1, mant2;
136
    __u64 mant1, mant2;
136
   
137
   
137
    expdiff = a.parts.exp - b.parts.exp;
138
    expdiff = a.parts.exp - b.parts.exp;
138
    if (expdiff < 0) {
139
    if (expdiff < 0) {
139
        if (isFloat64NaN(b)) {
140
        if (isFloat64NaN(b)) {
140
            //TODO: fix SigNaN
141
            //TODO: fix SigNaN
141
            if (isFloat64SigNaN(b)) {
142
            if (isFloat64SigNaN(b)) {
142
            };
143
            };
143
 
144
 
144
            return b;
145
            return b;
145
        };
146
        };
146
       
147
       
147
        /* b is infinity and a not */  
148
        /* b is infinity and a not */  
148
        if (b.parts.exp == FLOAT64_MAX_EXPONENT ) {
149
        if (b.parts.exp == FLOAT64_MAX_EXPONENT ) {
149
            return b;
150
            return b;
150
        }
151
        }
151
       
152
       
152
        mant1 = b.parts.mantisa;
153
        mant1 = b.parts.mantisa;
153
        exp1 = b.parts.exp;
154
        exp1 = b.parts.exp;
154
        mant2 = a.parts.mantisa;
155
        mant2 = a.parts.mantisa;
155
        exp2 = a.parts.exp;
156
        exp2 = a.parts.exp;
156
        expdiff *= -1;
157
        expdiff *= -1;
157
    } else {
158
    } else {
158
        if (isFloat64NaN(a)) {
159
        if (isFloat64NaN(a)) {
159
            //TODO: fix SigNaN
160
            //TODO: fix SigNaN
160
            if (isFloat64SigNaN(a) || isFloat64SigNaN(b)) {
161
            if (isFloat64SigNaN(a) || isFloat64SigNaN(b)) {
161
            };
162
            };
162
            return a;
163
            return a;
163
        };
164
        };
164
       
165
       
165
        /* a is infinity and b not */
166
        /* a is infinity and b not */
166
        if (a.parts.exp == FLOAT64_MAX_EXPONENT ) {
167
        if (a.parts.exp == FLOAT64_MAX_EXPONENT ) {
167
            return a;
168
            return a;
168
        }
169
        }
169
       
170
       
170
        mant1 = a.parts.mantisa;
171
        mant1 = a.parts.mantisa;
171
        exp1 = a.parts.exp;
172
        exp1 = a.parts.exp;
172
        mant2 = b.parts.mantisa;
173
        mant2 = b.parts.mantisa;
173
        exp2 = b.parts.exp;
174
        exp2 = b.parts.exp;
174
    };
175
    };
175
   
176
   
176
    if (exp1 == 0) {
177
    if (exp1 == 0) {
177
        /* both are denormalized */
178
        /* both are denormalized */
178
        mant1 += mant2;
179
        mant1 += mant2;
179
        if (mant1 & FLOAT64_HIDDEN_BIT_MASK) {
180
        if (mant1 & FLOAT64_HIDDEN_BIT_MASK) {
180
            /* result is not denormalized */
181
            /* result is not denormalized */
181
            a.parts.exp = 1;
182
            a.parts.exp = 1;
182
        };
183
        };
183
        a.parts.mantisa = mant1;
184
        a.parts.mantisa = mant1;
184
        return a;
185
        return a;
185
    };
186
    };
186
   
187
   
187
    /* add hidden bit - mant1 is sure not denormalized */
188
    /* add hidden bit - mant1 is sure not denormalized */
188
    mant1 |= FLOAT64_HIDDEN_BIT_MASK;
189
    mant1 |= FLOAT64_HIDDEN_BIT_MASK;
189
 
190
 
190
    /* second operand ... */
191
    /* second operand ... */
191
    if (exp2 == 0) {
192
    if (exp2 == 0) {
192
        /* ... is denormalized */
193
        /* ... is denormalized */
193
        --expdiff; 
194
        --expdiff; 
194
    } else {
195
    } else {
195
        /* is not denormalized */
196
        /* is not denormalized */
196
        mant2 |= FLOAT64_HIDDEN_BIT_MASK;
197
        mant2 |= FLOAT64_HIDDEN_BIT_MASK;
197
    };
198
    };
198
   
199
   
199
    /* create some space for rounding */
200
    /* create some space for rounding */
200
    mant1 <<= 6;
201
    mant1 <<= 6;
201
    mant2 <<= 6;
202
    mant2 <<= 6;
202
   
203
   
203
    if (expdiff > (FLOAT64_MANTISA_SIZE + 1) ) {
204
    if (expdiff > (FLOAT64_MANTISA_SIZE + 1) ) {
204
         goto done;
205
         goto done;
205
         };
206
         };
206
   
207
   
207
    mant2 >>= expdiff;
208
    mant2 >>= expdiff;
208
    mant1 += mant2;
209
    mant1 += mant2;
209
done:
210
done:
210
    if (mant1 & (FLOAT64_HIDDEN_BIT_MASK << 7) ) {
211
    if (mant1 & (FLOAT64_HIDDEN_BIT_MASK << 7) ) {
211
        ++exp1;
212
        ++exp1;
212
        mant1 >>= 1;
213
        mant1 >>= 1;
213
    };
214
    };
214
   
215
   
215
    /* rounding - if first bit after mantisa is set then round up */
216
    /* rounding - if first bit after mantisa is set then round up */
216
    mant1 += (0x1 << 5);
217
    mant1 += (0x1 << 5);
217
   
218
   
218
    if (mant1 & (FLOAT64_HIDDEN_BIT_MASK << 7)) {
219
    if (mant1 & (FLOAT64_HIDDEN_BIT_MASK << 7)) {
219
        ++exp1;
220
        ++exp1;
220
        mant1 >>= 1;
221
        mant1 >>= 1;
221
    };
222
    };
222
   
223
   
223
    a.parts.exp = exp1;
224
    a.parts.exp = exp1;
224
    /*Clear hidden bit and shift */
225
    /*Clear hidden bit and shift */
225
    a.parts.mantisa = ( (mant1 >> 6 ) & (~FLOAT64_HIDDEN_BIT_MASK));
226
    a.parts.mantisa = ( (mant1 >> 6 ) & (~FLOAT64_HIDDEN_BIT_MASK));
226
    return a;
227
    return a;
227
}
228
}
228
 
229
 
229
 
230
 
230
 
231