Skip to content

Commit

Permalink
adding proper throw_inexact exceptions for Integer conversions
Browse files Browse the repository at this point in the history
  • Loading branch information
leios committed Nov 1, 2021
1 parent 507f1bc commit 00fbdad
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/compiler.jl
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ function generate_overdubs(mod, Ctx)
@inline Cassette.overdub(::$Ctx, ::typeof(Base.throw_boundserror), args...) = Base.throw_boundserror(args...)
@inline Cassette.overdub(::$Ctx, ::typeof(Base.Math.throw_exp_domainerror), args...) = Base.Math.throw_exp_domainerror(args...)

@inline Cassette.overdub(::$Ctx, ::typeof(Core.throw_inexacterror), args...) = throw(InexactError(args...))
@inline Cassette.overdub(::$Ctx, ::Core.Typeof(Base.InexactError), args...) = InexactError(args...)

function Cassette.overdub(::$Ctx, ::typeof(:), start::T, step::T, stop::T) where T<:Union{Float16,Float32,Float64}
lf = (stop-start)/step
if lf < 0
Expand Down
64 changes: 64 additions & 0 deletions test/convert.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using KernelAbstractions, Test

# Not passing in typelist because CuArrays will not support passing in
# non-inlined types
@kernel function convert_kernel!(A, B)
tid = @index(Global, Linear)

# Int -> Int
tid = Int64(Int32(tid))

@inbounds B[tid, 1] = ceil(Int8, A[tid])
@inbounds B[tid, 2] = ceil(Int16, A[tid])
@inbounds B[tid, 3] = ceil(Int32, A[tid])
@inbounds B[tid, 4] = ceil(Int64, A[tid])
@inbounds B[tid, 5] = ceil(Int128, A[tid])
@inbounds B[tid, 6] = ceil(UInt8, A[tid])
@inbounds B[tid, 7] = ceil(UInt16, A[tid])
@inbounds B[tid, 8] = ceil(UInt32, A[tid])
@inbounds B[tid, 9] = ceil(UInt64, A[tid])
@inbounds B[tid, 10] = ceil(UInt128, A[tid])

@inbounds B[tid, 11] = floor(Int8, A[tid])
@inbounds B[tid, 12] = floor(Int16, A[tid])
@inbounds B[tid, 13] = floor(Int32, A[tid])
@inbounds B[tid, 14] = floor(Int64, A[tid])
@inbounds B[tid, 15] = floor(Int128, A[tid])
@inbounds B[tid, 16] = floor(UInt8, A[tid])
@inbounds B[tid, 17] = floor(UInt16, A[tid])
@inbounds B[tid, 18] = floor(UInt32, A[tid])
@inbounds B[tid, 19] = floor(UInt64, A[tid])
@inbounds B[tid, 20] = floor(UInt128, A[tid])

@inbounds B[tid, 21] = round(Int8, A[tid])
@inbounds B[tid, 22] = round(Int16, A[tid])
@inbounds B[tid, 23] = round(Int32, A[tid])
@inbounds B[tid, 24] = round(Int64, A[tid])
@inbounds B[tid, 25] = round(Int128, A[tid])
@inbounds B[tid, 26] = round(UInt8, A[tid])
@inbounds B[tid, 27] = round(UInt16, A[tid])
@inbounds B[tid, 28] = round(UInt32, A[tid])
@inbounds B[tid, 29] = round(UInt64, A[tid])
@inbounds B[tid, 30] = round(UInt128, A[tid])

end

function convert_testsuite(backend, ArrayT)

N = 32
d_A = ArrayT([rand()*3 for i = 1:N])

# 30 because we have 10 integer types and we have 3 operations
d_B = ArrayT(zeros(N, 30))

@testset "convert test" begin
kernel = convert_kernel!(backend(), 4)
wait(kernel(d_A, d_B, ndrange=(N),))

for i = 1:10
@test d_B[:,i] == ceil.(d_A)
@test d_B[:,i+10] == floor.(d_A)
@test d_B[:,i+20] == round.(d_A)
end
end
end
5 changes: 5 additions & 0 deletions test/testsuite.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ include("print_test.jl")
include("compiler.jl")
include("reflection.jl")
include("examples.jl")
include("convert.jl")

function testsuite(backend, backend_str, backend_mod, AT, DAT)
@testset "Unittests" begin
Expand Down Expand Up @@ -64,6 +65,10 @@ function testsuite(backend, backend_str, backend_mod, AT, DAT)
reflection_testsuite(backend, AT)
end

@testset "Convert" begin
convert_testsuite(backend, AT)
end

if backend_str == "CUDA"
@testset "Examples" begin
examples_testsuite()
Expand Down

0 comments on commit 00fbdad

Please sign in to comment.