Changeset c67aff2 in mainline for uspace/lib/softfloat/generic/add.c
- Timestamp:
- 2011-08-06T07:04:50Z (13 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- d3e241a, e0e922d
- Parents:
- 9a6034a
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/softfloat/generic/add.c
r9a6034a rc67aff2 1 1 /* 2 2 * Copyright (c) 2005 Josef Cejka 3 * Copyright (c) 2011 Petr Koupy 3 4 * All rights reserved. 4 5 * … … 30 31 * @{ 31 32 */ 32 /** @file 33 /** @file Addition functions. 33 34 */ 34 35 … … 36 37 #include <add.h> 37 38 #include <comparison.h> 38 39 /** Add two Float32 numbers with same signs 39 #include <common.h> 40 41 /** 42 * Add two single-precision floats with the same signs. 43 * 44 * @param a First input operand. 45 * @param b Second input operand. 46 * @return Result of addition. 40 47 */ 41 48 float32 addFloat32(float32 a, float32 b) 42 49 { 43 50 int expdiff; 44 uint32_t exp1, exp2, frac1, frac2;51 uint32_t exp1, exp2, frac1, frac2; 45 52 46 53 expdiff = a.parts.exp - b.parts.exp; … … 49 56 /* TODO: fix SigNaN */ 50 57 if (isFloat32SigNaN(b)) { 51 } ;52 53 return b; 54 } ;58 } 59 60 return b; 61 } 55 62 56 63 if (b.parts.exp == FLOAT32_MAX_EXPONENT) { … … 67 74 /* TODO: fix SigNaN */ 68 75 if (isFloat32SigNaN(a) || isFloat32SigNaN(b)) { 69 } ;70 return (isFloat32NaN(a) ?a:b);71 } ;76 } 77 return (isFloat32NaN(a) ? a : b); 78 } 72 79 73 80 if (a.parts.exp == FLOAT32_MAX_EXPONENT) { … … 79 86 frac2 = b.parts.fraction; 80 87 exp2 = b.parts.exp; 81 } ;88 } 82 89 83 90 if (exp1 == 0) { … … 87 94 /* result is not denormalized */ 88 95 a.parts.exp = 1; 89 } ;96 } 90 97 a.parts.fraction = frac1; 91 98 return a; 92 } ;99 } 93 100 94 101 frac1 |= FLOAT32_HIDDEN_BIT_MASK; /* add hidden bit */ … … 100 107 /* add hidden bit to second operand */ 101 108 frac2 |= FLOAT32_HIDDEN_BIT_MASK; 102 } ;109 } 103 110 104 111 /* create some space for rounding */ … … 118 125 ++exp1; 119 126 frac1 >>= 1; 120 } ;127 } 121 128 122 129 /* rounding - if first bit after fraction is set then round up */ … … 127 134 ++exp1; 128 135 frac1 >>= 1; 129 }; 130 136 } 131 137 132 138 if ((exp1 == FLOAT32_MAX_EXPONENT ) || (exp2 > exp1)) { 133 134 135 136 137 139 /* overflow - set infinity as result */ 140 a.parts.exp = FLOAT32_MAX_EXPONENT; 141 a.parts.fraction = 0; 142 return a; 143 } 138 144 139 145 a.parts.exp = exp1; 140 146 141 147 /* Clear hidden bit and shift */ 142 a.parts.fraction = ((frac1 >> 6) & (~FLOAT32_HIDDEN_BIT_MASK)) 148 a.parts.fraction = ((frac1 >> 6) & (~FLOAT32_HIDDEN_BIT_MASK)); 143 149 return a; 144 150 } 145 151 146 /** Add two Float64 numbers with same signs 152 /** 153 * Add two double-precision floats with the same signs. 154 * 155 * @param a First input operand. 156 * @param b Second input operand. 157 * @return Result of addition. 147 158 */ 148 159 float64 addFloat64(float64 a, float64 b) … … 152 163 uint64_t frac1, frac2; 153 164 154 expdiff = ((int )a.parts.exp) - b.parts.exp;165 expdiff = ((int) a.parts.exp) - b.parts.exp; 155 166 if (expdiff < 0) { 156 167 if (isFloat64NaN(b)) { 157 168 /* TODO: fix SigNaN */ 158 169 if (isFloat64SigNaN(b)) { 159 } ;160 161 return b; 162 } ;170 } 171 172 return b; 173 } 163 174 164 175 /* b is infinity and a not */ 165 if (b.parts.exp == FLOAT64_MAX_EXPONENT 176 if (b.parts.exp == FLOAT64_MAX_EXPONENT) { 166 177 return b; 167 178 } … … 176 187 /* TODO: fix SigNaN */ 177 188 if (isFloat64SigNaN(a) || isFloat64SigNaN(b)) { 178 } ;189 } 179 190 return a; 180 } ;191 } 181 192 182 193 /* a is infinity and b not */ 183 if (a.parts.exp == FLOAT64_MAX_EXPONENT 194 if (a.parts.exp == FLOAT64_MAX_EXPONENT) { 184 195 return a; 185 196 } … … 189 200 frac2 = b.parts.fraction; 190 201 exp2 = b.parts.exp; 191 } ;202 } 192 203 193 204 if (exp1 == 0) { … … 197 208 /* result is not denormalized */ 198 209 a.parts.exp = 1; 199 } ;210 } 200 211 a.parts.fraction = frac1; 201 212 return a; 202 } ;213 } 203 214 204 215 /* add hidden bit - frac1 is sure not denormalized */ … … 212 223 /* is not denormalized */ 213 224 frac2 |= FLOAT64_HIDDEN_BIT_MASK; 214 } ;225 } 215 226 216 227 /* create some space for rounding */ … … 218 229 frac2 <<= 6; 219 230 220 if (expdiff < (FLOAT64_FRACTION_SIZE + 2) 231 if (expdiff < (FLOAT64_FRACTION_SIZE + 2)) { 221 232 frac2 >>= expdiff; 222 233 frac1 += frac2; … … 227 238 } 228 239 229 if (frac1 & (FLOAT64_HIDDEN_BIT_MASK << 7) 240 if (frac1 & (FLOAT64_HIDDEN_BIT_MASK << 7)) { 230 241 ++exp1; 231 242 frac1 >>= 1; 232 } ;243 } 233 244 234 245 /* rounding - if first bit after fraction is set then round up */ … … 239 250 ++exp1; 240 251 frac1 >>= 1; 241 } ;252 } 242 253 243 254 if ((exp1 == FLOAT64_MAX_EXPONENT ) || (exp2 > exp1)) { 244 245 246 247 248 255 /* overflow - set infinity as result */ 256 a.parts.exp = FLOAT64_MAX_EXPONENT; 257 a.parts.fraction = 0; 258 return a; 259 } 249 260 250 261 a.parts.exp = exp1; 251 262 /* Clear hidden bit and shift */ 252 a.parts.fraction = ( (frac1 >> 6 ) & (~FLOAT64_HIDDEN_BIT_MASK)); 253 263 a.parts.fraction = ((frac1 >> 6 ) & (~FLOAT64_HIDDEN_BIT_MASK)); 254 264 return a; 255 265 } 256 266 267 /** 268 * Add two quadruple-precision floats with the same signs. 269 * 270 * @param a First input operand. 271 * @param b Second input operand. 272 * @return Result of addition. 273 */ 274 float128 addFloat128(float128 a, float128 b) 275 { 276 int expdiff; 277 uint32_t exp1, exp2; 278 uint64_t frac1_hi, frac1_lo, frac2_hi, frac2_lo, tmp_hi, tmp_lo; 279 280 expdiff = ((int) a.parts.exp) - b.parts.exp; 281 if (expdiff < 0) { 282 if (isFloat128NaN(b)) { 283 /* TODO: fix SigNaN */ 284 if (isFloat128SigNaN(b)) { 285 } 286 287 return b; 288 } 289 290 /* b is infinity and a not */ 291 if (b.parts.exp == FLOAT128_MAX_EXPONENT) { 292 return b; 293 } 294 295 frac1_hi = b.parts.frac_hi; 296 frac1_lo = b.parts.frac_lo; 297 exp1 = b.parts.exp; 298 frac2_hi = a.parts.frac_hi; 299 frac2_lo = a.parts.frac_lo; 300 exp2 = a.parts.exp; 301 expdiff *= -1; 302 } else { 303 if (isFloat128NaN(a)) { 304 /* TODO: fix SigNaN */ 305 if (isFloat128SigNaN(a) || isFloat128SigNaN(b)) { 306 } 307 return a; 308 } 309 310 /* a is infinity and b not */ 311 if (a.parts.exp == FLOAT128_MAX_EXPONENT) { 312 return a; 313 } 314 315 frac1_hi = a.parts.frac_hi; 316 frac1_lo = a.parts.frac_lo; 317 exp1 = a.parts.exp; 318 frac2_hi = b.parts.frac_hi; 319 frac2_lo = b.parts.frac_lo; 320 exp2 = b.parts.exp; 321 } 322 323 if (exp1 == 0) { 324 /* both are denormalized */ 325 add128(frac1_hi, frac1_lo, frac2_hi, frac2_lo, &frac1_hi, &frac1_lo); 326 327 and128(frac1_hi, frac1_lo, 328 FLOAT128_HIDDEN_BIT_MASK_HI, FLOAT128_HIDDEN_BIT_MASK_LO, 329 &tmp_hi, &tmp_lo); 330 if (lt128(0x0ll, 0x0ll, tmp_hi, tmp_lo)) { 331 /* result is not denormalized */ 332 a.parts.exp = 1; 333 } 334 335 a.parts.frac_hi = frac1_hi; 336 a.parts.frac_lo = frac1_lo; 337 return a; 338 } 339 340 /* add hidden bit - frac1 is sure not denormalized */ 341 or128(frac1_hi, frac1_lo, 342 FLOAT128_HIDDEN_BIT_MASK_HI, FLOAT128_HIDDEN_BIT_MASK_LO, 343 &frac1_hi, &frac1_lo); 344 345 /* second operand ... */ 346 if (exp2 == 0) { 347 /* ... is denormalized */ 348 --expdiff; 349 } else { 350 /* is not denormalized */ 351 or128(frac2_hi, frac2_lo, 352 FLOAT128_HIDDEN_BIT_MASK_HI, FLOAT128_HIDDEN_BIT_MASK_LO, 353 &frac2_hi, &frac2_lo); 354 } 355 356 /* create some space for rounding */ 357 lshift128(frac1_hi, frac1_lo, 6, &frac1_hi, &frac1_lo); 358 lshift128(frac2_hi, frac2_lo, 6, &frac2_hi, &frac2_lo); 359 360 if (expdiff < (FLOAT128_FRACTION_SIZE + 2)) { 361 rshift128(frac2_hi, frac2_lo, expdiff, &frac2_hi, &frac2_lo); 362 add128(frac1_hi, frac1_lo, frac2_hi, frac2_lo, &frac1_hi, &frac1_lo); 363 } else { 364 a.parts.exp = exp1; 365 366 rshift128(frac1_hi, frac1_lo, 6, &frac1_hi, &frac1_lo); 367 not128(FLOAT128_HIDDEN_BIT_MASK_HI, FLOAT128_HIDDEN_BIT_MASK_LO, 368 &tmp_hi, &tmp_lo); 369 and128(frac1_hi, frac1_lo, tmp_hi, tmp_lo, &tmp_hi, &tmp_lo); 370 371 a.parts.frac_hi = tmp_hi; 372 a.parts.frac_lo = tmp_lo; 373 return a; 374 } 375 376 lshift128(FLOAT128_HIDDEN_BIT_MASK_HI, FLOAT128_HIDDEN_BIT_MASK_LO, 7, 377 &tmp_hi, &tmp_lo); 378 and128(frac1_hi, frac1_lo, tmp_hi, tmp_lo, &tmp_hi, &tmp_lo); 379 if (lt128(0x0ll, 0x0ll, tmp_hi, tmp_lo)) { 380 ++exp1; 381 rshift128(frac1_hi, frac1_lo, 1, &frac1_hi, &frac1_lo); 382 } 383 384 /* rounding - if first bit after fraction is set then round up */ 385 add128(frac1_hi, frac1_lo, 0x0ll, 0x1ll << 5, &frac1_hi, &frac1_lo); 386 387 lshift128(FLOAT128_HIDDEN_BIT_MASK_HI, FLOAT128_HIDDEN_BIT_MASK_LO, 7, 388 &tmp_hi, &tmp_lo); 389 and128(frac1_hi, frac1_lo, tmp_hi, tmp_lo, &tmp_hi, &tmp_lo); 390 if (lt128(0x0ll, 0x0ll, tmp_hi, tmp_lo)) { 391 /* rounding overflow */ 392 ++exp1; 393 rshift128(frac1_hi, frac1_lo, 1, &frac1_hi, &frac1_lo); 394 } 395 396 if ((exp1 == FLOAT128_MAX_EXPONENT ) || (exp2 > exp1)) { 397 /* overflow - set infinity as result */ 398 a.parts.exp = FLOAT64_MAX_EXPONENT; 399 a.parts.frac_hi = 0; 400 a.parts.frac_lo = 0; 401 return a; 402 } 403 404 a.parts.exp = exp1; 405 406 /* Clear hidden bit and shift */ 407 rshift128(frac1_hi, frac1_lo, 6, &frac1_hi, &frac1_lo); 408 not128(FLOAT128_HIDDEN_BIT_MASK_HI, FLOAT128_HIDDEN_BIT_MASK_LO, 409 &tmp_hi, &tmp_lo); 410 and128(frac1_hi, frac1_lo, tmp_hi, tmp_lo, &tmp_hi, &tmp_lo); 411 412 a.parts.frac_hi = tmp_hi; 413 a.parts.frac_lo = tmp_lo; 414 415 return a; 416 } 417 257 418 /** @} 258 419 */
Note:
See TracChangeset
for help on using the changeset viewer.