xref: /freebsd/contrib/llvm-project/clang/lib/Headers/limits.h (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
10b57cec5SDimitry Andric /*===---- limits.h - Standard header for integer sizes --------------------===*\
20b57cec5SDimitry Andric  *
30b57cec5SDimitry Andric  * Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric  * See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric  * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric  *
70b57cec5SDimitry Andric \*===----------------------------------------------------------------------===*/
80b57cec5SDimitry Andric 
90b57cec5SDimitry Andric #ifndef __CLANG_LIMITS_H
100b57cec5SDimitry Andric #define __CLANG_LIMITS_H
110b57cec5SDimitry Andric 
12*0fca6ea1SDimitry Andric #if defined(__MVS__) && __has_include_next(<limits.h>)
13*0fca6ea1SDimitry Andric #include_next <limits.h>
14*0fca6ea1SDimitry Andric #else
15*0fca6ea1SDimitry Andric 
160b57cec5SDimitry Andric /* The system's limits.h may, in turn, try to #include_next GCC's limits.h.
170b57cec5SDimitry Andric    Avert this #include_next madness. */
180b57cec5SDimitry Andric #if defined __GNUC__ && !defined _GCC_LIMITS_H_
190b57cec5SDimitry Andric #define _GCC_LIMITS_H_
200b57cec5SDimitry Andric #endif
210b57cec5SDimitry Andric 
220b57cec5SDimitry Andric /* System headers include a number of constants from POSIX in <limits.h>.
230b57cec5SDimitry Andric    Include it if we're hosted. */
240b57cec5SDimitry Andric #if __STDC_HOSTED__ && __has_include_next(<limits.h>)
250b57cec5SDimitry Andric #include_next <limits.h>
260b57cec5SDimitry Andric #endif
270b57cec5SDimitry Andric 
280b57cec5SDimitry Andric /* Many system headers try to "help us out" by defining these.  No really, we
290b57cec5SDimitry Andric    know how big each datatype is. */
300b57cec5SDimitry Andric #undef  SCHAR_MIN
310b57cec5SDimitry Andric #undef  SCHAR_MAX
320b57cec5SDimitry Andric #undef  UCHAR_MAX
330b57cec5SDimitry Andric #undef  SHRT_MIN
340b57cec5SDimitry Andric #undef  SHRT_MAX
350b57cec5SDimitry Andric #undef  USHRT_MAX
360b57cec5SDimitry Andric #undef  INT_MIN
370b57cec5SDimitry Andric #undef  INT_MAX
380b57cec5SDimitry Andric #undef  UINT_MAX
390b57cec5SDimitry Andric #undef  LONG_MIN
400b57cec5SDimitry Andric #undef  LONG_MAX
410b57cec5SDimitry Andric #undef  ULONG_MAX
420b57cec5SDimitry Andric 
430b57cec5SDimitry Andric #undef  CHAR_BIT
440b57cec5SDimitry Andric #undef  CHAR_MIN
450b57cec5SDimitry Andric #undef  CHAR_MAX
460b57cec5SDimitry Andric 
470b57cec5SDimitry Andric /* C90/99 5.2.4.2.1 */
480b57cec5SDimitry Andric #define SCHAR_MAX __SCHAR_MAX__
490b57cec5SDimitry Andric #define SHRT_MAX  __SHRT_MAX__
500b57cec5SDimitry Andric #define INT_MAX   __INT_MAX__
510b57cec5SDimitry Andric #define LONG_MAX  __LONG_MAX__
520b57cec5SDimitry Andric 
530b57cec5SDimitry Andric #define SCHAR_MIN (-__SCHAR_MAX__-1)
540b57cec5SDimitry Andric #define SHRT_MIN  (-__SHRT_MAX__ -1)
550b57cec5SDimitry Andric #define INT_MIN   (-__INT_MAX__  -1)
560b57cec5SDimitry Andric #define LONG_MIN  (-__LONG_MAX__ -1L)
570b57cec5SDimitry Andric 
580b57cec5SDimitry Andric #define UCHAR_MAX (__SCHAR_MAX__*2  +1)
5906c3fb27SDimitry Andric #if __SHRT_WIDTH__ < __INT_WIDTH__
600b57cec5SDimitry Andric #define USHRT_MAX (__SHRT_MAX__ * 2 + 1)
6106c3fb27SDimitry Andric #else
6206c3fb27SDimitry Andric #define USHRT_MAX (__SHRT_MAX__ * 2U + 1U)
6306c3fb27SDimitry Andric #endif
640b57cec5SDimitry Andric #define UINT_MAX  (__INT_MAX__  *2U +1U)
650b57cec5SDimitry Andric #define ULONG_MAX (__LONG_MAX__ *2UL+1UL)
660b57cec5SDimitry Andric 
670b57cec5SDimitry Andric #ifndef MB_LEN_MAX
680b57cec5SDimitry Andric #define MB_LEN_MAX 1
690b57cec5SDimitry Andric #endif
700b57cec5SDimitry Andric 
710b57cec5SDimitry Andric #define CHAR_BIT  __CHAR_BIT__
720b57cec5SDimitry Andric 
735f757f3fSDimitry Andric /* C23 5.2.4.2.1 */
745f757f3fSDimitry Andric #if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 202311L
7504eeddc0SDimitry Andric #define BOOL_WIDTH   __BOOL_WIDTH__
7604eeddc0SDimitry Andric #define CHAR_WIDTH   CHAR_BIT
7704eeddc0SDimitry Andric #define SCHAR_WIDTH  CHAR_BIT
7804eeddc0SDimitry Andric #define UCHAR_WIDTH  CHAR_BIT
7904eeddc0SDimitry Andric #define USHRT_WIDTH  __SHRT_WIDTH__
8004eeddc0SDimitry Andric #define SHRT_WIDTH   __SHRT_WIDTH__
8104eeddc0SDimitry Andric #define UINT_WIDTH   __INT_WIDTH__
8204eeddc0SDimitry Andric #define INT_WIDTH    __INT_WIDTH__
8304eeddc0SDimitry Andric #define ULONG_WIDTH  __LONG_WIDTH__
8404eeddc0SDimitry Andric #define LONG_WIDTH   __LONG_WIDTH__
8504eeddc0SDimitry Andric #define ULLONG_WIDTH __LLONG_WIDTH__
8604eeddc0SDimitry Andric #define LLONG_WIDTH  __LLONG_WIDTH__
871fd87a68SDimitry Andric 
881fd87a68SDimitry Andric #define BITINT_MAXWIDTH __BITINT_MAXWIDTH__
8904eeddc0SDimitry Andric #endif
9004eeddc0SDimitry Andric 
910b57cec5SDimitry Andric #ifdef __CHAR_UNSIGNED__  /* -funsigned-char */
920b57cec5SDimitry Andric #define CHAR_MIN 0
930b57cec5SDimitry Andric #define CHAR_MAX UCHAR_MAX
940b57cec5SDimitry Andric #else
950b57cec5SDimitry Andric #define CHAR_MIN SCHAR_MIN
960b57cec5SDimitry Andric #define CHAR_MAX __SCHAR_MAX__
970b57cec5SDimitry Andric #endif
980b57cec5SDimitry Andric 
990b57cec5SDimitry Andric /* C99 5.2.4.2.1: Added long long.
1000b57cec5SDimitry Andric    C++11 18.3.3.2: same contents as the Standard C Library header <limits.h>.
1010b57cec5SDimitry Andric  */
102bdd1243dSDimitry Andric #if (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L) ||              \
103bdd1243dSDimitry Andric     (defined(__cplusplus) && __cplusplus >= 201103L)
1040b57cec5SDimitry Andric 
1050b57cec5SDimitry Andric #undef  LLONG_MIN
1060b57cec5SDimitry Andric #undef  LLONG_MAX
1070b57cec5SDimitry Andric #undef  ULLONG_MAX
1080b57cec5SDimitry Andric 
1090b57cec5SDimitry Andric #define LLONG_MAX  __LONG_LONG_MAX__
1100b57cec5SDimitry Andric #define LLONG_MIN  (-__LONG_LONG_MAX__-1LL)
1110b57cec5SDimitry Andric #define ULLONG_MAX (__LONG_LONG_MAX__*2ULL+1ULL)
1120b57cec5SDimitry Andric #endif
1130b57cec5SDimitry Andric 
1140b57cec5SDimitry Andric /* LONG_LONG_MIN/LONG_LONG_MAX/ULONG_LONG_MAX are a GNU extension.  It's too bad
1150b57cec5SDimitry Andric    that we don't have something like #pragma poison that could be used to
1160b57cec5SDimitry Andric    deprecate a macro - the code should just use LLONG_MAX and friends.
1170b57cec5SDimitry Andric  */
1180b57cec5SDimitry Andric #if defined(__GNU_LIBRARY__) ? defined(__USE_GNU) : !defined(__STRICT_ANSI__)
1190b57cec5SDimitry Andric 
1200b57cec5SDimitry Andric #undef   LONG_LONG_MIN
1210b57cec5SDimitry Andric #undef   LONG_LONG_MAX
1220b57cec5SDimitry Andric #undef   ULONG_LONG_MAX
1230b57cec5SDimitry Andric 
1240b57cec5SDimitry Andric #define LONG_LONG_MAX  __LONG_LONG_MAX__
1250b57cec5SDimitry Andric #define LONG_LONG_MIN  (-__LONG_LONG_MAX__-1LL)
1260b57cec5SDimitry Andric #define ULONG_LONG_MAX (__LONG_LONG_MAX__*2ULL+1ULL)
1270b57cec5SDimitry Andric #endif
1280b57cec5SDimitry Andric 
129*0fca6ea1SDimitry Andric #endif /* __MVS__ */
1300b57cec5SDimitry Andric #endif /* __CLANG_LIMITS_H */
131