Skip to content

Commit e076eb7

Browse files
AntoinePrvserge-sans-paille
authored andcommitted
Add assert for shift positive
1 parent 7cd0002 commit e076eb7

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

include/xsimd/arch/xsimd_neon.hpp

Lines changed: 19 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>
@@ -2372,6 +2373,17 @@ namespace xsimd
23722373
return bitwise_lshift(lhs, n, ::xsimd::detail::int_sequence<Is...>());
23732374
}
23742375
}
2376+
2377+
template <class A, class T>
2378+
XSIMD_INLINE bool all_positive(batch<T, A> const& b) noexcept
2379+
{
2380+
for (std::size_t k = 0; k < b.size; ++k)
2381+
{
2382+
if (b.get(k) < 0)
2383+
return false;
2384+
}
2385+
return true;
2386+
}
23752387
}
23762388

23772389
template <class A, class T>
@@ -2386,6 +2398,7 @@ namespace xsimd
23862398
XSIMD_INLINE batch<T, A> bitwise_lshift(batch<T, A> const& lhs, batch<T, A> const& rhs, requires_arch<neon>) noexcept
23872399
{
23882400
// Blindly converting to signed since out of bounds shifts are UB anyways
2401+
assert(detail::all_positive(rhs));
23892402
return vshlq_u8(lhs, vreinterpretq_s8_u8(rhs));
23902403
}
23912404

@@ -2399,6 +2412,7 @@ namespace xsimd
23992412
XSIMD_INLINE batch<T, A> bitwise_lshift(batch<T, A> const& lhs, batch<T, A> const& rhs, requires_arch<neon>) noexcept
24002413
{
24012414
// Blindly converting to signed since out of bounds shifts are UB anyways
2415+
assert(detail::all_positive(rhs));
24022416
return vshlq_u16(lhs, vreinterpretq_s16_u16(rhs));
24032417
}
24042418

@@ -2412,6 +2426,7 @@ namespace xsimd
24122426
XSIMD_INLINE batch<T, A> bitwise_lshift(batch<T, A> const& lhs, batch<T, A> const& rhs, requires_arch<neon>) noexcept
24132427
{
24142428
// Blindly converting to signed since out of bounds shifts are UB anyways
2429+
assert(detail::all_positive(rhs));
24152430
return vshlq_u32(lhs, vreinterpretq_s32_u32(rhs));
24162431
}
24172432

@@ -2425,6 +2440,7 @@ namespace xsimd
24252440
XSIMD_INLINE batch<T, A> bitwise_lshift(batch<T, A> const& lhs, batch<T, A> const& rhs, requires_arch<neon>) noexcept
24262441
{
24272442
// Blindly converting to signed since out of bounds shifts are UB
2443+
assert(detail::all_positive(rhs));
24282444
return vshlq_u64(lhs, vreinterpretq_s64_u64(rhs));
24292445
}
24302446

@@ -2626,6 +2642,7 @@ namespace xsimd
26262642
XSIMD_INLINE batch<T, A> bitwise_rshift(batch<T, A> const& lhs, batch<T, A> const& rhs, requires_arch<neon>) noexcept
26272643
{
26282644
// Blindly converting to signed since out of bounds shifts are UB anyways
2645+
assert(detail::all_positive(rhs));
26292646
return vshlq_u8(lhs, vnegq_s8(vreinterpretq_s8_u8(rhs)));
26302647
}
26312648

@@ -2639,6 +2656,7 @@ namespace xsimd
26392656
XSIMD_INLINE batch<T, A> bitwise_rshift(batch<T, A> const& lhs, batch<T, A> const& rhs, requires_arch<neon>) noexcept
26402657
{
26412658
// Blindly converting to signed since out of bounds shifts are UB anyways
2659+
assert(detail::all_positive(rhs));
26422660
return vshlq_u16(lhs, vnegq_s16(vreinterpretq_s16_u16(rhs)));
26432661
}
26442662

@@ -2652,6 +2670,7 @@ namespace xsimd
26522670
XSIMD_INLINE batch<T, A> bitwise_rshift(batch<T, A> const& lhs, batch<T, A> const& rhs, requires_arch<neon>) noexcept
26532671
{
26542672
// Blindly converting to signed since out of bounds shifts are UB anyways
2673+
assert(detail::all_positive(rhs));
26552674
return vshlq_u32(lhs, vnegq_s32(vreinterpretq_s32_u32(rhs)));
26562675
}
26572676

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>
@@ -1239,6 +1240,7 @@ namespace xsimd
12391240
XSIMD_INLINE batch<T, A> bitwise_rshift(batch<T, A> const& lhs, batch<T, A> const& rhs, requires_arch<neon64>) noexcept
12401241
{
12411242
// Blindly converting to signed since out of bounds shifts are UB anyways
1243+
assert(detail::all_positive(rhs));
12421244
return vshlq_u64(lhs, vnegq_s64(vreinterpretq_s64_u64(rhs)));
12431245
}
12441246

0 commit comments

Comments
 (0)