xref: /freebsd/contrib/llvm-project/clang/lib/Headers/limits.h (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
1 /*===---- limits.h - Standard header for integer sizes --------------------===*\
2  *
3  * Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4  * See https://llvm.org/LICENSE.txt for license information.
5  * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6  *
7 \*===----------------------------------------------------------------------===*/
8 
9 #ifndef __CLANG_LIMITS_H
10 #define __CLANG_LIMITS_H
11 
12 #if defined(__MVS__) && __has_include_next(<limits.h>)
13 #include_next <limits.h>
14 #else
15 
16 /* The system's limits.h may, in turn, try to #include_next GCC's limits.h.
17    Avert this #include_next madness. */
18 #if defined __GNUC__ && !defined _GCC_LIMITS_H_
19 #define _GCC_LIMITS_H_
20 #endif
21 
22 /* System headers include a number of constants from POSIX in <limits.h>.
23    Include it if we're hosted. */
24 #if __STDC_HOSTED__ && __has_include_next(<limits.h>)
25 #include_next <limits.h>
26 #endif
27 
28 /* Many system headers try to "help us out" by defining these.  No really, we
29    know how big each datatype is. */
30 #undef  SCHAR_MIN
31 #undef  SCHAR_MAX
32 #undef  UCHAR_MAX
33 #undef  SHRT_MIN
34 #undef  SHRT_MAX
35 #undef  USHRT_MAX
36 #undef  INT_MIN
37 #undef  INT_MAX
38 #undef  UINT_MAX
39 #undef  LONG_MIN
40 #undef  LONG_MAX
41 #undef  ULONG_MAX
42 
43 #undef  CHAR_BIT
44 #undef  CHAR_MIN
45 #undef  CHAR_MAX
46 
47 /* C90/99 5.2.4.2.1 */
48 #define SCHAR_MAX __SCHAR_MAX__
49 #define SHRT_MAX  __SHRT_MAX__
50 #define INT_MAX   __INT_MAX__
51 #define LONG_MAX  __LONG_MAX__
52 
53 #define SCHAR_MIN (-__SCHAR_MAX__-1)
54 #define SHRT_MIN  (-__SHRT_MAX__ -1)
55 #define INT_MIN   (-__INT_MAX__  -1)
56 #define LONG_MIN  (-__LONG_MAX__ -1L)
57 
58 #define UCHAR_MAX (__SCHAR_MAX__*2  +1)
59 #if __SHRT_WIDTH__ < __INT_WIDTH__
60 #define USHRT_MAX (__SHRT_MAX__ * 2 + 1)
61 #else
62 #define USHRT_MAX (__SHRT_MAX__ * 2U + 1U)
63 #endif
64 #define UINT_MAX  (__INT_MAX__  *2U +1U)
65 #define ULONG_MAX (__LONG_MAX__ *2UL+1UL)
66 
67 #ifndef MB_LEN_MAX
68 #define MB_LEN_MAX 1
69 #endif
70 
71 #define CHAR_BIT  __CHAR_BIT__
72 
73 /* C23 5.2.4.2.1 */
74 #if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 202311L
75 #define BOOL_WIDTH   __BOOL_WIDTH__
76 #define CHAR_WIDTH   CHAR_BIT
77 #define SCHAR_WIDTH  CHAR_BIT
78 #define UCHAR_WIDTH  CHAR_BIT
79 #define USHRT_WIDTH  __SHRT_WIDTH__
80 #define SHRT_WIDTH   __SHRT_WIDTH__
81 #define UINT_WIDTH   __INT_WIDTH__
82 #define INT_WIDTH    __INT_WIDTH__
83 #define ULONG_WIDTH  __LONG_WIDTH__
84 #define LONG_WIDTH   __LONG_WIDTH__
85 #define ULLONG_WIDTH __LLONG_WIDTH__
86 #define LLONG_WIDTH  __LLONG_WIDTH__
87 
88 #define BITINT_MAXWIDTH __BITINT_MAXWIDTH__
89 #endif
90 
91 #ifdef __CHAR_UNSIGNED__  /* -funsigned-char */
92 #define CHAR_MIN 0
93 #define CHAR_MAX UCHAR_MAX
94 #else
95 #define CHAR_MIN SCHAR_MIN
96 #define CHAR_MAX __SCHAR_MAX__
97 #endif
98 
99 /* C99 5.2.4.2.1: Added long long.
100    C++11 18.3.3.2: same contents as the Standard C Library header <limits.h>.
101  */
102 #if (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L) ||              \
103     (defined(__cplusplus) && __cplusplus >= 201103L)
104 
105 #undef  LLONG_MIN
106 #undef  LLONG_MAX
107 #undef  ULLONG_MAX
108 
109 #define LLONG_MAX  __LONG_LONG_MAX__
110 #define LLONG_MIN  (-__LONG_LONG_MAX__-1LL)
111 #define ULLONG_MAX (__LONG_LONG_MAX__*2ULL+1ULL)
112 #endif
113 
114 /* LONG_LONG_MIN/LONG_LONG_MAX/ULONG_LONG_MAX are a GNU extension.  It's too bad
115    that we don't have something like #pragma poison that could be used to
116    deprecate a macro - the code should just use LLONG_MAX and friends.
117  */
118 #if defined(__GNU_LIBRARY__) ? defined(__USE_GNU) : !defined(__STRICT_ANSI__)
119 
120 #undef   LONG_LONG_MIN
121 #undef   LONG_LONG_MAX
122 #undef   ULONG_LONG_MAX
123 
124 #define LONG_LONG_MAX  __LONG_LONG_MAX__
125 #define LONG_LONG_MIN  (-__LONG_LONG_MAX__-1LL)
126 #define ULONG_LONG_MAX (__LONG_LONG_MAX__*2ULL+1ULL)
127 #endif
128 
129 #endif /* __MVS__ */
130 #endif /* __CLANG_LIMITS_H */
131