Rev 731 | Rev 734 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 731 | Rev 732 | ||
---|---|---|---|
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==0xFF) { |
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==0xFF) { |
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 > 24) { |
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 << 6) ) { |
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 << 6)) { |
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 | __u64 exp1,exp2,mant1,mant2; |
135 | __u64 exp1, exp2, mant1, mant2; |
136 | 136 | ||
137 | expdiff=a.parts.exp - b.parts.exp; |
137 | expdiff = a.parts.exp - b.parts.exp; |
138 | if (expdiff<0) { |
138 | if (expdiff < 0) { |
139 | if (isFloat64NaN(b)) { |
139 | if (isFloat64NaN(b)) { |
140 | //TODO: fix SigNaN |
140 | //TODO: fix SigNaN |
141 | if (isFloat64SigNaN(b)) { |
141 | if (isFloat64SigNaN(b)) { |
142 | }; |
142 | }; |
143 | 143 | ||
144 | return b; |
144 | return b; |
145 | }; |
145 | }; |
146 | 146 | ||
147 | /* b is infinity and a not */ |
147 | /* b is infinity and a not */ |
148 | if (b.parts.exp==0x8FF) { |
148 | if (b.parts.exp == FLOAT64_MAX_EXPONENT ) { |
149 | return b; |
149 | return b; |
150 | } |
150 | } |
151 | 151 | ||
152 | mant1=b.parts.mantisa; |
152 | mant1 = b.parts.mantisa; |
153 | exp1=b.parts.exp; |
153 | exp1 = b.parts.exp; |
154 | mant2=a.parts.mantisa; |
154 | mant2 = a.parts.mantisa; |
155 | exp2=a.parts.exp; |
155 | exp2 = a.parts.exp; |
156 | expdiff*=-1; |
156 | expdiff *= -1; |
157 | } else { |
157 | } else { |
158 | if (isFloat64NaN(a)) { |
158 | if (isFloat64NaN(a)) { |
159 | //TODO: fix SigNaN |
159 | //TODO: fix SigNaN |
160 | if ((isFloat64SigNaN(a))||(isFloat64SigNaN(b))) { |
160 | if (isFloat64SigNaN(a) || isFloat64SigNaN(b)) { |
161 | }; |
161 | }; |
162 | return a; |
162 | return a; |
163 | }; |
163 | }; |
164 | 164 | ||
165 | /* a is infinity and b not */ |
165 | /* a is infinity and b not */ |
166 | if (a.parts.exp == 0x8FF) { |
166 | if (a.parts.exp == FLOAT64_MAX_EXPONENT ) { |
167 | return a; |
167 | return a; |
168 | } |
168 | } |
169 | 169 | ||
170 | mant1 = a.parts.mantisa; |
170 | mant1 = a.parts.mantisa; |
171 | exp1 = a.parts.exp; |
171 | exp1 = a.parts.exp; |
172 | mant2 = b.parts.mantisa; |
172 | mant2 = b.parts.mantisa; |
173 | exp2 = b.parts.exp; |
173 | exp2 = b.parts.exp; |
174 | }; |
174 | }; |
175 | 175 | ||
176 | if (exp1 == 0) { |
176 | if (exp1 == 0) { |
177 | /* both are denormalized */ |
177 | /* both are denormalized */ |
178 | mant1 += mant2; |
178 | mant1 += mant2; |
179 | if (mant1 & FLOAT64_HIDDEN_BIT_MASK) { |
179 | if (mant1 & FLOAT64_HIDDEN_BIT_MASK) { |
180 | /* result is not denormalized */ |
180 | /* result is not denormalized */ |
181 | a.parts.exp = 1; |
181 | a.parts.exp = 1; |
182 | }; |
182 | }; |
183 | a.parts.mantisa = mant1; |
183 | a.parts.mantisa = mant1; |
184 | return a; |
184 | return a; |
185 | }; |
185 | }; |
186 | 186 | ||
187 | /* add hidden bit - mant1 is sure not denormalized */ |
187 | /* add hidden bit - mant1 is sure not denormalized */ |
188 | mant1 |= FLOAT64_HIDDEN_BIT_MASK; |
188 | mant1 |= FLOAT64_HIDDEN_BIT_MASK; |
189 | 189 | ||
190 | /* second operand ... */ |
190 | /* second operand ... */ |
191 | if (exp2 == 0) { |
191 | if (exp2 == 0) { |
192 | /* ... is denormalized */ |
192 | /* ... is denormalized */ |
193 | --expdiff; |
193 | --expdiff; |
194 | } else { |
194 | } else { |
195 | /* is not denormalized */ |
195 | /* is not denormalized */ |
196 | mant2 |= FLOAT64_HIDDEN_BIT_MASK; |
196 | mant2 |= FLOAT64_HIDDEN_BIT_MASK; |
197 | }; |
197 | }; |
198 | 198 | ||
199 | /* create some space for rounding */ |
199 | /* create some space for rounding */ |
200 | mant1 <<= 6; |
200 | mant1 <<= 6; |
201 | mant2 <<= 6; |
201 | mant2 <<= 6; |
202 | 202 | ||
203 | if (expdiff > 53) { |
203 | if (expdiff > (FLOAT64_MANTISA_SIZE + 1) ) { |
204 | goto done; |
204 | goto done; |
205 | }; |
205 | }; |
206 | 206 | ||
207 | mant2 >>= expdiff; |
207 | mant2 >>= expdiff; |
208 | mant1 += mant2; |
208 | mant1 += mant2; |
209 | done: |
209 | done: |
210 | if (mant1 & (FLOAT64_HIDDEN_BIT_MASK << 6) ) { |
210 | if (mant1 & (FLOAT64_HIDDEN_BIT_MASK << 7) ) { |
211 | ++exp1; |
211 | ++exp1; |
212 | mant1 >>= 1; |
212 | mant1 >>= 1; |
213 | }; |
213 | }; |
214 | 214 | ||
215 | /* rounding - if first bit after mantisa is set then round up */ |
215 | /* rounding - if first bit after mantisa is set then round up */ |
216 | mant1 += (0x1 << 5); |
216 | mant1 += (0x1 << 5); |
217 | 217 | ||
218 | if (mant1 & (FLOAT64_HIDDEN_BIT_MASK << 6)) { |
218 | if (mant1 & (FLOAT64_HIDDEN_BIT_MASK << 7)) { |
219 | ++exp1; |
219 | ++exp1; |
220 | mant1 >> =1; |
220 | mant1 >>= 1; |
221 | }; |
221 | }; |
222 | 222 | ||
223 | a.parts.exp = exp1; |
223 | a.parts.exp = exp1; |
224 | /*Clear hidden bit and shift */ |
224 | /*Clear hidden bit and shift */ |
225 | a.parts.mantisa = ( (mant1 >>6 ) & (~ (FLOAT64_HIDDEN_BIT_MASK))); |
225 | a.parts.mantisa = ( (mant1 >> 6 ) & (~FLOAT64_HIDDEN_BIT_MASK)); |
226 | return a; |
226 | return a; |
227 | } |
227 | } |
228 | 228 | ||
229 | 229 | ||
230 | 230 |