Skip to content

Commit

Permalink
Add bswap
Browse files Browse the repository at this point in the history
  • Loading branch information
chriselrod committed Sep 13, 2021
1 parent 36be7d7 commit 5636185
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "VectorizationBase"
uuid = "3d5dd08c-fd9d-11e8-17fa-ed2836048c2f"
authors = ["Chris Elrod <elrodc@gmail.com>"]
version = "0.21.6"
version = "0.21.7"

[deps]
ArrayInterface = "4fba245c-0d91-5ea0-9b3e-6abc04ee57a9"
Expand Down
36 changes: 27 additions & 9 deletions src/llvm_intrin/unary_ops.jl
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@

function sub_quote(W, @nospecialize(T), fast::Bool)
vtyp = vtype(W, T)
instrs = "%res = fneg $(fast_flags(fast)) $vtyp %0\nret $vtyp %res"
quote
$(Expr(:meta, :inline))
Vec($LLVMCALL($instrs, _Vec{$W,$T}, Tuple{_Vec{$W,$T}}, data(v)))
end
function sub_quote(W::Int, T::Symbol, fast::Bool)::Expr
vtyp = vtype(W, T)
instrs = "%res = fneg $(fast_flags(fast)) $vtyp %0\nret $vtyp %res"
quote
$(Expr(:meta, :inline))
Vec($LLVMCALL($instrs, _Vec{$W,$T}, Tuple{_Vec{$W,$T}}, data(v)))
end
end

@generated vsub(v::Vec{W,T}) where {W, T <: Union{Float32,Float64}} = sub_quote(W, T, false)
@generated vsub_fast(v::Vec{W,T}) where {W, T <: Union{Float32,Float64}} = sub_quote(W, T, true)
@generated vsub(v::Vec{W,T}) where {W, T <: Union{Float32,Float64}} = sub_quote(W, JULIA_TYPES[T], false)
@generated vsub_fast(v::Vec{W,T}) where {W, T <: Union{Float32,Float64}} = sub_quote(W, JULIA_TYPES[T], true)

@inline vsub(v) = -v
@inline vsub_fast(v) = Base.FastMath.sub_fast(v)
Expand All @@ -32,3 +32,21 @@ end
@inline vround(v::AbstractSIMD{W,<:Integer}) where {W} = v
@inline vround(v::AbstractSIMD{W,<:Integer}, ::RoundingMode) where {W} = v


function bswap_quote(W::Int, T::Symbol, st::Int)::Expr
typ = 'i' * string(8st)
suffix = 'v' * string(W) * typ
vtyp = "<$W x $typ>"
decl = "declare $(vtyp) @llvm.bswap.$(suffix)($(vtyp))"
instrs = """
%res = call $vtyp @llvm.bswap.$(suffix)($vtyp %0)
ret $vtyp %res
"""
ret_type = :(_Vec{$W,$T})
llvmcall_expr(decl, instrs, ret_type, :(Tuple{$ret_type}), vtyp, [vtyp], [:(data(x))])
end
@generated Base.bswap(x::Vec{W,T}) where {T<:IntegerTypesHW,W} = bswap_quote(W, JULIA_TYPES[T], sizeof(T))
@inline Base.bswap(x::VecUnroll) = VecUnroll(fmap(bswap, data(x)))
@inline Base.bswap(x::AbstractSIMDVector) = bswap(Vec(x))


2 changes: 1 addition & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1123,7 +1123,7 @@ include("testsetup.jl")
xi32, yi32 = promote(vxi32, one(Int64))
@test xi32 === vxi32
@test yi32 === vbroadcast(VectorizationBase.pick_vector_width(Int32), one(Int32))

@test ntoh(vxi32) === Vec(map(ntoh, Tuple(vxi32))...)
end
println("Lazymul")
@time @testset "Lazymul" begin
Expand Down

2 comments on commit 5636185

@chriselrod
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/44792

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.21.7 -m "<description of version>" 56361859c5537917ee71f5c2e70bae5a2fcbf59f
git push origin v0.21.7

Please sign in to comment.