Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r50635 - in sandbox/mp_math: boost/mp_math/mp_int libs/mp_math/test
From: baraclese_at_[hidden]
Date: 2009-01-16 12:02:46


Author: baraclese
Date: 2009-01-16 12:02:45 EST (Fri, 16 Jan 2009)
New Revision: 50635
URL: http://svn.boost.org/trac/boost/changeset/50635

Log:
* mp_int/add.hpp
  Fixed bug reported by Jeroen N. Witmond on Jan 16, 2009 on boost dev mailinglist.
  Added small optimization by calling sub_single_digit directly.
* mp_int/sub.hpp
  Added small optimization by calling add_single_digit directly.

Text files modified:
   sandbox/mp_math/boost/mp_math/mp_int/add.hpp | 23 +++++++++++------------
   sandbox/mp_math/boost/mp_math/mp_int/sub.hpp | 9 +++++----
   sandbox/mp_math/libs/mp_math/test/integral_ops.cpp | 7 +++++++
   3 files changed, 23 insertions(+), 16 deletions(-)

Modified: sandbox/mp_math/boost/mp_math/mp_int/add.hpp
==============================================================================
--- sandbox/mp_math/boost/mp_math/mp_int/add.hpp (original)
+++ sandbox/mp_math/boost/mp_math/mp_int/add.hpp 2009-01-16 12:02:45 EST (Fri, 16 Jan 2009)
@@ -1,4 +1,4 @@
-// Copyright Kevin Sopp 2008.
+// Copyright Kevin Sopp 2008 - 2009.
 // 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)
@@ -16,27 +16,26 @@
   }
   else
   {
- if (digits_[0] >= b) // example: -16 + 5 = -11; or -5 + 5 = 0
- {
+ if (digits_[0] > b) // example: -16 + 5 = -11
       digits_[0] -= b;
- if (!*this)
- set_sign(1);
- }
     else
     {
- if (size_ == 1) // example: -1 + 5 = 4
- digits_[0] = b - digits_[0];
- else // example -11 + 5 = -6
+ if (size_ == 1) // example: -1 + 5 = 4, or -5 + 5 = 0
       {
+ digits_[0] = b - digits_[0];
         set_sign(1);
- sub_digit(b);
- set_sign(-1);
+ }
+ else // example -1000 + 5 = -995
+ {
+ ops_type::subtract_single_digit(digits_, digits_, size_, b);
+ if (!digits_[size_-1])
+ --size_;
       }
     }
   }
 }
 
-/* low level addition, based on HAC pp.594, Algorithm 14.7 */
+// low level addition, based on HAC pp.594, Algorithm 14.7
 // does not handle sign
 template<class A, class T>
 void mp_int<A,T>::add_magnitude(const mp_int& rhs)

Modified: sandbox/mp_math/boost/mp_math/mp_int/sub.hpp
==============================================================================
--- sandbox/mp_math/boost/mp_math/mp_int/sub.hpp (original)
+++ sandbox/mp_math/boost/mp_math/mp_int/sub.hpp 2009-01-16 12:02:45 EST (Fri, 16 Jan 2009)
@@ -1,4 +1,4 @@
-// Copyright Kevin Sopp 2008.
+// Copyright Kevin Sopp 2008 - 2009.
 // 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)
@@ -8,9 +8,10 @@
 {
   if (is_negative())
   {
- set_sign(1);
- add_digit(b);
- set_sign(-1);
+ const digit_type carry =
+ ops_type::add_single_digit(digits_, digits_, size_, b);
+ if (carry)
+ push(carry);
     return;
   }
 

Modified: sandbox/mp_math/libs/mp_math/test/integral_ops.cpp
==============================================================================
--- sandbox/mp_math/libs/mp_math/test/integral_ops.cpp (original)
+++ sandbox/mp_math/libs/mp_math/test/integral_ops.cpp 2009-01-16 12:02:45 EST (Fri, 16 Jan 2009)
@@ -82,6 +82,13 @@
   BOOST_CHECK_EQUAL(z, "987776");
 }
 
+BOOST_AUTO_TEST_CASE_TEMPLATE(add_signed_integral3, mp_int_type, mp_int_types)
+{
+ const mp_int_type x("-1");
+ const mp_int_type z = x + 5;
+ BOOST_CHECK_EQUAL(z, "4");
+}
+
 BOOST_AUTO_TEST_CASE_TEMPLATE(add_unsigned_integral1, mp_int_type, mp_int_types)
 {
   const mp_int_type x("9999999");


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