From dc323f3bee318514af9d7c6fed1e01c712bc71ae Mon Sep 17 00:00:00 2001 From: Rasmus Villemoes Date: Mon, 7 Jul 2025 22:36:53 +0200 Subject: move more limits from kernel.h to limits.h and standardize their definitions In a customer project that was building a stand-alone application, I hit a problem related to the fact that our LONG_MAX and friends are not standards-compliant, in that they are not "suitable for use in #if preprocessing directives" ... /toolchain_none/arm-cortexa8-eabi/sys-include/machine/_default_types.h:25:31: error: missing binary operator before token "long" 25 | || ( defined(LLONG_MAX) && (LLONG_MAX > 0x7fffffff) ) | ^~~~~~~~~ So following up on commit 13de8483388 ("mbedtls: add mbedtls into the build system"), move the rest of the macros associated to the standard C types {signed,unsigned} {char, short, int, long, long long} (and of course bare 'char') to limits.h. Make use of the fact that both gcc and clang provide suitable predefined __FOO_MAX__ macros for the signed types, and use a standard scheme for defining the FOO_MIN and UFOO_MAX macros in terms of FOO_MAX. Note that suffixes like L and ULL are allowed for preprocessor integers; it is (casts) which are not. And using appropriate suffixes, we can arrange for the type of e.g. UINT_MAX to be "unsigned int" due to integer promotion rules. Signed-off-by: Rasmus Villemoes --- include/linux/kernel.h | 11 ----------- 1 file changed, 11 deletions(-) (limited to 'include/linux/kernel.h') diff --git a/include/linux/kernel.h b/include/linux/kernel.h index 9467edd65ab..f26274fbe1d 100644 --- a/include/linux/kernel.h +++ b/include/linux/kernel.h @@ -5,17 +5,6 @@ #include /* for printf/pr_* utilities */ #include -#define USHRT_MAX ((u16)(~0U)) -#define SHRT_MAX ((s16)(USHRT_MAX>>1)) -#define SHRT_MIN ((s16)(-SHRT_MAX - 1)) -#define INT_MIN (-INT_MAX - 1) -#define LONG_MAX ((long)(~0UL>>1)) -#define LONG_MIN (-LONG_MAX - 1) -#define ULONG_MAX (~0UL) -#define LLONG_MAX ((long long)(~0ULL>>1)) -#define LLONG_MIN (-LLONG_MAX - 1) -#define ULLONG_MAX (~0ULL) - #define U8_MAX ((u8)~0U) #define S8_MAX ((s8)(U8_MAX>>1)) #define S8_MIN ((s8)(-S8_MAX - 1)) -- cgit v1.2.3 From 0604595c16a6bf3c0aed131b80d1a25d5d74057d Mon Sep 17 00:00:00 2001 From: Rasmus Villemoes Date: Mon, 7 Jul 2025 22:36:54 +0200 Subject: move limits for sNN/uNN types from kernel.h to limits.h Since we define the {s,u}{8,16,32,64} types the same way on all architectures, i.e. everybody uses asm-generic/int-ll64.h, we can just define the associated limit macros in terms of those for the corresponding types. This eliminates another set of limit macros that are not usable in #if conditionals. These type names and macros are not C or POSIX, so there's no language violation, but certainly a violation of developers' reasonable expectations. Signed-off-by: Rasmus Villemoes --- include/linux/kernel.h | 13 ------------- 1 file changed, 13 deletions(-) (limited to 'include/linux/kernel.h') diff --git a/include/linux/kernel.h b/include/linux/kernel.h index f26274fbe1d..d3d979834ae 100644 --- a/include/linux/kernel.h +++ b/include/linux/kernel.h @@ -5,19 +5,6 @@ #include /* for printf/pr_* utilities */ #include -#define U8_MAX ((u8)~0U) -#define S8_MAX ((s8)(U8_MAX>>1)) -#define S8_MIN ((s8)(-S8_MAX - 1)) -#define U16_MAX ((u16)~0U) -#define S16_MAX ((s16)(U16_MAX>>1)) -#define S16_MIN ((s16)(-S16_MAX - 1)) -#define U32_MAX ((u32)~0U) -#define S32_MAX ((s32)(U32_MAX>>1)) -#define S32_MIN ((s32)(-S32_MAX - 1)) -#define U64_MAX ((u64)~0ULL) -#define S64_MAX ((s64)(U64_MAX>>1)) -#define S64_MIN ((s64)(-S64_MAX - 1)) - #define INT32_MAX S32_MAX #define STACK_MAGIC 0xdeadbeef -- cgit v1.2.3 From dd260d95fd26deedc64f076b554759d09420f006 Mon Sep 17 00:00:00 2001 From: Rasmus Villemoes Date: Mon, 7 Jul 2025 22:36:55 +0200 Subject: limits.h: provide all limit macros for standard [u]intNN_t types Currently, we only have UINT32_MAX and UINT64_MAX in limits.h, and then stdint.h and kernel.h somewhat randomly define UINT8_MAX and INT32_MAX, respectively. Provide a full set of definitions in terms of the min/max macros for the types that [u]intNN_t are defined in terms of, namely the {s,u}NN ones. Try to avoid breaking whatever depended on getting UINT8_MAX from our compat stdint.h by replacing it with an include of limits.h. Signed-off-by: Rasmus Villemoes --- include/linux/kernel.h | 2 -- 1 file changed, 2 deletions(-) (limited to 'include/linux/kernel.h') diff --git a/include/linux/kernel.h b/include/linux/kernel.h index d3d979834ae..e0443ecac84 100644 --- a/include/linux/kernel.h +++ b/include/linux/kernel.h @@ -5,8 +5,6 @@ #include /* for printf/pr_* utilities */ #include -#define INT32_MAX S32_MAX - #define STACK_MAGIC 0xdeadbeef #define REPEAT_BYTE(x) ((~0ul / 0xff) * (x)) -- cgit v1.2.3