Skip to content

Commit

Permalink
implement select
Browse files Browse the repository at this point in the history
  • Loading branch information
vchuravy committed Nov 4, 2024
1 parent 799e7e2 commit 71dd9ab
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
18 changes: 13 additions & 5 deletions base/simd.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import Base: memoryrefget, memoryrefnew, memoryrefset!
import Core.Intrinsics: preferred_vector_width

export Vec
export vload, vstore!, preferred_vector, width
export vload, vstore!, preferred_vector, width, select

# TODO: See C# and Co Vec type

Check warning on line 12 in base/simd.jl

View workflow job for this annotation

GitHub Actions / Check whitespace

Whitespace check

trailing whitespace
# TODO: Hardware portable vector types...
Expand Down Expand Up @@ -47,12 +47,20 @@ function Base.show(io::IO, v::Vec{N, T}) where {N, T}
end

# TODO: llvm.vp expects a mask of i1
struct Mask{N}
data::NTuple{N, VecElement{Bool}}
end
const Mask{N} = Vec{N, Bool}

function mask_all(::Val{N}, val::Bool) where N
Mask(ntuple(_->VecElement(val),Val(N)))
Vec(ntuple(_->VecElement(val),Val(N)))
end

# select(m::Mask{N}, a::Vec{N, T}, b::Vec{N,T}) where {N,T} = Core.ifelse(m.data, a.data, b.data)
# ERROR: TypeError: non-boolean (NTuple{4, VecElement{Bool}}) used in boolean context
# Mocked select, relying on SLP
function select(m::Mask{N}, a::Vec{N, T}, b::Vec{N,T}) where {N,T}
data = ntuple(Val(N)) do j
VecElement(Core.ifelse(m.data[j].value, a.data[j].value, b.data[j].value))
end
return Vec(data)
end

# Mocked vload/vstore! relying on SLP
Expand Down
6 changes: 6 additions & 0 deletions test/simd.jl
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,9 @@ end
# TODO: Way to test Intrinsics directly?
#`-v` -> ERROR: neg_float_withtype: value is not a primitive type
end

@testset "select" begin
ir = sprint(io->code_llvm(io, select, (Vec{4, Bool}, Vec{4, Float64}, Vec{4, Float64})))
@test contains(ir, "icmp eq <4 x i8>")
@test contains(ir, "select <4 x i1>")
end

0 comments on commit 71dd9ab

Please sign in to comment.