浏览代码

Injecting an offset parameter into addition and subtraction.

Samuel Jaffe 8 年之前
父节点
当前提交
6737f6b06e
共有 2 个文件被更改,包括 11 次插入11 次删除
  1. 2 2
      include/bignum_helper.h
  2. 9 9
      src/bignum_helper.cpp

+ 2 - 2
include/bignum_helper.h

@@ -13,8 +13,8 @@ namespace math { namespace detail {
   using data_type = std::vector<int32_t>;
   // 1 => GREATER, 0 => EQUAL, -1 => LESS
   int compare(data_type const & rhs, data_type const & lhs);
-  void add(data_type & rhs, data_type const & lhs);
-  void subtract_nounderflow(data_type & rhs, data_type const & lhs);
+  void add(data_type & rhs, data_type const & lhs, size_t offset = 0);
+  void subtract_nounderflow(data_type & rhs, data_type const & lhs, size_t offset = 0);
   void multiply(data_type & rhs, data_type const & lhs);
   data_type divide(data_type & remainder, data_type const & divisor);
 } }

+ 9 - 9
src/bignum_helper.cpp

@@ -30,12 +30,12 @@ namespace math { namespace detail {
   }
 #undef IMPL_COMPARE
   
-  void add(data_type & rhs, data_type const & lhs) {
-    rhs.resize(std::max(rhs.size(), lhs.size())+1);
+  void add(data_type & rhs, data_type const & lhs, size_t offset) {
+    rhs.resize(std::max(rhs.size(), lhs.size()+offset)+1);
     auto const lbnd = lhs.size(), ubnd = rhs.size() - 1;
     // Add
     for (size_t i = 0; i < lbnd; ++i) {
-      rhs[i] += lhs[i];
+      rhs[i+offset] += lhs[i];
     }
     // Carry
     for (size_t i = 0; i < ubnd; ++i) {
@@ -47,11 +47,11 @@ namespace math { namespace detail {
     if (rhs[ubnd] == 0) { rhs.pop_back(); }
   }
   
-  void subtract_nounderflow(data_type & rhs, data_type const & lhs) {
+  void subtract_nounderflow(data_type & rhs, data_type const & lhs, size_t offset) {
     size_t const rbnd = rhs.size(), lbnd = lhs.size();
     // Subtract
     for (size_t i = 0; i < lbnd; ++i) {
-      rhs[i] -= lhs[i];
+      rhs[i+offset] -= lhs[i];
     }
     // Borrow
     for (size_t i = 0; i < rbnd; ++i) {
@@ -121,11 +121,11 @@ namespace math { namespace detail {
       auto const diff = std::max(1UL, digits(remainder) - dig) - 1;
       auto const shift = diff / SEG_DIGITS;
       auto const ipow = diff - (shift * SEG_DIGITS);
-      data_type step{shift10({1}, powers[ipow], shift)};
-      data_type value{shift10(divisor, powers[ipow], shift)};
+      data_type step{shift10({1}, powers[ipow], 0)};
+      data_type value{shift10(divisor, powers[ipow], 0)};
       do {
-        subtract_nounderflow(remainder, value);
-        add(accum, step);
+        subtract_nounderflow(remainder, value, shift);
+        add(accum, step, shift);
       } while (compare(remainder, value) >= 0);
     } while (compare(remainder, divisor) >= 0);
     return accum;