Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r50563 - in sandbox/mp_math: boost/mp_math/mp_int libs/mp_math/test
From: baraclese_at_[hidden]
Date: 2009-01-13 09:31:55


Author: baraclese
Date: 2009-01-13 09:31:54 EST (Tue, 13 Jan 2009)
New Revision: 50563
URL: http://svn.boost.org/trac/boost/changeset/50563

Log:
* mp_int/mp_int.hpp
  Added set_precision function.
  Fixed truncate function.
* test/bitmanipulation.cpp
  Added tests for truncate().

Text files modified:
   sandbox/mp_math/boost/mp_math/mp_int/mp_int.hpp | 26 +++++++++++++++++++-------
   sandbox/mp_math/libs/mp_math/test/bitmanipulation.cpp | 40 ++++++++++++++++++++++++++++++++++++++++
   2 files changed, 59 insertions(+), 7 deletions(-)

Modified: sandbox/mp_math/boost/mp_math/mp_int/mp_int.hpp
==============================================================================
--- sandbox/mp_math/boost/mp_math/mp_int/mp_int.hpp (original)
+++ sandbox/mp_math/boost/mp_math/mp_int/mp_int.hpp 2009-01-13 09:31:54 EST (Tue, 13 Jan 2009)
@@ -335,7 +335,6 @@
   void divide_by_2();
   digit_type divide_by_3();
   void modulo_2_to_the_power_of(size_type);
- size_type precision() const;
   size_type count_lsb() const;
   void shift_right(size_type b, mp_int* remainder);
 
@@ -361,6 +360,13 @@
 
   void truncate(size_type prec);
 
+ size_type precision() const;
+
+ void set_precision(size_type bits)
+ {
+ size_ = (bits + (valid_bits - 1)) / valid_bits;
+ }
+
   template<class A, class T>
   friend bool operator == (const mp_int<A,T>&, const mp_int<A,T>&);
 
@@ -745,16 +751,16 @@
 mp_int<A,T>::precision() const
 {
   // get number of digits and add that
- size_type r = (size_ - 1) * valid_bits;
+ size_type p = (size_ - 1) * valid_bits;
 
   // take the last digit and count the bits in it
   digit_type q = digits_[size_ - 1];
   while (q > 0U)
   {
- ++r;
+ ++p;
     q >>= 1;
   }
- return r;
+ return p;
 }
 
 // Counts the number of lsbs which are zero before the first one bit
@@ -850,12 +856,18 @@
   }
 }
 
+// don't forget to clamp() after truncating!
 template<class A, class T>
 void mp_int<A,T>::truncate(size_type prec)
 {
- set_size((prec + digit_bits - 1)/digit_bits);
- const size_type last_bits = prec % digit_bits;
- digits_[size()] &= ~digit_type(0) << (digit_bits - last_bits);
+ set_precision(prec);
+ const size_type last_bits = prec % valid_bits;
+ if (last_bits)
+ {
+ static const digit_type z = ~digit_type(0);
+ const digit_type mask = z >> (valid_bits - last_bits);
+ digits_[size_ - 1] &= mask;
+ }
 }
 
 

Modified: sandbox/mp_math/libs/mp_math/test/bitmanipulation.cpp
==============================================================================
--- sandbox/mp_math/libs/mp_math/test/bitmanipulation.cpp (original)
+++ sandbox/mp_math/libs/mp_math/test/bitmanipulation.cpp 2009-01-13 09:31:54 EST (Tue, 13 Jan 2009)
@@ -76,3 +76,43 @@
   BOOST_CHECK_EQUAL(x, "0x80000000");
 }
 
+BOOST_AUTO_TEST_CASE_TEMPLATE(truncate1, mp_int_type, mp_int_types)
+{
+ mp_int_type x("0xFFFFFFFFFFFFFFFFFFFFFFFFFFFF");
+ x.truncate(32);
+ x.clamp();
+ BOOST_CHECK_EQUAL(x, "0xFFFFFFFF");
+}
+
+BOOST_AUTO_TEST_CASE_TEMPLATE(truncate2, mp_int_type, mp_int_types)
+{
+ mp_int_type x("0xFFFFFFFFFFFFFFFFFFFFFFFFFFFF");
+ x.truncate(0);
+ x.clamp();
+ BOOST_CHECK_EQUAL(x, "");
+}
+
+BOOST_AUTO_TEST_CASE_TEMPLATE(truncate3, mp_int_type, mp_int_types)
+{
+ mp_int_type x("0xFFFFFFFFFFFFFFFFFFFFFFFFFFFF");
+ x.truncate(1);
+ x.clamp();
+ BOOST_CHECK_EQUAL(x, "1");
+}
+
+BOOST_AUTO_TEST_CASE_TEMPLATE(truncate4, mp_int_type, mp_int_types)
+{
+ mp_int_type x("0xFFFFFFFFFFFFFFFFFFFFFFFFFFFF");
+ x.truncate(31);
+ x.clamp();
+ BOOST_CHECK_EQUAL(x, "0x7FFFFFFF");
+}
+
+BOOST_AUTO_TEST_CASE_TEMPLATE(truncate5, mp_int_type, mp_int_types)
+{
+ mp_int_type x("0xFFFFFFFFFFFFFFFFFFFF");
+ x.truncate(80);
+ x.clamp();
+ BOOST_CHECK_EQUAL(x, "0xFFFFFFFFFFFFFFFFFFFF");
+}
+


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