Boost logo

Boost-Commit :

From: zeux_at_[hidden]
Date: 2007-05-17 15:16:02


Author: zeux
Date: 2007-05-17 15:16:01 EDT (Thu, 17 May 2007)
New Revision: 4101
URL: http://svn.boost.org/trac/boost/changeset/4101

Log:
Removed redundant from_number functions, implemented converting from 64-bit (both negative and positive), fixed test, updated todo.

Text files modified:
   sandbox/SOC/2007/bigint/boost/bigint/bigint.hpp | 10 ----------
   sandbox/SOC/2007/bigint/boost/bigint/bigint_gmp.hpp | 29 ++++++++++++++++++++++++++---
   sandbox/SOC/2007/bigint/libs/bigint/test/bigint_simple_test.cpp | 6 +++---
   sandbox/SOC/2007/bigint/libs/bigint/todo.txt | 6 +++---
   4 files changed, 32 insertions(+), 19 deletions(-)

Modified: sandbox/SOC/2007/bigint/boost/bigint/bigint.hpp
==============================================================================
--- sandbox/SOC/2007/bigint/boost/bigint/bigint.hpp (original)
+++ sandbox/SOC/2007/bigint/boost/bigint/bigint.hpp 2007-05-17 15:16:01 EDT (Thu, 17 May 2007)
@@ -402,16 +402,6 @@
                 
                 return lhs;
         }
-
- static bigint_base from_number(boost::int64_t value)
- {
- return bigint_base((int)value);
- }
-
- static bigint_base from_number(boost::uint64_t value)
- {
- return bigint_base((int)value);
- }
 };
 
 } // namespace boost

Modified: sandbox/SOC/2007/bigint/boost/bigint/bigint_gmp.hpp
==============================================================================
--- sandbox/SOC/2007/bigint/boost/bigint/bigint_gmp.hpp (original)
+++ sandbox/SOC/2007/bigint/boost/bigint/bigint_gmp.hpp 2007-05-17 15:16:01 EDT (Thu, 17 May 2007)
@@ -14,7 +14,7 @@
 
 #include <boost/bigint/bigint_util.hpp>
 
-#include "gmp.h"
+#include <gmp.h>
 
 namespace boost { namespace detail {
         // GMP-based implementation
@@ -59,12 +59,35 @@
 
                 void assign(int64_t number)
                 {
- mpz_set_si(data, number);
+ // number is [-2^32, 2^32-1]
+ // if number == -2^32, it's bit representation is 10...0, -number is 01...1+1 = 10...0 (the same)
+ // converting to uint64_t yields still 10...0, it's exactly 2^32. In other cases we're safe.
+ assign(static_cast<uint64_t>(number >= 0 ? number : -number));
+
+ if (number < 0) mpz_neg(data, data);
                 }
 
                 void assign(uint64_t number)
                 {
- mpz_set_ui(data, number);
+ mp_size_t size;
+
+ data->_mp_d[0] = number & GMP_NUMB_MAX;
+ size = number != 0;
+
+ if (number > GMP_NUMB_MAX)
+ {
+ mpz_realloc(data, 64 / GMP_NUMB_BITS); // we know that GMP_NUMB_BITS is 2^n
+
+ number >>= GMP_NUMB_BITS;
+
+ while (number > 0)
+ {
+ data->_mp_d[size++] = number & GMP_NUMB_MAX;
+ number >>= GMP_NUMB_BITS;
+ }
+ }
+
+ data->_mp_size = size;
                 }
                 
                 template <typename Ch> void assign_str(const Ch* str, int base)

Modified: sandbox/SOC/2007/bigint/libs/bigint/test/bigint_simple_test.cpp
==============================================================================
--- sandbox/SOC/2007/bigint/libs/bigint/test/bigint_simple_test.cpp (original)
+++ sandbox/SOC/2007/bigint/libs/bigint/test/bigint_simple_test.cpp 2007-05-17 15:16:01 EDT (Thu, 17 May 2007)
@@ -35,7 +35,7 @@
         
         b %= a;
         
- BOOST_CHECK(!(a / b));
+ BOOST_CHECK_EQUAL(!(a / b), false);
         BOOST_CHECK(a - b);
 
         BOOST_CHECK_EQUAL(a * b, bigint("42258228219342334666689684483132205000478474"));
@@ -56,11 +56,11 @@
         BOOST_CHECK_EQUAL(b.can_convert_to<short>(), false);
 
         boost::uint64_t ee = 0xffffffff;
- bigint e = bigint::from_number(ee);
+ bigint e = bigint(ee + 1);
 
         bigint f = ee;
 
- BOOST_CHECK_EQUAL(e, bigint("4294967295"));
+ BOOST_CHECK_EQUAL(e, bigint("4294967296"));
 
         unsigned short xx = 34;
         bigint g = xx;

Modified: sandbox/SOC/2007/bigint/libs/bigint/todo.txt
==============================================================================
--- sandbox/SOC/2007/bigint/libs/bigint/todo.txt (original)
+++ sandbox/SOC/2007/bigint/libs/bigint/todo.txt 2007-05-17 15:16:01 EDT (Thu, 17 May 2007)
@@ -68,10 +68,10 @@
 + revise bigint->string conversion, it is not standard-compliant (std::string is not required to be contiguous)
 Status: fixed
 
-- proper 64-bit support for converting from and to bigint (implement one of suggestions given for the request at gmp devlist)
-Status: needs implementing
++ proper 64-bit support for converting to bigint (implement one of suggestions given for the request at gmp devlist)
+Status: implemented
 
-- proper converting to numbers
+- proper converting to numbers (including 64-bit)
 Status: needs implementing
 
 - check semantics of bitwise and shift operations for negative numbers


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