5
ccall((:__gmpz_init,:libgmp), Void, (Ptr{Void},), z)
7
finalizer(b, BigInt_clear)
12
function BigInt(x::String)
14
err = ccall((:__gmpz_set_str, :libgmp), Int32, (Ptr{Void}, Ptr{Uint8}, Ptr{Int32}), z.mpz, bytestring(x), 0)
15
if err != 0; error("Invalid input"); end
19
function BigInt(x::Int)
21
ccall((:__gmpz_set_si, :libgmp), Void, (Ptr{Void}, Int), z.mpz, x)
24
BigInt{T<:Signed}(x::T) = BigInt(int(x))
25
BigInt(x::Int128) = BigInt(string(x))
27
function BigInt(x::Uint)
29
ccall((:__gmpz_set_ui, :libgmp), Void,
30
(Ptr{Void}, Uint), z.mpz, x)
33
BigInt{T<:Unsigned}(x::T) = BigInt(uint(x))
34
BigInt(x::Uint128) = BigInt(string(x))
36
convert(::Type{BigInt}, x::Int8) = BigInt(int(x))
37
convert(::Type{BigInt}, x::Int16) = BigInt(int(x))
38
convert(::Type{BigInt}, x::Int) = BigInt(x)
40
convert(::Type{BigInt}, x::Uint8) = BigInt(uint(x))
41
convert(::Type{BigInt}, x::Uint16) = BigInt(uint(x))
42
convert(::Type{BigInt}, x::Uint) = BigInt(x)
45
convert(::Type{BigInt}, x::Int32) = BigInt(int(x))
46
convert(::Type{BigInt}, x::Uint32) = BigInt(uint(x))
48
BigInt(l::Int64) = BigInt(string(l))
49
BigInt(l::Uint64) = BigInt(string(l))
50
convert(::Type{BigInt}, x::Int64) = BigInt(string(x))
51
convert(::Type{BigInt}, x::Uint64) = BigInt(string(x))
54
convert(::Type{Int}, n::BigInt) =
55
ccall((:__gmpz_get_si, :libgmp), Int, (Ptr{Void},), n.mpz)
57
convert(::Type{Uint}, n::BigInt) =
58
ccall((:__gmpz_get_ui, :libgmp), Uint, (Ptr{Void},), n.mpz)
60
promote_rule(::Type{BigInt}, ::Type{Int8}) = BigInt
61
promote_rule(::Type{BigInt}, ::Type{Int16}) = BigInt
62
promote_rule(::Type{BigInt}, ::Type{Int32}) = BigInt
63
promote_rule(::Type{BigInt}, ::Type{Int64}) = BigInt
64
promote_rule(::Type{BigInt}, ::Type{Int128}) = BigInt
66
promote_rule(::Type{BigInt}, ::Type{Uint8}) = BigInt
67
promote_rule(::Type{BigInt}, ::Type{Uint16}) = BigInt
68
promote_rule(::Type{BigInt}, ::Type{Uint32}) = BigInt
69
promote_rule(::Type{BigInt}, ::Type{Uint64}) = BigInt
70
promote_rule(::Type{BigInt}, ::Type{Uint128}) = BigInt
73
for (fJ, fC) in ((:+,:add), (:-,:sub), (:*,:mul), (:div,:fdiv_q), (:rem,:fdiv_r), (:gcd, :gcd))
75
function ($fJ)(x::BigInt, y::BigInt)
77
ccall(($(string(:__gmpz_,fC)), :libgmp), Void, (Ptr{Void}, Ptr{Void}, Ptr{Void}), z.mpz, x.mpz, y.mpz)
85
ccall((:__gmpz_neg, :libgmp), Void, (Ptr{Void}, Ptr{Void}), z.mpz, x.mpz)
89
function <<(x::BigInt, c::Uint)
91
ccall((:__gmpz_lshift, :libgmp), Void, (Ptr{Void}, Ptr{Void}, Uint), z.mpz, x.mpz, c)
94
<<(x::BigInt, c::Int32) = c<0 ? throw(DomainError()) : x<<uint(c)
95
<<(x::BigInt, c::Integer) = c<0 ? throw(DomainError()) : x<<uint(c)
97
function >>(x::BigInt, c::Uint)
99
ccall((:__gmpz_rshift, :libgmp), Void, (Ptr{Void}, Ptr{Void}, Uint), z.mpz, x.mpz, c)
102
>>(x::BigInt, c::Int32) = c<0 ? throw(DomainError()) : x>>uint(c)
103
>>(x::BigInt, c::Integer) = c<0 ? throw(DomainError()) : x>>uint(c)
105
function divmod(x::BigInt, y::BigInt)
108
ccall((:__gmpz_divmod, :libgmp), Void, (Ptr{Void}, Ptr{Void}, Ptr{Void}, Ptr{Void}), z1, z2, x.mpz, y.mpz)
109
BigInt(z1),BigInt(z2)
112
function cmp(x::BigInt, y::BigInt)
113
ccall((:__gmpz_cmp, :libgmp), Int32, (Ptr{Void}, Ptr{Void}), x.mpz, y.mpz)
116
function sqrt(x::BigInt)
118
ccall((:__gmpz_sqrt, :libgmp), Void, (Ptr{Void}, Ptr{Void}), z.mpz, x.mpz)
122
function ^(x::BigInt, y::Uint)
124
ccall((:__gmpz_pow_ui, :libgmp), Void, (Ptr{Void}, Ptr{Void}, Uint), z.mpz, x.mpz, y)
128
function bigint_pow(x::BigInt, y::Integer)
129
if y<0; throw(DomainError()); end
130
if x== 1; return x; end
131
if x==-1; return isodd(y) ? x : -x; end
132
if y>typemax(Uint); throw(DomainError()); end
136
^(x::BigInt , y::BigInt ) = bigint_pow(x, y)
137
^(x::BigInt , y::Integer) = bigint_pow(x, y)
138
^(x::Integer, y::BigInt ) = bigint_pow(BigInt(x), y)
140
function gcdx(a::BigInt, b::BigInt)
144
ccall((:__gmpz_gcdext, :libgmp), Void,
145
(Ptr{Void}, Ptr{Void}, Ptr{Void}, Ptr{Void}, Ptr{Void}),
146
g, s, t, a.mpz, b.mpz)
147
BigInt(g), BigInt(s), BigInt(t)
150
function factorial(bn::BigInt)
157
ccall((:__gmpz_fac_ui, :libgmp), Void,
158
(Ptr{Void}, Uint), z.mpz, n)
162
function binomial(n::BigInt, k::Uint)
164
ccall((:__gmpz_bin_ui, :libgmp), Void,
165
(Ptr{Void}, Ptr{Void}, Uint), z.mpz, n.mpz, k)
168
binomial(n::BigInt, k::Integer) = k<0 ? throw(DomainError()) : binomial(n, uint(k))
170
==(x::BigInt, y::BigInt) = cmp(x,y) == 0
171
<=(x::BigInt, y::BigInt) = cmp(x,y) <= 0
172
>=(x::BigInt, y::BigInt) = cmp(x,y) >= 0
173
<(x::BigInt, y::BigInt) = cmp(x,y) < 0
174
>(x::BigInt, y::BigInt) = cmp(x,y) > 0
176
function string(x::BigInt)
178
z = Array(Uint8, lng)
179
lng = ccall((:__gmp_snprintf,:libgmp), Int32, (Ptr{Uint8}, Int32, Ptr{Uint8}, Ptr{Void}), z, lng, "%Zd", x.mpz)
180
return bytestring(convert(Ptr{Uint8}, z[1:lng]))
183
function show(io::IO, x::BigInt)
187
function BigInt_clear(x::BigInt)
188
ccall((:__gmpz_clear, :libgmp), Void, (Ptr{Void},), x.mpz)
191
ndigits(x::BigInt) = ccall((:__gmpz_sizeinbase,:libgmp), Int32, (Ptr{Void}, Int32), x.mpz, 10)