Rev 732 | Go to most recent revision | Details | 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 | |||
29 | #include<sftypes.h> |
||
30 | #include<add.h> |
||
31 | #include<comparison.h> |
||
32 | |||
33 | /** Add two Float32 numbers with same signs |
||
34 | */ |
||
35 | float32 addFloat32(float32 a, float32 b) |
||
36 | { |
||
37 | int expdiff; |
||
38 | __u32 exp1, exp2,mant1, mant2; |
||
39 | |||
40 | expdiff=a.parts.exp - b.parts.exp; |
||
41 | if (expdiff<0) { |
||
42 | if (isFloat32NaN(b)) { |
||
43 | //TODO: fix SigNaN |
||
44 | if (isFloat32SigNaN(b)) { |
||
45 | }; |
||
46 | |||
47 | return b; |
||
48 | }; |
||
49 | |||
50 | if (b.parts.exp==0xFF) { |
||
51 | return b; |
||
52 | } |
||
53 | |||
54 | mant1=b.parts.mantisa; |
||
55 | exp1=b.parts.exp; |
||
56 | mant2=a.parts.mantisa; |
||
57 | exp2=a.parts.exp; |
||
58 | expdiff*=-1; |
||
59 | } else { |
||
60 | if (isFloat32NaN(a)) { |
||
61 | //TODO: fix SigNaN |
||
62 | if ((isFloat32SigNaN(a))||(isFloat32SigNaN(b))) { |
||
63 | }; |
||
64 | return a; |
||
65 | }; |
||
66 | |||
67 | if (a.parts.exp==0xFF) { |
||
68 | return a; |
||
69 | } |
||
70 | |||
71 | mant1=a.parts.mantisa; |
||
72 | exp1=a.parts.exp; |
||
73 | mant2=b.parts.mantisa; |
||
74 | exp2=b.parts.exp; |
||
75 | }; |
||
76 | |||
77 | if (exp1 == 0) { |
||
78 | /* both are denormalized */ |
||
79 | mant1 += mant2; |
||
80 | if (mant1 & FLOAT32_HIDDEN_BIT_MASK ) { |
||
81 | /* result is not denormalized */ |
||
82 | a.parts.exp = 1; |
||
83 | }; |
||
84 | a.parts.mantisa = mant1; |
||
85 | return a; |
||
86 | }; |
||
87 | |||
88 | mant1 |= FLOAT32_HIDDEN_BIT_MASK; //add hidden bit |
||
89 | |||
90 | if (exp2 == 0) { |
||
91 | /* second operand is denormalized */ |
||
92 | --expdiff; |
||
93 | } else { |
||
94 | /* add hidden bit to second operand */ |
||
95 | mant2 |= FLOAT32_HIDDEN_BIT_MASK; |
||
96 | }; |
||
97 | |||
98 | /* create some space for rounding */ |
||
99 | mant1 <<= 6; |
||
100 | mant2 <<= 6; |
||
101 | |||
102 | if (expdiff > 24) { |
||
103 | goto done; |
||
104 | }; |
||
105 | |||
106 | mant2>>=expdiff; |
||
107 | mant1+=mant2; |
||
108 | done: |
||
109 | if (mant1 & (FLOAT32_HIDDEN_BIT_MASK << 6) ) { |
||
110 | ++exp1; |
||
111 | mant1 >>= 1; |
||
112 | }; |
||
113 | |||
114 | /* rounding - if first bit after mantisa is set then round up */ |
||
115 | mant1 += (0x1 << 5); |
||
116 | |||
117 | if (mant1 & (FLOAT32_HIDDEN_BIT_MASK << 6)) { |
||
118 | ++exp1; |
||
119 | mant1 >> =1; |
||
120 | }; |
||
121 | |||
122 | a.parts.exp = exp1; |
||
123 | |||
124 | /*Clear hidden bit and shift */ |
||
125 | a.parts.mantisa = ((mant1>>6) & (~FLOAT32_HIDDEN_BIT_MASK)) ; |
||
126 | return a; |
||
127 | } |
||
128 | |||
129 | |||
130 | /** Add two Float64 numbers with same signs |
||
131 | */ |
||
132 | float64 addFloat64(float64 a, float64 b) |
||
133 | { |
||
134 | int expdiff; |
||
135 | __u64 exp1,exp2,mant1,mant2; |
||
136 | |||
137 | expdiff=a.parts.exp - b.parts.exp; |
||
138 | if (expdiff<0) { |
||
139 | if (isFloat64NaN(b)) { |
||
140 | //TODO: fix SigNaN |
||
141 | if (isFloat64SigNaN(b)) { |
||
142 | }; |
||
143 | |||
144 | return b; |
||
145 | }; |
||
146 | |||
147 | /* b is infinity and a not */ |
||
148 | if (b.parts.exp==0x8FF) { |
||
149 | return b; |
||
150 | } |
||
151 | |||
152 | mant1=b.parts.mantisa; |
||
153 | exp1=b.parts.exp; |
||
154 | mant2=a.parts.mantisa; |
||
155 | exp2=a.parts.exp; |
||
156 | expdiff*=-1; |
||
157 | } else { |
||
158 | if (isFloat64NaN(a)) { |
||
159 | //TODO: fix SigNaN |
||
160 | if ((isFloat64SigNaN(a))||(isFloat64SigNaN(b))) { |
||
161 | }; |
||
162 | return a; |
||
163 | }; |
||
164 | |||
165 | /* a is infinity and b not */ |
||
166 | if (a.parts.exp == 0x8FF) { |
||
167 | return a; |
||
168 | } |
||
169 | |||
170 | mant1 = a.parts.mantisa; |
||
171 | exp1 = a.parts.exp; |
||
172 | mant2 = b.parts.mantisa; |
||
173 | exp2 = b.parts.exp; |
||
174 | }; |
||
175 | |||
176 | if (exp1 == 0) { |
||
177 | /* both are denormalized */ |
||
178 | mant1 += mant2; |
||
179 | if (mant1 & FLOAT64_HIDDEN_BIT_MASK) { |
||
180 | /* result is not denormalized */ |
||
181 | a.parts.exp = 1; |
||
182 | }; |
||
183 | a.parts.mantisa = mant1; |
||
184 | return a; |
||
185 | }; |
||
186 | |||
187 | /* add hidden bit - mant1 is sure not denormalized */ |
||
188 | mant1 |= FLOAT64_HIDDEN_BIT_MASK; |
||
189 | |||
190 | /* second operand ... */ |
||
191 | if (exp2 == 0) { |
||
192 | /* ... is denormalized */ |
||
193 | --expdiff; |
||
194 | } else { |
||
195 | /* is not denormalized */ |
||
196 | mant2 |= FLOAT64_HIDDEN_BIT_MASK; |
||
197 | }; |
||
198 | |||
199 | /* create some space for rounding */ |
||
200 | mant1 <<= 6; |
||
201 | mant2 <<= 6; |
||
202 | |||
203 | if (expdiff > 53) { |
||
204 | goto done; |
||
205 | }; |
||
206 | |||
207 | mant2 >>= expdiff; |
||
208 | mant1 += mant2; |
||
209 | done: |
||
210 | if (mant1 & (FLOAT64_HIDDEN_BIT_MASK << 6) ) { |
||
211 | ++exp1; |
||
212 | mant1 >>= 1; |
||
213 | }; |
||
214 | |||
215 | /* rounding - if first bit after mantisa is set then round up */ |
||
216 | mant1 += (0x1 << 5); |
||
217 | |||
218 | if (mant1 & (FLOAT64_HIDDEN_BIT_MASK << 6)) { |
||
219 | ++exp1; |
||
220 | mant1 >> =1; |
||
221 | }; |
||
222 | |||
223 | a.parts.exp = exp1; |
||
224 | /*Clear hidden bit and shift */ |
||
225 | a.parts.mantisa = ( (mant1 >>6 ) & (~ (FLOAT64_HIDDEN_BIT_MASK))); |
||
226 | return a; |
||
227 | } |
||
228 | |||
229 |