Skip to content

Commit 4cdaedb

Browse files
authored
Add support for literals (#10)
1 parent 7564d38 commit 4cdaedb

File tree

5 files changed

+42
-1
lines changed

5 files changed

+42
-1
lines changed

Diff for: Project.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "GPUToolbox"
22
uuid = "096a3bc2-3ced-46d0-87f4-dd12716f4bfc"
3-
version = "0.1.0"
3+
version = "0.2.0"
44

55
[compat]
66
julia = "1.10"

Diff for: README.md

+1
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,6 @@ This package currently exports the following:
2323
- `@checked`: Add to a function definition to generate an unchecked and a checked version.
2424
- `@debug_ccall`: like `ccall` but prints the ccall, its arguments, and its return value
2525
- `@gcsafe_ccall`: like `@ccall` but marking it safe for the GC to run.
26+
- Suffix literals like `1i8` (`i8`, `i16`, `i32`, `u8`, `u16`, `u32`) for constructing literals of a certain type.
2627

2728
For more details on a specific symbol, check out its docstring in the Julia REPL.

Diff for: src/GPUToolbox.jl

+1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@ module GPUToolbox
22

33
include("simpleversion.jl") # exports SimpleVersion, @sv_str
44
include("ccalls.jl") # exports @checked, @debug_ccall, @gcsafe_ccall
5+
include("literals.jl")
56

67
end # module GPUToolbox

Diff for: src/literals.jl

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
export i8, i16, i32, u8, u16, u32
2+
# helper type for writing smaller than Int64 literals
3+
4+
"""
5+
Literal{T}
6+
7+
Construct an object that can be used to convert literals to other types.
8+
One can use the object in suffix form `1*i8` or `1i8` to perform the conversion.
9+
10+
## Exported constants
11+
- `i8`: Convert to `Int8`
12+
- `i16`: Convert to `Int16`
13+
- `i32`: Convert to `Int32`
14+
- `u8`: Convert to `UInt8`
15+
- `u16`: Convert to `UInt16`
16+
- `u32`: Convert to `UInt32`
17+
```
18+
"""
19+
struct Literal{T} end
20+
Base.:(*)(x::Number, ::Type{Literal{T}}) where {T} = T(x)
21+
22+
const i8 = Literal{Int8}
23+
const i16 = Literal{Int16}
24+
const i32 = Literal{Int32}
25+
const u8 = Literal{UInt8}
26+
const u16 = Literal{UInt16}
27+
const u32 = Literal{UInt32}

Diff for: test/runtests.jl

+12
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,18 @@ using InteractiveUtils
4343
@test !(sv2 > sv2) # Default
4444
end
4545

46+
@testset "Literals" begin
47+
@test 1i8 === Int8(1)
48+
@test 1i16 === Int16(1)
49+
@test 1i32 === Int32(1)
50+
@test_throws InexactError 128i8
51+
52+
@test 1u8 === UInt8(1)
53+
@test 1u16 === UInt16(1)
54+
@test 1u32 === UInt32(1)
55+
@test_throws InexactError 256u8
56+
end
57+
4658
@testset "gcsafe_ccall" begin
4759
function gc_safe_ccall()
4860
# jl_rand is marked as JL_NOTSAFEPOINT

0 commit comments

Comments
 (0)