From patchwork Tue Sep 13 17:49:53 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Joseph Myers X-Patchwork-Id: 669556 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3sYXJY6GThz9sQw for ; Wed, 14 Sep 2016 03:50:21 +1000 (AEST) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; secure) header.d=sourceware.org header.i=@sourceware.org header.b=H6i3be+j; dkim-atps=neutral DomainKey-Signature: a=rsa-sha1; c=nofws; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:date:from:to:subject:message-id:mime-version :content-type; q=dns; s=default; b=MnTNfEoRAEZMPVgcAzT3QkN5o4q0O bpJ3BLR0SdM9GtPaAlK9wL8SAHK3jN/1waYQghqNofQC3k4pMIe0SGvQuCumIjjm Uig4MmnXNBs7nmxOlw5E6lsRbvtlxD/5KaZCnOOKHERdQBc413yediLbSBZh7IxS WkdWHlxcG/TJwU= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:date:from:to:subject:message-id:mime-version :content-type; s=default; bh=iHj1NaWzN4H60vTTDQG0mX5wbmM=; b=H6i 3be+jKVS+UfpvCQsbE9+g4G3Felhj/pKtFTT0Ep+rIYqsb+0iCyVy/xWZG5N/dZR M9nRXiIUWja8qTXbGJvFgTAuaFmiMTHP1ViOroW53tp9k6nbPsIcjguxDCHcW1jx PbaJQ6z9fD73xE2VlEdo/h9Wi4mJZk+x3QR7tYYM= Received: (qmail 122998 invoked by alias); 13 Sep 2016 17:50:14 -0000 Mailing-List: contact libc-alpha-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: libc-alpha-owner@sourceware.org Delivered-To: mailing list libc-alpha@sourceware.org Received: (qmail 122969 invoked by uid 89); 13 Sep 2016 17:50:13 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.9 required=5.0 tests=AWL, BAYES_00, RCVD_IN_DNSWL_NONE, SPF_PASS, URIBL_RED autolearn=ham version=3.3.2 spammy=36, 6, news X-HELO: relay1.mentorg.com Date: Tue, 13 Sep 2016 17:49:53 +0000 From: Joseph Myers To: Subject: Add integer width macros Message-ID: User-Agent: Alpine 2.20 (DEB 67 2015-01-07) MIME-Version: 1.0 X-ClientProxiedBy: svr-ies-mbx-01.mgc.mentorg.com (139.181.222.1) To svr-ies-mbx-01.mgc.mentorg.com (139.181.222.1) 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. 2016-09-13 Joseph Myers * 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. diff --git a/NEWS b/NEWS index ba1ec71..0ea6bfa 100644 --- 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 : + CHAR_WIDTH, SCHAR_WIDTH, UCHAR_WIDTH, SHRT_WIDTH, USHRT_WIDTH, INT_WIDTH, + UINT_WIDTH, LONG_WIDTH, ULONG_WIDTH, LLONG_WIDTH, ULLONG_WIDTH. + * The header now includes the header. Support for the Linux quota interface which predates kernel version 2.4.22 has been removed. diff --git a/include/limits.h b/include/limits.h index 5add8fc..93cac49 100644 --- a/include/limits.h +++ b/include/limits.h @@ -22,7 +22,8 @@ #ifndef _LIBC_LIMITS_H_ #define _LIBC_LIMITS_H_ 1 -#include +#define __GLIBC_INTERNAL_STARTING_HEADER_IMPLEMENTATION +#include /* 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 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 . */ # include diff --git a/manual/lang.texi b/manual/lang.texi index 7f8a368..ea1a96b 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 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 diff --git a/stdlib/Makefile b/stdlib/Makefile index fc6f23d..ccc9aae 100644 --- a/stdlib/Makefile +++ b/stdlib/Makefile @@ -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 index 0000000..4972c8d --- /dev/null +++ b/stdlib/tst-width.c @@ -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 + . */ + +#include +#include + +#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"