]> git.ipfire.org Git - thirdparty/u-boot.git/commitdiff
move more limits from kernel.h to limits.h and standardize their definitions
authorRasmus Villemoes <ravi@prevas.dk>
Mon, 7 Jul 2025 20:36:53 +0000 (22:36 +0200)
committerTom Rini <trini@konsulko.com>
Mon, 14 Jul 2025 18:43:28 +0000 (12:43 -0600)
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 <ravi@prevas.dk>
include/limits.h
include/linux/kernel.h

index 1d0bbf69be7afad0d6cd1fae6b20dc6cba15ef9e..4dcfbe9b15665b68145db1ee3cf588aff45b6d85 100644 (file)
@@ -3,8 +3,34 @@
 #ifndef _LIMITS_H
 #define _LIMITS_H
 
-#define INT_MAX     0x7fffffff
-#define UINT_MAX    0xffffffffU
+#define SCHAR_MAX   __SCHAR_MAX__
+#define SCHAR_MIN   (-SCHAR_MAX - 1)
+#define UCHAR_MAX   (SCHAR_MAX * 2 + 1)
+
+#ifdef __CHAR_UNSIGNED__
+#define CHAR_MAX    UCHAR_MAX
+#define CHAR_MIN    0
+#else
+#define CHAR_MAX    SCHAR_MAX
+#define CHAR_MIN    SCHAR_MIN
+#endif
+
+#define SHRT_MAX    __SHRT_MAX__
+#define SHRT_MIN    (-SHRT_MAX - 1)
+#define USHRT_MAX   (SHRT_MAX * 2 + 1)
+
+#define INT_MAX     __INT_MAX__
+#define INT_MIN     (-INT_MAX - 1)
+#define UINT_MAX    (INT_MAX * 2U + 1U)
+
+#define LONG_MAX    __LONG_MAX__
+#define LONG_MIN    (-LONG_MAX - 1L)
+#define ULONG_MAX   (LONG_MAX * 2UL + 1UL)
+
+#define LLONG_MAX   __LONG_LONG_MAX__
+#define LLONG_MIN   (-LLONG_MAX - 1LL)
+#define ULLONG_MAX  (LLONG_MAX * 2ULL + 1ULL)
+
 #define CHAR_BIT    8
 #define UINT32_MAX  0xffffffffU
 #define UINT64_MAX  0xffffffffffffffffULL
index 9467edd65abbf98bd04e539af175c297b4731f35..f26274fbe1d247bfb304927f818e5c8fa70e7dc6 100644 (file)
@@ -5,17 +5,6 @@
 #include <linux/printk.h> /* for printf/pr_* utilities */
 #include <limits.h>
 
-#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))