Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r64761 - in sandbox/SOC/2010/bit_masks: boost/integer/detail/bitfield_vector lib/integer/test/bitfield_vector_testing
From: bbartmanboost_at_[hidden]
Date: 2010-08-12 13:16:18


Author: bbartman
Date: 2010-08-12 13:16:14 EDT (Thu, 12 Aug 2010)
New Revision: 64761
URL: http://svn.boost.org/trac/boost/changeset/64761

Log:
completed basic test suite for the signed value implicit cast operator for the proxy_reference_type
Text files modified:
   sandbox/SOC/2010/bit_masks/boost/integer/detail/bitfield_vector/bitfield_vector_member_impl.hpp | 84 ++++++++++++++++++++++++++++++-
   sandbox/SOC/2010/bit_masks/lib/integer/test/bitfield_vector_testing/proxy_reference_test.cpp | 103 ++++++++++++++++++++++++++++++++++++++-
   2 files changed, 177 insertions(+), 10 deletions(-)

Modified: sandbox/SOC/2010/bit_masks/boost/integer/detail/bitfield_vector/bitfield_vector_member_impl.hpp
==============================================================================
--- sandbox/SOC/2010/bit_masks/boost/integer/detail/bitfield_vector/bitfield_vector_member_impl.hpp (original)
+++ sandbox/SOC/2010/bit_masks/boost/integer/detail/bitfield_vector/bitfield_vector_member_impl.hpp 2010-08-12 13:16:14 EDT (Thu, 12 Aug 2010)
@@ -12,6 +12,7 @@
 #include <boost/mpl/if.hpp>
 #include <boost/mpl/equal.hpp>
 #include <boost/type_traits/is_signed.hpp>
+#include <boost/type_traits/make_unsigned.hpp>
 #include <boost/assert.hpp>
 #include <cstring>
 #include <boost/integer/high_low_bits.hpp>
@@ -107,10 +108,15 @@
     typedef unsigned char storage_type;
     typedef RetType value_type;
     typedef std::size_t offset_type;
+
+ typedef typename make_unsigned<value_type>::type unsigned_value_type;
     BOOST_STATIC_CONSTANT( std::size_t, width = Width );
     BOOST_STATIC_CONSTANT(value_type, sign_bit =
         (~value_type(0) <<(bit_width<value_type>::value-1) ));
 
+ BOOST_STATIC_CONSTANT(value_type, stored_sign_bit =
+ ( value_type(1) << (width -1)) );
+
     /** constructors and destructor for the proxy_reference_type type. */
     //@{
 
@@ -133,6 +139,73 @@
     }
     /** Implicit conversion operator.*/
     operator value_type() const {
+
+ value_type ret = 0;
+ // value_type value_bits = 0;
+ if(_mask._size == 1) {
+ ret = (value_type( _mask._first_byte & *_ptr ) >>
+ _mask._last_shift);
+
+ if((stored_sign_bit & ret) == stored_sign_bit) {
+ ret = (~value_type(0) << (width - 1)) |
+ ((~stored_sign_bit) & ret);
+ }
+ return ret;
+ }
+
+ storage_ptr_t byte_ptr = _ptr;
+
+ if(_mask._size == 2) {
+ ++byte_ptr;
+ byte_ptr = _ptr;
+ // getting first bits.
+ ret = value_type(_mask._first_byte & *byte_ptr)
+ << ( 8 - _mask._last_shift );
+ ++byte_ptr;
+ unsigned_value_type retrieved_value;
+ if( _mask._last_byte == 0xFF) {
+ ret <<= 8;
+ ret += 0xFF;
+ }else{
+ retrieved_value = (unsigned_value_type( _mask._last_byte)
+ & unsigned_value_type(*byte_ptr));
+ retrieved_value >>= _mask._last_shift;
+ ret += retrieved_value;
+ }
+ if(stored_sign_bit & ret) {
+ ret = (~value_type(0) << (width - 1)) |
+ (~stored_sign_bit & ret);
+ }
+ return ret;
+ }
+
+ const storage_t all_bits = 0xFF;
+ // gettting first byte.
+
+ ret = value_type( _mask._first_byte & *byte_ptr);
+ ++byte_ptr;
+
+ // getting middle bytes
+ for(std::size_t index = 0; index < _mask._size - 2; ++index) {
+ ret <<= 8;
+ ret += *byte_ptr & all_bits;
+ ++byte_ptr;
+ }
+ // shifting bits
+ if(_mask._last_byte == 0xFF) {
+ ret <<= 8;
+ ret += value_type(*byte_ptr & _mask._last_byte);
+ }else{
+ ret <<= 8 - _mask._last_shift;
+ ret += unsigned_value_type( *byte_ptr & _mask._last_byte )
+ >> ( _mask._last_shift);
+ }
+ if(stored_sign_bit & ret) {
+ ret = (~value_type(0) << (width - 1)) ^
+ (~stored_sign_bit & ret);
+ }
+
+ return ret;
     }
 
     /** value_type storage assignement operator.*/
