Large integer algorithm [12] symbolic multiplication

Source: Internet
Author: User

★ Intro

In the previous three articles, Comba multiplication and karatsuba multiplication, with these two algorithms, it is easy to construct signed number multiplication.

By the way: when talking about the implementation of the Comba multiplication, the implementation of the inline assembly in the x86 environment is given, and a recent inline compilation of the GCC x64 environment has been added to the post.

★ Achieve

the multiplication of signed numbers, the basic implementation is this: large integers with karatsuba multiplication, small integers with comba multiplication. For large integers, the Karatsuba multiplication will continue to be calculated recursively until the input integer is small to a certain scale, and the Comba method is used to calculate directly, so that the time complexity of multiplication can be reduced, but it does not take too much time to calculate 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;}

The algorithm initially checks the size of the input x and Y, and if the digits of x and Y are greater than or equal to the split point Karatsuba_mul_cutoff, use Karatsuba multiplication to calculate, otherwise use the Comba method.

When using the Comba method, first increase the precision of the target result, so as to be able to store the calculation result without loss, then set Z to 0, call the function of the Comba method to calculate, finally set the sign bit (the same number is positive, the different number is negative). After all calculations are complete, clear the memory for the temporary variable. Since the TA and TB are initialized at the outset, even without allocating memory, there is no error when purging.

When using the Comba method, in order to handle the case where the input and output are the same variable (x = x * y,y = x * y,x = x * y = y * y), the temporary variable TA and TB need to be used. When this happens, copy the input integer to the temporary variable and then point x or Y to TA or TB, which avoids setting x or Y to 0 in the calculation (because Z is set to 0 at the beginning).

        ★< Span style= "Font-family:microsoft Yahei; font-size:15px; " > summary

because there is the front cushion, so the algorithm is nothing good to say. In the next article about single-digit multiplication, single-digit multiplication cannot be done by single-digit addition and subtraction, because using Comba or Karatsuba increases the computational amount, so it is more reasonable to implement it separately.

"Back to this series of catalogs"

Copyright notice
Original blog post, reproduced must contain this statement, to keep this article complete, and in the form of hyperlinks annotated author Starrybird and this article original address: Http://www.cnblogs.com/starrybird/p/4451758.html

Large integer algorithm [12] signed multiplication

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.