diff options
author | Thomas Munro | 2024-12-04 01:46:59 +0000 |
---|---|---|
committer | Thomas Munro | 2024-12-04 02:05:38 +0000 |
commit | 962da900ac8f0927f1af2fd811ca67fa163c873a (patch) | |
tree | 5839c22d2ad7bc68ddfd1e4debc734ef5df3a9d1 /src/port | |
parent | 3b08d5224d7df71cc111d8522cf6190fc02f6fb9 (diff) |
Use <stdint.h> and <inttypes.h> for c.h integers.
Redefine our exact width types with standard C99 types and macros,
including int64_t, INT64_MAX, INT64_C(), PRId64 etc. We were already
using <stdint.h> types in a few places.
One complication is that Windows' <inttypes.h> uses format strings like
"%I64d", "%I32", "%I" for PRI*64, PRI*32, PTR*PTR, instead of mapping to
other standardized format strings like "%lld" etc as seen on other known
systems. Teach our snprintf.c to understand them.
This removes a lot of configure clutter, and should also allow 64-bit
numbers and other standard types to be used in localized messages
without casting.
Reviewed-by: Peter Eisentraut <[email protected]>
Discussion: https://postgr.es/m/ME3P282MB3166F9D1F71F787929C0C7E7B6312%40ME3P282MB3166.AUSP282.PROD.OUTLOOK.COM
Diffstat (limited to 'src/port')
-rw-r--r-- | src/port/pg_bitutils.c | 6 | ||||
-rw-r--r-- | src/port/snprintf.c | 64 |
2 files changed, 53 insertions, 17 deletions
diff --git a/src/port/pg_bitutils.c b/src/port/pg_bitutils.c index 87f56e82b80..c8399981ee0 100644 --- a/src/port/pg_bitutils.c +++ b/src/port/pg_bitutils.c @@ -370,12 +370,12 @@ static inline int pg_popcount64_slow(uint64 word) { #ifdef HAVE__BUILTIN_POPCOUNT -#if defined(HAVE_LONG_INT_64) +#if SIZEOF_LONG == 8 return __builtin_popcountl(word); -#elif defined(HAVE_LONG_LONG_INT_64) +#elif SIZEOF_LONG_LONG == 8 return __builtin_popcountll(word); #else -#error must have a working 64-bit integer datatype +#error "cannot find integer of the same size as uint64_t" #endif #else /* !HAVE__BUILTIN_POPCOUNT */ int result = 0; diff --git a/src/port/snprintf.c b/src/port/snprintf.c index 884f0262dd1..d83f81ade3f 100644 --- a/src/port/snprintf.c +++ b/src/port/snprintf.c @@ -560,6 +560,28 @@ nextch2: fmtpos = accum; accum = 0; goto nextch2; +#ifdef WIN32 + case 'I': + /* Windows PRI*{32,64,PTR} size */ + if (format[0] == '3' && format[1] == '2') + format += 2; + else if (format[0] == '6' && format[1] == '4') + { + format += 2; + longlongflag = 1; + } + else + { +#if SIZEOF_VOID_P == SIZEOF_LONG + longflag = 1; +#elif SIZEOF_VOID_P == SIZEOF_LONG_LONG + longlongflag = 1; +#else +#error "cannot find integer type of the same size as intptr_t" +#endif + } + goto nextch2; +#endif case 'l': if (longflag) longlongflag = 1; @@ -567,16 +589,12 @@ nextch2: longflag = 1; goto nextch2; case 'z': -#if SIZEOF_SIZE_T == 8 -#ifdef HAVE_LONG_INT_64 +#if SIZEOF_SIZE_T == SIZEOF_LONG longflag = 1; -#elif defined(HAVE_LONG_LONG_INT_64) +#elif SIZEOF_SIZE_T == SIZEOF_LONG_LONG longlongflag = 1; #else -#error "Don't know how to print 64bit integers" -#endif -#else - /* assume size_t is same size as int */ +#error "cannot find integer type of the same size as size_t" #endif goto nextch2; case 'h': @@ -827,6 +845,28 @@ nextch1: fmtpos = accum; accum = 0; goto nextch1; +#ifdef WIN32 + case 'I': + /* Windows PRI*{32,64,PTR} size */ + if (format[0] == '3' && format[1] == '2') + format += 2; + else if (format[0] == '6' && format[1] == '4') + { + format += 2; + longlongflag = 1; + } + else + { +#if SIZEOF_VOID_P == SIZEOF_LONG + longflag = 1; +#elif SIZEOF_VOID_P == SIZEOF_LONG_LONG + longlongflag = 1; +#else +#error "cannot find integer type of the same size as intptr_t" +#endif + } + goto nextch1; +#endif case 'l': if (longflag) longlongflag = 1; @@ -834,16 +874,12 @@ nextch1: longflag = 1; goto nextch1; case 'z': -#if SIZEOF_SIZE_T == 8 -#ifdef HAVE_LONG_INT_64 +#if SIZEOF_SIZE_T == SIZEOF_LONG longflag = 1; -#elif defined(HAVE_LONG_LONG_INT_64) +#elif SIZEOF_SIZE_T == SIZEOF_LONG_LONG longlongflag = 1; #else -#error "Don't know how to print 64bit integers" -#endif -#else - /* assume size_t is same size as int */ +#error "cannot find integer type of the same size as size_t" #endif goto nextch1; case 'h': |