bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH 2/2] intprops: new macro TYPE_WIDTH


From: Paul Eggert
Subject: [PATCH 2/2] intprops: new macro TYPE_WIDTH
Date: Tue, 13 Sep 2016 21:51:14 -0700

* lib/intprops.h (TYPE_WIDTH): New macro.
(TYPE_MAXIMUM, _GL_SIGNED_INT_MAXIMUM, INT_STRLEN_BOUND):
* lib/ftoastr.h (_GL_FLOAT_DIG_BITS_BOUND):
* lib/parse-datetime.y (parse_datetime2):
Use it.
---
 ChangeLog            |  7 +++++++
 lib/ftoastr.h        |  2 +-
 lib/intprops.h       | 15 +++++++++++----
 lib/parse-datetime.y |  3 +--
 4 files changed, 20 insertions(+), 7 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index bd7356f..a936166 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,12 @@
 2016-09-13  Paul Eggert  <address@hidden>
 
+       intprops: new macro TYPE_WIDTH
+       * lib/intprops.h (TYPE_WIDTH): New macro.
+       (TYPE_MAXIMUM, _GL_SIGNED_INT_MAXIMUM, INT_STRLEN_BOUND):
+       * lib/ftoastr.h (_GL_FLOAT_DIG_BITS_BOUND):
+       * lib/parse-datetime.y (parse_datetime2):
+       Use it.
+
        extensions: port to recent ISO C TRs
        * m4/extensions.m4 (AC_USE_SYSTEM_EXTENSIONS):
        Sync from Autoconf master, to add support for recent ISO C TRs.
diff --git a/lib/ftoastr.h b/lib/ftoastr.h
index 9207809..94816d1 100644
--- a/lib/ftoastr.h
+++ b/lib/ftoastr.h
@@ -88,7 +88,7 @@ enum
 # elif FLT_RADIX <= 16 /* IBM hex floating point has FLT_RADIX == 16.  */
 #  define _GL_FLOAT_DIG_BITS_BOUND 4
 # else /* no machine is this bad, but let's be complete */
-#  define _GL_FLOAT_DIG_BITS_BOUND (CHAR_BIT * (int) sizeof (int) - 1)
+#  define _GL_FLOAT_DIG_BITS_BOUND ((int) TYPE_WIDTH (int) - 1)
 # endif
 
 /* An upper bound on the number of decimal digits needed to represent
diff --git a/lib/intprops.h b/lib/intprops.h
index 4324e18..9734d07 100644
--- a/lib/intprops.h
+++ b/lib/intprops.h
@@ -51,12 +51,16 @@
 
 /* Minimum and maximum values for integer types and expressions.  */
 
+/* The width in bits of the integer type or expression T.
+   Padding bits are not supported; this is checked at compile-time below.  */
+#define TYPE_WIDTH(t) (sizeof (t) * CHAR_BIT)
+
 /* The maximum and minimum values for the integer type T.  */
 #define TYPE_MINIMUM(t) ((t) ~ TYPE_MAXIMUM (t))
 #define TYPE_MAXIMUM(t)                                                 \
   ((t) (! TYPE_SIGNED (t)                                               \
         ? (t) -1                                                        \
-        : ((((t) 1 << (sizeof (t) * CHAR_BIT - 2)) - 1) * 2 + 1)))
+        : ((((t) 1 << (TYPE_WIDTH (t) - 2)) - 1) * 2 + 1)))
 
 /* The maximum and minimum values for the type of the expression E,
    after integer promotion.  E should not have side effects.  */
@@ -69,7 +73,7 @@
    ? _GL_SIGNED_INT_MAXIMUM (e)                                         \
    : _GL_INT_NEGATE_CONVERT (e, 1))
 #define _GL_SIGNED_INT_MAXIMUM(e)                                       \
-  (((_GL_INT_CONVERT (e, 1) << (sizeof ((e) + 0) * CHAR_BIT - 2)) - 1) * 2 + 1)
+  (((_GL_INT_CONVERT (e, 1) << (TYPE_WIDTH ((e) + 0) - 2)) - 1) * 2 + 1)
 
 /* Work around OpenVMS incompatibility with C99.  */
 #if !defined LLONG_MAX && defined __INT64_MAX
@@ -94,6 +98,10 @@ verify (TYPE_MAXIMUM (long int) == LONG_MAX);
 verify (TYPE_MINIMUM (long long int) == LLONG_MIN);
 verify (TYPE_MAXIMUM (long long int) == LLONG_MAX);
 #endif
+/* Similarly, sanity-check one ISO/IEC TS 18661-1:2014 macro if defined.  */
+#ifdef UINT_WIDTH
+verify (TYPE_WIDTH (unsigned int) == UINT_WIDTH);
+#endif
 
 /* Does the __typeof__ keyword work?  This could be done by
    'configure', but for now it's easier to do it by hand.  */
@@ -126,8 +134,7 @@ verify (TYPE_MAXIMUM (long long int) == LLONG_MAX);
    signed, this macro may overestimate the true bound by one byte when
    applied to unsigned types of size 2, 4, 16, ... bytes.  */
 #define INT_STRLEN_BOUND(t)                                     \
-  (INT_BITS_STRLEN_BOUND (sizeof (t) * CHAR_BIT                 \
-                          - _GL_SIGNED_TYPE_OR_EXPR (t))        \
+  (INT_BITS_STRLEN_BOUND (TYPE_WIDTH (t) - _GL_SIGNED_TYPE_OR_EXPR (t)) \
    + _GL_SIGNED_TYPE_OR_EXPR (t))
 
 /* Bound on buffer size needed to represent an integer type or expression T,
diff --git a/lib/parse-datetime.y b/lib/parse-datetime.y
index 9978955..51ba4cc 100644
--- a/lib/parse-datetime.y
+++ b/lib/parse-datetime.y
@@ -2013,8 +2013,7 @@ parse_datetime2 (struct timespec *result, char const *p,
               long int abs_time_zone = time_zone < 0 ? - time_zone : time_zone;
               long int abs_time_zone_hour = abs_time_zone / 60;
               int abs_time_zone_min = abs_time_zone % 60;
-              char tz1buf[sizeof "XXX+0:00"
-                          + sizeof pc.time_zone * CHAR_BIT / 3];
+              char tz1buf[sizeof "XXX+0:00" + TYPE_WIDTH (pc.time_zone) / 3];
               if (!tz_was_altered)
                 tz0 = get_tz (tz0buf);
               sprintf (tz1buf, "XXX%s%ld:%02d", &"-"[time_zone < 0],
-- 
2.7.4




reply via email to

[Prev in Thread] Current Thread [Next in Thread]