Skip to content

Commit

Permalink
integer trunction: use n % T instead of mod(n, T).
Browse files Browse the repository at this point in the history
  • Loading branch information
StefanKarpinski committed Oct 15, 2014
1 parent 2ee9c15 commit 5cdfd21
Show file tree
Hide file tree
Showing 10 changed files with 29 additions and 28 deletions.
2 changes: 1 addition & 1 deletion base/datafmt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ function dlm_parse{T,D}(dbuff::T, eol::D, dlm::D, qchar::D, cchar::D, ign_adj_dl
all_ascii = (D <: Uint8) || (isascii(eol) && isascii(dlm) && (!allow_quote || isascii(qchar)) && (!allow_comments || isascii(cchar)))
(T <: UTF8String) && all_ascii && (return dlm_parse(dbuff.data, uint8(eol), uint8(dlm), uint8(qchar), uint8(cchar), ign_adj_dlm, allow_quote, allow_comments, skipstart, skipblanks, dh))
ncols = nrows = col = 0
is_default_dlm = (dlm == mod(invalid_dlm, D))
is_default_dlm = (dlm == invalid_dlm % D)
error_str = ""
# 0: begin field, 1: quoted field, 2: unquoted field, 3: second quote (could either be end of field or escape character), 4: comment, 5: skipstart
state = (skipstart > 0) ? 5 : 0
Expand Down
2 changes: 1 addition & 1 deletion base/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -196,4 +196,4 @@ const None = Union()
@deprecate Dict{V}(ks::Tuple, vs::(V...)) Dict{Any,V}(zip(ks, vs))
@deprecate Dict(ks, vs) Dict{Any,Any}(zip(ks, vs))

@deprecate itrunc{T<:Integer}(::Type{T}, n::Integer) mod(n, T)
@deprecate itrunc{T<:Integer}(::Type{T}, n::Integer) (n % T)
2 changes: 1 addition & 1 deletion base/hashing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ function hash_64_32(n::Uint64)
a = a $ a >> 11
a = a + a << 6
a = a $ a >> 22
return mod(a, Uint32)
return a % Uint32
end

function hash_32_32(n::Uint32)
Expand Down
25 changes: 13 additions & 12 deletions base/int.jl
Original file line number Diff line number Diff line change
Expand Up @@ -155,28 +155,29 @@ for to in tuple(IntTypes...,Char), from in tuple(IntTypes...,Char,Bool)
else
@eval convert(::Type{$to}, x::($from)) = box($to,checked_trunc_uint($to,unbox($from,x)))
end
@eval mod(x::($from), ::Type{$to}) = box($to,trunc_int($to,unbox($from,x)))
@eval rem(x::($from), ::Type{$to}) = box($to,trunc_int($to,unbox($from,x)))
elseif from.size < to.size || from === Bool
if issubtype(from, Signed)
@eval convert(::Type{$to}, x::($from)) = box($to,sext_int($to,unbox($from,x)))
else
@eval convert(::Type{$to}, x::($from)) = box($to,zext_int($to,unbox($from,x)))
end
@eval mod(x::($from), ::Type{$to}) = convert($to,x)
@eval rem(x::($from), ::Type{$to}) = convert($to,x)
else
if !(issubtype(from,Signed) === issubtype(to,Signed))
# raise InexactError if x's top bit is set
@eval convert(::Type{$to}, x::($from)) = box($to,check_top_bit(unbox($from,x)))
else
@eval convert(::Type{$to}, x::($from)) = box($to,unbox($from,x))
end
@eval mod(x::($from), ::Type{$to}) = box($to,unbox($from,x))
@eval rem(x::($from), ::Type{$to}) = box($to,unbox($from,x))
end
end
end

mod{T<:Integer}(x::T, ::Type{T}) = x
mod(x::Integer, ::Type{Bool}) = ((x&1)!=0)
rem{T<:Integer}(x::T, ::Type{T}) = x
rem(x::Integer, ::Type{Bool}) = ((x&1)!=0)
mod{T<:Integer}(x::Integer, ::Type{T}) = rem(x, T)

for to in (Int8, Int16, Int32, Int64)
@eval begin
Expand Down Expand Up @@ -240,7 +241,7 @@ int64(x) = convert(Int64,x)
int128(x) = convert(Int128,x)

uint8(x) = convert(Uint8,x)
uint8(x::Integer) = mod(x,Uint8)
uint8(x::Integer) = x % Uint8
uint8(x::Int8) = box(Uint8,unbox(Int8,x))
uint8(x::Bool) = convert(Uint8,x)

Expand Down Expand Up @@ -418,8 +419,8 @@ if WORD_SIZE==32
end

function *(u::Int128, v::Int128)
u0 = mod(u,Uint64); u1 = int64(u>>64)
v0 = mod(v,Uint64); v1 = int64(v>>64)
u0 = u % Uint64; u1 = int64(u>>64)
v0 = v % Uint64; v1 = int64(v>>64)
lolo = widemul(u0, v0)
lohi = widemul(reinterpret(Int64,u0), v1)
hilo = widemul(u1, reinterpret(Int64,v0))
Expand All @@ -429,8 +430,8 @@ if WORD_SIZE==32
end

function *(u::Uint128, v::Uint128)
u0 = mod(u,Uint64); u1 = int64(u>>64)
v0 = mod(v,Uint64); v1 = int64(v>>64)
u0 = u % Uint64; u1 = int64(u>>64)
v0 = v % Uint64; v1 = int64(v>>64)
lolo = widemul(u0, v0)
lohi = widemul(u0, v1)
hilo = widemul(u1, v0)
Expand Down Expand Up @@ -497,7 +498,7 @@ for T in (Int8,Uint8)
@eval function checked_mul(x::$T, y::$T)
xy = widemul(x,y)
(typemin($T) <= xy <= typemax($T)) || throw(OverflowError())
return mod(xy,$T)
return xy % $T
end
end

Expand All @@ -506,7 +507,7 @@ for T in (Int64,Uint64)
@eval function checked_mul(x::$T, y::$T)
xy = int128(x)*int128(y)
(typemin($T) <= xy <= typemax($T)) || throw(OverflowError())
return mod(xy,$T)
return xy % $T
end
end
else
Expand Down
2 changes: 1 addition & 1 deletion base/pkg/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ function intersect(A::VersionSet, B::VersionSet)
VersionSet(ivals)
end
==(A::VersionSet, B::VersionSet) = A.intervals == B.intervals
hash(s::VersionSet, h::Uint) = hash(s.intervals, h + mod(0x2fd2ca6efa023f44,Uint))
hash(s::VersionSet, h::Uint) = hash(s.intervals, h + (0x2fd2ca6efa023f44 % Uint))
deepcopy_internal(vs::VersionSet, ::ObjectIdDict) = VersionSet(copy(vs.intervals))

typealias Requires Dict{ByteString,VersionSet}
Expand Down
2 changes: 1 addition & 1 deletion base/profile.jl
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ const UNKNOWN = LineInfo("?", "?", -1, true, 0)
==(a::LineInfo, b::LineInfo) = a.line == b.line && a.fromC == b.fromC && a.func == b.func && a.file == b.file

function hash(li::LineInfo, h::Uint)
h += mod(0xf4fbda67fe20ce88,Uint)
h += 0xf4fbda67fe20ce88 % Uint
h = hash(li.line, h)
h = hash(li.file, h)
h = hash(li.func, h)
Expand Down
16 changes: 8 additions & 8 deletions base/random.jl
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,14 @@ rand(r::MersenneTwister) = dsfmt_genrand_close_open(r.state)
dsfmt_randui32() = dsfmt_gv_genrand_uint32()
dsfmt_randui64() = uint64(dsfmt_randui32()) | (uint64(dsfmt_randui32())<<32)

rand(::Type{Uint8}) = mod(rand(Uint32),Uint8)
rand(::Type{Uint16}) = mod(rand(Uint32),Uint16)
rand(::Type{Uint8}) = rand(Uint32) % Uint8
rand(::Type{Uint16}) = rand(Uint32) % Uint16
rand(::Type{Uint32}) = dsfmt_randui32()
rand(::Type{Uint64}) = dsfmt_randui64()
rand(::Type{Uint128}) = uint128(rand(Uint64))<<64 | rand(Uint64)

rand(::Type{Int8}) = mod(rand(Uint32),Int8)
rand(::Type{Int16}) = mod(rand(Uint32),Int16)
rand(::Type{Int8}) = rand(Uint32) % Int8
rand(::Type{Int16}) = rand(Uint32) % Int16
rand(::Type{Int32}) = reinterpret(Int32,rand(Uint32))
rand(::Type{Int64}) = reinterpret(Int64,rand(Uint64))
rand(::Type{Int128}) = reinterpret(Int128,rand(Uint128))
Expand Down Expand Up @@ -154,12 +154,12 @@ rem_knuth{T<:Unsigned}(a::T, b::T) = b != 0 ? a % b : a

# maximum multiple of k <= 2^bits(T) decremented by one,
# that is 0xFFFFFFFF if k = typemax(T) - typemin(T) with intentional underflow
maxmultiple(k::Uint32) = mod(div(0x0000000100000000,k + (k == 0))*k - 1, Uint32)
maxmultiple(k::Uint64) = mod(div(0x00000000000000010000000000000000, k + (k == 0))*k - 1, Uint64)
maxmultiple(k::Uint32) = (div(0x0000000100000000,k + (k == 0))*k - 1) % Uint32
maxmultiple(k::Uint64) = (div(0x00000000000000010000000000000000, k + (k == 0))*k - 1) % Uint64
# maximum multiple of k within 1:typemax(Uint128)
maxmultiple(k::Uint128) = div(typemax(Uint128), k + (k == 0))*k - 1
# maximum multiple of k within 1:2^32 or 1:2^64, depending on size
maxmultiplemix(k::Uint64) = mod(div((k >> 32 != 0)*0x0000000000000000FFFFFFFF00000000 + 0x0000000100000000, k + (k == 0))*k - 1, Uint64)
maxmultiplemix(k::Uint64) = (div((k >> 32 != 0)*0x0000000000000000FFFFFFFF00000000 + 0x0000000100000000, k + (k == 0))*k - 1) % Uint64

immutable RandIntGen{T<:Integer, U<:Unsigned}
a::T # first element of the range
Expand Down Expand Up @@ -206,7 +206,7 @@ function rand{T<:Integer, U<:Unsigned}(g::RandIntGen{T,U})
while x > g.u
x = rand(U)
end
mod(unsigned(g.a) + rem_knuth(x, g.k), T)
(unsigned(g.a) + rem_knuth(x, g.k)) % T
end

rand{T<:Union(Signed,Unsigned,Bool,Char)}(r::UnitRange{T}) = rand(RandIntGen(r))
Expand Down
2 changes: 1 addition & 1 deletion base/string.jl
Original file line number Diff line number Diff line change
Expand Up @@ -661,7 +661,7 @@ const memhash_seed = Uint === Uint64 ? 0x71e729fd56419c81 : 0x56419c81

function hash{T<:ByteString}(s::Union(T,SubString{T}), h::Uint)
h += memhash_seed
ccall(memhash, Uint, (Ptr{Uint8}, Csize_t, Uint32), s, sizeof(s), mod(h,Uint32)) + h
ccall(memhash, Uint, (Ptr{Uint8}, Csize_t, Uint32), s, sizeof(s), h % Uint32) + h
end
hash(s::String, h::Uint) = hash(bytestring(s), h)

Expand Down
2 changes: 1 addition & 1 deletion base/version.jl
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ function isless(a::VersionNumber, b::VersionNumber)
end

function hash(v::VersionNumber, h::Uint)
h += mod(0x8ff4ffdb75f9fede,Uint)
h += 0x8ff4ffdb75f9fede % Uint
h = hash(v.major, h)
h = hash(v.minor, h)
h = hash(v.patch, h)
Expand Down
2 changes: 1 addition & 1 deletion test/hashing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ function coerce(T::Type, x)
if !(T<:Integer) || T===Bool
convert(T, x)
elseif sizeof(T) < sizeof(x)
mod(x, T)
x % T
elseif sizeof(T) == sizeof(x)
reinterpret(T, x)
else
Expand Down

0 comments on commit 5cdfd21

Please sign in to comment.