1 /* A GNU-like <limits.h>.
3 Copyright 2016-2024 Free Software Foundation, Inc.
5 This file is free software: you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as
7 published by the Free Software Foundation; either version 2.1 of the
8 License, or (at your option) any later version.
10 This file is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License
16 along with this program. If not, see <https://www.gnu.org/licenses/>. */
19 @PRAGMA_SYSTEM_HEADER@
23 #if defined _GL_ALREADY_INCLUDING_LIMITS_H
24 /* Special invocation convention:
25 On Haiku/x86_64, we have a sequence of nested includes
26 <limits.h> -> <syslimits.h> -> <limits.h>.
27 In this situation, LONG_MAX and INT_MAX are not yet defined,
28 therefore we should not attempt to define LONG_BIT. */
30 #@INCLUDE_NEXT@ @NEXT_LIMITS_H@
33 /* Normal invocation convention. */
35 #ifndef _@GUARD_PREFIX@_LIMITS_H
37 # define _GL_ALREADY_INCLUDING_LIMITS_H
39 /* The include_next requires a split double-inclusion guard. */
40 # @INCLUDE_NEXT@ @NEXT_LIMITS_H@
42 # undef _GL_ALREADY_INCLUDING_LIMITS_H
44 #ifndef _@GUARD_PREFIX@_LIMITS_H
45 #define _@GUARD_PREFIX@_LIMITS_H
48 # if defined LONG_LONG_MIN /* HP-UX 11.31 */
49 # define LLONG_MIN LONG_LONG_MIN
50 # elif defined LONGLONG_MIN /* IRIX 6.5 */
51 # define LLONG_MIN LONGLONG_MIN
52 # elif defined __GNUC__
53 # define LLONG_MIN (- __LONG_LONG_MAX__ - 1LL)
57 # if defined LONG_LONG_MAX /* HP-UX 11.31 */
58 # define LLONG_MAX LONG_LONG_MAX
59 # elif defined LONGLONG_MAX /* IRIX 6.5 */
60 # define LLONG_MAX LONGLONG_MAX
61 # elif defined __GNUC__
62 # define LLONG_MAX __LONG_LONG_MAX__
66 # if defined ULONG_LONG_MAX /* HP-UX 11.31 */
67 # define ULLONG_MAX ULONG_LONG_MAX
68 # elif defined ULONGLONG_MAX /* IRIX 6.5 */
69 # define ULLONG_MAX ULONGLONG_MAX
70 # elif defined __GNUC__
71 # define ULLONG_MAX (__LONG_LONG_MAX__ * 2ULL + 1ULL)
75 /* The number of usable bits in an unsigned or signed integer type
76 with minimum value MIN and maximum value MAX, as an int expression
77 suitable in #if. Cover all known practical hosts. This
78 implementation exploits the fact that MAX is 1 less than a power of
79 2, and merely counts the number of 1 bits in MAX; "COBn" means
80 "count the number of 1 bits in the low-order n bits"). */
81 #define _GL_INTEGER_WIDTH(min, max) (((min) < 0) + _GL_COB128 (max))
82 #define _GL_COB128(n) (_GL_COB64 ((n) >> 31 >> 31 >> 2) + _GL_COB64 (n))
83 #define _GL_COB64(n) (_GL_COB32 ((n) >> 31 >> 1) + _GL_COB32 (n))
84 #define _GL_COB32(n) (_GL_COB16 ((n) >> 16) + _GL_COB16 (n))
85 #define _GL_COB16(n) (_GL_COB8 ((n) >> 8) + _GL_COB8 (n))
86 #define _GL_COB8(n) (_GL_COB4 ((n) >> 4) + _GL_COB4 (n))
87 #define _GL_COB4(n) (!!((n) & 8) + !!((n) & 4) + !!((n) & 2) + !!((n) & 1))
90 /* Assume 'int' is 32 bits wide. */
94 /* Assume 'long' is 32 or 64 bits wide. */
95 # if LONG_MAX == INT_MAX
102 /* Assume no multibyte character is longer than 16 bytes. */
104 # define MB_LEN_MAX 16
107 /* Macros specified by C23 and by ISO/IEC TS 18661-1:2014. */
109 #if (! defined ULLONG_WIDTH \
110 && (defined _GNU_SOURCE || defined __STDC_WANT_IEC_60559_BFP_EXT__ \
111 || (defined __STDC_VERSION__ && 201710 < __STDC_VERSION__)))
112 # define CHAR_WIDTH _GL_INTEGER_WIDTH (CHAR_MIN, CHAR_MAX)
113 # define SCHAR_WIDTH _GL_INTEGER_WIDTH (SCHAR_MIN, SCHAR_MAX)
114 # define UCHAR_WIDTH _GL_INTEGER_WIDTH (0, UCHAR_MAX)
115 # define SHRT_WIDTH _GL_INTEGER_WIDTH (SHRT_MIN, SHRT_MAX)
116 # define USHRT_WIDTH _GL_INTEGER_WIDTH (0, USHRT_MAX)
117 # define INT_WIDTH _GL_INTEGER_WIDTH (INT_MIN, INT_MAX)
118 # define UINT_WIDTH _GL_INTEGER_WIDTH (0, UINT_MAX)
119 # define LONG_WIDTH _GL_INTEGER_WIDTH (LONG_MIN, LONG_MAX)
120 # define ULONG_WIDTH _GL_INTEGER_WIDTH (0, ULONG_MAX)
121 # define LLONG_WIDTH _GL_INTEGER_WIDTH (LLONG_MIN, LLONG_MAX)
122 # define ULLONG_WIDTH _GL_INTEGER_WIDTH (0, ULLONG_MAX)
125 /* Macros specified by C23. */
127 #if (defined _GNU_SOURCE \
128 || (defined __STDC_VERSION__ && 201710 < __STDC_VERSION__))
129 # if ! defined BOOL_WIDTH
130 # define BOOL_WIDTH 1
132 # elif ! defined BOOL_MAX
137 /* Macro specified by POSIX. */
139 /* The maximum ssize_t value. Although it might not be of ssize_t type
140 as it should be, it's too much trouble to fix this minor detail. */
143 # define SSIZE_MAX LLONG_MAX
145 # define SSIZE_MAX LONG_MAX
149 #endif /* _@GUARD_PREFIX@_LIMITS_H */
150 #endif /* _@GUARD_PREFIX@_LIMITS_H */