Skip to content

Commit 50e7273

Browse files
committed
Add assert for shift positive
1 parent ec6cf6a commit 50e7273

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

include/xsimd/arch/xsimd_neon.hpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
#include <algorithm>
1616
#include <array>
17+
#include <cassert>
1718
#include <complex>
1819
#include <tuple>
1920
#include <type_traits>
@@ -2371,6 +2372,18 @@ namespace xsimd
23712372
return bitwise_lshift(lhs, n, ::xsimd::detail::int_sequence<Is...>());
23722373
}
23732374
}
2375+
2376+
template <class A, class T>
2377+
XSIMD_INLINE bool all_positive(batch<T, A> const& b) noexcept
2378+
{
2379+
for (std::size_t k = 0; k < b.size; ++k)
2380+
{
2381+
if (b.get(k) < 0)
2382+
return false;
2383+
}
2384+
return true;
2385+
}
2386+
23742387
}
23752388

23762389
template <class A, class T>
@@ -2385,6 +2398,7 @@ namespace xsimd
23852398
XSIMD_INLINE batch<T, A> bitwise_lshift(batch<T, A> const& lhs, batch<T, A> const& rhs, requires_arch<neon>) noexcept
23862399
{
23872400
// Blindly converting to signed since out of bounds shifts are UB anyways
2401+
assert(detail::all_positive(rhs));
23882402
return vshlq_u8(lhs, vreinterpretq_s8_u8(rhs));
23892403
}
23902404

@@ -2398,6 +2412,7 @@ namespace xsimd
23982412
XSIMD_INLINE batch<T, A> bitwise_lshift(batch<T, A> const& lhs, batch<T, A> const& rhs, requires_arch<neon>) noexcept
23992413
{
24002414
// Blindly converting to signed since out of bounds shifts are UB anyways
2415+
assert(detail::all_positive(rhs));
24012416
return vshlq_u16(lhs, vreinterpretq_s16_u16(rhs));
24022417
}
24032418

@@ -2411,6 +2426,7 @@ namespace xsimd
24112426
XSIMD_INLINE batch<T, A> bitwise_lshift(batch<T, A> const& lhs, batch<T, A> const& rhs, requires_arch<neon>) noexcept
24122427
{
24132428
// Blindly converting to signed since out of bounds shifts are UB anyways
2429+
assert(detail::all_positive(rhs));
24142430
return vshlq_u32(lhs, vreinterpretq_s32_u32(rhs));
24152431
}
24162432

@@ -2424,6 +2440,7 @@ namespace xsimd
24242440
XSIMD_INLINE batch<T, A> bitwise_lshift(batch<T, A> const& lhs, batch<T, A> const& rhs, requires_arch<neon>) noexcept
24252441
{
24262442
// Blindly converting to signed since out of bounds shifts are UB
2443+
assert(detail::all_positive(rhs));
24272444
return vshlq_u64(lhs, vreinterpretq_s64_u64(rhs));
24282445
}
24292446

@@ -2625,6 +2642,7 @@ namespace xsimd
26252642
XSIMD_INLINE batch<T, A> bitwise_rshift(batch<T, A> const& lhs, batch<T, A> const& rhs, requires_arch<neon>) noexcept
26262643
{
26272644
// Blindly converting to signed since out of bounds shifts are UB anyways
2645+
assert(detail::all_positive(rhs));
26282646
return vshlq_u8(lhs, vnegq_s8(vreinterpretq_s8_u8(rhs)));
26292647
}
26302648

@@ -2638,6 +2656,7 @@ namespace xsimd
26382656
XSIMD_INLINE batch<T, A> bitwise_rshift(batch<T, A> const& lhs, batch<T, A> const& rhs, requires_arch<neon>) noexcept
26392657
{
26402658
// Blindly converting to signed since out of bounds shifts are UB anyways
2659+
assert(detail::all_positive(rhs));
26412660
return vshlq_u16(lhs, vnegq_s16(vreinterpretq_s16_u16(rhs)));
26422661
}
26432662

@@ -2651,6 +2670,7 @@ namespace xsimd
26512670
XSIMD_INLINE batch<T, A> bitwise_rshift(batch<T, A> const& lhs, batch<T, A> const& rhs, requires_arch<neon>) noexcept
26522671
{
26532672
// Blindly converting to signed since out of bounds shifts are UB anyways
2673+
assert(detail::all_positive(rhs));
26542674
return vshlq_u32(lhs, vnegq_s32(vreinterpretq_s32_u32(rhs)));
26552675
}
26562676

include/xsimd/arch/xsimd_neon64.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#ifndef XSIMD_NEON64_HPP
1313
#define XSIMD_NEON64_HPP
1414

15+
#include <cassert>
1516
#include <complex>
1617
#include <cstddef>
1718
#include <tuple>
@@ -1212,6 +1213,7 @@ namespace xsimd
12121213
XSIMD_INLINE batch<T, A> bitwise_rshift(batch<T, A> const& lhs, batch<T, A> const& rhs, requires_arch<neon64>) noexcept
12131214
{
12141215
// Blindly converting to signed since out of bounds shifts are UB anyways
1216+
assert(detail::all_positive(rhs));
12151217
return vshlq_u64(lhs, vnegq_s64(vreinterpretq_s64_u64(rhs)));
12161218
}
12171219

0 commit comments

Comments
 (0)