Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r62452 - in sandbox/SOC/2010/bits_and_ints: boost/integer libs/integer/test
From: muriloufg_at_[hidden]
Date: 2010-06-05 13:34:38


Author: murilov
Date: 2010-06-05 13:34:37 EDT (Sat, 05 Jun 2010)
New Revision: 62452
URL: http://svn.boost.org/trac/boost/changeset/62452

Log:
Added same_sign() function and static_same_sign metafunction
Added:
   sandbox/SOC/2010/bits_and_ints/boost/integer/same_sign.hpp (contents, props changed)
   sandbox/SOC/2010/bits_and_ints/libs/integer/test/same_sign_test.cpp (contents, props changed)
Text files modified:
   sandbox/SOC/2010/bits_and_ints/boost/integer/bits_and_ints.hpp | 1 +
   1 files changed, 1 insertions(+), 0 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-05 13:34:37 EDT (Sat, 05 Jun 2010)
@@ -18,5 +18,6 @@
 #include <boost/integer/bit_reversal.hpp>
 #include <boost/integer/static_bit_reversal.hpp>
 #include <boost/integer/sign.hpp>
+#include <boost/integer/same_sign.hpp>
 
 #endif
\ No newline at end of file

Added: sandbox/SOC/2010/bits_and_ints/boost/integer/same_sign.hpp
==============================================================================
--- (empty file)
+++ sandbox/SOC/2010/bits_and_ints/boost/integer/same_sign.hpp 2010-06-05 13:34:37 EDT (Sat, 05 Jun 2010)
@@ -0,0 +1,51 @@
+// Boost integer/same_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/type_traits/is_integral.hpp>
+#include <boost/utility/enable_if.hpp>
+#include <boost/integer/bit_utils.hpp>
+#include <limits>
+
+namespace boost
+{
+
+/*
+ * same_sign(first, second) returns:
+ * - false: if the signs of first and second are different
+ * - true: if the signs are equal
+ */
+
+template <typename T>
+inline typename enable_if<is_integral<T>, bool>::type
+same_sign(T first, T second)
+{
+ T temp = first ^ second;
+ temp = temp >> ((sizeof(T) * 8) - 1);
+ temp = temp & T(1);
+
+ return !temp;
+}
+
+/*
+ * Compile-time version of same_sign
+ *
+ * static_same_sign<type, FIRST, SECOND>::value will be:
+ * - false: if the signs of FIRST and SECOND are different
+ * - true: if the signs are equal
+ */
+template <typename T, T first, T second, class Enable = typename enable_if<is_integral<T> >::type>
+struct static_same_sign
+{
+private:
+ static const int temp = (first ^ second) >> ((sizeof(T) * 8) - 1);
+public:
+ BOOST_STATIC_CONSTANT(bool, value = !(temp & 1));
+};
+
+}
\ No newline at end of file

Added: sandbox/SOC/2010/bits_and_ints/libs/integer/test/same_sign_test.cpp
==============================================================================
--- (empty file)
+++ sandbox/SOC/2010/bits_and_ints/libs/integer/test/same_sign_test.cpp 2010-06-05 13:34:37 EDT (Sat, 05 Jun 2010)
@@ -0,0 +1,39 @@
+// Boost same_sign_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 SAME_SIGN_TEST(n, m, r) \
+BOOST_TEST(::boost::same_sign(n, m) == r)
+
+#define STATIC_SAME_SIGN_TEST(t, n, m, r) \
+BOOST_TEST((::boost::static_same_sign<t, n, m>::value == r))
+
+
+// Main testing function
+int main(int, char* [])
+{
+ std::cout << "Doing tests on same_sign." << std::endl;
+
+ for (int i = 0; i != 101; ++i) {
+ for (int j = -100; j != 1; ++j) {
+ SAME_SIGN_TEST(i, j, false);
+ STATIC_SAME_SIGN_TEST(int, i, j, false);
+ }
+ for (int j = 0; j != 100; ++j) {
+ SAME_SIGN_TEST(i, j, true);
+ STATIC_SAME_SIGN_TEST(int, i, j, true);
+ }
+ }
+
+ 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