Skip to content

Commit

Permalink
Fix #81.
Browse files Browse the repository at this point in the history
  • Loading branch information
maleadt committed Jan 2, 2018
1 parent 66511c1 commit 59dfca3
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
15 changes: 8 additions & 7 deletions src/core/value/constant.jl
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,14 @@ export ConstantInt, ConstantFP
end
identify(::Type{Value}, ::Val{API.LLVMConstantIntValueKind}) = ConstantInt

# NOTE: fixed set for dispatch, and because we can't rely on sizeof(T)==width(T)
const SmallInteger = Union{Int8, Int16, Int32, Int64, UInt8, UInt16, UInt32, UInt64}
function ConstantInt(typ::IntegerType, val::SmallInteger, signed=false)
wideval = convert(Int64, val)
bits = reinterpret(Culonglong, wideval)
return ConstantInt(API.LLVMConstInt(ref(typ), bits, convert(Bool, signed)))
end
# NOTE: fixed set for dispatch, also because we can't rely on sizeof(T)==width(T)
const WideInteger = Union{Int64, UInt64}
ConstantInt(typ::IntegerType, val::WideInteger, signed=false) =
ConstantInt(API.LLVMConstInt(ref(typ), reinterpret(Culonglong, val),
convert(Bool, signed)))
const SmallInteger = Union{Int8, Int16, Int32, UInt8, UInt16, UInt32}
ConstantInt(typ::IntegerType, val::SmallInteger, signed=false) =
ConstantInt(typ, convert(Int64, val), signed)

function ConstantInt(typ::IntegerType, val::Integer, signed=false)
valbits = ceil(Int, log2(abs(val))) + 1
Expand Down
6 changes: 6 additions & 0 deletions test/core.jl
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,12 @@ Context() do ctx
@test convert(UInt, constval) == 1
end

# issue #81
for T in [Int32, UInt32, Int64, UInt64]
constval = ConstantInt(typemax(T))
@test convert(T, constval) == typemax(T)
end

end


Expand Down

0 comments on commit 59dfca3

Please sign in to comment.