21
21
end
22
22
const CdoubleMax = Union{Float16, Float32, Float64}
23
23
24
- version () = VersionNumber (unsafe_string (unsafe_load (cglobal ((:__gmp_version , :libgmp ), Ptr{Cchar}))))
25
- bits_per_limb () = Int (unsafe_load (cglobal ((:__gmp_bits_per_limb , :libgmp ), Cint)))
24
+ if Sys. iswindows ()
25
+ const libgmp = " libgmp-10.dll"
26
+ elseif Sys. isapple ()
27
+ const libgmp = " @rpath/libgmp.10.dylib"
28
+ else
29
+ const libgmp = " libgmp.so.10"
30
+ end
31
+
32
+ version () = VersionNumber (unsafe_string (unsafe_load (cglobal ((:__gmp_version , libgmp), Ptr{Cchar}))))
33
+ bits_per_limb () = Int (unsafe_load (cglobal ((:__gmp_bits_per_limb , libgmp), Cint)))
26
34
27
35
const VERSION = version ()
28
36
const BITS_PER_LIMB = bits_per_limb ()
@@ -54,7 +62,7 @@ mutable struct BigInt <: Signed
54
62
55
63
function BigInt (; nbits:: Integer = 0 )
56
64
b = MPZ. init2! (new (), nbits)
57
- finalizer (cglobal ((:__gmpz_clear , : libgmp )), b)
65
+ finalizer (cglobal ((:__gmpz_clear , libgmp)), b)
58
66
return b
59
67
end
60
68
end
@@ -100,7 +108,7 @@ function __init__()
100
108
bits_per_limb () != BITS_PER_LIMB ? @error (msg) : @warn (msg)
101
109
end
102
110
103
- ccall ((:__gmp_set_memory_functions , : libgmp ), Cvoid,
111
+ ccall ((:__gmp_set_memory_functions , libgmp), Cvoid,
104
112
(Ptr{Cvoid},Ptr{Cvoid},Ptr{Cvoid}),
105
113
cglobal (:jl_gc_counted_malloc ),
106
114
cglobal (:jl_gc_counted_realloc_with_old_size ),
@@ -112,7 +120,7 @@ function __init__()
112
120
end
113
121
# This only works with a patched version of GMP, ignore otherwise
114
122
try
115
- ccall ((:__gmp_set_alloc_overflow_function , : libgmp ), Cvoid,
123
+ ccall ((:__gmp_set_alloc_overflow_function , libgmp), Cvoid,
116
124
(Ptr{Cvoid},),
117
125
cglobal (:jl_throw_out_of_memory_error ))
118
126
ALLOC_OVERFLOW_FUNCTION[] = true
@@ -132,20 +140,20 @@ module MPZ
132
140
# - a method modifying its input has a "!" appended to its name, according to Julia's conventions
133
141
# - some convenient methods are added (in addition to the pure MPZ ones), e.g. `add(a, b) = add!(BigInt(), a, b)`
134
142
# and `add!(x, a) = add!(x, x, a)`.
135
- using .. GMP: BigInt, Limb, BITS_PER_LIMB
143
+ using .. GMP: BigInt, Limb, BITS_PER_LIMB, libgmp
136
144
137
145
const mpz_t = Ref{BigInt}
138
146
const bitcnt_t = Culong
139
147
140
- gmpz (op:: Symbol ) = (Symbol (:__gmpz_ , op), : libgmp )
148
+ gmpz (op:: Symbol ) = (Symbol (:__gmpz_ , op), libgmp)
141
149
142
- init! (x:: BigInt ) = (ccall ((:__gmpz_init , : libgmp ), Cvoid, (mpz_t,), x); x)
143
- init2! (x:: BigInt , a) = (ccall ((:__gmpz_init2 , : libgmp ), Cvoid, (mpz_t, bitcnt_t), x, a); x)
150
+ init! (x:: BigInt ) = (ccall ((:__gmpz_init , libgmp), Cvoid, (mpz_t,), x); x)
151
+ init2! (x:: BigInt , a) = (ccall ((:__gmpz_init2 , libgmp), Cvoid, (mpz_t, bitcnt_t), x, a); x)
144
152
145
- realloc2! (x, a) = (ccall ((:__gmpz_realloc2 , : libgmp ), Cvoid, (mpz_t, bitcnt_t), x, a); x)
153
+ realloc2! (x, a) = (ccall ((:__gmpz_realloc2 , libgmp), Cvoid, (mpz_t, bitcnt_t), x, a); x)
146
154
realloc2 (a) = realloc2! (BigInt (), a)
147
155
148
- sizeinbase (a:: BigInt , b) = Int (ccall ((:__gmpz_sizeinbase , : libgmp ), Csize_t, (mpz_t, Cint), a, b))
156
+ sizeinbase (a:: BigInt , b) = Int (ccall ((:__gmpz_sizeinbase , libgmp), Csize_t, (mpz_t, Cint), a, b))
149
157
150
158
for (op, nbits) in (:add => :(BITS_PER_LIMB* (1 + max (abs (a. size), abs (b. size)))),
151
159
:sub => :(BITS_PER_LIMB* (1 + max (abs (a. size), abs (b. size)))),
@@ -161,7 +169,7 @@ for (op, nbits) in (:add => :(BITS_PER_LIMB*(1 + max(abs(a.size), abs(b.size))))
161
169
end
162
170
163
171
invert! (x:: BigInt , a:: BigInt , b:: BigInt ) =
164
- ccall ((:__gmpz_invert , : libgmp ), Cint, (mpz_t, mpz_t, mpz_t), x, a, b)
172
+ ccall ((:__gmpz_invert , libgmp), Cint, (mpz_t, mpz_t, mpz_t), x, a, b)
165
173
invert (a:: BigInt , b:: BigInt ) = invert! (BigInt (), a, b)
166
174
invert! (x:: BigInt , b:: BigInt ) = invert! (x, x, b)
167
175
@@ -174,7 +182,7 @@ for op in (:add_ui, :sub_ui, :mul_ui, :mul_2exp, :fdiv_q_2exp, :pow_ui, :bin_ui)
174
182
end
175
183
end
176
184
177
- ui_sub! (x:: BigInt , a, b:: BigInt ) = (ccall ((:__gmpz_ui_sub , : libgmp ), Cvoid, (mpz_t, Culong, mpz_t), x, a, b); x)
185
+ ui_sub! (x:: BigInt , a, b:: BigInt ) = (ccall ((:__gmpz_ui_sub , libgmp), Cvoid, (mpz_t, Culong, mpz_t), x, a, b); x)
178
186
ui_sub (a, b:: BigInt ) = ui_sub! (BigInt (), a, b)
179
187
180
188
for op in (:scan1 , :scan0 )
@@ -183,7 +191,7 @@ for op in (:scan1, :scan0)
183
191
@eval $ op (a:: BigInt , b) = Int (signed (ccall ($ (gmpz (op)), Culong, (mpz_t, Culong), a, b)))
184
192
end
185
193
186
- mul_si! (x:: BigInt , a:: BigInt , b) = (ccall ((:__gmpz_mul_si , : libgmp ), Cvoid, (mpz_t, mpz_t, Clong), x, a, b); x)
194
+ mul_si! (x:: BigInt , a:: BigInt , b) = (ccall ((:__gmpz_mul_si , libgmp), Cvoid, (mpz_t, mpz_t, Clong), x, a, b); x)
187
195
mul_si (a:: BigInt , b) = mul_si! (BigInt (), a, b)
188
196
mul_si! (x:: BigInt , b) = mul_si! (x, x, b)
189
197
@@ -205,47 +213,47 @@ for (op, T) in ((:fac_ui, Culong), (:set_ui, Culong), (:set_si, Clong), (:set_d,
205
213
end
206
214
end
207
215
208
- popcount (a:: BigInt ) = Int (signed (ccall ((:__gmpz_popcount , : libgmp ), Culong, (mpz_t,), a)))
216
+ popcount (a:: BigInt ) = Int (signed (ccall ((:__gmpz_popcount , libgmp), Culong, (mpz_t,), a)))
209
217
210
- mpn_popcount (d:: Ptr{Limb} , s:: Integer ) = Int (ccall ((:__gmpn_popcount , : libgmp ), Culong, (Ptr{Limb}, Csize_t), d, s))
218
+ mpn_popcount (d:: Ptr{Limb} , s:: Integer ) = Int (ccall ((:__gmpn_popcount , libgmp), Culong, (Ptr{Limb}, Csize_t), d, s))
211
219
mpn_popcount (a:: BigInt ) = mpn_popcount (a. d, abs (a. size))
212
220
213
221
function tdiv_qr! (x:: BigInt , y:: BigInt , a:: BigInt , b:: BigInt )
214
- ccall ((:__gmpz_tdiv_qr , : libgmp ), Cvoid, (mpz_t, mpz_t, mpz_t, mpz_t), x, y, a, b)
222
+ ccall ((:__gmpz_tdiv_qr , libgmp), Cvoid, (mpz_t, mpz_t, mpz_t, mpz_t), x, y, a, b)
215
223
x, y
216
224
end
217
225
tdiv_qr (a:: BigInt , b:: BigInt ) = tdiv_qr! (BigInt (), BigInt (), a, b)
218
226
219
227
powm! (x:: BigInt , a:: BigInt , b:: BigInt , c:: BigInt ) =
220
- (ccall ((:__gmpz_powm , : libgmp ), Cvoid, (mpz_t, mpz_t, mpz_t, mpz_t), x, a, b, c); x)
228
+ (ccall ((:__gmpz_powm , libgmp), Cvoid, (mpz_t, mpz_t, mpz_t, mpz_t), x, a, b, c); x)
221
229
powm (a:: BigInt , b:: BigInt , c:: BigInt ) = powm! (BigInt (), a, b, c)
222
230
powm! (x:: BigInt , b:: BigInt , c:: BigInt ) = powm! (x, x, b, c)
223
231
224
232
function gcdext! (x:: BigInt , y:: BigInt , z:: BigInt , a:: BigInt , b:: BigInt )
225
- ccall ((:__gmpz_gcdext , : libgmp ), Cvoid, (mpz_t, mpz_t, mpz_t, mpz_t, mpz_t), x, y, z, a, b)
233
+ ccall ((:__gmpz_gcdext , libgmp), Cvoid, (mpz_t, mpz_t, mpz_t, mpz_t, mpz_t), x, y, z, a, b)
226
234
x, y, z
227
235
end
228
236
gcdext (a:: BigInt , b:: BigInt ) = gcdext! (BigInt (), BigInt (), BigInt (), a, b)
229
237
230
- cmp (a:: BigInt , b:: BigInt ) = Int (ccall ((:__gmpz_cmp , : libgmp ), Cint, (mpz_t, mpz_t), a, b))
231
- cmp_si (a:: BigInt , b) = Int (ccall ((:__gmpz_cmp_si , : libgmp ), Cint, (mpz_t, Clong), a, b))
232
- cmp_ui (a:: BigInt , b) = Int (ccall ((:__gmpz_cmp_ui , : libgmp ), Cint, (mpz_t, Culong), a, b))
233
- cmp_d (a:: BigInt , b) = Int (ccall ((:__gmpz_cmp_d , : libgmp ), Cint, (mpz_t, Cdouble), a, b))
238
+ cmp (a:: BigInt , b:: BigInt ) = Int (ccall ((:__gmpz_cmp , libgmp), Cint, (mpz_t, mpz_t), a, b))
239
+ cmp_si (a:: BigInt , b) = Int (ccall ((:__gmpz_cmp_si , libgmp), Cint, (mpz_t, Clong), a, b))
240
+ cmp_ui (a:: BigInt , b) = Int (ccall ((:__gmpz_cmp_ui , libgmp), Cint, (mpz_t, Culong), a, b))
241
+ cmp_d (a:: BigInt , b) = Int (ccall ((:__gmpz_cmp_d , libgmp), Cint, (mpz_t, Cdouble), a, b))
234
242
235
- mpn_cmp (a:: Ptr{Limb} , b:: Ptr{Limb} , c) = ccall ((:__gmpn_cmp , : libgmp ), Cint, (Ptr{Limb}, Ptr{Limb}, Clong), a, b, c)
243
+ mpn_cmp (a:: Ptr{Limb} , b:: Ptr{Limb} , c) = ccall ((:__gmpn_cmp , libgmp), Cint, (Ptr{Limb}, Ptr{Limb}, Clong), a, b, c)
236
244
mpn_cmp (a:: BigInt , b:: BigInt , c) = mpn_cmp (a. d, b. d, c)
237
245
238
- get_str! (x, a, b:: BigInt ) = (ccall ((:__gmpz_get_str ,: libgmp ), Ptr{Cchar}, (Ptr{Cchar}, Cint, mpz_t), x, a, b); x)
239
- set_str! (x:: BigInt , a, b) = Int (ccall ((:__gmpz_set_str , : libgmp ), Cint, (mpz_t, Ptr{UInt8}, Cint), x, a, b))
240
- get_d (a:: BigInt ) = ccall ((:__gmpz_get_d , : libgmp ), Cdouble, (mpz_t,), a)
246
+ get_str! (x, a, b:: BigInt ) = (ccall ((:__gmpz_get_str ,libgmp), Ptr{Cchar}, (Ptr{Cchar}, Cint, mpz_t), x, a, b); x)
247
+ set_str! (x:: BigInt , a, b) = Int (ccall ((:__gmpz_set_str , libgmp), Cint, (mpz_t, Ptr{UInt8}, Cint), x, a, b))
248
+ get_d (a:: BigInt ) = ccall ((:__gmpz_get_d , libgmp), Cdouble, (mpz_t,), a)
241
249
242
- limbs_write! (x:: BigInt , a) = ccall ((:__gmpz_limbs_write , : libgmp ), Ptr{Limb}, (mpz_t, Clong), x, a)
243
- limbs_finish! (x:: BigInt , a) = ccall ((:__gmpz_limbs_finish , : libgmp ), Cvoid, (mpz_t, Clong), x, a)
244
- import! (x:: BigInt , a, b, c, d, e, f) = ccall ((:__gmpz_import , : libgmp ), Cvoid,
250
+ limbs_write! (x:: BigInt , a) = ccall ((:__gmpz_limbs_write , libgmp), Ptr{Limb}, (mpz_t, Clong), x, a)
251
+ limbs_finish! (x:: BigInt , a) = ccall ((:__gmpz_limbs_finish , libgmp), Cvoid, (mpz_t, Clong), x, a)
252
+ import! (x:: BigInt , a, b, c, d, e, f) = ccall ((:__gmpz_import , libgmp), Cvoid,
245
253
(mpz_t, Csize_t, Cint, Csize_t, Cint, Csize_t, Ptr{Cvoid}), x, a, b, c, d, e, f)
246
254
247
- setbit! (x, a) = (ccall ((:__gmpz_setbit , : libgmp ), Cvoid, (mpz_t, bitcnt_t), x, a); x)
248
- tstbit (a:: BigInt , b) = ccall ((:__gmpz_tstbit , : libgmp ), Cint, (mpz_t, bitcnt_t), a, b) % Bool
255
+ setbit! (x, a) = (ccall ((:__gmpz_setbit , libgmp), Cvoid, (mpz_t, bitcnt_t), x, a); x)
256
+ tstbit (a:: BigInt , b) = ccall ((:__gmpz_tstbit , libgmp), Cint, (mpz_t, bitcnt_t), a, b) % Bool
249
257
250
258
end # module MPZ
251
259
@@ -884,9 +892,9 @@ module MPQ
884
892
885
893
# Rational{BigInt}
886
894
import . Base: unsafe_rational, __throw_rational_argerror_zero
887
- import .. GMP: BigInt, MPZ, Limb, isneg
895
+ import .. GMP: BigInt, MPZ, Limb, isneg, libgmp
888
896
889
- gmpq (op:: Symbol ) = (Symbol (:__gmpq_ , op), : libgmp )
897
+ gmpq (op:: Symbol ) = (Symbol (:__gmpq_ , op), libgmp)
890
898
891
899
mutable struct _MPQ
892
900
num_alloc:: Cint
@@ -923,20 +931,20 @@ function Rational{BigInt}(num::BigInt, den::BigInt)
923
931
return set_si (flipsign (1 , num), 0 )
924
932
end
925
933
xq = _MPQ (MPZ. set (num), MPZ. set (den))
926
- ccall ((:__gmpq_canonicalize , : libgmp ), Cvoid, (mpq_t,), xq)
934
+ ccall ((:__gmpq_canonicalize , libgmp), Cvoid, (mpq_t,), xq)
927
935
return sync_rational! (xq)
928
936
end
929
937
930
938
# define set, set_ui, set_si, set_z, and their inplace versions
931
939
function set! (z:: Rational{BigInt} , x:: Rational{BigInt} )
932
940
zq = _MPQ (z)
933
- ccall ((:__gmpq_set , : libgmp ), Cvoid, (mpq_t, mpq_t), zq, _MPQ (x))
941
+ ccall ((:__gmpq_set , libgmp), Cvoid, (mpq_t, mpq_t), zq, _MPQ (x))
934
942
return sync_rational! (zq)
935
943
end
936
944
937
945
function set_z! (z:: Rational{BigInt} , x:: BigInt )
938
946
zq = _MPQ (z)
939
- ccall ((:__gmpq_set_z , : libgmp ), Cvoid, (mpq_t, MPZ. mpz_t), zq, x)
947
+ ccall ((:__gmpq_set_z , libgmp), Cvoid, (mpq_t, MPZ. mpz_t), zq, x)
940
948
return sync_rational! (zq)
941
949
end
942
950
@@ -968,7 +976,7 @@ function add!(z::Rational{BigInt}, x::Rational{BigInt}, y::Rational{BigInt})
968
976
return set! (z, iszero (x. den) ? x : y)
969
977
end
970
978
zq = _MPQ (z)
971
- ccall ((:__gmpq_add , : libgmp ), Cvoid,
979
+ ccall ((:__gmpq_add , libgmp), Cvoid,
972
980
(mpq_t,mpq_t,mpq_t), zq, _MPQ (x), _MPQ (y))
973
981
return sync_rational! (zq)
974
982
end
@@ -982,7 +990,7 @@ function sub!(z::Rational{BigInt}, x::Rational{BigInt}, y::Rational{BigInt})
982
990
return set_si! (z, flipsign (- 1 , y. num), 0 )
983
991
end
984
992
zq = _MPQ (z)
985
- ccall ((:__gmpq_sub , : libgmp ), Cvoid,
993
+ ccall ((:__gmpq_sub , libgmp), Cvoid,
986
994
(mpq_t,mpq_t,mpq_t), zq, _MPQ (x), _MPQ (y))
987
995
return sync_rational! (zq)
988
996
end
@@ -995,7 +1003,7 @@ function mul!(z::Rational{BigInt}, x::Rational{BigInt}, y::Rational{BigInt})
995
1003
return set_si! (z, ifelse (xor (isneg (x. num), isneg (y. num)), - 1 , 1 ), 0 )
996
1004
end
997
1005
zq = _MPQ (z)
998
- ccall ((:__gmpq_mul , : libgmp ), Cvoid,
1006
+ ccall ((:__gmpq_mul , libgmp), Cvoid,
999
1007
(mpq_t,mpq_t,mpq_t), zq, _MPQ (x), _MPQ (y))
1000
1008
return sync_rational! (zq)
1001
1009
end
@@ -1016,7 +1024,7 @@ function div!(z::Rational{BigInt}, x::Rational{BigInt}, y::Rational{BigInt})
1016
1024
return set_si! (z, flipsign (1 , x. num), 0 )
1017
1025
end
1018
1026
zq = _MPQ (z)
1019
- ccall ((:__gmpq_div , : libgmp ), Cvoid,
1027
+ ccall ((:__gmpq_div , libgmp), Cvoid,
1020
1028
(mpq_t,mpq_t,mpq_t), zq, _MPQ (x), _MPQ (y))
1021
1029
return sync_rational! (zq)
1022
1030
end
@@ -1030,7 +1038,7 @@ for (fJ, fC) in ((:+, :add), (:-, :sub), (:*, :mul), (://, :div))
1030
1038
end
1031
1039
1032
1040
function Base. cmp (x:: Rational{BigInt} , y:: Rational{BigInt} )
1033
- Int (ccall ((:__gmpq_cmp , : libgmp ), Cint, (mpq_t, mpq_t), _MPQ (x), _MPQ (y)))
1041
+ Int (ccall ((:__gmpq_cmp , libgmp), Cint, (mpq_t, mpq_t), _MPQ (x), _MPQ (y)))
1034
1042
end
1035
1043
1036
1044
end # MPQ module
0 commit comments