]> git.ipfire.org Git - thirdparty/postgresql.git/commitdiff
Optimizations for integer to decimal output.
authorAndrew Gierth <rhodiumtoad@postgresql.org>
Sat, 1 Feb 2020 21:57:14 +0000 (21:57 +0000)
committerAndrew Gierth <rhodiumtoad@postgresql.org>
Sat, 1 Feb 2020 21:57:14 +0000 (21:57 +0000)
Using a lookup table of digit pairs reduces the number of divisions
needed, and calculating the length upfront saves some work; these
ideas are taken from the code previously committed for floats.

David Fetter, reviewed by Kyotaro Horiguchi, Tels, and me.

Discussion: https://postgr.es/m/20190924052620.GP31596%40fetter.org

src/backend/access/common/printsimple.c
src/backend/utils/adt/datetime.c
src/backend/utils/adt/int8.c
src/backend/utils/adt/numutils.c
src/include/utils/builtins.h

index 4006ebdabd3a00eb5b184d5abc5c3f6c088cb58b..0f0b54bdae37d36a311e337a16dc88ca46211f1d 100644 (file)
@@ -112,7 +112,7 @@ printsimple(TupleTableSlot *slot, DestReceiver *self)
                        case INT8OID:
                                {
                                        int64           num = DatumGetInt64(value);
-                                       char            str[23];        /* sign, 21 digits and '\0' */
+                                       char            str[MAXINT8LEN + 1];
 
                                        pg_lltoa(num, str);
                                        pq_sendcountedtext(&buf, str, strlen(str), false);
index 6159296b3db85aec7de97b1b69fa3b021bdb4a8c..4f109111d19724d3d657c6b35bdd81f08d6fe7bf 100644 (file)
@@ -388,9 +388,9 @@ AppendSeconds(char *cp, int sec, fsec_t fsec, int precision, bool fillzeros)
        Assert(precision >= 0);
 
        if (fillzeros)
-               cp = pg_ltostr_zeropad(cp, Abs(sec), 2);
+               cp = pg_ultostr_zeropad(cp, Abs(sec), 2);
        else
-               cp = pg_ltostr(cp, Abs(sec));
+               cp = pg_ultostr(cp, Abs(sec));
 
        /* fsec_t is just an int32 */
        if (fsec != 0)
@@ -430,7 +430,7 @@ AppendSeconds(char *cp, int sec, fsec_t fsec, int precision, bool fillzeros)
                 * which will generate a correct answer in the minimum valid width.
                 */
                if (value)
-                       return pg_ltostr(cp, Abs(fsec));
+                       return pg_ultostr(cp, Abs(fsec));
 
                return end;
        }
@@ -3831,20 +3831,20 @@ EncodeTimezone(char *str, int tz, int style)
 
        if (sec != 0)
        {
-               str = pg_ltostr_zeropad(str, hour, 2);
+               str = pg_ultostr_zeropad(str, hour, 2);
                *str++ = ':';
-               str = pg_ltostr_zeropad(str, min, 2);
+               str = pg_ultostr_zeropad(str, min, 2);
                *str++ = ':';
-               str = pg_ltostr_zeropad(str, sec, 2);
+               str = pg_ultostr_zeropad(str, sec, 2);
        }
        else if (min != 0 || style == USE_XSD_DATES)
        {
-               str = pg_ltostr_zeropad(str, hour, 2);
+               str = pg_ultostr_zeropad(str, hour, 2);
                *str++ = ':';
-               str = pg_ltostr_zeropad(str, min, 2);
+               str = pg_ultostr_zeropad(str, min, 2);
        }
        else
-               str = pg_ltostr_zeropad(str, hour, 2);
+               str = pg_ultostr_zeropad(str, hour, 2);
        return str;
 }
 
@@ -3861,40 +3861,40 @@ EncodeDateOnly(struct pg_tm *tm, int style, char *str)
                case USE_ISO_DATES:
                case USE_XSD_DATES:
                        /* compatible with ISO date formats */
