浏览代码

Adding offset handling to multiplication.

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

+ 1 - 1
include/bignum_helper.h

@@ -16,7 +16,7 @@ namespace math { namespace detail {
   int compare(data_type const & rhs, data_type const & lhs, size_t offset = 0);
   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);
+  void multiply(data_type & rhs, data_type const & lhs, size_t offset = 0);
   data_type divide(data_type & remainder, data_type const & divisor);
   data_type shift10(data_type const & data, int32_t places);
 } }

+ 5 - 4
src/bignum_helper.cpp

@@ -72,20 +72,21 @@ namespace math { namespace detail {
     if (rhs[rbnd-1] == 0 && rbnd > 1) { rhs.pop_back(); }
   }
   
-  void multiply(data_type & rhs, data_type const & lhs) {
+  void multiply(data_type & rhs, data_type const & lhs, size_t offset) {
     size_t const rbnd = rhs.size(), lbnd = lhs.size();
     size_t const ubnd = rbnd + lbnd;
     rhs.resize(ubnd);
     // Multiply
     for (size_t i = rbnd; i > 0; --i) {
       int32_t const value = rhs[i-1];
-      for (size_t j = lbnd; j > 0; --j) {
+      for (size_t j = lbnd; j > offset; --j) {
         // Max input              999,999,999
         // Max output 999,999,998,000,000,001
         int64_t product = int64_t(value) * lhs[j-1];
         int64_t overflow = product / OVER_SEG;
-        rhs[i+j-2] += int32_t(product - (overflow * OVER_SEG));
-        rhs[i+j-1] += int32_t(overflow);
+        size_t const to = i+j-2-offset;
+        rhs[to] += int32_t(product - (overflow * OVER_SEG));
+        rhs[to+1] += int32_t(overflow);
       }
       rhs[i-1] -= value;
     }