Skip to content

Commit

Permalink
fixing a bug in int2str on integer typemin arguments
Browse files Browse the repository at this point in the history
removing more uses of one() and zero()
making -Uint64 give a Uint64, like 0-Uint64 would
  • Loading branch information
JeffBezanson committed Dec 21, 2011
1 parent c082307 commit 7cd1f9e
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 13 deletions.
7 changes: 3 additions & 4 deletions j/env.j
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,10 @@ has(::EnvHash, k::String) = hasenv(k)
del(::EnvHash, k::String) = unsetenv(k)
assign(::EnvHash, v::String, k::String) = (setenv(k,v); v)

start(::EnvHash) = int32(0)
start(::EnvHash) = 0
done(::EnvHash, i) = (ccall(:jl_environ, Any, (Int32,), int32(i)) == nothing)
function next(::EnvHash, i)
i = int32(i)
env = ccall(:jl_environ, Any, (Int32,), i)
env = ccall(:jl_environ, Any, (Int32,), int32(i))
if env == nothing
error("environ: index out of range")
end
Expand All @@ -66,7 +65,7 @@ function next(::EnvHash, i)
if m == nothing
error("malformed environment entry: $env")
end
(m.captures, i+one(i))
(m.captures, i+1)
end

function show(::EnvHash)
Expand Down
2 changes: 1 addition & 1 deletion j/int.j
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ promote_rule(::Type{Uint64}, ::Type{Int64}) = Uint64 # LOSSY
-(x::Uint8 ) = boxsi16(neg_int(zext16(unbox8 (x))))
-(x::Uint16) = boxsi32(neg_int(zext32(unbox16(x))))
-(x::Uint32) = boxsi64(neg_int(zext64(unbox32(x))))
-(x::Uint64) = boxsi64(neg_int(unbox64(x))) # LOSSY
-(x::Uint64) = boxui64(neg_int(unbox64(x))) # LOSSY

+(x::Int8 , y::Int8 ) = boxsi8 (add_int(unbox8 (x), unbox8 (y)))
+(x::Int16, y::Int16) = boxsi16(add_int(unbox16(x), unbox16(y)))
Expand Down
8 changes: 4 additions & 4 deletions j/sparse.j
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ end
function istril{T}(A::SparseMatrixCSC{T})
for col = 1:A.n
for i = A.colptr[col]:(A.colptr[col]-1)
if A.rowval[i] < col && A.nzval[i] != zero(T); return false; end
if A.rowval[i] < col && A.nzval[i] != 0; return false; end
end
end
return true
Expand All @@ -237,7 +237,7 @@ end
function istriu{T}(A::SparseMatrixCSC{T})
for col = 1:A.n
for i = A.colptr[col]:(A.colptr[col]-1)
if A.rowval[i] > col && A.nzval[i] != zero(T); return false; end
if A.rowval[i] > col && A.nzval[i] != 0; return false; end
end
end
return true
Expand Down Expand Up @@ -512,7 +512,7 @@ function assign{T,T_int}(A::SparseMatrixCSC{T,T_int}, v, i0::Integer, i1::Intege
i1 = convert(T_int, i1)
if i0 < 1 || i0 > A.m || i1 < 1 || i1 > A.n; error("assign: index out of bounds"); end
v = convert(T, v)
if v == zero(T) #either do nothing or delete entry if it exists
if v == 0 #either do nothing or delete entry if it exists
first = A.colptr[i1]
last = A.colptr[i1+1]-1
loc = -1
Expand Down Expand Up @@ -884,7 +884,7 @@ assign{T,N}(S::SparseAccumulator{T}, v::AbstractArray{T,N}, i::Integer) =
invoke(assign, (SparseAccumulator{T}, Any, Integer), S, v, i)

function assign{T}(S::SparseAccumulator{T}, v, i::Integer)
if v == zero(T)
if v == 0
if S.flags[i]
S.vals[i] = v
S.flags[i] = false
Expand Down
11 changes: 9 additions & 2 deletions j/string.j
Original file line number Diff line number Diff line change
Expand Up @@ -831,9 +831,16 @@ function ndigits(n::Integer, b::Integer)
return nd
end

function int2str(n::Integer, b::Integer)
int2str(n::Integer, b::Integer) = int2str(int64(n), b)

function int2str(n::Union(Int64,Uint64), b::Integer)
if b < 2 || b > 40; error("int2str: invalid base ", b); end
neg = n<zero(n) ? 1 : 0
if n == typemin(Int64)
# this is really cheap, but the algorithm fails in this case
# since abs(n) is still negative.
return "-9223372036854775808"
end
neg = n<0 ? 1 : 0
n = abs(n)
b = convert(typeof(n), b)
ndig = ndigits(n, b)
Expand Down
4 changes: 2 additions & 2 deletions j/utf8.j
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ function next(s::UTF8String, i::Int)
for j = 1:trailing
c += s.data[i]
c <<= 6
i += one(i)
i += 1
end
c += s.data[i]
i += one(i)
i += 1
c -= _jl_utf8_offset[trailing+1]
char(c), i
end
Expand Down
4 changes: 4 additions & 0 deletions test/core.j
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,10 @@ end
@assert hex(0xffffffffffff+1)=="1000000000000"
@assert hex(typemax(Uint64))=="ffffffffffffffff"
@assert int2str(typemin(Int64), 10) == "-9223372036854775808"
@assert int2str(typemin(Int16), 10) == "-32768"
@assert int2str(typemin(Int8 ), 10) == "-128"
# conversions
function fooo()
local x::Int8
Expand Down

0 comments on commit 7cd1f9e

Please sign in to comment.