135#ifndef __ADS_BigInteger
136 #define __ADS_BigInteger
153#include <initializer_list>
163#if defined( ADS_WITH_GMP )
166 #if ! defined( ADS_BIGINT_GMP ) && ( GMP_LIMB_BITS == 64 ) && \
167 ( GMP_NAIL_BITS == 0 )
168 #define ADS_BIGINT_GMP 1
171#ifndef ADS_BIGINT_GMP
172 #define ADS_BIGINT_GMP 0
199 using T = std::uint64_t;
202 Limbs(
void )
noexcept { }
204 explicit Limbs( std::size_t n ) { resize( n ); }
206 Limbs( std::size_t n , T v ) { resize( n , v ); }
208 Limbs(
const T * b ,
const T * e ) {
209 reserve( std::size_t( e - b ) );
210 std::copy( b , e , f_p );
211 f_n = std::size_t( e - b );
214 Limbs( std::initializer_list< T > l ) : Limbs( l.begin() , l.end() ) { }
216 Limbs(
const Limbs & o ) : Limbs( o.f_p , o.f_p + o.f_n ) { }
218 Limbs( Limbs && o )
noexcept { take( o ); }
220 Limbs & operator=(
const Limbs & o ) {
222 if( o.f_n > f_cap ) {
223 T * q = std::allocator< T >().allocate( o.f_n );
228 std::copy( o.f_p , o.f_p + o.f_n , f_p );
234 Limbs & operator=( Limbs && o )
noexcept {
241 ~Limbs(
void ) { release(); }
244 [[nodiscard]] std::size_t size(
void )
const {
return( f_n ); }
246 [[nodiscard]]
bool empty(
void )
const {
return( f_n == 0 ); }
248 T * data(
void ) {
return( f_p ); }
250 const T * data(
void )
const {
return( f_p ); }
252 T * begin(
void ) {
return( f_p ); }
254 T * end(
void ) {
return( f_p + f_n ); }
256 const T * begin(
void )
const {
return( f_p ); }
258 const T * end(
void )
const {
return( f_p + f_n ); }
260 T & operator[]( std::size_t i ) {
return( f_p[ i ] ); }
262 const T & operator[]( std::size_t i )
const {
return( f_p[ i ] ); }
264 T & back(
void ) {
return( f_p[ f_n - 1 ] ); }
266 const T & back(
void )
const {
return( f_p[ f_n - 1 ] ); }
269 void reserve( std::size_t n ) {
271 regrow( std::max( n , 2 * f_cap ) );
274 void resize( std::size_t n , T v = 0 ) {
277 std::fill( f_p + f_n , f_p + n , v );
281 void assign( std::size_t n , T v ) {
286 void push_back( T v ) {
291 void pop_back(
void ) { --f_n; }
294 friend bool operator==(
const Limbs & a ,
const Limbs & b ) {
295 return( ( a.f_n == b.f_n ) && std::equal( a.f_p , a.f_p + a.f_n , b.f_p ) );
299 static constexpr std::size_t kIn = 2;
302 void regrow( std::size_t c ) {
303 T * q = std::allocator< T >().allocate( c );
304 std::copy( f_p , f_p + f_n , q );
310 void release(
void )
noexcept {
312 std::allocator< T >().deallocate( f_p , f_cap );
319 void take( Limbs & o )
noexcept {
322 if( o.f_p != o.f_in )
325 std::copy( o.f_in , o.f_in + o.f_n , f_in );
334 std::size_t f_n = 0 ,
359 static constexpr std::size_t
kFFT = 4096;
383 explicit BigInteger(
const std::string & s ,
int base = 10 );
405 [[nodiscard]] std::string
toString(
int base = 10 )
const;
419 [[nodiscard]]
int sign(
void )
const;
424 [[nodiscard]] std::size_t
limbs(
void )
const;
426#if defined( ADS_WITH_GMP )
434 requires requires(
const Z & z ) {
435 { z.get_mpz_t() } -> std::convertible_to< mpz_srcptr >;
442 void toGmp( mpz_ptr z )
const;
447 requires requires( Z & z ) {
448 { z.get_mpz_t() } -> std::convertible_to< mpz_ptr >;
450 [[nodiscard]] Z toGmp(
void )
const {
452 toGmp( z.get_mpz_t() );
533 static std::pair< BigInteger , BigInteger >
722 using Mag = detail::Limbs;
726 static constexpr std::size_t kNTTMax = std::size_t( 1 ) << 25;
741 static void trim( Mag & d );
743 static int compareMag(
const Mag & a ,
const Mag & b );
747 static Limb addN(
Limb * r ,
const Limb * a , std::size_t na ,
752 static Limb subN(
Limb * r ,
const Limb * a , std::size_t na ,
761 static Mag addMag(
const Mag & a ,
const Mag & b );
763 static Mag subMag(
const Mag & a ,
const Mag & b );
770 static void mulMag( Mag & r ,
const Mag & a ,
const Mag & b ,
771 Method how = Method::AUTO );
777 static void mulSchool(
Limb * r ,
const Limb * a , std::size_t na ,
778 const Limb * b , std::size_t nb );
781 static std::size_t mulScratch( std::size_t n );
786 static void mulRec(
Limb * r ,
const Limb * a , std::size_t na ,
788 std::size_t nb , Method how ,
Limb * w );
790 static Mag mulNTT(
const Limb * a , std::size_t na ,
const Limb * b ,
795 static Limb divSmall( Mag & a ,
Limb d );
797 static void mulAddSmall( Mag & a ,
Limb m ,
Limb c );
800 static std::pair< Mag , Mag > divModMag(
const Mag & u ,
const Mag & v ,
805 static void figures( Mag a ,
int base , std::string & out );
808 static std::uint64_t gcdWord( std::uint64_t a , std::uint64_t b );
810 static Mag gcdLehmer( Mag u , Mag v );
814 static mpz_srcptr gmpView( mpz_t z ,
const Limb * d , std::size_t n );
816 static mpz_srcptr gmpView( mpz_t z ,
const BigInteger & x );
826 [[nodiscard]]
bool normal(
void )
const;
841#include "ADS/numeric/impl/BigInteger.tpp"
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
The primitives of the processor that the compilers spell each in their own way, in one form for all: ...
The arithmetic of two words that C++20 does not spell, in one form for every compiler: the product of...
an integer of any length, sign and magnitude in base
Definition BigInteger.h:348
friend std::strong_ordering operator<=>(const BigInteger &a, const BigInteger &b)
the order of and , by the signs, the lengths and the digits from the most significant
static std::pair< BigInteger, BigInteger > sqrtRem(const BigInteger &a)
the integer square root and : by mpn_sqrtrem() with the backend, else by sqrtNewton()
std::string toStringSchool(int base=10) const
the figures of toString() by the code of the book, with no backend,
std::size_t limbs(void) const
the number of digits (limbs) in base
friend class Rational
Definition BigInteger.h:823
static BigInteger multiplyKaratsuba(const BigInteger &a, const BigInteger &b)
the product by Karatsuba at every length down to factors of fewer than 4 digits, which Algorithm M mu...
BigInteger(const std::string &s, int base=10)
the number written in in the base, with an optional sign
friend bool operator==(const BigInteger &a, const BigInteger &b)=default
true if the two numbers are equal
BigInteger & operator-=(const BigInteger &b)
the difference into
static BigInteger sqrt(const BigInteger &a)
the root of sqrtRem(), with no remainder
BigInteger(long long v=0)
the number
static constexpr std::size_t kFFT
limbs for the NTT
Definition BigInteger.h:359
static BigInteger gcdEuclid(BigInteger a, BigInteger b)
the greatest common divisor of and , by Euclid
friend BigInteger operator*(const BigInteger &a, const BigInteger &b)
the product , by the method of the length of the factors
static BigInteger multiplyFFT(const BigInteger &a, const BigInteger &b)
the product by the number theoretic transform at every length
BigInteger abs(void) const
the absolute value
BigInteger & operator*=(const BigInteger &b)
the product into
friend BigInteger operator%(const BigInteger &a, const BigInteger &b)
the remainder of divMod(), of the sign of
static BigInteger multiplySchool(const BigInteger &a, const BigInteger &b)
the product by Algorithm M,
static BigInteger fromStringSchool(const std::string &s, int base=10)
the number of the constructor from a string by the code of the book, with no backend,...
static std::pair< BigInteger, BigInteger > divModSchool(const BigInteger &a, const BigInteger &b)
the quotient and the remainder by Algorithm D,
int sign(void) const
the sign, , 0 or 1
friend BigInteger operator-(const BigInteger &a, const BigInteger &b)
the difference , as the sum with the sign of changed
std::string toString(int base=10) const
the figures of the number in the base, with a sign if negative
static BigInteger pow(const BigInteger &x, unsigned long n)
the power , by mpz_pow_ui() with the backend, else by the binary method
static BigInteger gcd(const BigInteger &a, const BigInteger &b)
the greatest common divisor of and , by mpz_gcd() with the backend, else by Lehmer
std::uint64_t Limb
a digit, in base
Definition BigInteger.h:352
static void addMul(BigInteger &r, const BigInteger &a, const BigInteger &b)
static std::pair< BigInteger, BigInteger > divMod(const BigInteger &a, const BigInteger &b)
the quotient truncated toward zero and the remainder, by Algorithm D [see divModSchool()] or by the b...
friend BigInteger operator/(const BigInteger &a, const BigInteger &b)
the quotient truncated toward zero
BigInteger operator-(void) const
the opposite
static BigInteger sqrtNewton(const BigInteger &a)
the integer square root by Newton's method from above
BigInteger & operator+=(const BigInteger &b)
the sum into
friend BigInteger operator+(const BigInteger &a, const BigInteger &b)
the sum , by Algorithm 4.3.1A on the magnitudes of the same sign and 4.3.1S on those of opposite sign...
static constexpr std::size_t kKaratsuba
limbs for Karatsuba
Definition BigInteger.h:358
static BigInteger divExact(BigInteger a, const BigInteger &b)
the quotient for a divisor of , by mpz_divexact() with the backend, else as
constexpr detail::Reserve reserve
room for items in : s.reserve( n ), else reserve( s , n ) found by ADL, else nothing (the builders)
Definition Capabilities.h:444