Big integer algorithm [12] signed multiplication, integer multiplication

Source: Internet
Author: User

Big integer algorithm [12] signed multiplication, integer multiplication

★Introduction

In the previous three articles, we talked about Comba multiplication and Karatsuba multiplication. With these two algorithms, we can easily construct signed number multiplication.

By the way: when we talk about the implementation of Comba multiplication, we provide the inline assembly implementation in the x86 environment, and recently added the inline assembly in the GCC x64 environment, which has been added to the post.

 

★Implementation

The basic implementation of the multiplication of signed numbers is as follows: Use Karatsuba to multiply large integers, and use Comba to multiply small integers. For a large integer, Karatsuba multiplication is performed recursively until the input integer is smaller than a certain scale. Then, the Comba method is used for direct calculation, which reduces the time complexity of multiplication, however, it does not spend too much time on small integers. The specific implementation code is as follows:

int bn_mul_bn(bignum *z, const bignum *x, const bignum *y){    int ret;    bignum ta[1], tb[1];    bn_init(ta);    bn_init(tb);    if(BN_MIN(x->used, y->used) >= KARATSUBA_MUL_CUTOFF)    {        BN_CHECK(bn_mul_karatsuba(z, x, y));    }    else    {        if(x == z)        {            BN_CHECK(bn_copy(ta, x));            x = ta;        }        if(y == z)        {            BN_CHECK(bn_copy(tb, y));            y = tb;        }        BN_CHECK(bn_grow(z, x->used + y->used));        BN_CHECK(bn_set_word(z, 0));        z->used = x->used + y->used;        bn_mul_comba(z, x, y);        z->sign = (x->sign == y->sign) ? 1 : -1;    }clean:    bn_free(ta);    bn_free(tb);    return ret;}

At the beginning, the algorithm checks the size of input x and y. If the digits of x and y are greater than or equal to the split point KARATSUBA_MUL_CUTOFF, Karatsuba multiplication is used for calculation. Otherwise, the Comba method is used.

When using the Comba method, first increase the precision of the target result so that the computing result can be stored lossless, then set z to 0, and call the function of the Comba method for calculation, finally, set the symbol bit (positive for the same number and negative for the difference ). After all calculations are completed, the memory of the temporary variables is cleared. Since ta and tb are initialized at the beginning, even if no memory is allocated, there will be no error in clearing them.

When using the Comba method, in order to process the same input and output variables (x = x * y, y = x * y, x = x * x, y = y * y). Use the Temporary Variable ta and tb. In this case, copy the input integer to the temporary variable, and then point x or y to ta or tb, this avoids setting x or y to 0 in the calculation (Because z is set to 0 at the beginning ).

★Summary

Because of the above preparations, there is nothing to say about this algorithm. In the next article, we will talk about single-digit multiplication. single-digit multiplication cannot follow the single-digit addition and subtraction method, because the use of Comba or Karatsuba increases the calculation workload, so it is more reasonable to implement it separately.

 

 

[Back to the directory of this series]

 

 

Copyright Notice
Original blog, reproduced must contain this statement, to maintain the integrity of this article, and in the form of hyperlink to indicate the author Starrybird and the original address of this article: http://www.cnblogs.com/starrybird/p/4451758.html

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.