~ubuntu-branches/ubuntu/wily/julia/wily

« back to all changes in this revision

Viewing changes to extras/bigint.jl

  • Committer: Package Import Robot
  • Author(s): Sébastien Villemot
  • Date: 2013-01-16 12:29:42 UTC
  • mfrom: (1.1.2)
  • Revision ID: package-import@ubuntu.com-20130116122942-x86e42akjq31repw
Tags: 0.0.0+20130107.gitd9656f41-1
* New upstream snashot
* No longer try to rebuild helpdb.jl.
   + debian/rules: remove helpdb.jl from build-arch rule
   + debian/control: move back python-sphinx to Build-Depends-Indep
* debian/copyright: reflect upstream changes
* Add Build-Conflicts on libatlas3-base (makes linalg tests fail)
* debian/rules: replace obsolete USE_DEBIAN makeflag by a list of
  USE_SYSTEM_* flags
* debian/rules: on non-x86 systems, use libm instead of openlibm
* dpkg-buildflags.patch: remove patch, applied upstream
* Refreshed other patches

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
import Base.convert, Base.promote_rule, Base.+, Base.-, Base.*, Base.<<
2
 
import Base.^, Base.div, Base.rem, Base.cmp, Base.sqrt
3
 
import Base.gcd, Base.gcdx, Base.factorial, Base.binomial
4
 
import Base.==, Base.<=, Base.>=, Base.<, Base.>, Base.string, Base.show
5
 
 
6
 
_jl_libgmp_wrapper = dlopen("libgmp_wrapper")
7
 
 
8
1
type BigInt <: Integer
9
2
    mpz::Ptr{Void}
10
3
 
11
4
    function BigInt(x::String)
12
 
        z = _jl_bigint_init()
13
 
        ccall(dlsym(_jl_libgmp_wrapper, :_jl_mpz_set_string), Void, (Ptr{Void}, Ptr{Uint8}),z,bytestring(x))
 
5
        z = BigInt_init()
 
6
        ccall((:jl_mpz_set_string, :libgmp_wrapper), Void, (Ptr{Void}, Ptr{Uint8}),z,bytestring(x))
14
7
        b = new(z)
15
 
        finalizer(b, _jl_bigint_clear)
 
8
        finalizer(b, BigInt_clear)
16
9
        b
17
10
    end
18
11
 
19
12
    function BigInt(x::Int)
20
 
        z = _jl_bigint_init()
21
 
        ccall(dlsym(_jl_libgmp_wrapper, :_jl_mpz_set_si), Void, (Ptr{Void}, Int),z,x)
 
13
        z = BigInt_init()
 
14
        ccall((:jl_mpz_set_si, :libgmp_wrapper), Void, (Ptr{Void}, Int),z,x)
22
15
        b = new(z)
23
 
        finalizer(b, _jl_bigint_clear)
 
16
        finalizer(b, BigInt_clear)
24
17
        b
25
18
    end
26
19
    BigInt{T<:Signed}(x::T) = BigInt(int(x))
27
20
    BigInt(x::Int128) = BigInt(string(x))
28
21
 
29
22
    function BigInt(x::Uint)
30
 
        z = _jl_bigint_init()
