changeset 17415:0b644adf4f31

speak CHAR_BIT in C++-ese * bitfcns.cc, typecast.cc, data-conv.cc: Instead of CHAR_BIT, use std::numeric_limits<unsigned char>::digits.
author John W. Eaton <jwe@octave.org>
date Mon, 09 Sep 2013 17:13:11 -0400
parents ed149e891876
children 0bf2fc8562c9
files libinterp/corefcn/bitfcns.cc libinterp/corefcn/typecast.cc liboctave/util/data-conv.cc
diffstat 3 files changed, 20 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- a/libinterp/corefcn/bitfcns.cc
+++ b/libinterp/corefcn/bitfcns.cc
@@ -24,6 +24,8 @@
 #include <config.h>
 #endif
 
+#include <limits>
+
 #include "str-vec.h"
 #include "quit.h"
 
@@ -588,7 +590,7 @@
             mask = mask >> (bits_in_mantissa - nbits);
           else if (nbits < 1)
             mask = 0;
-          int bits_in_type = sizeof (double) * CHAR_BIT;
+          int bits_in_type = sizeof (double) * std::numeric_limits<unsigned char>::digits;
           NDArray m = m_arg.array_value ();
           DO_BITSHIFT ( );
         }
@@ -601,7 +603,7 @@
             mask = mask >> (bits_in_mantissa - nbits);
           else if (nbits < 1)
             mask = 0;
-          int bits_in_type = sizeof (float) * CHAR_BIT;
+          int bits_in_type = sizeof (float) * std::numeric_limits<unsigned char>::digits;
           FloatNDArray m = m_arg.float_array_value ();
           DO_BITSHIFT (Float);
         }
--- a/libinterp/corefcn/typecast.cc
+++ b/libinterp/corefcn/typecast.cc
@@ -25,7 +25,7 @@
 #include <config.h>
 #endif
 
-#include <climits>
+#include <limits>
 
 #include "mx-base.h"
 
@@ -242,9 +242,9 @@
 do_bitpack (const boolNDArray& bitp)
 {
   typedef typename ArrayType::element_type T;
-  octave_idx_type n = bitp.numel () / (sizeof (T) * CHAR_BIT);
+  octave_idx_type n = bitp.numel () / (sizeof (T) * std::numeric_limits<unsigned char>::digits);
 
-  if (n * static_cast<int> (sizeof (T)) * CHAR_BIT == bitp.numel ())
+  if (n * static_cast<int> (sizeof (T)) * std::numeric_limits<unsigned char>::digits == bitp.numel ())
     {
 
       ArrayType retval (get_vec_dims (bitp.dims (), n));
@@ -257,11 +257,11 @@
       for (octave_idx_type i = 0; i < m; i++)
         {
           char c = bits[0];
-          for (int j = 1; j < CHAR_BIT; j++)
+          for (int j = 1; j < std::numeric_limits<unsigned char>::digits; j++)
             c |= bits[j] << j;
 
           packed[i] = c;
-          bits += CHAR_BIT;
+          bits += std::numeric_limits<unsigned char>::digits;
         }
 
       return retval;
@@ -361,22 +361,22 @@
 do_bitunpack (const ArrayType& array)
 {
   typedef typename ArrayType::element_type T;
-  octave_idx_type n = array.numel () * sizeof (T) * CHAR_BIT;
+  octave_idx_type n = array.numel () * sizeof (T) * std::numeric_limits<unsigned char>::digits;
 
   boolNDArray retval (get_vec_dims (array.dims (), n));
 
   const char *packed = reinterpret_cast<const char *> (array.fortran_vec ());
   bool *bits = retval.fortran_vec ();
 
-  octave_idx_type m = n / CHAR_BIT;
+  octave_idx_type m = n / std::numeric_limits<unsigned char>::digits;
 
   for (octave_idx_type i = 0; i < m; i++)
     {
       char c = packed[i];
       bits[0] = c & 1;
-      for (int j = 1; j < CHAR_BIT; j++)
+      for (int j = 1; j < std::numeric_limits<unsigned char>::digits; j++)
         bits[j] = (c >>= 1) & 1;
-      bits += CHAR_BIT;
+      bits += std::numeric_limits<unsigned char>::digits;
     }
 
   return retval;
--- a/liboctave/util/data-conv.cc
+++ b/liboctave/util/data-conv.cc
@@ -25,10 +25,10 @@
 #endif
 
 #include <cctype>
-#include <climits>
 #include <cstdlib>
 
 #include <iostream>
+#include <limits>
 #include <vector>
 
 #include "byte-swap.h"
@@ -45,7 +45,7 @@
 #define FIND_SIZED_INT_TYPE(VAL, BITS, TQ, Q) \
   do \
     { \
-      int sz = BITS / CHAR_BIT; \
+      int sz = BITS / std::numeric_limits<unsigned char>::digits; \
       if (sizeof (TQ char) == sz) \
         VAL = oct_data_conv::dt_ ## Q ## char; \
       else if (sizeof (TQ short) == sz) \
@@ -64,7 +64,7 @@
 #define FIND_SIZED_INT_TYPE(VAL, BITS, TQ, Q) \
   do \
     { \
-      int sz = BITS / CHAR_BIT; \
+      int sz = BITS / std::numeric_limits<unsigned char>::digits; \
       if (sizeof (TQ char) == sz) \
         VAL = oct_data_conv::dt_ ## Q ## char; \
       else if (sizeof (TQ short) == sz) \
@@ -82,7 +82,7 @@
 #define FIND_SIZED_FLOAT_TYPE(VAL, BITS) \
   do \
     { \
-      int sz = BITS / CHAR_BIT; \
+      int sz = BITS / std::numeric_limits<unsigned char>::digits; \
       if (sizeof (float) == sz) \
         VAL = oct_data_conv::dt_float; \
       else if (sizeof (double) == sz) \
@@ -94,8 +94,9 @@
 
 // I'm not sure it is worth the trouble, but let's use a lookup table
 // for the types that are supposed to be a specific number of bits
-// wide.  Given the macros above, this should work as long as CHAR_BIT
-// is a multiple of 8 and there are types with the right sizes.
+// wide.  Given the macros above, this should work as long as
+// std::numeric_limits<unsigned char>::digits is a multiple of 8 and
+// there are types with the right sizes.
 //
 // The sized data type lookup table has the following format:
 //