@@ -176,9 +249,7 @@
     /** pointer, offset constructor. */
     explicit proxy_reference_type(storage_type* ptr, offset_type offset)
         :_ptr(ptr), _mask(get_mask_detail<Width>(offset))
-
     { }
-
     //@}
 
     /** Copy assignment. */
@@ -236,7 +307,8 @@
             ret += value_type(*byte_ptr & _mask._last_byte);
         }else{
             ret <<= 8 - _mask._last_shift;
- ret += value_type( *byte_ptr & _mask._last_byte ) >> ( _mask._last_shift);
+ ret += value_type( *byte_ptr & _mask._last_byte )
+ >> ( _mask._last_shift);
         }
         return ret;
     }
@@ -277,12 +349,14 @@
         mask = _mask._first_byte;
         mask <<= width - bits_in_mask;
 
- *byte_ptr = (*byte_ptr & ~_mask._first_byte) | ((x & mask ) >> (width - bits_in_mask));
+ *byte_ptr = (*byte_ptr & ~_mask._first_byte) | ((x & mask )
+ >> (width - bits_in_mask));
         ++byte_ptr;
         mask = 0xFF;
         mask <<= width - bits_in_mask - 8;
         for(std::size_t index = 0; index < _mask._size - 2;++index) {
- *byte_ptr = (mask & x) >> (width - (bits_in_mask + (8 * index))- 8);
+ *byte_ptr = (mask & x) >> (width - (bits_in_mask +
+ (8 * index))- 8);
             mask >>= 8;
             ++byte_ptr;
         }

Modified: sandbox/SOC/2010/bit_masks/lib/integer/test/bitfield_vector_testing/proxy_reference_test.cpp
==============================================================================
--- sandbox/SOC/2010/bit_masks/lib/integer/test/bitfield_vector_testing/proxy_reference_test.cpp (original)
+++ sandbox/SOC/2010/bit_masks/lib/integer/test/bitfield_vector_testing/proxy_reference_test.cpp 2010-08-12 13:16:14 EDT (Thu, 12 Aug 2010)
@@ -22,8 +22,8 @@
 
 // signed type testing.
 typedef proxy_reference_type<int, 3> test_type_6;
-typedef proxy_reference_type<char, 9> test_type_7;
-typedef proxy_reference_type<long, 7> test_type_8;
+typedef proxy_reference_type<char, 7> test_type_7;
+typedef proxy_reference_type<long, 9> test_type_8;
 typedef proxy_reference_type<long long, 17> test_type_9;
 typedef proxy_reference_type<long long, 50> test_type_10;
 typedef proxy_reference_type<short, 13> test_type_11;
@@ -466,7 +466,7 @@
 
 
         ASSIGN_MONITOR(t1,7)
- ASSIGN_MONITOR(t2,1);
+ ASSIGN_MONITOR(t2,47);
         ASSIGN_MONITOR(t3,2);
         ASSIGN_MONITOR(t4,3);
         ASSIGN_MONITOR(t5,4);
@@ -475,7 +475,7 @@
         ASSIGN_MONITOR(t8,7);
 
         BOOST_PRINT_ON_TEST_FAILURE_2(t1, 7);
- BOOST_PRINT_ON_TEST_FAILURE_2(t2, 1);
+ BOOST_PRINT_ON_TEST_FAILURE_2(t2, 47);
         BOOST_PRINT_ON_TEST_FAILURE_2(t3, 2);
         BOOST_PRINT_ON_TEST_FAILURE_2(t4, 3);
         BOOST_PRINT_ON_TEST_FAILURE_2(t5, 4);
@@ -572,6 +572,9 @@
         ll_type ll_sign_bit = ~ll_type(0) <<
             (boost::bit_width<ll_type>::value-1);
 
