From a292f45acdf0a35266e4f1dd1e51b95ca5325d2a Mon Sep 17 00:00:00 2001 From: Joseph Myers Date: Mon, 19 Sep 2016 12:25:36 +0000 Subject: Add integer width macros. 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 and . This patch adds the macros to glibc's header, with the 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 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 will only be used with -ffreestanding). Tested for x86_64 and x86. * include/limits.h: Define __GLIBC_INTERNAL_STARTING_HEADER_IMPLEMENTATION and include instead of including . [__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. --- manual/lang.texi | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) (limited to 'manual') diff --git a/manual/lang.texi b/manual/lang.texi index 7f8a36800c..3705df0599 100644 --- a/manual/lang.texi +++ b/manual/lang.texi @@ -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 -- cgit 1.4.1