incubator-stdcxx-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Scott Zhong" <Scott.Zh...@roguewave.com>
Subject [PATCH] STDCXX-423 LIMITS.cpp assumes integers with no padding bits
Date Wed, 19 Mar 2008 21:20:08 GMT
Index: LIMITS.cpp
===================================================================
--- LIMITS.cpp  (revision 638996)
+++ LIMITS.cpp  (working copy)
@@ -223,7 +223,19 @@
     return bits;
 }
 
+template <class T>
+unsigned compute_type_bits()
+{
+    T max = T (one);
+    T current = T(one);
+    int bits = 1;
 
+    for (; T (current * 2) > max; current *=2, max *= 2, bits++) { }
+
+    return bits;
+}
+
+
 // used to compute the size of a pointer to a member function
 struct EmptyStruct { };
 
@@ -397,6 +409,12 @@
     // 1 for a 16-bit integer, etc)
     int width_bits = 0;
 
+    // store exact bit size of each type
+    int ushort_bits = compute_type_bits<unsigned short> ();
+    int uint_bits = compute_type_bits<unsigned int> ();
+    int ulong_bits = compute_type_bits<unsigned long> ();
+    int ullong_bits = compute_type_bits<unsigned LLong> ();
+
 #define PRINT_SPECIFIC(width, least, type)
\
     do {
\
         /* avoid warnings about expression being constant */
\
@@ -430,59 +448,59 @@
         PRINT_SPECIFIC (64, "", "char");
     }
 
-    if (16 == char_bits * sizeof (short) && !(width_bits & 2)) {
+    if (16 == ushort_bits && !(width_bits & 2)) {
         width_bits |= 2;
         PRINT_SPECIFIC (16, "", "short");
     }
-    else if (32 == char_bits * sizeof (short) && !(width_bits & 4)) {
+    else if (32 == ushort_bits && !(width_bits & 4)) {
         width_bits |= 4;
         PRINT_SPECIFIC (32, "", "short");
     }
-    else if (64 == char_bits * sizeof (short) && !(width_bits & 8)) {
+    else if (64 == ushort_bits && !(width_bits & 8)) {
         width_bits |= 8;
         PRINT_SPECIFIC (64, "", "short");
     }
-    else if (128 == char_bits * sizeof (short) && !(width_bits & 16)) {
+    else if (128 == ushort_bits && !(width_bits & 16)) {
         width_bits |= 16;
         PRINT_SPECIFIC (128, "", "short");
     }
 
-    if (32 == char_bits * sizeof (int) && !(width_bits & 4)) {
+    if (32 == uint_bits && !(width_bits & 4)) {
         width_bits |= 4;
         PRINT_SPECIFIC (32, "", "int");
     }
-    else if (64 == char_bits * sizeof (int) && !(width_bits & 8)) {
+    else if (64 == uint_bits && !(width_bits & 8)) {
         width_bits |= 8;
         PRINT_SPECIFIC (64, "", "int");
     }
-    else if (128 == char_bits * sizeof (int) && !(width_bits & 16)) {
+    else if (128 == uint_bits && !(width_bits & 16)) {
         width_bits |= 16;
         PRINT_SPECIFIC (128, "", "int");
     }
 
-    if (32 == char_bits * sizeof (long) && !(width_bits & 4)) {
+    if (32 == ulong_bits && !(width_bits & 4)) {
         width_bits |= 4;
         PRINT_SPECIFIC (32, "", "long");
     }
-    else if (64 == char_bits * sizeof (long) && !(width_bits & 8)) {
+    else if (64 == ulong_bits && !(width_bits & 8)) {
         width_bits |= 8;
         PRINT_SPECIFIC (64, "", "long");
     }
-    else if (128 == char_bits * sizeof (long) && !(width_bits & 16)) {
+    else if (128 == ulong_bits && !(width_bits & 16)) {
         width_bits |= 16;
         PRINT_SPECIFIC (128, "", "long");
     }

-    if (32 == char_bits * sizeof (LLong) && !(width_bits & 4)) {
+    if (32 == ullong_bits && !(width_bits & 4)) {
         width_bits |= 4;
         PRINT_SPECIFIC (32, "", llong_name);
     }
-    else if (64 == char_bits * sizeof (LLong) && !(width_bits & 8)) {
+    else if (64 == ullong_bits && !(width_bits & 8)) {
         width_bits |= 8;
         PRINT_SPECIFIC (64, "", llong_name);
 
     }
-    else if (128 == char_bits * sizeof (LLong) && !(width_bits & 16)) {
+    else if (128 == ullong_bits && !(width_bits & 16)) {
         width_bits |= 16;
         PRINT_SPECIFIC (128, "", llong_name);
     }

Mime
View raw message