From 9c6ba05e204b6cd198eb14501f57a31b8b6bc94f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felipe=20A=2E=20V=2E=20de=20Bragan=C3=A7a=20Alves?= Date: Tue, 10 Sep 2024 09:12:03 -0300 Subject: [PATCH 1/4] Define `real` and `conj` for `Vec{N,<:Real}` --- src/simdvec.jl | 8 ++++++++ test/runtests.jl | 8 ++++++++ 2 files changed, 16 insertions(+) diff --git a/src/simdvec.jl b/src/simdvec.jl index 2eebf5b..dff142b 100644 --- a/src/simdvec.jl +++ b/src/simdvec.jl @@ -492,3 +492,11 @@ end @inline function shufflevector(x::Vec{N, T}, y::Vec{N, T}, ::Val{I}) where {N, T, I} Vec(Intrinsics.shufflevector(x.data, y.data, Val(I))) end + +################### +# No-op functions # +################### + +for op in (:real, :conj) + @eval @inline Base.$op(v::Vec{<:Any, <:Union{<:FloatingTypes, <:IntegerTypes}}) = v +end diff --git a/test/runtests.jl b/test/runtests.jl index cf21ec3..adfe9be 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -988,4 +988,12 @@ llvm_ir(f, args) = sprint(code_llvm, f, Base.typesof(args...)) @test all(r == Vec(4.0, 3.0, 3.0, 4.0)) end + @testset "No-op functions" begin + for x in (Vec(1.0, 2.0, 3.0, 4.0), Vec(1, 2)) + for op in (real, conj) + @test op(x) === x + end + end + end + # end From 42190ade2f133374fd0dfac20b511bca619f90ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felipe=20A=2E=20V=2E=20de=20Bragan=C3=A7a=20Alves?= Date: Tue, 17 Sep 2024 10:25:10 -0300 Subject: [PATCH 2/4] Define `abs2` for `Vec`s --- src/simdvec.jl | 1 + test/runtests.jl | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/simdvec.jl b/src/simdvec.jl index dff142b..8e934c3 100644 --- a/src/simdvec.jl +++ b/src/simdvec.jl @@ -164,6 +164,7 @@ Base.FastMath.sub_fast(v::Vec{<:Any, <:FloatingTypes}) = Vec(Intrinsics.fneg(v.d Base.:~(v::Vec{N, T}) where {N, T<:IntegerTypes} = Vec(Intrinsics.xor(v.data, Vec{N, T}(-1).data)) Base.:~(v::Vec{N, Bool}) where {N} = Vec(Intrinsics.xor(v.data, Vec{N, Bool}(true).data)) Base.abs(v::Vec{N, T}) where {N, T} = Vec(vifelse(v < zero(T), -v, v)) +Base.abs2(v::Vec{N, T}) where {N, T} = v*v Base.:!(v1::Vec{N,Bool}) where {N} = ~v1 Base.inv(v::Vec{N, T}) where {N, T<:FloatingTypes} = one(T) / v diff --git a/test/runtests.jl b/test/runtests.jl index adfe9be..07870e2 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -102,7 +102,7 @@ llvm_ir(f, args) = sprint(code_llvm, f, Base.typesof(args...)) global const v8i32c = map(x->Int32(x*2), v8i32) notbool(x) = !(x>=typeof(x)(0)) - for op in (~, +, -, abs, notbool, sign, signbit, count_ones, count_zeros, + for op in (~, +, -, abs, abs2, notbool, sign, signbit, count_ones, count_zeros, leading_ones, leading_zeros, trailing_ones, trailing_zeros) @test Tuple(op(V8I32(v8i32))) == map(op, v8i32) end @@ -181,7 +181,7 @@ llvm_ir(f, args) = sprint(code_llvm, f, Base.typesof(args...)) sqrtabs(x) = sqrt(abs(x)) for op in ( +, -, - abs, ceil, inv, isfinite, isinf, isnan, issubnormal, floor, powi4, + abs, abs2, ceil, inv, isfinite, isinf, isnan, issubnormal, floor, powi4, round, sign, signbit, sqrtabs, trunc) @test Tuple(op(V4F64(v4f64))) === map(op, v4f64) end From 1110296b1bf5559fb65f10d06f0167b2b59945f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felipe=20A=2E=20V=2E=20de=20Bragan=C3=A7a=20Alves?= Date: Tue, 17 Sep 2024 11:10:19 -0300 Subject: [PATCH 3/4] Define `angle` and `imag` function for `Vec`s Reorganize tests. --- src/simdvec.jl | 15 ++++++++------- test/runtests.jl | 14 +++----------- 2 files changed, 11 insertions(+), 18 deletions(-) diff --git a/src/simdvec.jl b/src/simdvec.jl index 8e934c3..1e65afa 100644 --- a/src/simdvec.jl +++ b/src/simdvec.jl @@ -494,10 +494,11 @@ end Vec(Intrinsics.shufflevector(x.data, y.data, Val(I))) end -################### -# No-op functions # -################### - -for op in (:real, :conj) - @eval @inline Base.$op(v::Vec{<:Any, <:Union{<:FloatingTypes, <:IntegerTypes}}) = v -end +############################ +# Complex Number functions # +############################ + +@inline Base.real(v::Vec{N, T}) where {N, T<:ScalarTypes} = v +@inline Base.conj(v::Vec{N, T}) where {N, T<:ScalarTypes} = v +@inline Base.angle(v::Vec{N, T}) where {N, T <: FloatingTypes} = vifelse(signbit(v), Vec(ntuple(i -> convert(T, pi), Val(N))), zero(v)) +@inline Base.imag(v::Vec{N, T}) where {N, T <: ScalarTypes} = zero(v) diff --git a/test/runtests.jl b/test/runtests.jl index 07870e2..dbf9880 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -103,7 +103,7 @@ llvm_ir(f, args) = sprint(code_llvm, f, Base.typesof(args...)) notbool(x) = !(x>=typeof(x)(0)) for op in (~, +, -, abs, abs2, notbool, sign, signbit, count_ones, count_zeros, - leading_ones, leading_zeros, trailing_ones, trailing_zeros) + leading_ones, leading_zeros, conj, real, imag, trailing_ones, trailing_zeros) @test Tuple(op(V8I32(v8i32))) == map(op, v8i32) end @@ -181,8 +181,8 @@ llvm_ir(f, args) = sprint(code_llvm, f, Base.typesof(args...)) sqrtabs(x) = sqrt(abs(x)) for op in ( +, -, - abs, abs2, ceil, inv, isfinite, isinf, isnan, issubnormal, floor, powi4, - round, sign, signbit, sqrtabs, trunc) + abs, abs2, angle, ceil, conj, inv, imag, isfinite, isinf, isnan, issubnormal, floor, powi4, + real, round, sign, signbit, sqrtabs, trunc) @test Tuple(op(V4F64(v4f64))) === map(op, v4f64) end function Base.isapprox(t1::Tuple,t2::Tuple) @@ -988,12 +988,4 @@ llvm_ir(f, args) = sprint(code_llvm, f, Base.typesof(args...)) @test all(r == Vec(4.0, 3.0, 3.0, 4.0)) end - @testset "No-op functions" begin - for x in (Vec(1.0, 2.0, 3.0, 4.0), Vec(1, 2)) - for op in (real, conj) - @test op(x) === x - end - end - end - # end From 7932d1758f3b699380ae84fb9bf8fdaec013d1e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felipe=20A=2E=20V=2E=20de=20Bragan=C3=A7a=20Alves?= Date: Tue, 17 Sep 2024 20:22:37 -0300 Subject: [PATCH 4/4] Simplify code --- src/simdvec.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/simdvec.jl b/src/simdvec.jl index 1e65afa..fe44af0 100644 --- a/src/simdvec.jl +++ b/src/simdvec.jl @@ -500,5 +500,5 @@ end @inline Base.real(v::Vec{N, T}) where {N, T<:ScalarTypes} = v @inline Base.conj(v::Vec{N, T}) where {N, T<:ScalarTypes} = v -@inline Base.angle(v::Vec{N, T}) where {N, T <: FloatingTypes} = vifelse(signbit(v), Vec(ntuple(i -> convert(T, pi), Val(N))), zero(v)) +@inline Base.angle(v::Vec{N, T}) where {N, T <: FloatingTypes} = vifelse(signbit(v), Vec{N, T}(T(pi)), zero(v)) @inline Base.imag(v::Vec{N, T}) where {N, T <: ScalarTypes} = zero(v)