From 8758443aab2b8517ec1500725f00e6d46bef11be Mon Sep 17 00:00:00 2001 From: Kristoffer Date: Tue, 7 Nov 2023 12:36:35 +0100 Subject: [PATCH] fix some shift operations to agree with Base (and prevent undefined behavior) --- src/simdvec.jl | 6 +++--- test/runtests.jl | 7 +++++++ 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/simdvec.jl b/src/simdvec.jl index 6f205b0..96dd9a1 100644 --- a/src/simdvec.jl +++ b/src/simdvec.jl @@ -321,19 +321,19 @@ end # Shifting with a value larger than the number of bits in the type is undefined behavior # so set to zero in those cases. @inline function shl_int(x::Vec{N, T1}, y::Vec{N, T2}) where {N, T1<:IntegerTypes, T2<:IntegerTypes} - vifelse(y > sizeof(T1) * 8, + vifelse(y >= sizeof(T1) * 8, zero(Vec{N, T1}), Vec(Intrinsics.shl(x.data, convert(Vec{N,T1}, y).data))) end @inline function lshr_int(x::Vec{N, T1}, y::Vec{N, T2}) where {N, T1<:IntegerTypes, T2<:IntegerTypes} - vifelse(y > sizeof(T1) * 8, + vifelse(y >= sizeof(T1) * 8, zero(Vec{N, T1}), Vec(Intrinsics.lshr(x.data, convert(Vec{N,T1}, y).data))) end @inline function ashr_int(x::Vec{N, T1}, y::Vec{N, T2}) where {N, T1<:IntegerTypes, T2<:IntegerTypes} - vifelse(y > sizeof(T1) * 8, + vifelse(y >= sizeof(T1) * 8, Vec(Intrinsics.ashr(x.data, Vec{N,T1}(sizeof(T1)*8-1).data)), Vec(Intrinsics.ashr(x.data, Vec{N,T1}(y).data))) end diff --git a/test/runtests.jl b/test/runtests.jl index 9981e06..59c7b43 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -129,6 +129,13 @@ llvm_ir(f, args) = sprint(code_llvm, f, Base.typesof(args...)) end end + f(v) = v << 64 + f2(v) = v >> 64 + f3(v) = v >>> 64 + @test all(f(Vec(1,2,3,4)) == Vec(0,0,0,0)) + @test all(f2(Vec(1,2,3,4)) == Vec(0,0,0,0)) + @test all(f3(Vec(1,2,3,4)) == Vec(0,0,0,0)) + @test Tuple(V8I32(v8i32)^0) === v8i32.^0 @test Tuple(V8I32(v8i32)^1) === v8i32.^1 @test Tuple(V8I32(v8i32)^2) === v8i32.^2