Skip to content

Commit 8b0921c

Browse files
committed
use the right type (Limb) instead of Culong for limbs
1 parent e209872 commit 8b0921c

File tree

2 files changed

+21
-10
lines changed

2 files changed

+21
-10
lines changed

base/gmp.jl

+11-1
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,21 @@ end
1919
typealias CdoubleMax Union(Float16, Float32, Float64)
2020

2121
const GMP_VERSION = VersionNumber(bytestring(unsafe_load(cglobal((:__gmp_version, :libgmp), Ptr{Cchar}))))
22+
const GMP_BITS_PER_LIMB = Int(unsafe_load(cglobal((:__gmp_bits_per_limb, :libgmp), Cint)))
23+
24+
if GMP_BITS_PER_LIMB == 32
25+
typealias Limb UInt32
26+
elseif GMP_BITS_PER_LIMB == 64
27+
typealias Limb UInt64
28+
else
29+
error("GMP: cannot determine the type mp_limb_t")
30+
end
31+
2232

2333
type BigInt <: Integer
2434
alloc::Cint
2535
size::Cint
26-
d::Ptr{Culong}
36+
d::Ptr{Limb}
2737
function BigInt()
2838
b = new(zero(Cint), zero(Cint), C_NULL)
2939
ccall((:__gmpz_init,:libgmp), Void, (Ptr{BigInt},), &b)

base/random.jl

+10-9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
module Random
22

3-
using Base: dSFMT, Base.GMP.GMP_VERSION
3+
using Base.dSFMT
4+
using Base.GMP: GMP_VERSION, Limb
45

56
export srand,
67
rand, rand!,
@@ -391,17 +392,17 @@ if GMP_VERSION.major >= 6
391392
a::BigInt # first
392393
m::BigInt # range length - 1
393394
nlimbs::Int # number of limbs in generated BigInt's
394-
mask::Culong # applied to the highest limb
395+
mask::Limb # applied to the highest limb
395396
end
396397

397398
else
398399
immutable RangeGeneratorBigInt <: RangeGenerator
399400
a::BigInt # first
400401
m::BigInt # range length - 1
401-
limbs::Array{Culong} # buffer to be copied into generated BigInt's
402-
mask::Culong # applied to the highest limb
402+
limbs::Array{Limb} # buffer to be copied into generated BigInt's
403+
mask::Limb # applied to the highest limb
403404

404-
RangeGeneratorBigInt(a, m, nlimbs, mask) = new(a, m, Array(Culong, nlimbs), mask)
405+
RangeGeneratorBigInt(a, m, nlimbs, mask) = new(a, m, Array(Limb, nlimbs), mask)
405406
end
406407
end
407408

@@ -411,9 +412,9 @@ function RangeGenerator(r::UnitRange{BigInt})
411412
m = last(r) - first(r)
412413
m < 0 && error("range must be non-empty")
413414
nd = ndigits(m, 2)
414-
nlimbs, highbits = divrem(nd, 8*sizeof(Culong))
415+
nlimbs, highbits = divrem(nd, 8*sizeof(Limb))
415416
highbits > 0 && (nlimbs += 1)
416-
mask = highbits == 0 ? ~zero(Culong) : one(Culong)<<highbits - one(Culong)
417+
mask = highbits == 0 ? ~zero(Limb) : one(Limb)<<highbits - one(Limb)
417418
return RangeGeneratorBigInt(first(r), m, nlimbs, mask)
418419
end
419420

@@ -450,7 +451,7 @@ if GMP_VERSION.major >= 6
450451
x = BigInt()
451452
while true
452453
# note: on CRAY computers, the second argument may be of type Cint (48 bits) and not Clong
453-
xd = ccall((:__gmpz_limbs_write, :libgmp), Ptr{Culong}, (Ptr{BigInt}, Clong), &x, g.nlimbs)
454+
xd = ccall((:__gmpz_limbs_write, :libgmp), Ptr{Limb}, (Ptr{BigInt}, Clong), &x, g.nlimbs)
454455
limbs = pointer_to_array(xd, g.nlimbs)
455456
rand!(rng, limbs)
456457
limbs[end] &= g.mask
@@ -468,7 +469,7 @@ else
468469
g.limbs[end] &= g.mask
469470
ccall((:__gmpz_import, :libgmp), Void,
470471
(Ptr{BigInt}, Csize_t, Cint, Csize_t, Cint, Csize_t, Ptr{Void}),
471-
&x, length(g.limbs), -1, sizeof(Culong), 0, 0, &g.limbs)
472+
&x, length(g.limbs), -1, sizeof(Limb), 0, 0, &g.limbs)
472473
x <= g.m && break
473474
end
474475
ccall((:__gmpz_add, :libgmp), Void, (Ptr{BigInt}, Ptr{BigInt}, Ptr{BigInt}), &x, &x, &g.a)

0 commit comments

Comments
 (0)