Skip to content

Commit 9cfadee

Browse files
authored
Merge pull request #273 from leios/convert_fixes
fix to allow for converting to Int
2 parents 507f1bc + 00fbdad commit 9cfadee

File tree

3 files changed

+72
-0
lines changed

3 files changed

+72
-0
lines changed

src/compiler.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ function generate_overdubs(mod, Ctx)
5151
@inline Cassette.overdub(::$Ctx, ::typeof(Base.throw_boundserror), args...) = Base.throw_boundserror(args...)
5252
@inline Cassette.overdub(::$Ctx, ::typeof(Base.Math.throw_exp_domainerror), args...) = Base.Math.throw_exp_domainerror(args...)
5353

54+
@inline Cassette.overdub(::$Ctx, ::typeof(Core.throw_inexacterror), args...) = throw(InexactError(args...))
55+
@inline Cassette.overdub(::$Ctx, ::Core.Typeof(Base.InexactError), args...) = InexactError(args...)
56+
5457
function Cassette.overdub(::$Ctx, ::typeof(:), start::T, step::T, stop::T) where T<:Union{Float16,Float32,Float64}
5558
lf = (stop-start)/step
5659
if lf < 0

test/convert.jl

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
using KernelAbstractions, Test
2+
3+
# Not passing in typelist because CuArrays will not support passing in
4+
# non-inlined types
5+
@kernel function convert_kernel!(A, B)
6+
tid = @index(Global, Linear)
7+
8+
# Int -> Int
9+
tid = Int64(Int32(tid))
10+
11+
@inbounds B[tid, 1] = ceil(Int8, A[tid])
12+
@inbounds B[tid, 2] = ceil(Int16, A[tid])
13+
@inbounds B[tid, 3] = ceil(Int32, A[tid])
14+
@inbounds B[tid, 4] = ceil(Int64, A[tid])
15+
@inbounds B[tid, 5] = ceil(Int128, A[tid])
16+
@inbounds B[tid, 6] = ceil(UInt8, A[tid])
17+
@inbounds B[tid, 7] = ceil(UInt16, A[tid])
18+
@inbounds B[tid, 8] = ceil(UInt32, A[tid])
19+
@inbounds B[tid, 9] = ceil(UInt64, A[tid])
20+
@inbounds B[tid, 10] = ceil(UInt128, A[tid])
21+
22+
@inbounds B[tid, 11] = floor(Int8, A[tid])
23+
@inbounds B[tid, 12] = floor(Int16, A[tid])
24+
@inbounds B[tid, 13] = floor(Int32, A[tid])
25+
@inbounds B[tid, 14] = floor(Int64, A[tid])
26+
@inbounds B[tid, 15] = floor(Int128, A[tid])
27+
@inbounds B[tid, 16] = floor(UInt8, A[tid])
28+
@inbounds B[tid, 17] = floor(UInt16, A[tid])
29+
@inbounds B[tid, 18] = floor(UInt32, A[tid])
30+
@inbounds B[tid, 19] = floor(UInt64, A[tid])
31+
@inbounds B[tid, 20] = floor(UInt128, A[tid])
32+
33+
@inbounds B[tid, 21] = round(Int8, A[tid])
34+
@inbounds B[tid, 22] = round(Int16, A[tid])
35+
@inbounds B[tid, 23] = round(Int32, A[tid])
36+
@inbounds B[tid, 24] = round(Int64, A[tid])
37+
@inbounds B[tid, 25] = round(Int128, A[tid])
38+
@inbounds B[tid, 26] = round(UInt8, A[tid])
39+
@inbounds B[tid, 27] = round(UInt16, A[tid])
40+
@inbounds B[tid, 28] = round(UInt32, A[tid])
41+
@inbounds B[tid, 29] = round(UInt64, A[tid])
42+
@inbounds B[tid, 30] = round(UInt128, A[tid])
43+
44+
end
45+
46+
function convert_testsuite(backend, ArrayT)
47+
48+
N = 32
49+
d_A = ArrayT([rand()*3 for i = 1:N])
50+
51+
# 30 because we have 10 integer types and we have 3 operations
52+
d_B = ArrayT(zeros(N, 30))
53+
54+
@testset "convert test" begin
55+
kernel = convert_kernel!(backend(), 4)
56+
wait(kernel(d_A, d_B, ndrange=(N),))
57+
58+
for i = 1:10
59+
@test d_B[:,i] == ceil.(d_A)
60+
@test d_B[:,i+10] == floor.(d_A)
61+
@test d_B[:,i+20] == round.(d_A)
62+
end
63+
end
64+
end

test/testsuite.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ include("print_test.jl")
1414
include("compiler.jl")
1515
include("reflection.jl")
1616
include("examples.jl")
17+
include("convert.jl")
1718

1819
function testsuite(backend, backend_str, backend_mod, AT, DAT)
1920
@testset "Unittests" begin
@@ -64,6 +65,10 @@ function testsuite(backend, backend_str, backend_mod, AT, DAT)
6465
reflection_testsuite(backend, AT)
6566
end
6667

68+
@testset "Convert" begin
69+
convert_testsuite(backend, AT)
70+
end
71+
6772
if backend_str == "CUDA"
6873
@testset "Examples" begin
6974
examples_testsuite()

0 commit comments

Comments
 (0)