Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions include/xsimd/arch/common/xsimd_common_memory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,10 +161,7 @@ namespace xsimd
gather(batch<T, A> const&, T const* src, batch<V, A> const& index,
kernel::requires_arch<common>) noexcept
{
static_assert(batch<T, A>::size == batch<V, A>::size,
"Index and destination sizes must match");

return detail::gather<batch<V, A>::size - 1, T, A>(src, index, {});
return detail::gather<batch<T, A>::size - 1, T, A>(src, index, {});
}

// Gather with runtime indexes and mismatched strides.
Expand Down
17 changes: 14 additions & 3 deletions include/xsimd/arch/xsimd_avx2.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -572,12 +572,23 @@ namespace xsimd
return _mm256_i32gather_epi32(reinterpret_cast<const int*>(src), index, sizeof(T));
}

template <class T, class A, class U, detail::enable_sized_integral_t<T, 8> = 0, detail::enable_sized_integral_t<U, 8> = 0>
template <class T, class A, class U, detail::enable_sized_integral_t<T, 8> = 0, detail::enable_integral_t<U> = 0>
XSIMD_INLINE batch<T, A> gather(batch<T, A> const&, T const* src, batch<U, A> const& index,
kernel::requires_arch<avx2>) noexcept
{
// scatter for this one is AVX512F+AVX512VL
return _mm256_i64gather_epi64(reinterpret_cast<const long long int*>(src), index, sizeof(T));
if constexpr (sizeof(U) == 4)
{
// vpgatherdq reads 4 int32 offsets: the low half of the index register
const __m128i narrow = _mm256_castsi256_si128(index);
if constexpr (std::is_signed_v<U>)
return _mm256_i32gather_epi64(reinterpret_cast<const long long int*>(src), narrow, sizeof(T));
else
return _mm256_i64gather_epi64(reinterpret_cast<const long long int*>(src), _mm256_cvtepu32_epi64(narrow), sizeof(T));
}
else
{
return _mm256_i64gather_epi64(reinterpret_cast<const long long int*>(src), index, sizeof(T));
}
}

template <class A, class U,
Expand Down
16 changes: 14 additions & 2 deletions include/xsimd/arch/xsimd_avx512f.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1190,11 +1190,23 @@ namespace xsimd
return _mm512_i32gather_epi32(index, static_cast<const void*>(src), sizeof(T));
}

template <class T, class A, class U, detail::enable_sized_integral_t<T, 8> = 0, detail::enable_sized_integral_t<U, 8> = 0>
template <class T, class A, class U, detail::enable_sized_integral_t<T, 8> = 0, detail::enable_integral_t<U> = 0>
XSIMD_INLINE batch<T, A> gather(batch<T, A> const&, T const* src, batch<U, A> const& index,
kernel::requires_arch<avx512f>) noexcept
{
return _mm512_i64gather_epi64(index, static_cast<const void*>(src), sizeof(T));
if constexpr (sizeof(U) == 4)
{
// vpgatherdq reads 8 int32 offsets: the low half of the index register
const __m256i narrow = _mm512_castsi512_si256(index);
if constexpr (std::is_signed_v<U>)
return _mm512_i32gather_epi64(narrow, static_cast<const void*>(src), sizeof(T));
else
return _mm512_i64gather_epi64(_mm512_cvtepu32_epi64(narrow), static_cast<const void*>(src), sizeof(T));
}
else
{
return _mm512_i64gather_epi64(index, static_cast<const void*>(src), sizeof(T));
}
}

template <class A, class U, detail::enable_sized_integral_t<U, 4> = 0>
Expand Down
28 changes: 21 additions & 7 deletions include/xsimd/arch/xsimd_rvv.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -553,15 +553,29 @@ namespace xsimd
}

// gather
template <class A, class T, class U, detail_rvv::rvv_enable_sg_t<T, U> = 0>
template <class A, class T, class U, std::enable_if_t<sizeof(T) == 4 || sizeof(T) == 8, int> = 0>
XSIMD_INLINE batch<T, A> gather(batch<T, A> const&, T const* src, batch<U, A> const& index, kernel::requires_arch<rvv>) noexcept
{
using UU = as_unsigned_integer_t<U>;
const auto uindex = detail_rvv::rvv_to_unsigned_batch(index);
auto const* base = reinterpret_cast<map_to_sized_type_t<T> const*>(src);
// or rvvluxei
const auto bi = detail_rvv::rvvmul_splat(uindex, sizeof(T));
return detail_rvv::rvvloxei(base, bi);
if constexpr (sizeof(T) == 8 && sizeof(U) == 4)
{
// 64-bit lanes with 32-bit offsets: widen the low size offsets
using indices = batch<widen_t<U>, A>;
const auto wide = [&]()
{
if constexpr (std::is_signed_v<U>)
return __riscv_vsext_vf2_i64m1(__riscv_vlmul_trunc_i32mf2(index.data), indices::size);
else
return __riscv_vzext_vf2_u64m1(__riscv_vlmul_trunc_u32mf2(index.data), indices::size);
}();
return batch<T, A>::gather(src, indices(typename indices::register_type(wide)));
}
else
{
const auto uindex = detail_rvv::rvv_to_unsigned_batch(index);
auto const* base = reinterpret_cast<map_to_sized_type_t<T> const*>(src);
const auto bi = detail_rvv::rvvmul_splat(uindex, sizeof(T));
return detail_rvv::rvvloxei(base, bi);
}
}

