From 6e8136ece5b01ca9cd16bdb974c4d54e939c92cf Mon Sep 17 00:00:00 2001
From: David Fetter <david@fetter.org>
Date: Tue, 10 Sep 2019 02:06:31 -0700
Subject: [PATCH v1 1/2] Output digits two at a time in sprintf.c
To: hackers
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="------------2.21.0"

This is a multi-part message in MIME format.
--------------2.21.0
Content-Type: text/plain; charset=UTF-8; format=fixed
Content-Transfer-Encoding: 8bit


diff --git a/src/port/snprintf.c b/src/port/snprintf.c
index 8fd997553e..fd9d384144 100644
--- a/src/port/snprintf.c
+++ b/src/port/snprintf.c
@@ -1014,9 +1014,60 @@ fmtint(long long value, char type, int forcesign, int leftjust,
 	   PrintfTarget *target)
 {
 	unsigned long long base;
+	unsigned long long square;
 	unsigned long long uvalue;
 	int			dosign;
-	const char *cvt = "0123456789abcdef";
+	/* Maps for octal, decimal, and two flavors of hexadecimal */
+	const char *digits;
+	const char	decimal_digits[200] =
+					/* 10^2 * 2 decimal digits */
+					"0001020304050607080910111213141516171819"
+					"2021222324252627282930313233343536373839"
+					"4041424344454647484950515253545556575859"
+					"6061626364656667686970717273747576777879"
+					"8081828384858687888990919293949596979899";
+	const char	octal_digits[128] =
+					/* 8^2 * 2 octal digits */
+					"00010203040506071011121314151617"
+					"20212223242526273031323334353637"
+					"40414243444546475051525354555657"
+					"60616263646566677071727374757677";
+					/* 16^2 * 2 hex digits */
+	const char	hex_lower_digits[512] =
+					"000102030405060708090a0b0c0d0e0f"
+					"101112131415161718191a1b1c1d1e1f"
+					"202122232425262728292a2b2c2d2e2f"
+					"303132333435363738393a3b3c3d3e3f"
+					"404142434445464748494a4b4c4d4e4f"
+					"505152535455565758595a5b5c5d5e5f"
+					"606162636465666768696a6b6c6d6e6f"
+					"707172737475767778797a7b7c7d7e7f"
+					"808182838485868788898a8b8c8d8e8f"
+					"909192939495969798999a9b9c9d9e9f"
+					"a0a1a2a3a4a5a6a7a8a9aaabacadaeaf"
+					"b0b1b2b3b4b5b6b7b8b9babbbcbdbebf"
+					"c0c1c2c3c4c5c6c7c8c9cacbcccdcecf"
+					"d0d1d2d3d4d5d6d7d8d9dadbdcdddedf"
+					"e0e1e2e3e4e5e6e7e8e9eaebecedeeef"
+					"f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff";
+	const char	hex_upper_digits[512] =
+					/* 16^2 * 2 HEX DIGITS */
+					"000102030405060708090A0B0C0D0E0F"
+					"101112131415161718191A1B1C1D1E1F"
+					"202122232425262728292A2B2C2D2E2F"
+					"303132333435363738393A3B3C3D3E3F"
+					"404142434445464748494A4B4C4D4E4F"
+					"505152535455565758595A5B5C5D5E5F"
+					"606162636465666768696A6B6C6D6E6F"
+					"707172737475767778797A7B7C7D7E7F"
+					"808182838485868788898A8B8C8D8E8F"
+					"909192939495969798999A9B9C9D9E9F"
+					"A0A1A2A3A4A5A6A7A8A9AAABACADAEAF"
+					"B0B1B2B3B4B5B6B7B8B9BABBBCBDBEBF"
+					"C0C1C2C3C4C5C6C7C8C9CACBCCCDCECF"
+					"D0D1D2D3D4D5D6D7D8D9DADBDCDDDEDF"
+					"E0E1E2E3E4E5E6E7E8E9EAEBECEDEEEF"
+					"F0F1F2F3F4F5F6F7F8F9FAFBFCFDFEFF";
 	int			signvalue = 0;
 	char		convert[64];
 	int			vallen = 0;
@@ -1027,23 +1078,27 @@ fmtint(long long value, char type, int forcesign, int leftjust,
 	{
 		case 'd':
 		case 'i':
+			digits = decimal_digits;
 			base = 10;
 			dosign = 1;
 			break;
 		case 'o':
+			digits = octal_digits;
 			base = 8;
 			dosign = 0;
 			break;
 		case 'u':
+			digits = decimal_digits;
 			base = 10;
 			dosign = 0;
 			break;
 		case 'x':
+			digits = hex_lower_digits;
 			base = 16;
 			dosign = 0;
 			break;
 		case 'X':
-			cvt = "0123456789ABCDEF";
+			digits = hex_upper_digits;
 			base = 16;
 			dosign = 0;
 			break;
@@ -1051,6 +1106,8 @@ fmtint(long long value, char type, int forcesign, int leftjust,
 			return;				/* keep compiler quiet */
 	}
 
+	square = base * base;
+
 	/* disable MSVC warning about applying unary minus to an unsigned value */
 #if _MSC_VER
 #pragma warning(push)
@@ -1073,12 +1130,20 @@ fmtint(long long value, char type, int forcesign, int leftjust,
 		vallen = 0;
 	else
 	{
-		/* make integer string */
-		do
+		/* make integer string, two digits at a time */
+		while(uvalue >= base)
 		{
-			convert[sizeof(convert) - (++vallen)] = cvt[uvalue % base];
-			uvalue = uvalue / base;
-		} while (uvalue);
+			const int i = (uvalue % square) * 2;
+			uvalue /= square;
+			vallen += 2;
+			memcpy(convert + sizeof(convert) - vallen, digits + i, 2);
+		}
+		/* Account for single digit */
+		if (uvalue > 0 || vallen == 0)
+		{
+			vallen++;
+			memcpy(convert + sizeof(convert) - vallen, digits + uvalue * 2 + 1, 1);
+		}
 	}
 
 	zeropad = Max(0, precision - vallen);

--------------2.21.0--