31
 
        ccall(dlsym(_jl_libgmp_wrapper, :_jl_mpz_set_ui), Void,
 
23
        z = BigInt_init()
 
24
        ccall((:jl_mpz_set_ui, :libgmp_wrapper), Void,
32
25
            (Ptr{Void}, Uint), z, x)
33
26
        b = new(z)
34
 
        finalizer(b, _jl_bigint_clear)
 
27
        finalizer(b, BigInt_clear)
35
28
        b
36
29
    end
37
30
    BigInt{T<:Unsigned}(x::T) = BigInt(uint(x))
39
32
 
40
33
    function BigInt(z::Ptr{Void})
41
34
        b = new(z)
42
 
        finalizer(b, _jl_bigint_clear)
 
35
        finalizer(b, BigInt_clear)
43
36
        b
44
37
    end
45
38
end
63
56
end
64
57
 
65
58
convert(::Type{Int}, n::BigInt) =
66
 
    ccall(dlsym(_jl_libgmp_wrapper, :_jl_mpz_get_si), Int, (Ptr{Void},), n.mpz)
 
59
    ccall((:jl_mpz_get_si, :libgmp_wrapper), Int, (Ptr{Void},), n.mpz)
67
60
 
68
61
convert(::Type{Uint}, n::BigInt) =
69
 
    ccall(dlsym(_jl_libgmp_wrapper, :_jl_mpz_get_ui), Uint, (Ptr{Void},), n.mpz)
 
62
    ccall((:jl_mpz_get_ui, :libgmp_wrapper), Uint, (Ptr{Void},), n.mpz)
70
63
 
71
64
promote_rule(::Type{BigInt}, ::Type{Int8}) = BigInt
72
65
promote_rule(::Type{BigInt}, ::Type{Int16}) = BigInt
81
74
promote_rule(::Type{BigInt}, ::Type{Uint128}) = BigInt
82
75
 
83
76
function +(x::BigInt, y::BigInt)
84
 
    z= _jl_bigint_init()
85
 
    ccall(dlsym(_jl_libgmp_wrapper, :_jl_mpz_add), Void, (Ptr{Void}, Ptr{Void}, Ptr{Void}),z,x.mpz,y.mpz)
 
77
    z = BigInt_init()
 
78
    ccall((:jl_mpz_add, :libgmp_wrapper), Void, (Ptr{Void}, Ptr{Void}, Ptr{Void}),z,x.mpz,y.mpz)
86
79
    BigInt(z)
87
80
end
88
81
 
89
82
function -(x::BigInt)
90
 
    z= _jl_bigint_init()
91
 
    ccall(dlsym(_jl_libgmp_wrapper, :_jl_mpz_neg), Void, (Ptr{Void}, Ptr{Void}),z,x.mpz)
 
83
    z = BigInt_init()
 
84
    ccall((:jl_mpz_neg, :libgmp_wrapper), Void, (Ptr{Void}, Ptr{Void}),z,x.mpz)
92
85
    BigInt(z)
93
86
end
94
87
 
95
88
function -(x::BigInt, y::BigInt)
96
 
    z= _jl_bigint_init()
97
 
    ccall(dlsym(_jl_libgmp_wrapper, :_jl_mpz_sub), Void, (Ptr{Void}, Ptr{Void}, Ptr{Void}),z,x.mpz,y.mpz)
 
89
    z = BigInt_init()
 
90
    ccall((:jl_mpz_sub, :libgmp_wrapper), Void, (Ptr{Void}, Ptr{Void}, Ptr{Void}),z,x.mpz,y.mpz)
98
91
    BigInt(z)
99
92
end
100
93
 
101
94
function *(x::BigInt, y::BigInt)
102
 
    z= _jl_bigint_init()
103
 
    ccall(dlsym(_jl_libgmp_wrapper, :_jl_mpz_mul), Void, (Ptr{Void}, Ptr{Void}, Ptr{Void}),z,x.mpz,y.mpz)
 
95
    z = BigInt_init()
 
96
    ccall((:jl_mpz_mul, :libgmp_wrapper), Void, (Ptr{Void}, Ptr{Void}, Ptr{Void}),z,x.mpz,y.mpz)
104
97
    BigInt(z)
105
98
end
106
99
 
107
100
function <<(x::BigInt, c::Uint)
108
 
    z= _jl_bigint_init()
109
 
    ccall(dlsym(_jl_libgmp_wrapper, :_jl_mpz_lshift), Void, (Ptr{Void}, Ptr{Void}, Uint), z, x.mpz, c)
 
101
    z = BigInt_init()
 
102
    ccall((:jl_mpz_lshift, :libgmp_wrapper), Void, (Ptr{Void}, Ptr{Void}, Uint), z, x.mpz, c)
110
103
    BigInt(z)
111
104
end
112
105
<<(x::BigInt, c::Int32)   = c<0 ? throw(DomainError()) : x<<uint(c)
113
106
<<(x::BigInt, c::Integer) = c<0 ? throw(DomainError()) : x<<uint(c)
114
107
 
 
108
function >>(x::BigInt, c::Uint)
 
109
    z = BigInt_init()
 
110
    ccall((:jl_mpz_rshift, :libgmp_wrapper), Void, (Ptr{Void}, Ptr{Void}, Uint), z, x.mpz, c)
 
111
    BigInt(z)
 
112
end
 
113
>>(x::BigInt, c::Int32)   = c<0 ? throw(DomainError()) : x>>uint(c)
 
114
>>(x::BigInt, c::Integer) = c<0 ? throw(DomainError()) : x>>uint(c)
 
115
 
115
116
function div(x::BigInt, y::BigInt)
116
 
    z= _jl_bigint_init()
117
 
    ccall(dlsym(_jl_libgmp_wrapper, :_jl_mpz_div), Void, (Ptr{Void}, Ptr{Void}, Ptr{Void}),z,x.mpz,y.mpz)
 
117
    z = BigInt_init()
 
118
    ccall((:jl_mpz_div, :libgmp_wrapper), Void, (Ptr{Void}, Ptr{Void}, Ptr{Void}),z,x.mpz,y.mpz)
118
119
    BigInt(z)
119
120
end
120
121
 
121
122
function divmod(x::BigInt, y::BigInt)
122
 
    z1= _jl_bigint_init()
123
 
    z2= _jl_bigint_init()
124
 
    ccall(dlsym(_jl_libgmp_wrapper, :_jl_mpz_divmod), Void, (Ptr{Void}, Ptr{Void}, Ptr{Void}, Ptr{Void}), z1, z2, x.mpz, y.mpz)
 
123
    z1 = BigInt_init()
 
124
    z2 = BigInt_init()
 
125
    ccall((:jl_mpz_divmod, :libgmp_wrapper), Void, (Ptr{Void}, Ptr{Void}, Ptr{Void}, Ptr{Void}), z1, z2, x.mpz, y.mpz)
125
126
    BigInt(z1),BigInt(z2)
126
127
end
127
128
 
128
129
function rem(x::BigInt, y::BigInt)
129
 
    z= _jl_bigint_init()
130
 
    ccall(dlsym(_jl_libgmp_wrapper, :_jl_mpz_rem), Void, (Ptr{Void}, Ptr{Void}, Ptr{Void}),z,x.mpz,y.mpz)
 
130
    z = BigInt_init()
 
131
    ccall((:jl_mpz_rem, :libgmp_wrapper), Void, (Ptr{Void}, Ptr{Void}, Ptr{Void}),z,x.mpz,y.mpz)
131
132
    BigInt(z)
132
133
end
133
134
 
134
135
function cmp(x::BigInt, y::BigInt)
135
 
    ccall(dlsym(_jl_libgmp_wrapper, :_jl_mpz_cmp), Int32, (Ptr{Void}, Ptr{Void}),x.mpz, y.mpz)
 
136
    ccall((:jl_mpz_cmp, :libgmp_wrapper), Int32, (Ptr{Void}, Ptr{Void}),x.mpz, y.mpz)
136
137
end
137
138
 
138
139
function sqrt(x::BigInt)
139
 
    z = _jl_bigint_init()
140
 
    ccall(dlsym(_jl_libgmp_wrapper, :_jl_mpz_sqrt), Void, (Ptr{Void}, Ptr{Void}),z,x.mpz)
 
140
    z = BigInt_init()
 
141
    ccall((:jl_mpz_sqrt, :libgmp_wrapper), Void, (Ptr{Void}, Ptr{Void}),z,x.mpz)
141
142
    BigInt(z)
142
143
end
143
144
 
144
145
function ^(x::BigInt, y::Uint)
145
 
    z = _jl_bigint_init()
146
 
    ccall(dlsym(_jl_libgmp_wrapper, :_jl_mpz_pow_ui), Void, (Ptr{Void}, Ptr{Void}, Uint), z, x.mpz, y)
 
146
    z = BigInt_init()
 
147
    ccall((:jl_mpz_pow_ui, :libgmp_wrapper), Void, (Ptr{Void}, Ptr{Void}, Uint), z, x.mpz, y)
147
148
    BigInt(z)
148
149
end
149
150
^(x::BigInt, y::Integer) = y<0 ? throw(DomainError()) : ^(x, uint(y))
150
151
 
151
152
function gcd(x::BigInt, y::BigInt)
152
 
    z = _jl_bigint_init()
153
 
    ccall(dlsym(_jl_libgmp_wrapper, :_jl_mpz_gcd), Void,
 
153
    z = BigInt_init()
 
154
    ccall((:jl_mpz_gcd, :libgmp_wrapper), Void,
154
155
        (Ptr{Void}, Ptr{Void}, Ptr{Void}), z, x.mpz, y.mpz)
155
156
    BigInt(z)
156
157
end
157
158
 
158
159
function gcdx(a::BigInt, b::BigInt)
159
 
    g = _jl_bigint_init()
160
 
    s = _jl_bigint_init()
161
 
    t = _jl_bigint_init()
162
 
    ccall(dlsym(_jl_libgmp_wrapper, :_jl_mpz_gcdext), Void,
 
160
    g = BigInt_init()
 
161
    s = BigInt_init()
 
162
    t = BigInt_init()
 
163
    ccall((:jl_mpz_gcdext, :libgmp_wrapper), Void,
163
164
        (Ptr{Void}, Ptr{Void}, Ptr{Void}, Ptr{Void}, Ptr{Void}),
164
165
        g, s, t, a.mpz, b.mpz)
165
166
    BigInt(g), BigInt(s), BigInt(t)
171
172
    else
172
173
        n = uint(bn)
173
174
    end
174
 
    z = _jl_bigint_init()
175
 
    ccall(dlsym(_jl_libgmp_wrapper, :_jl_mpz_fac_ui), Void,
 
175
    z = BigInt_init()
 
176
    ccall((:jl_mpz_fac_ui, :libgmp_wrapper), Void,
176
177
        (Ptr{Void}, Uint), z, n)
177
178
    BigInt(z)
178
179
end
179
180
 
180
181
function binomial(n::BigInt, k::Uint)
181
 
    z = _jl_bigint_init()
182
 
    ccall(dlsym(_jl_libgmp_wrapper, :_jl_mpz_bin_ui), Void,
 
182
    z = BigInt_init()
 
183
    ccall((:jl_mpz_bin_ui, :libgmp_wrapper), Void,
183
184
        (Ptr{Void}, Ptr{Void}, Uint), z, n.mpz, k)
184
185
    BigInt(z)
185
186
end
192
193
>(x::BigInt, y::BigInt) = cmp(x,y) > 0
193
194
 
194
195
function string(x::BigInt)
195
 
    s=ccall(dlsym(_jl_libgmp_wrapper, :_jl_mpz_printf), Ptr{Uint8}, (Ptr{Void},),x.mpz)
 
196
    s = ccall((:jl_mpz_printf, :libgmp_wrapper), Ptr{Uint8}, (Ptr{Void},),x.mpz)
196
197
    ret = bytestring(s) #This copies s.
197
 
    ccall(dlsym(_jl_libgmp_wrapper,:_jl_gmp_free), Void, (Ptr{Void},), s)
 
198
    ccall((:jl_gmp_free, :libgmp_wrapper), Void, (Ptr{Void},), s)
198
199
    ret
199
200
end
200
201
 
202
203
    print(io, string(x))
203
204
end
204
205
 
205
 
function _jl_bigint_clear(x::BigInt)
206
 
    ccall(dlsym(_jl_libgmp_wrapper, :_jl_mpz_clear), Void, (Ptr{Void},),x.mpz)
 
206
function BigInt_clear(x::BigInt)
 
207
    ccall((:jl_mpz_clear, :libgmp_wrapper), Void, (Ptr{Void},),x.mpz)
207
208
end
208
209
 
209
 
function _jl_bigint_init()
210
 
    return ccall(dlsym(_jl_libgmp_wrapper, :_jl_mpz_init), Ptr{Void}, ())
 
210
function BigInt_init()
 
211
    return ccall((:jl_mpz_init, :libgmp_wrapper), Ptr{Void}, ())
211
212
end