[llvm-commits] [llvm] r79375 - /llvm/trunk/lib/Support/raw_ostream.cpp

Daniel Dunbar daniel at zuster.org
Tue Aug 18 15:24:01 PDT 2009


Author: ddunbar
Date: Tue Aug 18 17:24:00 2009
New Revision: 79375

URL: http://llvm.org/viewvc/llvm-project?rev=79375&view=rev
Log:
Speed up raw_ostream::<<(unsigned long long) for 32-bit systems by doing most
div/mods in 32-bits.

Modified:
    llvm/trunk/lib/Support/raw_ostream.cpp

Modified: llvm/trunk/lib/Support/raw_ostream.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/raw_ostream.cpp?rev=79375&r1=79374&r2=79375&view=diff

==============================================================================
--- llvm/trunk/lib/Support/raw_ostream.cpp (original)
+++ llvm/trunk/lib/Support/raw_ostream.cpp Tue Aug 18 17:24:00 2009
@@ -125,19 +125,24 @@
 }
 
 raw_ostream &raw_ostream::operator<<(unsigned long long N) {
-  // Output using 32-bit div/mod when possible.
+  // Handle simple case when value fits in long already.
   if (N == static_cast<unsigned long>(N))
     return this->operator<<(static_cast<unsigned long>(N));
 
-  char NumberBuffer[20];
-  char *EndPtr = NumberBuffer+sizeof(NumberBuffer);
-  char *CurPtr = EndPtr;
-  
-  while (N) {
-    *--CurPtr = '0' + char(N % 10);
-    N /= 10;
-  }
-  return write(CurPtr, EndPtr-CurPtr);
+  // Otherwise divide into at two or three 10**9 chunks and write out using
+  // long div/mod, this is substantially faster on a 32-bit system.
+  unsigned long Top = 0, Mid = 0, Bot = N % 1000000000;
+  N /= 1000000000;
+  if (N > 1000000000) {
+    Mid = N % 1000000000;
+    Top = N / 1000000000;
+  } else
+    Mid = N;
+
+  if (Top)
+    this->operator<<(static_cast<unsigned long>(Top));
+  this->operator<<(static_cast<unsigned long>(Mid));
+  return this->operator<<(static_cast<unsigned long>(Bot));
 }
 
 raw_ostream &raw_ostream::operator<<(long long N) {





More information about the llvm-commits mailing list