Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r62435 - in sandbox/SOC/2010/bits_and_ints: boost/integer libs/integer/test
From: muriloufg_at_[hidden]
Date: 2010-06-04 19:22:24


Author: murilov
Date: 2010-06-04 19:22:22 EDT (Fri, 04 Jun 2010)
New Revision: 62435
URL: http://svn.boost.org/trac/boost/changeset/62435

Log:
Added sign functions (sign.hpp)
Added:
   sandbox/SOC/2010/bits_and_ints/boost/integer/sign.hpp (contents, props changed)
   sandbox/SOC/2010/bits_and_ints/libs/integer/test/sign_test.cpp (contents, props changed)
Text files modified:
   sandbox/SOC/2010/bits_and_ints/boost/integer/bits_and_ints.hpp | 1 +
   sandbox/SOC/2010/bits_and_ints/libs/integer/test/Jamfile.v2 | 1 +
   sandbox/SOC/2010/bits_and_ints/libs/integer/test/bit_reversal_test.cpp | 4 ++--
   sandbox/SOC/2010/bits_and_ints/libs/integer/test/sign_extend_test.cpp | 2 +-
   4 files changed, 5 insertions(+), 3 deletions(-)

Modified: sandbox/SOC/2010/bits_and_ints/boost/integer/bits_and_ints.hpp
==============================================================================
--- sandbox/SOC/2010/bits_and_ints/boost/integer/bits_and_ints.hpp (original)
+++ sandbox/SOC/2010/bits_and_ints/boost/integer/bits_and_ints.hpp 2010-06-04 19:22:22 EDT (Fri, 04 Jun 2010)
@@ -17,5 +17,6 @@
 #include <boost/integer/static_sign_extend.hpp>
 #include <boost/integer/bit_reversal.hpp>
 #include <boost/integer/static_bit_reversal.hpp>
+#include <boost/integer/sign.hpp>
 
 #endif
\ No newline at end of file

Added: sandbox/SOC/2010/bits_and_ints/boost/integer/sign.hpp
==============================================================================
--- (empty file)
+++ sandbox/SOC/2010/bits_and_ints/boost/integer/sign.hpp 2010-06-04 19:22:22 EDT (Fri, 04 Jun 2010)
@@ -0,0 +1,46 @@
+// Boost integer/sign.hpp header file ------------------------------//
+
+// (C) Copyright Murilo Adriano Vasconcelos 2010.
+// Distributed under the Boost Software License, Version 1.0. (See
+// accompanying file LICENSE_1_0.txt or copy at
+// http://www.boost.org/LICENSE_1_0.txt
+
+// See http://www.boost.org for updates, documentation, and revision history.
+
+#include <boost/utility/enable_if.hpp>
+#include <boost/type_traits/is_integral.hpp>
+#include <boost/type_traits/is_signed.hpp>
+#include <boost/type_traits/is_unsigned.hpp>
+#include <boost/type_traits/make_unsigned.hpp>
+#include <iostream>
+#include <limits>
+
+namespace boost
+{
+
+template <typename T>
+inline typename enable_if<is_signed<T>, int>::type
+sign(T data)
+{
+ const int shift = std::numeric_limits<T>::digits;
+
+ // -1 if data is negative
+ register const int is_negative = data >> shift;
+
+ typename make_unsigned<T>::type negative =
+ static_cast<typename make_unsigned<T>::type>(-data);
+
+ // 1 if data is positive
+ register const int is_positive = negative >> shift;
+
+ return is_negative | is_positive;
+}
+
+template <typename T>
+inline typename enable_if<is_unsigned<T>, int>::type
+sign(T data)
+{
+ return int(!!data);
+}
+
+} // boost
\ No newline at end of file

Modified: sandbox/SOC/2010/bits_and_ints/libs/integer/test/Jamfile.v2
==============================================================================
--- sandbox/SOC/2010/bits_and_ints/libs/integer/test/Jamfile.v2 (original)
+++ sandbox/SOC/2010/bits_and_ints/libs/integer/test/Jamfile.v2 2010-06-04 19:22:22 EDT (Fri, 04 Jun 2010)
@@ -17,6 +17,7 @@
         [ run static_min_max_test.cpp ]
                 [ run sign_extend_test.cpp ]
                 [ run bit_reversal_test.cpp ]
+ [ run sign_test.cpp ]
         [ compile cstdint_include_test.cpp ]
         [ compile integer_traits_include_test.cpp ]
         [ compile integer_include_test.cpp ]

