lmi-commits
[Top][All Lists]
Advanced

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

[lmi-commits] [lmi] master 1cc2ae5 4/4: Use fixed-width integers where a


From: Greg Chicares
Subject: [lmi-commits] [lmi] master 1cc2ae5 4/4: Use fixed-width integers where appropriate
Date: Wed, 6 Jun 2018 18:55:32 -0400 (EDT)

branch: master
commit 1cc2ae59338528d2df437be145d71196d3a27080
Author: Gregory W. Chicares <address@hidden>
Commit: Gregory W. Chicares <address@hidden>

    Use fixed-width integers where appropriate
    
    * crc32.cpp: For an LP64 architecture (almost all contemporary *nix),
      type long int might have been wrong. Incidentally, 'i' counts only
      up to 256, so a plain int suffices: it would need to be unsigned for
      this purpose only if int were a one-byte type.
    * main_common.hpp: Assert that an exact 32-bit type exists (the standard
      doesn't require it to).
    * md5.hpp: Because this is glibc code, it shouldn't be modified without
      good reason; the decisive reason here is that it defined macros.
---
 crc32.cpp       | 14 +++++++++-----
 main_common.hpp |  5 +++--
 md5.hpp         | 43 ++-----------------------------------------
 3 files changed, 14 insertions(+), 48 deletions(-)

diff --git a/crc32.cpp b/crc32.cpp
index a1ff982..e908068 100644
--- a/crc32.cpp
+++ b/crc32.cpp
@@ -23,6 +23,10 @@
 
 #include "crc32.hpp"
 
+#include "ssize_lmi.hpp"
+
+#include <cstdint>
+
 // This is a derived work based on Mark Adler's original 'makecrc.c',
 // extracted from gnu gzip
 //   gnu/gzip/gzip-1.2.4a.tar.gz
@@ -74,17 +78,17 @@ namespace
         static int const n = 256;
         static unsigned int crc_array[n];
 
-        unsigned long int c; // CRC shift register.
-        unsigned long int e; // Polynomial exclusive-or pattern.
-        unsigned int i;      // Counter for all possible eight bit values.
-        int k;               // Byte being shifted into crc apparatus.
+        std::uint32_t c; // CRC shift register.
+        std::uint32_t e; // Polynomial exclusive-or pattern.
+        int i;           // Counter for all possible eight bit values.
+        int k;           // Byte being shifted into crc apparatus.
 
         // Terms of polynomial defining this crc (except x^32).
         static int p[] = {0,1,2,4,5,7,8,10,11,12,16,22,23,26};
 
         // Make exclusive-or pattern from polynomial (0xedb88320).
         e = 0;
-        for(i = 0; i < sizeof p / sizeof(int); ++i)
+        for(i = 0; i < lmi::ssize(p); ++i)
             {
             e |= 1L << (31 - p[i]);
             }
diff --git a/main_common.hpp b/main_common.hpp
index e51fdd0..09697e6 100644
--- a/main_common.hpp
+++ b/main_common.hpp
@@ -24,10 +24,11 @@
 
 #include "config.hpp"
 
-// Included so that try_main() definitions can use EXIT_SUCCESS, EXIT_FAILURE.
-#include <cstdlib>
+#include <cstdint>
+#include <cstdlib>                      // EXIT_SUCCESS, EXIT_FAILURE
 
 static_assert(8 == CHAR_BIT);
+static_assert(4 == sizeof(std::int32_t)); // ensure that this exact type exists
 static_assert(4 <= sizeof(int));
 
 void initialize_application();
diff --git a/md5.hpp b/md5.hpp
index 66a5a32..32258a6 100644
--- a/md5.hpp
+++ b/md5.hpp
@@ -48,6 +48,7 @@
 #include "config.hpp"
 
 #include <cstddef>                      // size_t
+#include <cstdint>                      // GWC added this.
 #include <cstdio>                       // GWC added this required header.
 
 /* GWC: Make this header usable with C++. */
@@ -62,47 +63,7 @@ extern "C" {
  */
 #include <limits.h>
 
-/* The following contortions are an attempt to use the C preprocessor
- * to determine an unsigned integral type that is 32 bits wide. An
- * alternative approach is to use autoconf's AC_CHECK_SIZEOF macro, but
- * doing that would require that the configure script compile and *run*
- * the resulting executable. Locally running cross-compiled executables
- * is usually not possible.
- */
-
-#if defined _LIBC
-# include <sys/types.h>
-typedef u_int32_t md5_uint32;
-#else // !defined _LIBC
-#  define INT_MAX_32_BITS 2147483647
-
-/* If UINT_MAX isn't defined, assume it's a 32-bit type.
- * This should be valid for all systems GNU cares about because
- * that doesn't include 16-bit systems, and only modern systems
- * (that certainly have <limits.h>) have 64+-bit integral types.
- */
-
-# if !defined INT_MAX
-#  define INT_MAX INT_MAX_32_BITS
-# endif // !defined INT_MAX
-
-# if INT_MAX == INT_MAX_32_BITS
-   typedef unsigned int md5_uint32;
-# else // !INT_MAX == INT_MAX_32_BITS
-#  if SHRT_MAX == INT_MAX_32_BITS
-    typedef unsigned short int md5_uint32;
-#  else // !SHRT_MAX == INT_MAX_32_BITS
-#   if LONG_MAX == INT_MAX_32_BITS
-     typedef unsigned long int md5_uint32;
-#   else // !LONG_MAX == INT_MAX_32_BITS
-     /* The following line is intended to evoke an error.
-      * Using #error is not portable enough.
-      */
-     "Cannot determine unsigned 32-bit data type."
-#   endif // !LONG_MAX == INT_MAX_32_BITS
-#  endif // !SHRT_MAX == INT_MAX_32_BITS
-# endif // !INT_MAX == INT_MAX_32_BITS
-#endif // !defined _LIBC
+typedef std::uint32_t md5_uint32;
 
 /* GWC: Here, __STDC__ is tested, but what should be done if that test
  * fails? The gnu project assumes it may have a pre-1989 C compiler. I



reply via email to

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