|
ADS
Algorithms and Data Structures
|
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 |
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:
\[ 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 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.
| #define ADS_BIGINT_GMP 0 |