Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "GPUToolbox"
uuid = "096a3bc2-3ced-46d0-87f4-dd12716f4bfc"
version = "0.1.0"
version = "0.2.0"

[compat]
julia = "1.10"
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
1 change: 1 addition & 0 deletions src/GPUToolbox.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
27 changes: 27 additions & 0 deletions src/literals.jl
Original file line number Diff line number Diff line change
@@ -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}
12 changes: 12 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading