Skip to content

Commit 335cd5e

Browse files
authored
Merge pull request #47676 from gbaraldi/gmp-libblas-version
Use versioned libblastrampoline and GMP
2 parents 0231c22 + 6b0b3a7 commit 335cd5e

File tree

9 files changed

+252
-235
lines changed

9 files changed

+252
-235
lines changed

base/gmp.jl

Lines changed: 50 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,16 @@ else
2121
end
2222
const CdoubleMax = Union{Float16, Float32, Float64}
2323

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)))
2634

2735
const VERSION = version()
2836
const BITS_PER_LIMB = bits_per_limb()
@@ -54,7 +62,7 @@ mutable struct BigInt <: Signed
5462

5563
function BigInt(; nbits::Integer=0)
5664
b = MPZ.init2!(new(), nbits)
57-
finalizer(cglobal((:__gmpz_clear, :libgmp)), b)
65+
finalizer(cglobal((:__gmpz_clear, libgmp)), b)
5866
return b
5967
end
6068
end
@@ -100,7 +108,7 @@ function __init__()
100108
bits_per_limb() != BITS_PER_LIMB ? @error(msg) : @warn(msg)
101109
end
102110

103-
ccall((:__gmp_set_memory_functions, :libgmp), Cvoid,
111+
ccall((:__gmp_set_memory_functions, libgmp), Cvoid,
104112
(Ptr{Cvoid},Ptr{Cvoid},Ptr{Cvoid}),
105113
cglobal(:jl_gc_counted_malloc),
106114
cglobal(:jl_gc_counted_realloc_with_old_size),
@@ -112,7 +120,7 @@ function __init__()
112120
end
113121
# This only works with a patched version of GMP, ignore otherwise
114122
try
115-
ccall((:__gmp_set_alloc_overflow_function, :libgmp), Cvoid,
123+
ccall((:__gmp_set_alloc_overflow_function, libgmp), Cvoid,
116124
(Ptr{Cvoid},),
117125
cglobal(:jl_throw_out_of_memory_error))
118126
ALLOC_OVERFLOW_FUNCTION[] = true
@@ -132,20 +140,20 @@ module MPZ
132140
# - a method modifying its input has a "!" appended to its name, according to Julia's conventions
133141
# - some convenient methods are added (in addition to the pure MPZ ones), e.g. `add(a, b) = add!(BigInt(), a, b)`
134142
# 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
136144

137145
const mpz_t = Ref{BigInt}
138146
const bitcnt_t = Culong
139147

140-
gmpz(op::Symbol) = (Symbol(:__gmpz_, op), :libgmp)
148+
gmpz(op::Symbol) = (Symbol(:__gmpz_, op), libgmp)
141149

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)
144152

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)
146154
realloc2(a) = realloc2!(BigInt(), a)
147155

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))
149157

150158
for (op, nbits) in (:add => :(BITS_PER_LIMB*(1 + max(abs(a.size), abs(b.size)))),
151159
: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))))
161169
end
162170

163171
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)
165173
invert(a::BigInt, b::BigInt) = invert!(BigInt(), a, b)
166174
invert!(x::BigInt, b::BigInt) = invert!(x, x, b)
167175

@@ -174,7 +182,7 @@ for op in (:add_ui, :sub_ui, :mul_ui, :mul_2exp, :fdiv_q_2exp, :pow_ui, :bin_ui)
174182
end
175183
end
176184

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)
178186
ui_sub(a, b::BigInt) = ui_sub!(BigInt(), a, b)
179187

180188
for op in (:scan1, :scan0)
@@ -183,7 +191,7 @@ for op in (:scan1, :scan0)
183191
@eval $op(a::BigInt, b) = Int(signed(ccall($(gmpz(op)), Culong, (mpz_t, Culong), a, b)))
184192
end
185193

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)
187195
mul_si(a::BigInt, b) = mul_si!(BigInt(), a, b)
188196
mul_si!(x::BigInt, b) = mul_si!(x, x, b)
189197

@@ -205,47 +213,47 @@ for (op, T) in ((:fac_ui, Culong), (:set_ui, Culong), (:set_si, Clong), (:set_d,
205213
end
206214
end
207215

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)))
209217

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))
211219
mpn_popcount(a::BigInt) = mpn_popcount(a.d, abs(a.size))
212220

213221
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)
215223
x, y
216224
end
217225
tdiv_qr(a::BigInt, b::BigInt) = tdiv_qr!(BigInt(), BigInt(), a, b)
218226