+
+
+
 #define PRINT_ON_SIGNBIT_FAILURE(P1, P2) \
     if(display_debug) { \
         if(P1 != P2) {\
@@ -589,12 +592,101 @@
         PRINT_ON_SIGNBIT_FAILURE(test_type_9::sign_bit,ll_sign_bit);
         PRINT_ON_SIGNBIT_FAILURE(test_type_10::sign_bit,ll_sign_bit);
         PRINT_ON_SIGNBIT_FAILURE(test_type_11::sign_bit,short_sign_bit);
+
+ // testing stored sign bit retrieval.
+ PRINT_ON_SIGNBIT_FAILURE(test_type_6::stored_sign_bit,
+ int(1)<<(test_type_6::width-1) );
+ PRINT_ON_SIGNBIT_FAILURE(test_type_7::stored_sign_bit,
+ test_type_7::value_type(1)<<(test_type_7::width-1) );
+ PRINT_ON_SIGNBIT_FAILURE(test_type_8::stored_sign_bit,
+ test_type_8::value_type(1)<<(test_type_8::width-1) );
+ PRINT_ON_SIGNBIT_FAILURE(test_type_9::stored_sign_bit,
+ test_type_9::value_type(1)<<(test_type_9::width-1) );
+ PRINT_ON_SIGNBIT_FAILURE(test_type_10::stored_sign_bit,
+ test_type_10::value_type(1)<<(test_type_10::width-1) );
+ PRINT_ON_SIGNBIT_FAILURE(test_type_11::stored_sign_bit,
+ test_type_11::value_type(1)<<(test_type_11::width-1) );
 #undef PRINT_ON_SIGNBIT_FAILURE
 
+ }
+
+ // testing signed field retrieval.
+ {
+ // int t = -15;
+ // int value_bits = (~(~int(0) << 6)) & t;
+ // std::cout << "t: " << to_binary(t) << std::endl;
+ // std::cout << "value_bits: "<< to_binary(value_bits) << std::endl;
+
+ // int int_min_val = ~int(0) << 6;
+ // std::cout << "int_min_val: "<< to_binary(int_min_val) << std::endl;
+ // t = value_bits ^ int_min_val;
+ // std::cout << "resulting value: " << to_binary(t) << std::endl;
+ // std::cout << "resulting value: " << std::dec << t << std::endl;
+ // BOOST_TEST(false);
+ typedef unsigned char storage_type;
+ typedef storage_type* storage_ptr;
+ storage_type storage[20];
+ storage_ptr ptr = storage;
+ std::memset(ptr,0,20);
 
+ *ptr = 0xA0;
+
+ test_type_6 t1(ptr, 0);
+ BOOST_TEST( t1 == -3);
+ *ptr = 0x20;
+ BOOST_TEST( t1 == 1 );
+ *ptr = 0x5E;
+ test_type_7 t2(ptr, 0);
+ BOOST_TEST(t2 == 47);
+ *ptr = 0x51 << 1;
+
+ BOOST_TEST(t2 == -47);
+ // std::cout << to_binary(-47) <<std::endl;
+ *ptr = 0x50 >> 1;
+ ++ptr;
+ *ptr = 0x80;
+ ptr = storage;
+ test_type_7 t3(ptr,2);
+// std::cout << to_binary(*ptr) << " ";
+// ++ptr;
+// std::cout << to_binary(*ptr) << std::endl;
+
+ BOOST_TEST( t3 == -47);
+
+
+ ptr = storage;
+ std::memset(ptr,0,2);
+ test_type_10 t4(ptr, 4);
+ // 2710
+ ++ptr;
+ ++ptr; // 16
+ ++ptr;
+ ++ptr; // 32
+ ++ptr;
+ *ptr = 0x9c;
+ ++ptr;
+ *ptr = 0x40;
+ ++ptr;
+ BOOST_TEST( t4 == 10000);
+
+ ptr = storage;
+ *ptr = 0x01 << 3;
+ BOOST_TEST(t4 == -562949953411312);
+
+
+ // long long t = t4;
+ // std::cout << "hex value of ptr: " << std::hex << std::size_t(*ptr) << std::endl;
+ // std::cout << "t4: " << to_binary(t) << std::endl;
+ // std::cout << "Test type 7: " << std::dec << t << std::endl;
+ // std::cout << "binary of storage_sign_bit: " << to_binary(test_type_7::stored_sign_bit) << std::endl;
+
+ // 0000 00 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000
+ // 00 10 0111 1 0 0000 00
+ // std::memset(ptr,0,10);
+
 /*
 typedef proxy_reference_type<int, 3> test_type_6;
-typedef proxy_reference_type<char, 9> test_type_7;
+typedef proxy_reference_type<char, 7> test_type_7;
 typedef proxy_reference_type<long, 7> test_type_8;
 typedef proxy_reference_type<long long, 17> test_type_9;
 typedef proxy_reference_type<long long, 50> test_type_10;
@@ -607,6 +699,7 @@
 test_type_10;
 test_type_11
 */
+ BOOST_TEST(false);
 
     }
     return boost::report_errors();


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk