ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
BigInteger.h File Reference

The integers of any length of Knuth (4.3.1): a sign and the magnitude as a vector of digits (limbs) in base \( b = 2^{64} \), the least significant first, with no leading zero, the first two in place in the number, so that the numbers of one or two digits are made with no allocation, and the longer ones in a block of the heap that grows by doubling (detail::Limbs). More...

#include "ADS/Contract.h"
#include "ADS/Portable.h"
#include "ADS/Wide.h"
#include <algorithm>
#include <bit>
#include <compare>
#include <concepts>
#include <cstddef>
#include <cstdint>
#include <initializer_list>
#include <memory>
#include <string>
#include <utility>
#include <vector>
#include "ADS/numeric/impl/BigInteger.tpp"

Go to the source code of this file.

Classes

class  ADS::BigInteger
 an integer of any length, sign and magnitude in base \( 2^{64} \) More...

Namespaces

namespace  ADS

Macros

#define ADS_BIGINT_GMP   0

Detailed description

The integers of any length of Knuth (4.3.1): a sign and the magnitude as a vector of digits (limbs) in base \( b = 2^{64} \), the least significant first, with no leading zero, the first two in place in the number, so that the numbers of one or two digits are made with no allocation, and the longer ones in a block of the heap that grows by doubling (detail::Limbs).

With ADS_WITH_GMP defined (by the option of CMake of the same name, which defines it whenever GMP is found, by the makefile, which does the same, or by hand), every operation goes to GMP, at every length: the digits of BigInteger are those of the mpn layer, read in place, and the sum, the difference, the order, the product, the quotient with the remainder, the square root and the conversion from a string write their result in place too (mpn_add(), mpn_sub(), mpn_cmp(), mpn_mul(), mpn_sqr(), mpn_tdiv_qr(), mpn_sqrtrem(), mpn_set_str()), while the quotient alone, the exact quotient, the greatest common divisor, the power and the conversion to a string go to the mpz layer (mpz_tdiv_q(), mpz_divexact(), mpz_gcd(), mpz_pow_ui(), mpz_get_str()) on views of the digits in place (mpz_roinit_n()), the digits of the result copied from it in \( O( n ) \), against the superlinear cost of the operation. Where the result fits the digits in place, the number of GMP is spared: the quotient by a divisor of one or two digits goes to mpn_tdiv_qr(), its remainder kept in place, and the gcd with a number of one digit to mpn_gcd_1(). GMP chooses its own algorithms and thresholds for the processor it was built for. All this holds where a limb of GMP is a word of 64 bits, all of it number; elsewhere, and with ADS_BIGINT_GMP defined to 0, BigInteger keeps the code of the book below. GMP is free software under the LGPL v3 (or the GPL v2), whose terms apply to a program that enables it; note that GMP aborts where it cannot allocate, while the code of the book throws std::bad_alloc [see The backends of GMP and of a CBLAS].

Without the backend, and in the methods asked by name (multiplySchool(), multiplyKaratsuba(), multiplyFFT(), divModSchool(), toStringSchool(), fromStringSchool(), sqrtNewton(), gcdEuclid()), which the audits compare with the operations, the algorithms are those of the book, written for clarity and portability, with no assembly, no vector instructions and no threshold measured on a processor:

  • the sum and the difference are Algorithms 4.3.1A and 4.3.1S, a carry or a borrow per digit, \( O( n ) \);
  • the product is Algorithm 4.3.1M, \( O( m n ) \), below kKaratsuba digits of the shorter factor; from there the method of Karatsuba (4.3.3), with \( a = a_1 \beta + a_0 \) and \( b = b_1 \beta + b_0 \),

    \[ a b = z_2 \beta^2 + \bigl( ( a_0 + a_1 ) ( b_0 + b_1 ) - z_0 - z_2 \bigr) \beta + z_0 , \qquad z_0 = a_0 b_0 , \; z_2 = a_1 b_1 , \]

    \( M( n ) = 3 M( n / 2 ) + O( n ) = O( n^{\log_2 3} ) \); from kFFT digits the convolution of the digits by the number theoretic transform (4.3.3 and 4.6.4): the digits cut in pieces of 32 bits, the cyclic convolution of the pieces modulo three primes \( p < 2^{31} \) with \( 2^{26} \mid p - 1 \) by the transform of Cooley and Tukey, and each coefficient, below \( 2^{89} \) and so below the product of the primes, from its three residues by the Chinese remainder theorem in the form of Garner (4.3.2), \( O( n \log n ) \) operations on words up to \( 2^{25} \) digits in all, past which Karatsuba splits the factors;
  • the quotient and the remainder are Algorithm 4.3.1D, \( O( m n ) \) for a quotient of \( m \) digits and a divisor of \( n \): the divisor shifted until its leading digit has the top bit set, each trial quotient \( \hat q \) from the two leading digits of the remainder and corrected by the test of step D3, which leaves it at most one above the true one, the rare excess undone by the add back of step D6; by a divisor of one digit the short division of exercise 16 of section 4.3.1;
  • the conversion to and from the bases 2 to 36 is that of section 4.4: in the bases that are powers of 2 a slice of bits per figure, \( O( n ) \), in the others by the largest power \( P \) of the base in one digit (\( 10^{19} \) for the base 10), the remainders of the short divisions by \( P \) on the way out and the products \( x \gets x P + c \) on the way in, \( O( n^2 ) \);
  • pow() is the binary method of 4.6.3, from left to right;
  • gcd() is the algorithm of Lehmer (4.5.2L): the leading 60 bits \( \hat u \), \( \hat v \) of the two numbers run the algorithm of Euclid on \( ( \hat u + A , \hat v + C ) \) and on \( ( \hat u + B , \hat v + D ) \) while the two quotients agree, so that they are those of the numbers, and one pass makes \( ( u , v ) \gets ( A u + B v , C u + D v ) \), about 30 bits off both; a pass that the leading bits cannot make is a division (4.5.2A), and the last digit goes by the binary algorithm on words (4.5.2B), \( O( n^2 ) \) in all;
  • sqrtRem() and sqrt() take the root of sqrtNewton(), Newton's method from above, \( x \gets \lfloor ( x + \lfloor a / x \rfloor ) / 2 \rfloor \) from a power of 2 at least the root, and the remainder \( a - s^2 \);
  • divExact() is the quotient of Algorithm D.

The contracts [see Contract.h]: the divisor is not 0, the base is in \( [ 2 , 36 ] \) and the string a number in it; the invariant is the normal form (no leading zero, 0 not negative); divMod() ensures \( a = q b + r \) with \( |r| < |b| \) and \( r \) of the sign of \( a \), and that it equals divModSchool(), operator/() that its quotient does, divExact() that its result times \( b \) is \( a \); multiplyKaratsuba() and multiplyFFT() that they equal multiplySchool(); the conversions that they equal toStringSchool() and fromStringSchool(), gcd() that it equals gcdEuclid(), sqrtRem() that \( s^2 + r = a \) and that \( s \) equals sqrtNewton(), all audits. The operations are defined in BigInteger.tpp.

See also
D. E. Knuth, The Art of Computer Programming, vol. 2, 3rd ed., Addison-Wesley, 1998, sections 4.3.1, 4.3.2, 4.3.3, 4.4, 4.5.2, 4.6.3 and 4.6.4
A. Karatsuba, Yu. Ofman, Multiplication of multidigit numbers on automata, Soviet Physics Doklady 7, 595-596, 1963
J. M. Pollard, The fast Fourier transform in a finite field, Mathematics of Computation 25(114), 365-374, 1971, doi:10.1090/S0025-5718-1971-0301966-0
J. W. Cooley, J. W. Tukey, An algorithm for the machine calculation of complex Fourier series, Mathematics of Computation 19(90), 297-301, 1965, doi:10.1090/S0025-5718-1965-0178586-1
D. H. Lehmer, Euclid's algorithm for large numbers, The American Mathematical Monthly 45(4), 227-233, 1938, doi:10.1080/00029890.1938.11990797
T. Granlund and the GMP development team, GNU MP: The GNU Multiple Precision Arithmetic Library, version 6.3.0, 2023, https://gmplib.org/gmp-man-6.3.0.pdf, the layers mpn and mpz of the backend
Author
Donato Meoli
Dipartimento di Informatica
Università di Pisa

Macro definition documentation

◆ ADS_BIGINT_GMP

#define ADS_BIGINT_GMP   0