219227
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)
221229
powm(a::BigInt, b::BigInt, c::BigInt) = powm!(BigInt(), a, b, c)
222230
powm!(x::BigInt, b::BigInt, c::BigInt) = powm!(x, x, b, c)
223231

224232
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)
226234
x, y, z
227235
end
228236
gcdext(a::BigInt, b::BigInt) = gcdext!(BigInt(), BigInt(), BigInt(), a, b)
229237

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))
234242

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)
236244
mpn_cmp(a::BigInt, b::BigInt, c) = mpn_cmp(a.d, b.d, c)
237245

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)
241249

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,
245253
(mpz_t, Csize_t, Cint, Csize_t, Cint, Csize_t, Ptr{Cvoid}), x, a, b, c, d, e, f)
246254

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
249257

250258
end # module MPZ
251259

@@ -884,9 +892,9 @@ module MPQ
884892

885893
# Rational{BigInt}
886894
import .Base: unsafe_rational, __throw_rational_argerror_zero
887-
import ..GMP: BigInt, MPZ, Limb, isneg
895+
import ..GMP: BigInt, MPZ, Limb, isneg, libgmp
888896

889-
gmpq(op::Symbol) = (Symbol(:__gmpq_, op), :libgmp)
897+
gmpq(op::Symbol) = (Symbol(:__gmpq_, op), libgmp)
890898

891899
mutable struct _MPQ
892900
num_alloc::Cint
@@ -923,20 +931,20 @@ function Rational{BigInt}(num::BigInt, den::BigInt)
923931
return set_si(flipsign(1, num), 0)
924932
end
925933
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)
927935
return sync_rational!(xq)
928936
end
929937

930938
# define set, set_ui, set_si, set_z, and their inplace versions
931939
function set!(z::Rational{BigInt}, x::Rational{BigInt})
932940
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))
934942
return sync_rational!(zq)
935943
end
936944

937945
function set_z!(z::Rational{BigInt}, x::BigInt)
938946
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)
940948
return sync_rational!(zq)
941949
end
942950

@@ -968,7 +976,7 @@ function add!(z::Rational{BigInt}, x::Rational{BigInt}, y::Rational{BigInt})
968976
return set!(z, iszero(x.den) ? x : y)
969977
end
970978
zq = _MPQ(z)
971-
ccall((:__gmpq_add, :libgmp), Cvoid,
979+
ccall((:__gmpq_add, libgmp), Cvoid,
972980
(mpq_t,mpq_t,mpq_t), zq, _MPQ(x), _MPQ(y))
973981
return sync_rational!(zq)
974982
end
@@ -982,7 +990,7 @@ function sub!(z::Rational{BigInt}, x::Rational{BigInt}, y::Rational{BigInt})
982990
return set_si!(z, flipsign(-1, y.num), 0)
983991
end
984992
zq = _MPQ(z)
985-
ccall((:__gmpq_sub, :libgmp), Cvoid,
993+
ccall((:__gmpq_sub, libgmp), Cvoid,
986994
(mpq_t,mpq_t,mpq_t), zq, _MPQ(x), _MPQ(y))
987995
return sync_rational!(zq)
988996
end
@@ -995,7 +1003,7 @@ function mul!(z::Rational{BigInt}, x::Rational{BigInt}, y::Rational{BigInt})
9951003
return set_si!(z, ifelse(xor(isneg(x.num), isneg(y.num)), -1, 1), 0)
9961004
end
9971005
zq = _MPQ(z)
998-
ccall((:__gmpq_mul, :libgmp), Cvoid,
1006+
ccall((:__gmpq_mul, libgmp), Cvoid,
9991007
(mpq_t,mpq_t,mpq_t), zq, _MPQ(x), _MPQ(y))
10001008
return sync_rational!(zq)
10011009
end
@@ -1016,7 +1024,7 @@ function div!(z::Rational{BigInt}, x::Rational{BigInt}, y::Rational{BigInt})
10161024
return set_si!(z, flipsign(1, x.num), 0)
10171025
end
10181026
zq = _MPQ(z)
1019-
ccall((:__gmpq_div, :libgmp), Cvoid,
1027+
ccall((:__gmpq_div, libgmp), Cvoid,
10201028
(mpq_t,mpq_t,mpq_t), zq, _MPQ(x), _MPQ(y))
10211029
return sync_rational!(zq)
10221030
end
@@ -1030,7 +1038,7 @@ for (fJ, fC) in ((:+, :add), (:-, :sub), (:*, :mul), (://, :div))
10301038
end
10311039

10321040
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)))
10341042
end
10351043

10361044
end # MPQ module

0 commit comments

Comments
 (0)