portability: add missing PRI* constants, signed 1 in bitfield hack

Add missing uses of PRI constants.

Create a PRI constant for size_t, since %z isn't available on all
platforms. Notably, the legacy Windows runtime needs %I instead of %z.
Use that on UCRT as well, since there doesn't seem to be a way to
determine if you are compiling for MSVCRT or UCRT.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
This commit is contained in:
H. Peter Anvin 2025-09-30 09:31:40 -07:00
parent 1185c1f118
commit 595afc6f4b
8 changed files with 38 additions and 11 deletions

View file

@ -99,6 +99,30 @@
# include <sys/types.h>
#endif
/*
* This is impossible to do 100% accurately, because the actual type
* of size_t may differ from its range.
*/
#ifdef _WIN32
# define PRIz "I" /* Needed for msvcrt, not ucrt */
#elif defined(PRINTF_SUPPORTS_Z) || \
(defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L)
# define PRIz "z"
#elif SIZE_MAX == UINT_MAX
# define PRIz ""
#elif SIZE_MAX == ULONG_MAX
# define PRIz "l"
#elif SIZE_MAX == ULONGLONG_MAX
# define PRIz "ll"
#else
# error "Unable to determine printf format for size_t"
#endif
#define PRIzd PRIz "u" /* size_t is always unsigned */
#define PRIzu PRIz "u"
#define PRIzu PRIz "u"
#define PRIzx PRIz "x"
#define PRIzX PRIz "X"
#ifdef HAVE_STDBIT_H
# include <stdbit.h>

View file

@ -75,7 +75,7 @@ typedef union intorptr intorptr;
#define mk_field(name,pos,width) \
mk_field_mask(name,pos,width, \
(ONE << name ## _WIDTH)-1)
(1 << name ## _WIDTH)-1)
/*
* Cast a value to a suitable type to represent the encoded