Skip to content
This repository
tree: 71d75fb7d4
Fetching contributors…

Cannot retrieve contributors at this time

file 153 lines (127 sloc) 4.55 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
_jl_libgmp_wrapper = dlopen("libgmp_wrapper")

type BigInt <: Integer
    mpz::Ptr{Void}

    function BigInt(x::String)
        z = _jl_bigint_init()
        ccall(dlsym(_jl_libgmp_wrapper, :_jl_mpz_set_string), Void, (Ptr{Void}, Ptr{Uint8}),z,bytestring(x))
        b = new(z)
        finalizer(b, _jl_bigint_clear)
        b
    end

    function BigInt(x::Int)
        z = _jl_bigint_init()
        ccall(dlsym(_jl_libgmp_wrapper, :_jl_mpz_set_si), Void, (Ptr{Void}, Int),z,x)
        b = new(z)
        finalizer(b, _jl_bigint_clear)
        b
    end

    function BigInt(z::Ptr{Void})
        b = new(z)
        finalizer(b, _jl_bigint_clear)
        b
    end
end

convert(::Type{BigInt}, x::Int8) = BigInt(int(x))
convert(::Type{BigInt}, x::Int16) = BigInt(int(x))
convert(::Type{BigInt}, x::Int) = BigInt(x)

if WORD_SIZE == 64
    convert(::Type{BigInt}, x::Int32) = BigInt(int(x))
else
    BigInt(l::Int64) = BigInt(string(l))
    convert(::Type{BigInt}, x::Int64) = BigInt(string(x))
end

convert(::Type{Int}, n::BigInt) =
    ccall(dlsym(_jl_libgmp_wrapper, :_jl_mpz_get_si), Int, (Ptr{Void},), n.mpz)

promote_rule(::Type{BigInt}, ::Type{Int8}) = BigInt
promote_rule(::Type{BigInt}, ::Type{Int16}) = BigInt
promote_rule(::Type{BigInt}, ::Type{Int32}) = BigInt
promote_rule(::Type{BigInt}, ::Type{Int64}) = BigInt

function +(x::BigInt, y::BigInt)
    z= _jl_bigint_init()
    ccall(dlsym(_jl_libgmp_wrapper, :_jl_mpz_add), Void, (Ptr{Void}, Ptr{Void}, Ptr{Void}),z,x.mpz,y.mpz)
    BigInt(z)
end

function -(x::BigInt)
    z= _jl_bigint_init()
    ccall(dlsym(_jl_libgmp_wrapper, :_jl_mpz_neg), Void, (Ptr{Void}, Ptr{Void}),z,x.mpz)
    BigInt(z)
end

function lshift(x::BigInt, c::Uint)
    z= _jl_bigint_init()
    ccall(dlsym(_jl_libgmp_wrapper, :_jl_mpz_lshift), Void, (Ptr{Void}, Ptr{Void}, Uint), z, x.mpz, c)
    BigInt(z)
end

function -(x::BigInt, y::BigInt)
    z= _jl_bigint_init()
    ccall(dlsym(_jl_libgmp_wrapper, :_jl_mpz_sub), Void, (Ptr{Void}, Ptr{Void}, Ptr{Void}),z,x.mpz,y.mpz)
    BigInt(z)
end

function *(x::BigInt, y::BigInt)
    z= _jl_bigint_init()
    ccall(dlsym(_jl_libgmp_wrapper, :_jl_mpz_mul), Void, (Ptr{Void}, Ptr{Void}, Ptr{Void}),z,x.mpz,y.mpz)
    BigInt(z)
end

function div(x::BigInt, y::BigInt)
    z= _jl_bigint_init()
    ccall(dlsym(_jl_libgmp_wrapper, :_jl_mpz_div), Void, (Ptr{Void}, Ptr{Void}, Ptr{Void}),z,x.mpz,y.mpz)
    BigInt(z)
end

function divmod(x::BigInt, y::BigInt)
    z1= _jl_bigint_init()
    z2= _jl_bigint_init()
    ccall(dlsym(_jl_libgmp_wrapper, :_jl_mpz_divmod), Void, (Ptr{Void}, Ptr{Void}, Ptr{Void}, Ptr{Void}), z1, z2, x.mpz, y.mpz)
    BigInt(z1),BigInt(z2)
end

function rem(x::BigInt, y::BigInt)
    z= _jl_bigint_init()
    ccall(dlsym(_jl_libgmp_wrapper, :_jl_mpz_rem), Void, (Ptr{Void}, Ptr{Void}, Ptr{Void}),z,x.mpz,y.mpz)
    BigInt(z)
end

function cmp(x::BigInt, y::BigInt)
    ccall(dlsym(_jl_libgmp_wrapper, :_jl_mpz_cmp), Int32, (Ptr{Void}, Ptr{Void}),x.mpz, y.mpz)
end

function sqrt(x::BigInt)
    z = _jl_bigint_init()
    ccall(dlsym(_jl_libgmp_wrapper, :_jl_mpz_sqrt), Void, (Ptr{Void}, Ptr{Void}),z,x.mpz)
    BigInt(z)
end

function pow(x::BigInt, y::Uint)
    z = _jl_bigint_init()
    ccall(dlsym(_jl_libgmp_wrapper, :_jl_mpz_pow_ui), Void, (Ptr{Void}, Ptr{Void}, Uint), z, x.mpz, y)
    BigInt(z)
end

function gcd(x::BigInt, y::BigInt)
    z = _jl_bigint_init()
    ccall(dlsym(_jl_libgmp_wrapper, :_jl_mpz_gcd), Void,
        (Ptr{Void}, Ptr{Void}, Ptr{Void}), z, x.mpz, y.mpz)
    BigInt(z)
end

function gcdext(a::BigInt, b::BigInt)
    g = _jl_bigint_init()
    s = _jl_bigint_init()
    t = _jl_bigint_init()
    ccall(dlsym(_jl_libgmp_wrapper, :_jl_mpz_gcdext), Void,
        (Ptr{Void}, Ptr{Void}, Ptr{Void}, Ptr{Void}, Ptr{Void}),
        g, s, t, a.mpz, b.mpz)
    BigInt(g), BigInt(s), BigInt(t)
end

==(x::BigInt, y::BigInt) = cmp(x,y) == 0
<=(x::BigInt, y::BigInt) = cmp(x,y) <= 0
>=(x::BigInt, y::BigInt) = cmp(x,y) >= 0
<(x::BigInt, y::BigInt) = cmp(x,y) < 0
>(x::BigInt, y::BigInt) = cmp(x,y) > 0

function string(x::BigInt)
    s=ccall(dlsym(_jl_libgmp_wrapper, :_jl_mpz_printf), Ptr{Uint8}, (Ptr{Void},),x.mpz)
    ret = bytestring(s) #This copies s.
    ccall(dlsym(_jl_libgmp_wrapper,:_jl_gmp_free), Void, (Ptr{Void},), s)
    ret
end

function show(io, x::BigInt)
    print(io, string(x))
end

function _jl_bigint_clear(x::BigInt)
    ccall(dlsym(_jl_libgmp_wrapper, :_jl_mpz_clear), Void, (Ptr{Void},),x.mpz)
end

function _jl_bigint_init()
    return ccall(dlsym(_jl_libgmp_wrapper, :_jl_mpz_init), Ptr{Void}, ())
end
Something went wrong with that request. Please try again.