]> sourceware.org Git - glibc.git/commitdiff
Add <limits.h> integer width macros.
authorJoseph Myers <joseph@codesourcery.com>
Mon, 19 Sep 2016 12:25:36 +0000 (12:25 +0000)
committerJoseph Myers <joseph@codesourcery.com>
Mon, 19 Sep 2016 12:25:36 +0000 (12:25 +0000)
TS 18661-1 defines macros for the width of integer types, intended for
use with the fromfp functions to convert from floating-point types to
integer types of any width, in any rounding mode and with control over
whether "inexact" is raised.  Such macros are, of course, more
generally useful than just with those functions.

Those macros are added to <limits.h> and <stdint.h>.  This patch adds
the <limits.h> macros to glibc's header, with the <stdint.h> ones
intended to be added in a separate patch (which would add to the NEWS
entry created by this patch).  I've also added these macros to GCC's
headers for GCC 7, but definitions in glibc's <limits.h> are still
useful for older GCC, for non-GNU compilers and for when it's
_GNU_SOURCE rather than __STDC_WANT_IEC_60559_BFP_EXT__ that implies
the macros should be defined since the GCC header only considers
__STDC_WANT_IEC_60559_BFP_EXT__ (and for glibc systems, the
definitions in GCC's <stdint.h> will only be used with
-ffreestanding).

Tested for x86_64 and x86.

* include/limits.h: Define
__GLIBC_INTERNAL_STARTING_HEADER_IMPLEMENTATION and include
<bits/libc-header-start.h> instead of including <features.h>.
[__GLIBC_USE (IEC_60559_BFP_EXT)] (CHAR_WIDTH): New macro.
[__GLIBC_USE (IEC_60559_BFP_EXT)] (SCHAR_WIDTH): Likewise.
[__GLIBC_USE (IEC_60559_BFP_EXT)] (UCHAR_WIDTH): Likewise.
[__GLIBC_USE (IEC_60559_BFP_EXT)] (SHRT_WIDTH): Likewise.
[__GLIBC_USE (IEC_60559_BFP_EXT)] (USHRT_WIDTH): Likewise.
[__GLIBC_USE (IEC_60559_BFP_EXT)] (INT_WIDTH): Likewise.
[__GLIBC_USE (IEC_60559_BFP_EXT)] (UINT_WIDTH): Likewise.
[__GLIBC_USE (IEC_60559_BFP_EXT)] (LONG_WIDTH): Likewise.
[__GLIBC_USE (IEC_60559_BFP_EXT)] (ULONG_WIDTH): Likewise.
[__GLIBC_USE (IEC_60559_BFP_EXT)] (LLONG_WIDTH): Likewise.
[__GLIBC_USE (IEC_60559_BFP_EXT)] (ULLONG_WIDTH): Likewise.
* manual/lang.texi (Width of Type): Document these macros.
* stdlib/tst-width.c: New file.
* stdlib/Makefile (tests): Add tst-width.

ChangeLog
NEWS
include/limits.h
manual/lang.texi
stdlib/Makefile
stdlib/tst-width.c [new file with mode: 0644]

index d614df0ae478db119b976b3d415f3b85eb3f0516..f3ad4289c40b9d0423d7ec4241d33cec03ac4e0c 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,23 @@
+2016-09-19  Joseph Myers  <joseph@codesourcery.com>
+
+       * include/limits.h: Define
+       __GLIBC_INTERNAL_STARTING_HEADER_IMPLEMENTATION and include
+       <bits/libc-header-start.h> instead of including <features.h>.
+       [__GLIBC_USE (IEC_60559_BFP_EXT)] (CHAR_WIDTH): New macro.
+       [__GLIBC_USE (IEC_60559_BFP_EXT)] (SCHAR_WIDTH): Likewise.
+       [__GLIBC_USE (IEC_60559_BFP_EXT)] (UCHAR_WIDTH): Likewise.
+       [__GLIBC_USE (IEC_60559_BFP_EXT)] (SHRT_WIDTH): Likewise.
+       [__GLIBC_USE (IEC_60559_BFP_EXT)] (USHRT_WIDTH): Likewise.
+       [__GLIBC_USE (IEC_60559_BFP_EXT)] (INT_WIDTH): Likewise.
+       [__GLIBC_USE (IEC_60559_BFP_EXT)] (UINT_WIDTH): Likewise.
+       [__GLIBC_USE (IEC_60559_BFP_EXT)] (LONG_WIDTH): Likewise.
+       [__GLIBC_USE (IEC_60559_BFP_EXT)] (ULONG_WIDTH): Likewise.
+       [__GLIBC_USE (IEC_60559_BFP_EXT)] (LLONG_WIDTH): Likewise.
+       [__GLIBC_USE (IEC_60559_BFP_EXT)] (ULLONG_WIDTH): Likewise.
+       * manual/lang.texi (Width of Type): Document these macros.
+       * stdlib/tst-width.c: New file.
+       * stdlib/Makefile (tests): Add tst-width.
+
 2016-09-18  Samuel Thibault  <samuel.thibault@ens-lyon.org>
 
        * intl/dcigettext.c (PATH_MAX): Call __pathconf instead of pathconf.
diff --git a/NEWS b/NEWS
index ba1ec715a928d3f199608cb5633934342eb2091d..0ea6bfa92526a384a771f07ca1f9f4482a03626f 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -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.
index 5add8fceb9200c31dbf00dc2f2e515ba258455fe..93cac49e55f84a9d577d8b7f0187d0c57c131be1 100644 (file)
@@ -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.
 # 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>
index 7f8a36800ccf3ac10ea3a3749f3b49b226e55c98..3705df05990ff1c4bc6f8057ca4705e1ff5e86b0 100644 (file)
@@ -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 other
+than @code{_Bool} 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
index fc6f23dcaf6e1b75fde4ffb4c97ebf6724a910cd..ccc9aaef335770bad86c2a716aeb144fdb02641d 100644 (file)
@@ -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
diff --git a/stdlib/tst-width.c b/stdlib/tst-width.c
new file mode 100644 (file)
index 0000000..4972c8d
--- /dev/null
@@ -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"
This page took 0.17575 seconds and 5 git commands to generate.