Rev 660 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 660 | Rev 661 | ||
---|---|---|---|
Line 109... | Line 109... | ||
109 | }; |
109 | }; |
110 | 110 | ||
111 | //rounding - if first bit after mantisa is set then round up |
111 | //rounding - if first bit after mantisa is set then round up |
112 | mant1+=0x20; |
112 | mant1+=0x20; |
113 | 113 | ||
- | 114 | if (mant1&0x40000000) { |
|
- | 115 | ++exp1; |
|
- | 116 | mant1>>=1; |
|
- | 117 | }; |
|
- | 118 | ||
114 | a.parts.exp=exp1; |
119 | a.parts.exp=exp1; |
115 | a.parts.mantisa=mant1>>6; |
120 | a.parts.mantisa = ((mant1&(~0x20000000))>>6); /*Clear hidden bit and shift */ |
116 | return a; |
121 | return a; |
117 | }; |
122 | }; |
118 | 123 | ||
119 | /** Substract two float32 numbers with same signs |
124 | /** Subtract two float32 numbers with same signs |
120 | */ |
125 | */ |
121 | float32 subFloat32(float32 a, float32 b) |
126 | float32 subFloat32(float32 a, float32 b) |
122 | { |
127 | { |
- | 128 | int expdiff; |
|
- | 129 | __u32 exp1,exp2,mant1,mant2; |
|
- | 130 | float32 result; |
|
- | 131 | ||
- | 132 | result.f = 0; |
|
- | 133 | ||
- | 134 | expdiff=a.parts.exp - b.parts.exp; |
|
- | 135 | if ((expdiff<0)||((expdiff==0)&&(a.parts.mantisa<b.parts.mantisa))) { |
|
- | 136 | if (isFloat32NaN(b)) { |
|
- | 137 | //TODO: fix SigNaN |
|
- | 138 | if (isFloat32SigNaN(b)) { |
|
- | 139 | }; |
|
- | 140 | return b; |
|
- | 141 | }; |
|
- | 142 | ||
- | 143 | if (b.parts.exp==0xFF) { |
|
- | 144 | b.parts.sign=!b.parts.sign; /* num -(+-inf) = -+inf */ |
|
- | 145 | return b; |
|
- | 146 | } |
|
- | 147 | ||
- | 148 | result.parts.sign = !a.parts.sign; |
|
- | 149 | ||
- | 150 | mant1=b.parts.mantisa; |
|
- | 151 | exp1=b.parts.exp; |
|
- | 152 | mant2=a.parts.mantisa; |
|
- | 153 | exp2=a.parts.exp; |
|
- | 154 | expdiff*=-1; |
|
- | 155 | } else { |
|
- | 156 | if (isFloat32NaN(a)) { |
|
- | 157 | //TODO: fix SigNaN |
|
- | 158 | if ((isFloat32SigNaN(a))||(isFloat32SigNaN(b))) { |
|
- | 159 | }; |
|
- | 160 | return a; |
|
- | 161 | }; |
|
- | 162 | ||
- | 163 | if (a.parts.exp==0xFF) { |
|
- | 164 | if (b.parts.exp==0xFF) { |
|
- | 165 | /* inf - inf => nan */ |
|
- | 166 | //TODO: fix exception |
|
- | 167 | result.binary = FLOAT32_NAN; |
|
- | 168 | return result; |
|
- | 169 | }; |
|
- | 170 | return a; |
|
- | 171 | } |
|
- | 172 | ||
- | 173 | result.parts.sign = a.parts.sign; |
|
- | 174 | ||
- | 175 | mant1=a.parts.mantisa; |
|
- | 176 | exp1=a.parts.exp; |
|
- | 177 | mant2=b.parts.mantisa; |
|
- | 178 | exp2=b.parts.exp; |
|
- | 179 | ||
- | 180 | ||
- | 181 | ||
- | 182 | }; |
|
- | 183 | ||
- | 184 | if (exp1==0) { |
|
- | 185 | //both are denormalized |
|
- | 186 | result.parts.mantisa=mant1-mant2; |
|
- | 187 | if (result.parts.mantisa>mant1) { |
|
- | 188 | //TODO: underflow exception |
|
- | 189 | return result; |
|
- | 190 | }; |
|
- | 191 | result.parts.exp=0; |
|
- | 192 | return result; |
|
- | 193 | }; |
|
- | 194 | ||
- | 195 | // create some space for rounding |
|
- | 196 | mant1<<=6; |
|
- | 197 | mant2<<=6; |
|
- | 198 | ||
- | 199 | mant1|=0x20000000; //add hidden bit |
|
- | 200 | ||
- | 201 | ||
- | 202 | if (exp2==0) { |
|
- | 203 | --expdiff; |
|
- | 204 | } else { |
|
- | 205 | mant2|=0x20000000; //hidden bit |
|
- | 206 | }; |
|
- | 207 | ||
- | 208 | if (expdiff>24) { |
|
- | 209 | goto done; |
|
- | 210 | }; |
|
- | 211 | ||
- | 212 | mant1 = mant1-(mant2>>expdiff); |
|
- | 213 | done: |
|
- | 214 | ||
- | 215 | //TODO: find first nonzero digit and shift result and detect possibly underflow |
|
- | 216 | while ((exp1>0)&&(!(mant1&0x20000000))) { |
|
- | 217 | exp1--; |
|
- | 218 | mant1 <<= 1; |
|
- | 219 | if(mant1 == 0) { |
|
- | 220 | /* Realy is it an underflow? ... */ |
|
- | 221 | //TODO: fix underflow |
|
- | 222 | }; |
|
- | 223 | }; |
|
- | 224 | ||
- | 225 | //rounding - if first bit after mantisa is set then round up |
|
- | 226 | mant1 += 0x20; |
|
- | 227 | ||
- | 228 | if (mant1&0x40000000) { |
|
- | 229 | ++exp1; |
|
- | 230 | mant1>>=1; |
|
- | 231 | }; |
|
123 | 232 | ||
- | 233 | result.parts.mantisa = ((mant1&(~0x20000000))>>6); /*Clear hidden bit and shift */ |
|
- | 234 | result.parts.exp = exp1; |
|
124 | 235 | ||
- | 236 | return result; |
|
125 | }; |
237 | }; |
126 | 238 |