diff options
author | Reid Spencer <rspencer@reidspencer.com> | 2007-02-27 01:28:10 +0000 |
---|---|---|
committer | Reid Spencer <rspencer@reidspencer.com> | 2007-02-27 01:28:10 +0000 |
commit | 974551a7e9307799e0efbce85f6e3f952e6cfd02 (patch) | |
tree | 6c8293ff7140be0da6fa889d2ad1689722671fb4 /llvm/lib/Support/APInt.cpp | |
parent | b31bffed96fa83ed53bae22433f1987b74eb3bb1 (diff) | |
download | bcm5719-llvm-974551a7e9307799e0efbce85f6e3f952e6cfd02.tar.gz bcm5719-llvm-974551a7e9307799e0efbce85f6e3f952e6cfd02.zip |
Simplify and document RoundDoubleToAPInt.
llvm-svn: 34648
Diffstat (limited to 'llvm/lib/Support/APInt.cpp')
-rw-r--r-- | llvm/lib/Support/APInt.cpp | 18 |
1 files changed, 15 insertions, 3 deletions
diff --git a/llvm/lib/Support/APInt.cpp b/llvm/lib/Support/APInt.cpp index 31327cb6c42..965182f5ec4 100644 --- a/llvm/lib/Support/APInt.cpp +++ b/llvm/lib/Support/APInt.cpp @@ -803,15 +803,27 @@ APInt llvm::APIntOps::RoundDoubleToAPInt(double Double) { uint64_t I; } T; T.D = Double; + + // Get the sign bit from the highest order bit bool isNeg = T.I >> 63; + + // Get the 11-bit exponent and adjust for the 1023 bit bias int64_t exp = ((T.I >> 52) & 0x7ff) - 1023; + + // If the exponent is negative, the value is < 0 so just return 0. if (exp < 0) - return APInt(64ull, 0u); - uint64_t mantissa = ((T.I << 12) >> 12) | (1ULL << 52); + return APInt(64u, 0u); + + // Extract the mantissa by clearing the top 12 bits (sign + exponent). + uint64_t mantissa = (T.I & (~0ULL >> 12)) | 1ULL << 52; + + // If the exponent doesn't shift all bits out of the mantissa if (exp < 52) return isNeg ? -APInt(64u, mantissa >> (52 - exp)) : APInt(64u, mantissa >> (52 - exp)); - APInt Tmp(exp + 1, mantissa); + + // Otherwise, we have to shift the mantissa bits up to the right location + APInt Tmp(exp+1, mantissa); Tmp = Tmp.shl(exp - 52); return isNeg ? -Tmp : Tmp; } |