Modified: sandbox/SOC/2010/bits_and_ints/libs/integer/test/bit_reversal_test.cpp
==============================================================================
--- sandbox/SOC/2010/bits_and_ints/libs/integer/test/bit_reversal_test.cpp (original)
+++ sandbox/SOC/2010/bits_and_ints/libs/integer/test/bit_reversal_test.cpp 2010-06-04 19:22:22 EDT (Fri, 04 Jun 2010)
@@ -30,7 +30,7 @@
         BIT_REVERSAL_TEST(uint8_t(0), uint8_t(0));
         BIT_REVERSAL_TEST(uint8_t(1), uint8_t(1) << 7);
         BIT_REVERSAL_TEST(uint8_t(0xF0), uint8_t(0x0F));
- BIT_REVERSAL_TEST(uint8_t(0x50), uint8_t(0x0A));
+ BIT_REVERSAL_TEST(uint8_t(0x50), uint8_t(0x0A));
         
         for (uint8_t i = 2, acc = 2; i < 8; ++i) {
                 BIT_REVERSAL_TEST(acc, (uint8_t(1) << (8 - i)));
@@ -40,7 +40,7 @@
         BIT_REVERSAL_TEST(uint16_t(0), uint16_t(0));
         BIT_REVERSAL_TEST(uint16_t(1), uint16_t(1) << 15);
         BIT_REVERSAL_TEST(uint16_t(0xF000), uint16_t(0x000F));
- BIT_REVERSAL_TEST(uint16_t(0x5000), uint16_t(0x000A));
+ BIT_REVERSAL_TEST(uint16_t(0x5000), uint16_t(0x000A));
         BIT_REVERSAL_TEST(uint16_t(0xA000), uint16_t(0x0005));
         BIT_REVERSAL_TEST(uint16_t(0x1000), uint16_t(0x0008));
         BIT_REVERSAL_TEST(uint16_t(0xFF00), uint16_t(0x00FF));

Modified: sandbox/SOC/2010/bits_and_ints/libs/integer/test/sign_extend_test.cpp
==============================================================================
--- sandbox/SOC/2010/bits_and_ints/libs/integer/test/sign_extend_test.cpp (original)
+++ sandbox/SOC/2010/bits_and_ints/libs/integer/test/sign_extend_test.cpp 2010-06-04 19:22:22 EDT (Fri, 04 Jun 2010)
@@ -11,7 +11,7 @@
 #include <boost/cstdlib.hpp> // for boost::exit_success
 #include <boost/integer/bits_and_ints.hpp> // for boost::sign_extend and boost::static_sign_extend
 
- #include <iostream> // for std::cout (std::endl indirectly)
+#include <iostream> // for std::cout (std::endl indirectly)
 
 
 // Macros to compact code

Added: sandbox/SOC/2010/bits_and_ints/libs/integer/test/sign_test.cpp
==============================================================================
--- (empty file)
+++ sandbox/SOC/2010/bits_and_ints/libs/integer/test/sign_test.cpp 2010-06-04 19:22:22 EDT (Fri, 04 Jun 2010)
@@ -0,0 +1,57 @@
+// Boost bit_reversal_test.hpp test program --------------------------------------//
+
+// (C) Copyright Murilo Adriano Vasconcelos 2010.
+// Distributed under the Boost Software License, Version 1.0. (See
+// accompanying file LICENSE_1_0.txt or copy at
+// http://www.boost.org/LICENSE_1_0.txt
+
+#include <boost/detail/lightweight_test.hpp>
+#include <boost/integer/bits_and_ints.hpp>
+#include <iostream>
+
+
+// Macros to compact code
+#define SIGN_TEST(n, m) \
+BOOST_TEST(::boost::sign(n) == m)
+
+
+// Main testing function
+int main(int, char* [])
+{
+ std::cout << "Doing tests on sign functions." << std::endl;
+
+ // Signed tests
+ for (int i = -10000; i != 0; ++i) {
+ SIGN_TEST(i, -1);
+ }
+
+ for (int i = 1; i != 10000; ++i) {
+ SIGN_TEST(i, 1);
+ }
+
+ SIGN_TEST(0, 0);
+
+ // Unsigned tests
+ SIGN_TEST(unsigned(-1), 1);
+ for (unsigned i = -1000; i != 40000; ++i) SIGN_TEST(i, (i != 0));
+
+ SIGN_TEST(unsigned(0), 0);
+
+
+#ifndef BOOST_NO_INT64_T
+
+ // 64-bit tests
+ for (int64_t i = -10000002000; i != -10000000000; ++i) {
+ SIGN_TEST(i, -1);
+ }
+
+ for (int64_t i = 11000000000; i != 11000002000; ++i) {
+ SIGN_TEST(i, 1);
+ }
+
+ SIGN_TEST(int64_t(0), 0);
+#endif
+
+ 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