From cb7e1b82e1ef135fbdf81641f5ee760d7ef07875 Mon Sep 17 00:00:00 2001 From: Sam Clegg Date: Wed, 15 Apr 2026 11:43:39 -0700 Subject: [PATCH] Use clang builtin macros throughout stdint.h and alltypes.h We were using these in inconsistently, which led to some inconsistencies. --- .../libc/musl/arch/emscripten/bits/alltypes.h | 26 +-- .../libc/musl/arch/emscripten/bits/stdint.h | 20 -- system/lib/libc/musl/include/alltypes.h.in | 26 +-- system/lib/libc/musl/include/stdint.h | 172 ++++++++---------- test/other/test_stdint_limits.64.out | 26 ++- test/other/test_stdint_limits.c | 32 +++- test/other/test_stdint_limits.out | 28 ++- 7 files changed, 188 insertions(+), 142 deletions(-) delete mode 100644 system/lib/libc/musl/arch/emscripten/bits/stdint.h diff --git a/system/lib/libc/musl/arch/emscripten/bits/alltypes.h b/system/lib/libc/musl/arch/emscripten/bits/alltypes.h index 713e0816a9645..0db4d998f29c8 100644 --- a/system/lib/libc/musl/arch/emscripten/bits/alltypes.h +++ b/system/lib/libc/musl/arch/emscripten/bits/alltypes.h @@ -147,58 +147,62 @@ typedef _Int64 suseconds_t; #endif +// XXX EMSCRIPTEN: This file has been modified from the upstream musl version +// to make use of clang pre-defined macros whereever possible, eliminating +// possible inconsistencies. + #if defined(__NEED_int8_t) && !defined(__DEFINED_int8_t) -typedef signed char int8_t; +typedef __INT8_TYPE__ int8_t; #define __DEFINED_int8_t #endif #if defined(__NEED_int16_t) && !defined(__DEFINED_int16_t) -typedef signed short int16_t; +typedef __INT16_TYPE__ int16_t; #define __DEFINED_int16_t #endif #if defined(__NEED_int32_t) && !defined(__DEFINED_int32_t) -typedef signed int int32_t; +typedef __INT32_TYPE__ int32_t; #define __DEFINED_int32_t #endif #if defined(__NEED_int64_t) && !defined(__DEFINED_int64_t) -typedef signed _Int64 int64_t; +typedef __INT64_TYPE__ int64_t; #define __DEFINED_int64_t #endif #if defined(__NEED_intmax_t) && !defined(__DEFINED_intmax_t) -typedef signed _Int64 intmax_t; +typedef __INTMAX_TYPE__ intmax_t; #define __DEFINED_intmax_t #endif #if defined(__NEED_uint8_t) && !defined(__DEFINED_uint8_t) -typedef unsigned char uint8_t; +typedef __UINT8_TYPE__ uint8_t; #define __DEFINED_uint8_t #endif #if defined(__NEED_uint16_t) && !defined(__DEFINED_uint16_t) -typedef unsigned short uint16_t; +typedef __UINT16_TYPE__ uint16_t; #define __DEFINED_uint16_t #endif #if defined(__NEED_uint32_t) && !defined(__DEFINED_uint32_t) -typedef unsigned int uint32_t; +typedef __UINT32_TYPE__ uint32_t; #define __DEFINED_uint32_t #endif #if defined(__NEED_uint64_t) && !defined(__DEFINED_uint64_t) -typedef unsigned _Int64 uint64_t; +typedef __UINT64_TYPE__ uint64_t; #define __DEFINED_uint64_t #endif #if defined(__NEED_u_int64_t) && !defined(__DEFINED_u_int64_t) -typedef unsigned _Int64 u_int64_t; +typedef __UINT64_TYPE__ u_int64_t; #define __DEFINED_u_int64_t #endif #if defined(__NEED_uintmax_t) && !defined(__DEFINED_uintmax_t) -typedef unsigned _Int64 uintmax_t; +typedef __UINTMAX_TYPE__ uintmax_t; #define __DEFINED_uintmax_t #endif diff --git a/system/lib/libc/musl/arch/emscripten/bits/stdint.h b/system/lib/libc/musl/arch/emscripten/bits/stdint.h deleted file mode 100644 index af54a204419d3..0000000000000 --- a/system/lib/libc/musl/arch/emscripten/bits/stdint.h +++ /dev/null @@ -1,20 +0,0 @@ -typedef int32_t int_fast16_t; -typedef int32_t int_fast32_t; -typedef uint32_t uint_fast16_t; -typedef uint32_t uint_fast32_t; - -#define INT_FAST16_MIN INT32_MIN -#define INT_FAST32_MIN INT32_MIN - -#define INT_FAST16_MAX INT32_MAX -#define INT_FAST32_MAX INT32_MAX - -#define UINT_FAST16_MAX UINT32_MAX -#define UINT_FAST32_MAX UINT32_MAX - -#define INTPTR_MIN (-1-__INTPTR_MAX__) -#define INTPTR_MAX __INTPTR_MAX__ -#define UINTPTR_MAX __UINTPTR_MAX__ -#define PTRDIFF_MIN (-1-__PTRDIFF_MAX__) -#define PTRDIFF_MAX __PTRDIFF_MAX__ -#define SIZE_MAX __SIZE_MAX__ diff --git a/system/lib/libc/musl/include/alltypes.h.in b/system/lib/libc/musl/include/alltypes.h.in index 79a1eca366306..2da1da767eb97 100644 --- a/system/lib/libc/musl/include/alltypes.h.in +++ b/system/lib/libc/musl/include/alltypes.h.in @@ -12,17 +12,21 @@ TYPEDEF _Reg register_t; TYPEDEF _Int64 time_t; TYPEDEF _Int64 suseconds_t; -TYPEDEF signed char int8_t; -TYPEDEF signed short int16_t; -TYPEDEF signed int int32_t; -TYPEDEF signed _Int64 int64_t; -TYPEDEF signed _Int64 intmax_t; -TYPEDEF unsigned char uint8_t; -TYPEDEF unsigned short uint16_t; -TYPEDEF unsigned int uint32_t; -TYPEDEF unsigned _Int64 uint64_t; -TYPEDEF unsigned _Int64 u_int64_t; -TYPEDEF unsigned _Int64 uintmax_t; +// XXX EMSCRIPTEN: This file has been modified from the upstream musl version +// to make use of clang pre-defined macros whereever possible, eliminating +// possible inconsistencies. + +TYPEDEF __INT8_TYPE__ int8_t; +TYPEDEF __INT16_TYPE__ int16_t; +TYPEDEF __INT32_TYPE__ int32_t; +TYPEDEF __INT64_TYPE__ int64_t; +TYPEDEF __INTMAX_TYPE__ intmax_t; +TYPEDEF __UINT8_TYPE__ uint8_t; +TYPEDEF __UINT16_TYPE__ uint16_t; +TYPEDEF __UINT32_TYPE__ uint32_t; +TYPEDEF __UINT64_TYPE__ uint64_t; +TYPEDEF __UINT64_TYPE__ u_int64_t; +TYPEDEF __UINTMAX_TYPE__ uintmax_t; TYPEDEF unsigned mode_t; TYPEDEF unsigned _Reg nlink_t; diff --git a/system/lib/libc/musl/include/stdint.h b/system/lib/libc/musl/include/stdint.h index cb1991b44502c..8239d4703523a 100644 --- a/system/lib/libc/musl/include/stdint.h +++ b/system/lib/libc/musl/include/stdint.h @@ -19,23 +19,30 @@ #include -typedef int8_t int_fast8_t; -typedef int64_t int_fast64_t; +// XXX EMSCRIPTEN: This file has been modified from the upstream musl version +// to make use of clang pre-defined macros whereever possible, eliminating +// possible inconsistencies. + +typedef __INT_FAST8_TYPE__ int_fast8_t; +typedef __INT_FAST16_TYPE__ int_fast16_t; +typedef __INT_FAST32_TYPE__ int_fast32_t; +typedef __INT_FAST64_TYPE__ int_fast64_t; + +typedef __INT_LEAST8_TYPE__ int_least8_t; +typedef __INT_LEAST16_TYPE__ int_least16_t; +typedef __INT_LEAST32_TYPE__ int_least32_t; +typedef __INT_LEAST64_TYPE__ int_least64_t; + +typedef __UINT_FAST8_TYPE__ uint_fast8_t; +typedef __UINT_FAST16_TYPE__ uint_fast16_t; +typedef __UINT_FAST32_TYPE__ uint_fast32_t; +typedef __UINT_FAST64_TYPE__ uint_fast64_t; + +typedef __UINT_LEAST8_TYPE__ uint_least8_t; +typedef __UINT_LEAST16_TYPE__ uint_least16_t; +typedef __UINT_LEAST32_TYPE__ uint_least32_t; +typedef __UINT_LEAST64_TYPE__ uint_least64_t; -typedef int8_t int_least8_t; -typedef int16_t int_least16_t; -typedef int32_t int_least32_t; -typedef int64_t int_least64_t; - -typedef uint8_t uint_fast8_t; -typedef uint64_t uint_fast64_t; - -typedef uint8_t uint_least8_t; -typedef uint16_t uint_least16_t; -typedef uint32_t uint_least32_t; -typedef uint64_t uint_least64_t; - -#ifdef __EMSCRIPTEN__ #define INT8_MIN (-1-__INT8_MAX__) #define INT16_MIN (-1-__INT16_MAX__) #define INT32_MIN (-1-__INT32_MAX__) @@ -50,91 +57,66 @@ typedef uint64_t uint_least64_t; #define UINT16_MAX __UINT16_MAX__ #define UINT32_MAX __UINT32_MAX__ #define UINT64_MAX __UINT64_MAX__ -#else -#define INT8_MIN (-1-0x7f) -#define INT16_MIN (-1-0x7fff) -#define INT32_MIN (-1-0x7fffffff) -#define INT64_MIN (-1-0x7fffffffffffffff) - -#define INT8_MAX (0x7f) -#define INT16_MAX (0x7fff) -#define INT32_MAX (0x7fffffff) -#define INT64_MAX (0x7fffffffffffffff) - -#define UINT8_MAX (0xff) -#define UINT16_MAX (0xffff) -#define UINT32_MAX (0xffffffffu) -#define UINT64_MAX (0xffffffffffffffffu) -#endif - -#define INT_FAST8_MIN INT8_MIN -#define INT_FAST64_MIN INT64_MIN - -#define INT_LEAST8_MIN INT8_MIN -#define INT_LEAST16_MIN INT16_MIN -#define INT_LEAST32_MIN INT32_MIN -#define INT_LEAST64_MIN INT64_MIN - -#define INT_FAST8_MAX INT8_MAX -#define INT_FAST64_MAX INT64_MAX - -#define INT_LEAST8_MAX INT8_MAX -#define INT_LEAST16_MAX INT16_MAX -#define INT_LEAST32_MAX INT32_MAX -#define INT_LEAST64_MAX INT64_MAX - -#define UINT_FAST8_MAX UINT8_MAX -#define UINT_FAST64_MAX UINT64_MAX -#define UINT_LEAST8_MAX UINT8_MAX -#define UINT_LEAST16_MAX UINT16_MAX -#define UINT_LEAST32_MAX UINT32_MAX -#define UINT_LEAST64_MAX UINT64_MAX - -#define INTMAX_MIN INT64_MIN -#define INTMAX_MAX INT64_MAX -#define UINTMAX_MAX UINT64_MAX - -#define WINT_MIN 0U -#define WINT_MAX UINT32_MAX - -#if L'\0'-1 > 0 -#define WCHAR_MAX (0xffffffffu+L'\0') -#define WCHAR_MIN (0+L'\0') -#else -#define WCHAR_MAX (0x7fffffff+L'\0') -#define WCHAR_MIN (-1-0x7fffffff+L'\0') -#endif +#define INT_FAST8_MIN (-1-__INT_FAST8_MAX__) +#define INT_FAST16_MIN (-1-__INT_FAST16_MAX__) +#define INT_FAST32_MIN (-1-__INT_FAST32_MAX__) +#define INT_FAST64_MIN (-1-__INT_FAST64_MAX__) + +#define INT_LEAST8_MIN (-1-__INT_LEAST8_MAX__) +#define INT_LEAST16_MIN (-1-__INT_LEAST16_MAX__) +#define INT_LEAST32_MIN (-1-__INT_LEAST32_MAX__) +#define INT_LEAST64_MIN (-1-__INT_LEAST64_MAX__) + +#define INT_FAST8_MAX __INT_FAST8_MAX__ +#define INT_FAST16_MAX __INT_FAST16_MAX__ +#define INT_FAST32_MAX __INT_FAST32_MAX__ +#define INT_FAST64_MAX __INT_FAST64_MAX__ + +#define INT_LEAST8_MAX __INT_LEAST8_MAX__ +#define INT_LEAST16_MAX __INT_LEAST16_MAX__ +#define INT_LEAST32_MAX __INT_LEAST32_MAX__ +#define INT_LEAST64_MAX __INT_LEAST64_MAX__ + +#define UINT_FAST8_MAX __UINT_FAST8_MAX__ +#define UINT_FAST16_MAX __UINT_FAST16_MAX__ +#define UINT_FAST32_MAX __UINT_FAST32_MAX__ +#define UINT_FAST64_MAX __UINT_FAST64_MAX__ + +#define UINT_LEAST8_MAX __UINT_LEAST8_MAX__ +#define UINT_LEAST16_MAX __UINT_LEAST16_MAX__ +#define UINT_LEAST32_MAX __UINT_LEAST32_MAX__ +#define UINT_LEAST64_MAX __UINT_LEAST64_MAX__ + +#define INTMAX_MIN (-1-__INTMAX_MAX__) +#define INTMAX_MAX __INTMAX_MAX__ +#define UINTMAX_MAX __UINTMAX_MAX__ + +#define WCHAR_MAX __WCHAR_MAX__ +#define WINT_MAX __WINT_MAX__ +#define INTPTR_MAX __INTPTR_MAX__ +#define UINTPTR_MAX __UINTPTR_MAX__ +#define PTRDIFF_MAX __PTRDIFF_MAX__ +#define SIZE_MAX __SIZE_MAX__ + +#define WINT_MIN (-1-__WINT_MAX__) +#define WCHAR_MIN (-1-__WCHAR_MAX__) +#define INTPTR_MIN (-1-__INTPTR_MAX__) +#define PTRDIFF_MIN (-1-__PTRDIFF_MAX__) #define SIG_ATOMIC_MIN INT32_MIN #define SIG_ATOMIC_MAX INT32_MAX -#include - -#define INT8_C(c) c -#define INT16_C(c) c -#define INT32_C(c) c - -#define UINT8_C(c) c -#define UINT16_C(c) c -#define UINT32_C(c) c ## U - -// XXX EMSCRIPTEN: Use the clang-builtin macros here when available. -#ifdef __UINT64_C -#define INT64_C __INT64_C -#define UINT64_C __UINT64_C +#define INT8_C __INT8_C +#define INT16_C __INT16_C +#define INT32_C __INT32_C +#define INT64_C __INT64_C #define INTMAX_C __INTMAX_C + +#define UINT8_C __UINT8_C +#define UINT16_C __UINT16_C +#define UINT32_C __UINT32_C +#define UINT64_C __UINT64_C #define UINTMAX_C __UINTMAX_C -#elif UINTPTR_MAX == UINT64_MAX -#define INT64_C(c) c ## L -#define UINT64_C(c) c ## UL -#define INTMAX_C(c) c ## L -#define UINTMAX_C(c) c ## UL -#else -#define INT64_C(c) c ## LL -#define UINT64_C(c) c ## ULL -#define INTMAX_C(c) c ## LL -#define UINTMAX_C(c) c ## ULL -#endif #endif diff --git a/test/other/test_stdint_limits.64.out b/test/other/test_stdint_limits.64.out index 9b760133b633d..e6cbbbce71288 100644 --- a/test/other/test_stdint_limits.64.out +++ b/test/other/test_stdint_limits.64.out @@ -13,7 +13,7 @@ INTPTR_MAX: 9223372036854775807 WCHAR_MIN: -2147483648 WCHAR_MAX: 2147483647 WINT_MIN: 0 -WINT_MAX: 4294967295 +WINT_MAX: -1 UINTPTR_MAX: 18446744073709551615 SIZE_MAX: 18446744073709551615 SSIZE_MAX: 9223372036854775807 @@ -25,10 +25,34 @@ INT32_MIN: -2147483648 INT32_MAX: 2147483647 INT64_MIN: -9223372036854775808 INT64_MAX: 9223372036854775807 +INT_LEAST8_MIN: -128 +INT_LEAST8_MAX: 127 +INT_LEAST16_MIN: -32768 +INT_LEAST16_MAX: 32767 +INT_LEAST32_MIN: -2147483648 +INT_LEAST32_MAX: 2147483647 +INT_LEAST64_MIN: -9223372036854775808 +INT_LEAST64_MAX: 9223372036854775807 +INT_FAST8_MIN: -128 +INT_FAST8_MAX: 127 +INT_FAST16_MIN: -2147483648 +INT_FAST16_MAX: 2147483647 +INT_FAST32_MIN: -2147483648 +INT_FAST32_MAX: 2147483647 +INT_FAST64_MIN: -9223372036854775808 +INT_FAST64_MAX: 9223372036854775807 UINT8_MAX: 255 UINT16_MAX: 65535 UINT32_MAX: 4294967295 UINT64_MAX: 18446744073709551615 +UINT_LEAST8_MAX: 255 +UINT_LEAST16_MAX: 65535 +UINT_LEAST32_MAX: 4294967295 +UINT_LEAST64_MAX: 18446744073709551615 +UINT_FAST8_MAX: 255 +UINT_FAST16_MAX: 4294967295 +UINT_FAST32_MAX: 4294967295 +UINT_FAST64_MAX: 18446744073709551615 INT8_C: (d) 42 INT16_C: (d) 42 INT32_C: (d) 42 diff --git a/test/other/test_stdint_limits.c b/test/other/test_stdint_limits.c index d44aac591b315..3e69c3dc22339 100644 --- a/test/other/test_stdint_limits.c +++ b/test/other/test_stdint_limits.c @@ -26,8 +26,8 @@ int main () { printf("WCHAR_MIN: %d\n", WCHAR_MIN); printf("WCHAR_MAX: %d\n", WCHAR_MAX); - printf("WINT_MIN: %u\n", WINT_MIN); - printf("WINT_MAX: %u\n", WINT_MAX); + printf("WINT_MIN: %d\n", WINT_MIN); + printf("WINT_MAX: %d\n", WINT_MAX); printf("UINTPTR_MAX: %lu\n", UINTPTR_MAX); @@ -45,11 +45,39 @@ int main () { printf("INT64_MIN: %" PRId64 "\n", INT64_MIN); printf("INT64_MAX: %" PRId64 "\n", INT64_MAX); + printf("INT_LEAST8_MIN: %" PRId8 "\n", INT_LEAST8_MIN); + printf("INT_LEAST8_MAX: %" PRId8 "\n", INT_LEAST8_MAX); + printf("INT_LEAST16_MIN: %" PRId16 "\n", INT_LEAST16_MIN); + printf("INT_LEAST16_MAX: %" PRId16 "\n", INT_LEAST16_MAX); + printf("INT_LEAST32_MIN: %" PRId32 "\n", INT_LEAST32_MIN); + printf("INT_LEAST32_MAX: %" PRId32 "\n", INT_LEAST32_MAX); + printf("INT_LEAST64_MIN: %" PRId64 "\n", INT_LEAST64_MIN); + printf("INT_LEAST64_MAX: %" PRId64 "\n", INT_LEAST64_MAX); + + printf("INT_FAST8_MIN: %" PRId8 "\n", INT_FAST8_MIN); + printf("INT_FAST8_MAX: %" PRId8 "\n", INT_FAST8_MAX); + printf("INT_FAST16_MIN: %" PRId16 "\n", INT_FAST16_MIN); + printf("INT_FAST16_MAX: %" PRId16 "\n", INT_FAST16_MAX); + printf("INT_FAST32_MIN: %" PRId32 "\n", INT_FAST32_MIN); + printf("INT_FAST32_MAX: %" PRId32 "\n", INT_FAST32_MAX); + printf("INT_FAST64_MIN: %" PRId64 "\n", INT_FAST64_MIN); + printf("INT_FAST64_MAX: %" PRId64 "\n", INT_FAST64_MAX); + printf("UINT8_MAX: %" PRIu8 "\n", UINT8_MAX); printf("UINT16_MAX: %" PRIu16 "\n", UINT16_MAX); printf("UINT32_MAX: %" PRIu32 "\n", UINT32_MAX); printf("UINT64_MAX: %" PRIu64 "\n", UINT64_MAX); + printf("UINT_LEAST8_MAX: %" PRIu8 "\n", UINT_LEAST8_MAX); + printf("UINT_LEAST16_MAX: %" PRIu16 "\n", UINT_LEAST16_MAX); + printf("UINT_LEAST32_MAX: %" PRIu32 "\n", UINT_LEAST32_MAX); + printf("UINT_LEAST64_MAX: %" PRIu64 "\n", UINT_LEAST64_MAX); + + printf("UINT_FAST8_MAX: %" PRIu8 "\n", UINT_FAST8_MAX); + printf("UINT_FAST16_MAX: %" PRIu16 "\n", UINT_FAST16_MAX); + printf("UINT_FAST32_MAX: %" PRIu32 "\n", UINT_FAST32_MAX); + printf("UINT_FAST64_MAX: %" PRIu64 "\n", UINT_FAST64_MAX); + // Test macros for creating integer constants printf("INT8_C: (" PRId8 ") %" PRId8 "\n", INT8_C(42)); printf("INT16_C: (" PRId16 ") %" PRId16 "\n", INT16_C(42)); diff --git a/test/other/test_stdint_limits.out b/test/other/test_stdint_limits.out index d2a9c6852eb1e..1451549bfa74e 100644 --- a/test/other/test_stdint_limits.out +++ b/test/other/test_stdint_limits.out @@ -12,8 +12,8 @@ INTPTR_MIN: -2147483648 INTPTR_MAX: 2147483647 WCHAR_MIN: -2147483648 WCHAR_MAX: 2147483647 -WINT_MIN: 0 -WINT_MAX: 4294967295 +WINT_MIN: -2147483648 +WINT_MAX: 2147483647 UINTPTR_MAX: 4294967295 SIZE_MAX: 4294967295 SSIZE_MAX: 2147483647 @@ -25,10 +25,34 @@ INT32_MIN: -2147483648 INT32_MAX: 2147483647 INT64_MIN: -9223372036854775808 INT64_MAX: 9223372036854775807 +INT_LEAST8_MIN: -128 +INT_LEAST8_MAX: 127 +INT_LEAST16_MIN: -32768 +INT_LEAST16_MAX: 32767 +INT_LEAST32_MIN: -2147483648 +INT_LEAST32_MAX: 2147483647 +INT_LEAST64_MIN: -9223372036854775808 +INT_LEAST64_MAX: 9223372036854775807 +INT_FAST8_MIN: -128 +INT_FAST8_MAX: 127 +INT_FAST16_MIN: -32768 +INT_FAST16_MAX: 32767 +INT_FAST32_MIN: -2147483648 +INT_FAST32_MAX: 2147483647 +INT_FAST64_MIN: -9223372036854775808 +INT_FAST64_MAX: 9223372036854775807 UINT8_MAX: 255 UINT16_MAX: 65535 UINT32_MAX: 4294967295 UINT64_MAX: 18446744073709551615 +UINT_LEAST8_MAX: 255 +UINT_LEAST16_MAX: 65535 +UINT_LEAST32_MAX: 4294967295 +UINT_LEAST64_MAX: 18446744073709551615 +UINT_FAST8_MAX: 255 +UINT_FAST16_MAX: 65535 +UINT_FAST32_MAX: 4294967295 +UINT_FAST64_MAX: 18446744073709551615 INT8_C: (d) 42 INT16_C: (d) 42 INT32_C: (d) 42