-                       str = pg_ltostr_zeropad(str,
+                       str = pg_ultostr_zeropad(str,
                                                                        (tm->tm_year > 0) ? tm->tm_year : -(tm->tm_year - 1), 4);
                        *str++ = '-';
-                       str = pg_ltostr_zeropad(str, tm->tm_mon, 2);
+                       str = pg_ultostr_zeropad(str, tm->tm_mon, 2);
                        *str++ = '-';
-                       str = pg_ltostr_zeropad(str, tm->tm_mday, 2);
+                       str = pg_ultostr_zeropad(str, tm->tm_mday, 2);
                        break;
 
                case USE_SQL_DATES:
                        /* compatible with Oracle/Ingres date formats */
                        if (DateOrder == DATEORDER_DMY)
                        {
-                               str = pg_ltostr_zeropad(str, tm->tm_mday, 2);
+                               str = pg_ultostr_zeropad(str, tm->tm_mday, 2);
                                *str++ = '/';
-                               str = pg_ltostr_zeropad(str, tm->tm_mon, 2);
+                               str = pg_ultostr_zeropad(str, tm->tm_mon, 2);
                        }
                        else
                        {
-                               str = pg_ltostr_zeropad(str, tm->tm_mon, 2);
+                               str = pg_ultostr_zeropad(str, tm->tm_mon, 2);
                                *str++ = '/';
-                               str = pg_ltostr_zeropad(str, tm->tm_mday, 2);
+                               str = pg_ultostr_zeropad(str, tm->tm_mday, 2);
                        }
                        *str++ = '/';
-                       str = pg_ltostr_zeropad(str,
+                       str = pg_ultostr_zeropad(str,
                                                                        (tm->tm_year > 0) ? tm->tm_year : -(tm->tm_year - 1), 4);
                        break;
 
                case USE_GERMAN_DATES:
                        /* German-style date format */
-                       str = pg_ltostr_zeropad(str, tm->tm_mday, 2);
+                       str = pg_ultostr_zeropad(str, tm->tm_mday, 2);
                        *str++ = '.';
-                       str = pg_ltostr_zeropad(str, tm->tm_mon, 2);
+                       str = pg_ultostr_zeropad(str, tm->tm_mon, 2);
                        *str++ = '.';
-                       str = pg_ltostr_zeropad(str,
+                       str = pg_ultostr_zeropad(str,
                                                                        (tm->tm_year > 0) ? tm->tm_year : -(tm->tm_year - 1), 4);
                        break;
 
@@ -3903,18 +3903,18 @@ EncodeDateOnly(struct pg_tm *tm, int style, char *str)
                        /* traditional date-only style for Postgres */
                        if (DateOrder == DATEORDER_DMY)
                        {
-                               str = pg_ltostr_zeropad(str, tm->tm_mday, 2);
+                               str = pg_ultostr_zeropad(str, tm->tm_mday, 2);
                                *str++ = '-';
-                               str = pg_ltostr_zeropad(str, tm->tm_mon, 2);
+                               str = pg_ultostr_zeropad(str, tm->tm_mon, 2);
                        }
                        else
                        {
-                               str = pg_ltostr_zeropad(str, tm->tm_mon, 2);
+                               str = pg_ultostr_zeropad(str, tm->tm_mon, 2);
                                *str++ = '-';
-                               str = pg_ltostr_zeropad(str, tm->tm_mday, 2);
+                               str = pg_ultostr_zeropad(str, tm->tm_mday, 2);
                        }
                        *str++ = '-';
-                       str = pg_ltostr_zeropad(str,
+                       str = pg_ultostr_zeropad(str,
                                                                        (tm->tm_year > 0) ? tm->tm_year : -(tm->tm_year - 1), 4);
                        break;
        }
@@ -3939,9 +3939,9 @@ EncodeDateOnly(struct pg_tm *tm, int style, char *str)
 void
 EncodeTimeOnly(struct pg_tm *tm, fsec_t fsec, bool print_tz, int tz, int style, char *str)
 {
-       str = pg_ltostr_zeropad(str, tm->tm_hour, 2);
+       str = pg_ultostr_zeropad(str, tm->tm_hour, 2);
        *str++ = ':';
-       str = pg_ltostr_zeropad(str, tm->tm_min, 2);
+       str = pg_ultostr_zeropad(str, tm->tm_min, 2);
        *str++ = ':';
        str = AppendSeconds(str, tm->tm_sec, fsec, MAX_TIME_PRECISION, true);
        if (print_tz)
@@ -3984,16 +3984,16 @@ EncodeDateTime(struct pg_tm *tm, fsec_t fsec, bool print_tz, int tz, const char
                case USE_ISO_DATES:
                case USE_XSD_DATES:
                        /* Compatible with ISO-8601 date formats */
-                       str = pg_ltostr_zeropad(str,
+                       str = pg_ultostr_zeropad(str,
                                                                        (tm->tm_year > 0) ? tm->tm_year : -(tm->tm_year - 1), 4);
                        *str++ = '-';
-                       str = pg_ltostr_zeropad(str, tm->tm_mon, 2);
+                       str = pg_ultostr_zeropad(str, tm->tm_mon, 2);
                        *str++ = '-';
-                       str = pg_ltostr_zeropad(str, tm->tm_mday, 2);
+                       str = pg_ultostr_zeropad(str, tm->tm_mday, 2);
                        *str++ = (style == USE_ISO_DATES) ? ' ' : 'T';
-                       str = pg_ltostr_zeropad(str, tm->tm_hour, 2);
+                       str = pg_ultostr_zeropad(str, tm->tm_hour, 2);
                        *str++ = ':';
-                       str = pg_ltostr_zeropad(str, tm->tm_min, 2);
+                       str = pg_ultostr_zeropad(str, tm->tm_min, 2);
                        *str++ = ':';
                        str = AppendTimestampSeconds(str, tm, fsec);
                        if (print_tz)
@@ -4004,23 +4004,23 @@ EncodeDateTime(struct pg_tm *tm, fsec_t fsec, bool print_tz, int tz, const char
                        /* Compatible with Oracle/Ingres date formats */
                        if (DateOrder == DATEORDER_DMY)
                        {
-                               str = pg_ltostr_zeropad(str, tm->tm_mday, 2);
+                               str = pg_ultostr_zeropad(str, tm->tm_mday, 2);
                                *str++ = '/';
-                               str = pg_ltostr_zeropad(str, tm->tm_mon, 2);
+                               str = pg_ultostr_zeropad(str, tm->tm_mon, 2);
                        }
                        else
                        {
-                               str = pg_ltostr_zeropad(str, tm->tm_mon, 2);
+                               str = pg_ultostr_zeropad(str, tm->tm_mon, 2);
                                *str++ = '/';
-                               str = pg_ltostr_zeropad(str, tm->tm_mday, 2);
+                               str = pg_ultostr_zeropad(str, tm->tm_mday, 2);
                        }
                        *str++ = '/';
-                       str = pg_ltostr_zeropad(str,
+                       str = pg_ultostr_zeropad(str,
                                                                        (tm->tm_year > 0) ? tm->tm_year : -(tm->tm_year - 1), 4);
                        *str++ = ' ';
-                       str = pg_ltostr_zeropad(str, tm->tm_hour, 2);
+                       str = pg_ultostr_zeropad(str, tm->tm_hour, 2);
                        *str++ = ':';
-                       str = pg_ltostr_zeropad(str, tm->tm_min, 2);
+                       str = pg_ultostr_zeropad(str, tm->tm_min, 2);
                        *str++ = ':';
                        str = AppendTimestampSeconds(str, tm, fsec);
 
@@ -4043,16 +4043,16 @@ EncodeDateTime(struct pg_tm *tm, fsec_t fsec, bool print_tz, int tz, const char
 
                case USE_GERMAN_DATES:
                        /* German variant on European style */
-                       str = pg_ltostr_zeropad(str, tm->tm_mday, 2);
+                       str = pg_ultostr_zeropad(str, tm->tm_mday, 2);
                        *str++ = '.';
-                       str = pg_ltostr_zeropad(str, tm->tm_mon, 2);
+                       str = pg_ultostr_zeropad(str, tm->tm_mon, 2);
                        *str++ = '.';
-                       str = pg_ltostr_zeropad(str,
+                       str = pg_ultostr_zeropad(str,
                                                                        (tm->tm_year > 0) ? tm->tm_year : -(tm->tm_year - 1), 4);
                        *str++ = ' ';
-                       str = pg_ltostr_zeropad(str, tm->tm_hour, 2);
+                       str = pg_ultostr_zeropad(str, tm->tm_hour, 2);
                        *str++ = ':';
-                       str = pg_ltostr_zeropad(str, tm->tm_min, 2);
+                       str = pg_ultostr_zeropad(str, tm->tm_min, 2);
                        *str++ = ':';
                        str = AppendTimestampSeconds(str, tm, fsec);
 
@@ -4078,7 +4078,7 @@ EncodeDateTime(struct pg_tm *tm, fsec_t fsec, bool print_tz, int tz, const char
                        *str++ = ' ';
                        if (DateOrder == DATEORDER_DMY)
                        {
-                               str = pg_ltostr_zeropad(str, tm->tm_mday, 2);
+                               str = pg_ultostr_zeropad(str, tm->tm_mday, 2);
                                *str++ = ' ';
                                memcpy(str, months[tm->tm_mon - 1], 3);
                                str += 3;
@@ -4088,16 +4088,16 @@ EncodeDateTime(struct pg_tm *tm, fsec_t fsec, bool print_tz, int tz, const char
                                memcpy(str, months[tm->tm_mon - 1], 3);
                                str += 3;
                                *str++ = ' ';
-                               str = pg_ltostr_zeropad(str, tm->tm_mday, 2);
+                               str = pg_ultostr_zeropad(str, tm->tm_mday, 2);
                        }
                        *str++ = ' ';
-                       str = pg_ltostr_zeropad(str, tm->tm_hour, 2);
+                       str = pg_ultostr_zeropad(str, tm->tm_hour, 2);
                        *str++ = ':';
-                       str = pg_ltostr_zeropad(str, tm->tm_min, 2);
+                       str = pg_ultostr_zeropad(str, tm->tm_min, 2);
                        *str++ = ':';
                        str = AppendTimestampSeconds(str, tm, fsec);
                        *str++ = ' ';
-                       str = pg_ltostr_zeropad(str,
+                       str = pg_ultostr_zeropad(str,
                                                                        (tm->tm_year > 0) ? tm->tm_year : -(tm->tm_year - 1), 4);
 
                        if (print_tz)
index 494768c190151de90303a6fe6f66c2329d0d4d06..55e0eb05dac5bc455e5795e4f0c7f21ba4de49e3 100644 (file)
@@ -26,7 +26,6 @@
 #include "utils/builtins.h"
 #include "utils/int8.h"
 
-#define MAXINT8LEN             25
 
 typedef struct
 {
index aca1102b36a7a701057e2e0b378e3ac0c58dc829..a7467adb037ece81950580f45bb6cbb85980260a 100644 (file)
 
 #include "common/int.h"
 #include "utils/builtins.h"
+#include "port/pg_bitutils.h"
+
+/*
+ * A table of all two-digit numbers. This is used to speed up decimal digit
+ * generation by copying pairs of digits into the final output.
+ */
+static const char DIGIT_TABLE[200] =
+"00" "01" "02" "03" "04" "05" "06" "07" "08" "09"
+"10" "11" "12" "13" "14" "15" "16" "17" "18" "19"
+"20" "21" "22" "23" "24" "25" "26" "27" "28" "29"
+"30" "31" "32" "33" "34" "35" "36" "37" "38" "39"
+"40" "41" "42" "43" "44" "45" "46" "47" "48" "49"
+"50" "51" "52" "53" "54" "55" "56" "57" "58" "59"
+"60" "61" "62" "63" "64" "65" "66" "67" "68" "69"
+"70" "71" "72" "73" "74" "75" "76" "77" "78" "79"
+"80" "81" "82" "83" "84" "85" "86" "87" "88" "89"
+"90" "91" "92" "93" "94" "95" "96" "97" "98" "99";
+
+/*
+ * Adapted from http://graphics.stanford.edu/~seander/bithacks.html#IntegerLog10
+ */
+static inline int
+decimalLength32(const uint32 v)
+{
+       int                     t;
+       static uint32   PowersOfTen[] =
+       {1,                10,                100,
+        1000,             10000,             100000,
+        1000000,          10000000,          100000000,
+        1000000000};
+       /*
+        * Compute base-10 logarithm by dividing the base-2 logarithm by a
+        * good-enough approximation of the base-2 logarithm of 10
+        */
+       t = (pg_leftmost_one_pos32(v) + 1) * 1233 / 4096;
+       return t + (v >= PowersOfTen[t]);
+}
+
+static inline int
+decimalLength64(const uint64 v)
+{
+       int                     t;
+       static uint64   PowersOfTen[] = {
+               UINT64CONST(1),                   UINT64CONST(10),
+               UINT64CONST(100),                 UINT64CONST(1000),
+               UINT64CONST(10000),               UINT64CONST(100000),
+               UINT64CONST(1000000),             UINT64CONST(10000000),
+               UINT64CONST(100000000),           UINT64CONST(1000000000),
+               UINT64CONST(10000000000),         UINT64CONST(100000000000),
+               UINT64CONST(1000000000000),       UINT64CONST(10000000000000),
+               UINT64CONST(100000000000000),     UINT64CONST(1000000000000000),
+               UINT64CONST(10000000000000000),   UINT64CONST(100000000000000000),
+               UINT64CONST(1000000000000000000), UINT64CONST(10000000000000000000)
+       };
+
+       /*
+        * Compute base-10 logarithm by dividing the base-2 logarithm by a
+        * good-enough approximation of the base-2 logarithm of 10
+        */
+       t = (pg_leftmost_one_pos64(v) + 1) * 1233 / 4096;
+       return t + (v >= PowersOfTen[t]);
+}
 
 /*
  * pg_atoi: convert string to integer
@@ -276,116 +338,201 @@ pg_itoa(int16 i, char *a)
 }
 
 /*
- * pg_ltoa: converts a signed 32-bit integer to its string representation
+ * pg_ultoa_n: converts an unsigned 32-bit integer to its string representation,
+ * not NUL-terminated, and returns the length of that string representation
  *
- * Caller must ensure that 'a' points to enough memory to hold the result
- * (at least 12 bytes, counting a leading sign and trailing NUL).
+ * Caller must ensure that 'a' points to enough memory to hold the result (at
+ * least 10 bytes)
  */
-void
-pg_ltoa(int32 value, char *a)
+int
+pg_ultoa_n(uint32 value, char *a)
 {
-       char       *start = a;
-       bool            neg = false;
+       int                     olength,
+                               i = 0;
 
-       /*
-        * Avoid problems with the most negative integer not being representable
-        * as a positive integer.
-        */
-       if (value == PG_INT32_MIN)
-       {
-               memcpy(a, "-2147483648", 12);
-               return;
-       }
-       else if (value < 0)
+       /* Degenerate case */
+       if (value == 0)
        {
-               value = -value;
-               neg = true;
+               *a = '0';
+               return 1;
        }
 
-       /* Compute the result string backwards. */
-       do
+       olength = decimalLength32(value);
+
+       /* Compute the result string. */
+       while (value >= 10000)
        {
-               int32           remainder;
-               int32           oldval = value;
+               const uint32 c = value - 10000 * (value / 10000);
+               const uint32 c0 = (c % 100) << 1;
+               const uint32 c1 = (c / 100) << 1;
 
-               value /= 10;
-               remainder = oldval - value * 10;
-               *a++ = '0' + remainder;
-       } while (value != 0);
+               char       *pos = a + olength - i;
 
-       if (neg)
-               *a++ = '-';
+               value /= 10000;
 
-       /* Add trailing NUL byte, and back up 'a' to the last character. */
-       *a-- = '\0';
+               memcpy(pos - 2, DIGIT_TABLE + c0, 2);
+               memcpy(pos - 4, DIGIT_TABLE + c1, 2);
+               i += 4;
+       }
+       if (value >= 100)
+       {
+               const uint32 c = (value % 100) << 1;
+
+               char       *pos = a + olength - i;
+
+               value /= 100;
 
-       /* Reverse string. */
-       while (start < a)
+               memcpy(pos - 2, DIGIT_TABLE + c, 2);
+               i += 2;
+       }
+       if (value >= 10)
        {
-               char            swap = *start;
+               const uint32 c = value << 1;
+
+               char       *pos = a + olength - i;
 
-               *start++ = *a;
-               *a-- = swap;
+               memcpy(pos - 2, DIGIT_TABLE + c, 2);
+       }
+       else
+       {
+               *a = (char) ('0' + value);
        }
+
+       return olength;
 }
 
 /*
- * pg_lltoa: convert a signed 64-bit integer to its string representation
+ * NUL-terminate the output of pg_ultoa_n.
  *
- * Caller must ensure that 'a' points to enough memory to hold the result
- * (at least MAXINT8LEN+1 bytes, counting a leading sign and trailing NUL).
+ * It is the caller's responsibility to ensure that a is at least 12 bytes long,
+ * which is enough room to hold a minus sign, a maximally long int32, and the
+ * above terminating NUL.
  */
 void
-pg_lltoa(int64 value, char *a)
+pg_ltoa(int32 value, char *a)
 {
-       char       *start = a;
-       bool            neg = false;
 
-       /*
-        * Avoid problems with the most negative integer not being representable
-        * as a positive integer.
-        */
-       if (value == PG_INT64_MIN)
+       uint32          uvalue = (uint32) value;
+       int                     len;
+
+       if (value < 0)
        {
-               memcpy(a, "-9223372036854775808", 21);
-               return;
+               uvalue = (uint32) 0 - uvalue;
+               *a++ = '-';
        }
-       else if (value < 0)
+       len = pg_ultoa_n(uvalue, a);
+       a[len] = '\0';
+}
+
+/*
+ * Get the decimal representation, not NUL-terminated, and return the length of
+ * same.  Caller must ensure that a points to at least MAXINT8LEN bytes.
+ */
+int
+pg_ulltoa_n(uint64 value, char *a)
+{
+       int                     olength,
+                               i = 0;
+       uint32          value2;
+
+
+       /* Degenerate case */
+       if (value == 0)
        {
-               value = -value;
-               neg = true;
+               *a = '0';
+               return 1;
        }
 
-       /* Compute the result string backwards. */
-       do
+       olength = decimalLength64(value);
+
+       /* Compute the result string. */
+       while (value >= 100000000)
        {
-               int64           remainder;
-               int64           oldval = value;
+               const uint64 q = value / 100000000;
+               uint32          value2 = (uint32) (value - 100000000 * q);
 
-               value /= 10;
-               remainder = oldval - value * 10;
-               *a++ = '0' + remainder;
-       } while (value != 0);
+               const uint32 c = value2 % 10000;
+               const uint32 d = value2 / 10000;
+               const uint32 c0 = (c % 100) << 1;
+               const uint32 c1 = (c / 100) << 1;
+               const uint32 d0 = (d % 100) << 1;
+               const uint32 d1 = (d / 100) << 1;
 
-       if (neg)
-               *a++ = '-';
+               char       *pos = a + olength - i;
+
+               value = q;
+
+               memcpy(pos - 2, DIGIT_TABLE + c0, 2);
+               memcpy(pos - 4, DIGIT_TABLE + c1, 2);
+               memcpy(pos - 6, DIGIT_TABLE + d0, 2);
+               memcpy(pos - 8, DIGIT_TABLE + d1, 2);
+               i += 8;
+       }
+
+       /* Switch to 32-bit for speed */
+       value2 = (uint32) value;
 
-       /* Add trailing NUL byte, and back up 'a' to the last character. */
-       *a-- = '\0';
+       if (value2 >= 10000)
+       {
+               const uint32 c = value2 - 10000 * (value2 / 10000);
+               const uint32 c0 = (c % 100) << 1;
+               const uint32 c1 = (c / 100) << 1;
+
+               char       *pos = a + olength - i;
 
-       /* Reverse string. */
-       while (start < a)
+               value2 /= 10000;
+
+               memcpy(pos - 2, DIGIT_TABLE + c0, 2);
+               memcpy(pos - 4, DIGIT_TABLE + c1, 2);
+               i += 4;
+       }
+       if (value2 >= 100)
        {
-               char            swap = *start;
+               const uint32 c = (value2 % 100) << 1;
+               char       *pos = a + olength - i;
 
-               *start++ = *a;
-               *a-- = swap;
+               value2 /= 100;
+
+               memcpy(pos - 2, DIGIT_TABLE + c, 2);
+               i += 2;
        }
+       if (value2 >= 10)
+       {
+               const uint32 c = value2 << 1;
+               char       *pos = a + olength - i;
+
+               memcpy(pos - 2, DIGIT_TABLE + c, 2);
+       }
+       else
+               *a = (char) ('0' + value2);
+
+       return olength;
+}
+
+/*
+ * pg_lltoa: convert a signed 64-bit integer to its string representation
+ *
+ * Caller must ensure that 'a' points to enough memory to hold the result
+ * (at least MAXINT8LEN + 1 bytes, counting a leading sign and trailing NUL).
+ */
+void
+pg_lltoa(int64 value, char *a)
+{
+       int                     len;
+       uint64          uvalue = value;
+
+       if (value < 0)
+       {
+               *a++ = '-';
+               uvalue = (uint64) 0 - uvalue;
+       }
+       len = pg_ulltoa_n(uvalue, a);
+       a[len] = 0;
 }
 
 
 /*
- * pg_ltostr_zeropad
+ * pg_ultostr_zeropad
  *             Converts 'value' into a decimal string representation stored at 'str'.
  *             'minwidth' specifies the minimum width of the result; any extra space
  *             is filled up by prefixing the number with zeros.
@@ -407,62 +554,25 @@ pg_lltoa(int64 value, char *a)
  * result.
  */
 char *
-pg_ltostr_zeropad(char *str, int32 value, int32 minwidth)
+pg_ultostr_zeropad(char *str, uint32 value, int32 minwidth)
 {
-       char       *start = str;
-       char       *end = &str[minwidth];
-       int32           num = value;
+       int                     len;
 
        Assert(minwidth > 0);
 
-       /*
-        * Handle negative numbers in a special way.  We can't just write a '-'
-        * prefix and reverse the sign as that would overflow for INT32_MIN.
-        */
-       if (num < 0)
+       if (value < 100 && minwidth == 2)       /* Short cut for common case */
        {
-               *start++ = '-';
-               minwidth--;
-
-               /*
-                * Build the number starting at the last digit.  Here remainder will
-                * be a negative number, so we must reverse the sign before adding '0'
-                * in order to get the correct ASCII digit.
-                */
-               while (minwidth--)
-               {
-                       int32           oldval = num;
-                       int32           remainder;
-
-                       num /= 10;
-                       remainder = oldval - num * 10;
-                       start[minwidth] = '0' - remainder;
-               }
-       }
-       else
-       {
-               /* Build the number starting at the last digit */
-               while (minwidth--)
-               {
-                       int32           oldval = num;
-                       int32           remainder;
-
-                       num /= 10;
-                       remainder = oldval - num * 10;
-                       start[minwidth] = '0' + remainder;
-               }
+               memcpy(str, DIGIT_TABLE + value * 2, 2);
+               return str + 2;
        }
 
-       /*
-        * If minwidth was not high enough to fit the number then num won't have
-        * been divided down to zero.  We punt the problem to pg_ltostr(), which
-        * will generate a correct answer in the minimum valid width.
-        */
-       if (num != 0)
-               return pg_ltostr(str, value);
+       len = pg_ultoa_n(value, str);
+       if (len >= minwidth)
+               return str + len;
 
-       /* Otherwise, return last output character + 1 */
-       return end;
+       memmove(str + minwidth - len, str, len);
+       memset(str, '0', minwidth - len);
+       return str + minwidth;
 }
 
 /*
@@ -484,64 +594,11 @@ pg_ltostr_zeropad(char *str, int32 value, int32 minwidth)
  * result.
  */
 char *
-pg_ltostr(char *str, int32 value)
+pg_ultostr(char *str, uint32 value)
 {
-       char       *start;
-       char       *end;
-
-       /*
-        * Handle negative numbers in a special way.  We can't just write a '-'
-        * prefix and reverse the sign as that would overflow for INT32_MIN.
-        */
-       if (value < 0)
-       {
-               *str++ = '-';
-
-               /* Mark the position we must reverse the string from. */
-               start = str;
-
-               /* Compute the result string backwards. */
-               do
-               {
-                       int32           oldval = value;
-                       int32           remainder;
-
-                       value /= 10;
-                       remainder = oldval - value * 10;
-                       /* As above, we expect remainder to be negative. */
-                       *str++ = '0' - remainder;
-               } while (value != 0);
-       }
-       else
-       {
-               /* Mark the position we must reverse the string from. */
-               start = str;
-
-               /* Compute the result string backwards. */
-               do
-               {
-                       int32           oldval = value;
-                       int32           remainder;
-
-                       value /= 10;
-                       remainder = oldval - value * 10;
-                       *str++ = '0' + remainder;
-               } while (value != 0);
-       }
-
-       /* Remember the end+1 and back up 'str' to the last character. */
-       end = str--;
-
-       /* Reverse string. */
-       while (start < str)
-       {
-               char            swap = *start;
-
-               *start++ = *str;
-               *str-- = swap;
-       }
+       int                     len = pg_ultoa_n(value, str);
 
-       return end;
+       return str + len;
 }
 
 /*
index a241af4bdf9019757e9e05c55da84a7147fead88..e2016a8bc2d8378505183be6833c04b7e00e5e1a 100644 (file)
@@ -18,6 +18,8 @@
 #include "nodes/nodes.h"
 #include "utils/fmgrprotos.h"
 
+/* Sign + the most decimal digits an 8-byte number could have */
+#define MAXINT8LEN 20
 
 /* bool.c */
 extern bool parse_bool(const char *value, bool *result);
@@ -46,10 +48,12 @@ extern int32 pg_atoi(const char *s, int size, int c);
 extern int16 pg_strtoint16(const char *s);
 extern int32 pg_strtoint32(const char *s);
 extern void pg_itoa(int16 i, char *a);
+int                    pg_ultoa_n(uint32 l, char *a);
+int                    pg_ulltoa_n(uint64 l, char *a);
 extern void pg_ltoa(int32 l, char *a);
 extern void pg_lltoa(int64 ll, char *a);
-extern char *pg_ltostr_zeropad(char *str, int32 value, int32 minwidth);
-extern char *pg_ltostr(char *str, int32 value);
+extern char *pg_ultostr_zeropad(char *str, uint32 value, int32 minwidth);
+extern char *pg_ultostr(char *str, uint32 value);
 extern uint64 pg_strtouint64(const char *str, char **endptr, int base);
 
 /* oid.c */