conversion logic based on table at http://en.wikipedia.org/wiki/Extended_precision CDH TODO put this somewhere more general
| 406 | // conversion logic based on table at http://en.wikipedia.org/wiki/Extended_precision |
| 407 | //CDH TODO put this somewhere more general |
| 408 | static double ConvertFloat80ToDouble(const byte fp80[10]) |
| 409 | { |
| 410 | uint16 e = *((uint16*)&fp80[8]); |
| 411 | uint64 m = *((uint64*)&fp80[0]); |
| 412 | uint64 bit63 = (uint64)1 << 63; |
| 413 | uint64 bit62 = (uint64)1 << 62; |
| 414 | |
| 415 | bool isNegative = (e & 0x8000) != 0; |
| 416 | double s = isNegative ? -1.0 : 1.0; |
| 417 | e &= 0x7fff; |
| 418 | |
| 419 | if (!e) |
| 420 | { |
| 421 | // the high bit and mantissa content will determine whether it's an actual zero, or a denormal or |
| 422 | // pseudo-denormal number with an effective exponent of -16382. But since that exponent is so far |
| 423 | // below anything we can handle in double-precision (even accounting for denormal bit shifts), we're |
| 424 | // effectively still dealing with zero. |
| 425 | return s * 0.0; |
| 426 | } |
| 427 | else if (e == 0x7fff) |
| 428 | { |
| 429 | if (m & bit63) |
| 430 | { |
| 431 | if (m & bit62) |
| 432 | { |
| 433 | return std::numeric_limits<double>::quiet_NaN(); |
| 434 | } |
| 435 | else |
| 436 | { |
| 437 | if (m == bit63) |
| 438 | return s * std::numeric_limits<double>::infinity(); |
| 439 | else |
| 440 | return std::numeric_limits<double>::signaling_NaN(); |
| 441 | } |
| 442 | } |
| 443 | else |
| 444 | { |
| 445 | return std::numeric_limits<double>::quiet_NaN(); |
| 446 | } |
| 447 | } |
| 448 | else |
| 449 | { |
| 450 | if (!(m & bit63)) |
| 451 | return std::numeric_limits<double>::quiet_NaN(); // unnormal (we don't handle these since 80387 and later treat them as invalid operands anyway) |
| 452 | |
| 453 | // else is a normalized value |
| 454 | } |
| 455 | |
| 456 | int useExponent = (int)e - 16383; |
| 457 | if (useExponent < -1022) |
| 458 | return s * 0.0; // we could technically support e from -1023 to -1074 as denormals, but don't bother with that for now. |
| 459 | else if (useExponent > 1023) |
| 460 | return s * HUGE_VAL; |
| 461 | |
| 462 | useExponent += 1023; |
| 463 | |
| 464 | BF_ASSERT((useExponent > 0) && (useExponent < 0x7ff)); // assume we've filtered for valid exponent range |
| 465 | BF_ASSERT(m & bit63); // assume we've filtered out values that aren't normalized by now |
no outgoing calls
no test coverage detected