Skip to content

Commit

Permalink
Fix alignment check for non 16-byte alignments
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasfaingnaert committed Nov 14, 2023
1 parent 912fa05 commit 8caa172
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions src/layout.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,21 @@ struct Vec{N, T} end
error()
end

@inline function checkalignment(ptr)
checkalignment(Bool, ptr) || throw_alignmenterror(ptr)
@inline function checkalignment(ptr, alignment)
checkalignment(Bool, ptr, alignment) || throw_alignmenterror(ptr)
nothing
end

@inline function checkalignment(::Type{Bool}, ptr)
Int(ptr) % 16 == 0
@inline function checkalignment(::Type{Bool}, ptr, alignment)
Int(ptr) % alignment == 0
end

@inline @generated function vloada(::Type{Vec{N, T}}, ptr::Core.LLVMPtr{T, AS}) where {N, T, AS}
alignment = sizeof(T) * N

return quote
vec_ptr = Base.bitcast(Core.LLVMPtr{NTuple{N, VecElement{T}}, AS}, ptr)
@boundscheck checkalignment(vec_ptr)
@boundscheck checkalignment(vec_ptr, $alignment)
return unsafe_load(vec_ptr, 1, Val($alignment))
end
end
Expand All @@ -55,7 +55,7 @@ end
append!(ex.args, (quote
y = @ntuple $N j -> VecElement{T}(x[j+$offset].value)
vec_ptr = Base.bitcast(Core.LLVMPtr{NTuple{N, VecElement{T}}, AS}, ptr)
@boundscheck checkalignment(vec_ptr)
@boundscheck checkalignment(vec_ptr, $alignment)
unsafe_store!(vec_ptr, y, $offset ÷ N + 1, Val($alignment))
end).args)
end
Expand Down

0 comments on commit 8caa172

Please sign in to comment.