[libcxx] r319106 - Fix problems with r'890 when building on machines where sizeof(size_t) != sizeof(unsigned long long) and C++03

Marshall Clow via cfe-commits cfe-commits at lists.llvm.org
Mon Nov 27 14:27:22 PST 2017


Author: marshall
Date: Mon Nov 27 14:27:22 2017
New Revision: 319106

URL: http://llvm.org/viewvc/llvm-project?rev=319106&view=rev
Log:
Fix problems with r'890 when building on machines where sizeof(size_t) != sizeof(unsigned long long) and C++03

Modified:
    libcxx/trunk/include/bitset
    libcxx/trunk/test/std/utilities/template.bitset/bitset.members/to_ullong.pass.cpp
    libcxx/trunk/test/std/utilities/template.bitset/bitset.members/to_ulong.pass.cpp

Modified: libcxx/trunk/include/bitset
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/include/bitset?rev=319106&r1=319105&r2=319106&view=diff
==============================================================================
--- libcxx/trunk/include/bitset (original)
+++ libcxx/trunk/include/bitset Mon Nov 27 14:27:22 2017
@@ -235,8 +235,13 @@ void
 __bitset<_N_words, _Size>::__init(unsigned long long __v, false_type) _NOEXCEPT
 {
     __storage_type __t[sizeof(unsigned long long) / sizeof(__storage_type)];
-    for (size_t __i = 0; __i < sizeof(__t)/sizeof(__t[0]); ++__i, __v >>= __bits_per_word)
-        __t[__i] = static_cast<__storage_type>(__v);
+    size_t __sz = _Size;
+    for (size_t __i = 0; __i < sizeof(__t)/sizeof(__t[0]); ++__i, __v >>= __bits_per_word, __sz -= __bits_per_word )
+        if ( __sz < __bits_per_word)
+        	__t[__i] = static_cast<__storage_type>(__v) & ( 1ULL << __sz ) - 1;
+        else
+        	__t[__i] = static_cast<__storage_type>(__v);
+
     _VSTD::copy(__t, __t + sizeof(__t)/sizeof(__t[0]), __first_);
     _VSTD::fill(__first_ + sizeof(__t)/sizeof(__t[0]), __first_ + sizeof(__first_)/sizeof(__first_[0]),
                __storage_type(0));
@@ -248,6 +253,9 @@ void
 __bitset<_N_words, _Size>::__init(unsigned long long __v, true_type) _NOEXCEPT
 {
     __first_[0] = __v;
+    if (_Size < __bits_per_word)
+    	__first_[0] &= ( 1ULL << _Size ) - 1;
+
     _VSTD::fill(__first_ + 1, __first_ + sizeof(__first_)/sizeof(__first_[0]), __storage_type(0));
 }
 
@@ -261,7 +269,9 @@ __bitset<_N_words, _Size>::__bitset(unsi
 #if __SIZEOF_SIZE_T__ == 8
     : __first_{__v}
 #elif __SIZEOF_SIZE_T__ == 4
-    : __first_{static_cast<__storage_type>(__v), static_cast<__storage_type>(__v >> __bits_per_word)}
+    : __first_{static_cast<__storage_type>(__v), 
+    			_Size >= 2 * __bits_per_word ? static_cast<__storage_type>(__v >> __bits_per_word)
+    			: static_cast<__storage_type>((__v >> __bits_per_word) & (__storage_type(1) << (_Size - __bits_per_word)) - 1)}
 #else
 #error This constructor has not been ported to this platform
 #endif

Modified: libcxx/trunk/test/std/utilities/template.bitset/bitset.members/to_ullong.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/std/utilities/template.bitset/bitset.members/to_ullong.pass.cpp?rev=319106&r1=319105&r2=319106&view=diff
==============================================================================
--- libcxx/trunk/test/std/utilities/template.bitset/bitset.members/to_ullong.pass.cpp (original)
+++ libcxx/trunk/test/std/utilities/template.bitset/bitset.members/to_ullong.pass.cpp Mon Nov 27 14:27:22 2017
@@ -8,6 +8,7 @@
 //===----------------------------------------------------------------------===//
 
 // test unsigned long long to_ullong() const;
+extern "C" int printf(const char *, ...);
 
 #include <bitset>
 #include <algorithm>
@@ -37,7 +38,7 @@ void test_to_ullong()
         assert(j == v.to_ullong());
     }
     { // test values bigger than can fit into the bitset
-    const unsigned long long val = 0xAAAAAAAAAAAAAAAAULL;
+    const unsigned long long val = 0x55AAAAFFFFAAAA55ULL;
     const bool canFit = N < sizeof(unsigned long long) * CHAR_BIT;
     const unsigned long long mask = canFit ? (1ULL << N) - 1 : (unsigned long long)(-1);
     std::bitset<N> v(val);

Modified: libcxx/trunk/test/std/utilities/template.bitset/bitset.members/to_ulong.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/std/utilities/template.bitset/bitset.members/to_ulong.pass.cpp?rev=319106&r1=319105&r2=319106&view=diff
==============================================================================
--- libcxx/trunk/test/std/utilities/template.bitset/bitset.members/to_ulong.pass.cpp (original)
+++ libcxx/trunk/test/std/utilities/template.bitset/bitset.members/to_ulong.pass.cpp Mon Nov 27 14:27:22 2017
@@ -16,12 +16,9 @@
 #include <climits>
 #include <cassert>
 
-#include <iostream>
-
 template <std::size_t N>
 void test_to_ulong()
 {
-    std::cout << "Testing size = " << N << std::endl;
     const std::size_t M = sizeof(unsigned long) * CHAR_BIT < N ? sizeof(unsigned long) * CHAR_BIT : N;
     const bool is_M_zero = std::integral_constant<bool, M == 0>::value; // avoid compiler warnings
     const std::size_t X = is_M_zero ? sizeof(unsigned long) * CHAR_BIT - 1 : sizeof(unsigned long) * CHAR_BIT - M;
@@ -37,13 +34,12 @@ void test_to_ulong()
     for (std::size_t i = 0; i < sizeof(tests)/sizeof(tests[0]); ++i)
     {
         std::size_t j = tests[i];
-    std::cout << "  Testing value = " << j << std::endl;
         std::bitset<N> v(j);
         assert(j == v.to_ulong());
     }
 
     { // test values bigger than can fit into the bitset
-    const unsigned long val = 0xAAAAAAAAULL;
+    const unsigned long val = 0x5AFFFFA5ULL;
     const bool canFit = N < sizeof(unsigned long) * CHAR_BIT;
     const unsigned long mask = canFit ? (1ULL << N) - 1 : (unsigned long)(-1);
     std::bitset<N> v(val);




More information about the cfe-commits mailing list