Skip to content

Commit

Permalink
fix some shift operations to agree with Base (and prevent undefined b…
Browse files Browse the repository at this point in the history
…ehavior) (#119)
  • Loading branch information
KristofferC authored Nov 7, 2023
1 parent 0334b82 commit a07992d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/simdvec.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit a07992d

Please sign in to comment.