diff --git a/Project.toml b/Project.toml index 4d76b97..6dc11c7 100644 --- a/Project.toml +++ b/Project.toml @@ -1,6 +1,6 @@ name = "GPUToolbox" uuid = "096a3bc2-3ced-46d0-87f4-dd12716f4bfc" -version = "0.1.0" +version = "0.2.0" [compat] julia = "1.10" diff --git a/README.md b/README.md index 0c0912c..2763a42 100644 --- a/README.md +++ b/README.md @@ -23,5 +23,6 @@ This package currently exports the following: - `@checked`: Add to a function definition to generate an unchecked and a checked version. - `@debug_ccall`: like `ccall` but prints the ccall, its arguments, and its return value - `@gcsafe_ccall`: like `@ccall` but marking it safe for the GC to run. +- Suffix literals like `1i8` (`i8`, `i16`, `i32`, `u8`, `u16`, `u32`) for constructing literals of a certain type. For more details on a specific symbol, check out its docstring in the Julia REPL. diff --git a/src/GPUToolbox.jl b/src/GPUToolbox.jl index 2dad6f2..c67f992 100644 --- a/src/GPUToolbox.jl +++ b/src/GPUToolbox.jl @@ -2,5 +2,6 @@ module GPUToolbox include("simpleversion.jl") # exports SimpleVersion, @sv_str include("ccalls.jl") # exports @checked, @debug_ccall, @gcsafe_ccall +include("literals.jl") end # module GPUToolbox diff --git a/src/literals.jl b/src/literals.jl new file mode 100644 index 0000000..b8e4509 --- /dev/null +++ b/src/literals.jl @@ -0,0 +1,27 @@ +export i8, i16, i32, u8, u16, u32 +# helper type for writing smaller than Int64 literals + +""" + Literal{T} + +Construct an object that can be used to convert literals to other types. +One can use the object in suffix form `1*i8` or `1i8` to perform the conversion. + +## Exported constants +- `i8`: Convert to `Int8` +- `i16`: Convert to `Int16` +- `i32`: Convert to `Int32` +- `u8`: Convert to `UInt8` +- `u16`: Convert to `UInt16` +- `u32`: Convert to `UInt32` +``` +""" +struct Literal{T} end +Base.:(*)(x::Number, ::Type{Literal{T}}) where {T} = T(x) + +const i8 = Literal{Int8} +const i16 = Literal{Int16} +const i32 = Literal{Int32} +const u8 = Literal{UInt8} +const u16 = Literal{UInt16} +const u32 = Literal{UInt32} diff --git a/test/runtests.jl b/test/runtests.jl index d26ca7f..9def2e8 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -43,6 +43,18 @@ using InteractiveUtils @test !(sv2 > sv2) # Default end + @testset "Literals" begin + @test 1i8 === Int8(1) + @test 1i16 === Int16(1) + @test 1i32 === Int32(1) + @test_throws InexactError 128i8 + + @test 1u8 === UInt8(1) + @test 1u16 === UInt16(1) + @test 1u32 === UInt32(1) + @test_throws InexactError 256u8 + end + @testset "gcsafe_ccall" begin function gc_safe_ccall() # jl_rand is marked as JL_NOTSAFEPOINT