diff options
author | Daniel Dunbar <daniel@zuster.org> | 2008-10-16 07:32:01 +0000 |
---|---|---|
committer | Daniel Dunbar <daniel@zuster.org> | 2008-10-16 07:32:01 +0000 |
commit | be947083d294c5c160c99ea804772a663c75465f (patch) | |
tree | 8d731e9a6615019549ae72b4a55bb728a90f41fd | |
parent | 305fb0a7bacc6e939be4a8e9ba043424e206ead0 (diff) | |
download | bcm5719-llvm-be947083d294c5c160c99ea804772a663c75465f.tar.gz bcm5719-llvm-be947083d294c5c160c99ea804772a663c75465f.zip |
Speed up NumericLiteralParser::GetIntegerValue.
- Implement fast path when value easily fits in a uint64.
- ~6x faster, translates to 1-2% on Cocoa.h
llvm-svn: 57632
-rw-r--r-- | clang/lib/Lex/LiteralSupport.cpp | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/clang/lib/Lex/LiteralSupport.cpp b/clang/lib/Lex/LiteralSupport.cpp index 1b86ba5def8..34b59255ac7 100644 --- a/clang/lib/Lex/LiteralSupport.cpp +++ b/clang/lib/Lex/LiteralSupport.cpp @@ -457,6 +457,26 @@ void NumericLiteralParser::ParseNumberStartingWithZero(SourceLocation TokLoc) { /// matches Val's input width. If there is an overflow, set Val to the low bits /// of the result and return true. Otherwise, return false. bool NumericLiteralParser::GetIntegerValue(llvm::APInt &Val) { + // Fast path: Compute a conservative bound on the maximum number of + // bits per digit in this radix. If we can't possibly overflow a + // uint64 based on that bound then do the simple conversion to + // integer. This avoids the expensive overflow checking below, and + // handles the common cases that matter (small decimal integers and + // hex/octal values which don't overflow). + unsigned MaxBitsPerDigit = 1; + while ((1U << MaxBitsPerDigit) < radix) + MaxBitsPerDigit += 1; + if ((SuffixBegin - DigitsBegin) * MaxBitsPerDigit <= 64) { + uint64_t N = 0; + for (s = DigitsBegin; s != SuffixBegin; ++s) + N = N*radix + HexDigitValue(*s); + + // This will truncate the value to Val's input width. Simply check + // for overflow by comparing. + Val = N; + return Val.getZExtValue() != N; + } + Val = 0; s = DigitsBegin; |