/**************
Expand Down
9 changes: 7 additions & 2 deletions include/xsimd/arch/xsimd_sve.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,10 +204,15 @@ namespace xsimd
}

// gather
template <class A, class T, class U, detail_sve::enable_sg_t<T, U> = 0>
template <class A, class T, class U,
std::enable_if_t<(sizeof(T) == 4 || sizeof(T) == 8), int> = 0>
XSIMD_INLINE batch<T, A> gather(batch<T, A> const&, T const* src, batch<U, A> const& index, kernel::requires_arch<sve>) noexcept
{
return svld1_gather_index(detail_sve::ptrue<T>(), src, index.data);
const auto* base = reinterpret_cast<map_to_sized_type_t<T> const*>(src);
if constexpr (sizeof(T) == sizeof(U))
return svld1_gather_index(detail_sve::ptrue<T>(), base, index.data);
else
return svld1_gather_index(detail_sve::ptrue<T>(), base, svunpklo(index.data));
}

/********************
Expand Down
6 changes: 6 additions & 0 deletions include/xsimd/types/xsimd_batch.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -907,6 +907,9 @@ namespace xsimd
/**
* Create a new batch gathering elements starting at address \c src and
* offset by each element in \c index.
* When gathering 64-bit integers of type \c T using 32-bit integer indices,
* only the first \c size offsets are used, in lane order. Constructing
* \c index with \c load_unaligned requires a full batch of readable indices.
* If \c T is not of the same size as \c U, a \c static_cast is performed
* at element gather time.
*/
Expand All @@ -916,6 +919,9 @@ namespace xsimd
{
detail::static_check_supported_config<T, A>();
static_assert(std::is_convertible_v<T, U>, "Can't convert from src to this batch's type!");
static_assert(batch<V, A>::size == size
|| (std::is_same_v<T, U> && std::is_integral_v<T> && sizeof(T) == 8 && std::is_integral_v<V> && sizeof(V) == 4),
"gather requires one offset per result lane or 32-bit integer offsets for 64-bit integer elements");
return kernel::gather(batch {}, src, index, A {});
}

Expand Down
24 changes: 24 additions & 0 deletions test/test_load_store.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "test_utils.hpp"

#include <algorithm>
#include <numeric>
#include <random>
#include <type_traits>

Expand Down Expand Up @@ -178,6 +179,13 @@ struct load_store_test
#if !XSIMD_WITH_NEON || XSIMD_WITH_NEON64
test_gather_impl(d_vec, "gather double");
#endif
if constexpr (sizeof(value_type) == 8 && std::is_integral_v<value_type>)
{
test_gather_indexed<int64_t>("int64_t");
test_gather_indexed<uint64_t>("uint64_t");
test_gather_indexed<int32_t>("int32_t");
test_gather_indexed<uint32_t>("uint32_t");
}
}

void test_scatter()
Expand Down Expand Up @@ -402,6 +410,22 @@ struct load_store_test
CHECK_BATCH_EQ(b, expected);
}

template <class V>
void test_gather_indexed(const std::string& index_name)
{
using indices = xsimd::batch<V, typename B::arch_type>;
array_type data;
std::iota(data.begin(), data.end(), value_type(0x100000001LL));
std::reverse_copy(data.cbegin(), data.cend(), expected.begin());
std::array<V, indices::size> offsets {};
const V origin = std::is_signed_v<V> ? V(size - 1) : V(0);
for (std::size_t i = 0; i < size; ++i)
offsets[i] = V(size - 1 - i) - origin;
INFO("gather 64-bit integers with ", index_name);
const auto result = batch_type::gather(data.data() + origin, indices::load_unaligned(offsets.data()));
CHECK_BATCH_EQ(result, expected);
}

template <class V>
void test_scatter_impl(const V& v, const std::string& name)
{
Expand Down
Loading