@@ -36,6 +36,10 @@ Version 2.25
fesetexcept, fetestexceptflag, fegetmode and fesetmode functions,
the femode_t type and the FE_DFL_MODE macro.
+* Integer width macros from TS 18661-1:2014 are added to <limits.h>:
+ CHAR_WIDTH, SCHAR_WIDTH, UCHAR_WIDTH, SHRT_WIDTH, USHRT_WIDTH, INT_WIDTH,
+ UINT_WIDTH, LONG_WIDTH, ULONG_WIDTH, LLONG_WIDTH, ULLONG_WIDTH.
+
* The <sys/quota.h> header now includes the <linux/quota.h> header. Support
for the Linux quota interface which predates kernel version 2.4.22 has
been removed.
@@ -22,7 +22,8 @@
#ifndef _LIBC_LIMITS_H_
#define _LIBC_LIMITS_H_ 1
-#include <features.h>
+#define __GLIBC_INTERNAL_STARTING_HEADER_IMPLEMENTATION
+#include <bits/libc-header-start.h>
/* Maximum length of any multibyte character in any locale.
@@ -138,6 +139,54 @@
# endif
#endif
+/* The integer width macros are not defined by GCC's <limits.h> before
+ GCC 7, or if _GNU_SOURCE rather than
+ __STDC_WANT_IEC_60559_BFP_EXT__ is used to enable this feature. */
+#if __GLIBC_USE (IEC_60559_BFP_EXT)
+# ifndef CHAR_WIDTH
+# define CHAR_WIDTH 8
+# endif
+# ifndef SCHAR_WIDTH
+# define SCHAR_WIDTH 8
+# endif
+# ifndef UCHAR_WIDTH
+# define UCHAR_WIDTH 8
+# endif
+# ifndef SHRT_WIDTH
+# define SHRT_WIDTH 16
+# endif
+# ifndef USHRT_WIDTH
+# define USHRT_WIDTH 16
+# endif
+# ifndef INT_WIDTH
+# define INT_WIDTH 32
+# endif
+# ifndef UINT_WIDTH
+# define UINT_WIDTH 32
+# endif
+# if LONG_MAX == 0x7fffffffL
+# ifndef LONG_WIDTH
+# define LONG_WIDTH 32
+# endif
+# ifndef ULONG_WIDTH
+# define ULONG_WIDTH 32
+# endif
+# else
+# ifndef LONG_WIDTH
+# define LONG_WIDTH 64
+# endif
+# ifndef ULONG_WIDTH
+# define ULONG_WIDTH 64
+# endif
+# endif
+# ifndef LLONG_WIDTH
+# define LLONG_WIDTH 64
+# endif
+# ifndef ULLONG_WIDTH
+# define ULLONG_WIDTH 64
+# endif
+#endif /* Use IEC_60559_BFP_EXT. */
+
#ifdef __USE_POSIX
/* POSIX adds things to <limits.h>. */
# include <bits/posix1_lim.h>
@@ -653,6 +653,56 @@ sizeof (@var{type}) * CHAR_BIT
@end smallexample
@end table
+That expression includes padding bits as well as value and sign bits.
+On all systems supported by @theglibc{}, standard integer types do not
+have any padding bits. TS 18661-1:2014 defines additional macros for
+the width of integer types (the number of value and sign bits); these
+macros can also be used in @code{#if} preprocessor directives, whereas
+@code{sizeof} cannot. The following macros are defined in
+@file{limits.h}.
+
+@table @code
+@comment limits.h
+@comment ISO
+@item CHAR_WIDTH
+@comment limits.h
+@comment ISO
+@itemx SCHAR_WIDTH
+@comment limits.h
+@comment ISO
+@itemx UCHAR_WIDTH
+@comment limits.h
+@comment ISO
+@itemx SHRT_WIDTH
+@comment limits.h
+@comment ISO
+@itemx USHRT_WIDTH
+@comment limits.h
+@comment ISO
+@itemx INT_WIDTH
+@comment limits.h
+@comment ISO
+@itemx UINT_WIDTH
+@comment limits.h
+@comment ISO
+@itemx LONG_WIDTH
+@comment limits.h
+@comment ISO
+@itemx ULONG_WIDTH
+@comment limits.h
+@comment ISO
+@itemx LLONG_WIDTH
+@comment limits.h
+@comment ISO
+@itemx ULLONG_WIDTH
+
+These are the widths of the types @code{char}, @code{signed char},
+@code{unsigned char}, @code{short int}, @code{unsigned short int},
+@code{int}, @code{unsigned int}, @code{long int}, @code{unsigned long
+int}, @code{long long int} and @code{unsigned long long int},
+respectively.
+@end table
+
@node Range of Type
@subsection Range of an Integer Type
@cindex integer type range
@@ -77,7 +77,7 @@ tests := tst-strtol tst-strtod testmb testrand testsort testdiv \
tst-tininess tst-strtod-underflow tst-tls-atexit \
tst-setcontext3 tst-tls-atexit-nodelete \
tst-strtol-locale tst-strtod-nan-locale tst-strfmon_l \
- tst-quick_exit tst-thread-quick_exit
+ tst-quick_exit tst-thread-quick_exit tst-width
tests-static := tst-secure-getenv
ifeq ($(have-cxx-thread_local),yes)
CFLAGS-tst-quick_exit.o = -std=c++11
new file mode 100644
@@ -0,0 +1,87 @@
+/* Test integer width macros.
+ Copyright (C) 2016 Free Software Foundation, Inc.
+ This file is part of the GNU C Library.
+
+ The GNU C Library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ The GNU C Library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with the GNU C Library; if not, see
+ <http://www.gnu.org/licenses/>. */
+
+#include <limits.h>
+#include <stdio.h>
+
+#define CHECK_WIDTH(TYPE, MAX, WIDTH) \
+ do \
+ { \
+ if ((MAX >> ((TYPE) -1 < 0 ? (WIDTH - 2) : (WIDTH - 1))) != 1) \
+ { \
+ puts ("bad width of " #TYPE); \
+ result = 1; \
+ } \
+ else \
+ puts ("width of " #TYPE " OK"); \
+ } \
+ while (0)
+
+static int
+do_test (void)
+{
+ int result = 0;
+#ifndef CHAR_WIDTH
+# error "missing CHAR_WIDTH"
+#endif
+ CHECK_WIDTH (char, CHAR_MAX, CHAR_WIDTH);
+#ifndef SCHAR_WIDTH
+# error "missing SCHAR_WIDTH"
+#endif
+ CHECK_WIDTH (signed char, SCHAR_MAX, SCHAR_WIDTH);
+#ifndef UCHAR_WIDTH
+# error "missing UCHAR_WIDTH"
+#endif
+ CHECK_WIDTH (unsigned char, UCHAR_MAX, UCHAR_WIDTH);
+#ifndef SHRT_WIDTH
+# error "missing SHRT_WIDTH"
+#endif
+ CHECK_WIDTH (signed short, SHRT_MAX, SHRT_WIDTH);
+#ifndef USHRT_WIDTH
+# error "missing USHRT_WIDTH"
+#endif
+ CHECK_WIDTH (unsigned short, USHRT_MAX, USHRT_WIDTH);
+#ifndef INT_WIDTH
+# error "missing INT_WIDTH"
+#endif
+ CHECK_WIDTH (signed int, INT_MAX, INT_WIDTH);
+#ifndef UINT_WIDTH
+# error "missing UINT_WIDTH"
+#endif
+ CHECK_WIDTH (unsigned int, UINT_MAX, UINT_WIDTH);
+#ifndef LONG_WIDTH
+# error "missing LONG_WIDTH"
+#endif
+ CHECK_WIDTH (signed long, LONG_MAX, LONG_WIDTH);
+#ifndef ULONG_WIDTH
+# error "missing ULONG_WIDTH"
+#endif
+ CHECK_WIDTH (unsigned long, ULONG_MAX, ULONG_WIDTH);
+#ifndef LLONG_WIDTH
+# error "missing LLONG_WIDTH"
+#endif
+ CHECK_WIDTH (signed long long, LLONG_MAX, LLONG_WIDTH);
+#ifndef ULLONG_WIDTH
+# error "missing ULLONG_WIDTH"
+#endif
+ CHECK_WIDTH (unsigned long long, ULLONG_MAX, ULLONG_WIDTH);
+ return result;
+}
+
+#define TEST_FUNCTION do_test ()
+#include "../test-skeleton.c"