From 5cdfd216acd87df93a29320b23000ae11d747335 Mon Sep 17 00:00:00 2001 From: Stefan Karpinski Date: Wed, 15 Oct 2014 17:17:19 -0400 Subject: [PATCH] integer trunction: use `n % T` instead of `mod(n, T)`. --- base/datafmt.jl | 2 +- base/deprecated.jl | 2 +- base/hashing.jl | 2 +- base/int.jl | 25 +++++++++++++------------ base/pkg/types.jl | 2 +- base/profile.jl | 2 +- base/random.jl | 16 ++++++++-------- base/string.jl | 2 +- base/version.jl | 2 +- test/hashing.jl | 2 +- 10 files changed, 29 insertions(+), 28 deletions(-) diff --git a/base/datafmt.jl b/base/datafmt.jl index 3d515a87a5e1a..2290000a9d0f0 100644 --- a/base/datafmt.jl +++ b/base/datafmt.jl @@ -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 diff --git a/base/deprecated.jl b/base/deprecated.jl index 2f14bdd7cec01..b2d0e8bdfaba0 100644 --- a/base/deprecated.jl +++ b/base/deprecated.jl @@ -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) diff --git a/base/hashing.jl b/base/hashing.jl index 0b396ba37e145..6ef396328b983 100644 --- a/base/hashing.jl +++ b/base/hashing.jl @@ -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) diff --git a/base/int.jl b/base/int.jl index 6f453116edf45..38aeae865858a 100644 --- a/base/int.jl +++ b/base/int.jl @@ -155,14 +155,14 @@ 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 @@ -170,13 +170,14 @@ for to in tuple(IntTypes...,Char), from in tuple(IntTypes...,Char,Bool) 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 @@ -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) @@ -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)) @@ -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) @@ -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 @@ -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 diff --git a/base/pkg/types.jl b/base/pkg/types.jl index 58f3294a7998c..fc3c9793ac842 100644 --- a/base/pkg/types.jl +++ b/base/pkg/types.jl @@ -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} diff --git a/base/profile.jl b/base/profile.jl index 1dd1616064d0b..f1a77694b8061 100644 --- a/base/profile.jl +++ b/base/profile.jl @@ -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) diff --git a/base/random.jl b/base/random.jl index 680cd62c3a0bf..98c59e03eb884 100644 --- a/base/random.jl +++ b/base/random.jl @@ -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)) @@ -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 @@ -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)) diff --git a/base/string.jl b/base/string.jl index d25c6fd8cb747..22392651bded9 100644 --- a/base/string.jl +++ b/base/string.jl @@ -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) diff --git a/base/version.jl b/base/version.jl index b1e785be3b78a..6c822d6c52627 100644 --- a/base/version.jl +++ b/base/version.jl @@ -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) diff --git a/test/hashing.jl b/test/hashing.jl index 630caaf8e98c1..bb3f59979030e 100644 --- a/test/hashing.jl +++ b/test/hashing.jl @@ -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