diff --git a/include/xsimd/arch/common/xsimd_common_memory.hpp b/include/xsimd/arch/common/xsimd_common_memory.hpp index a8dae1575..3f62fbc92 100644 --- a/include/xsimd/arch/common/xsimd_common_memory.hpp +++ b/include/xsimd/arch/common/xsimd_common_memory.hpp @@ -161,10 +161,7 @@ namespace xsimd gather(batch const&, T const* src, batch const& index, kernel::requires_arch) noexcept { - static_assert(batch::size == batch::size, - "Index and destination sizes must match"); - - return detail::gather::size - 1, T, A>(src, index, {}); + return detail::gather::size - 1, T, A>(src, index, {}); } // Gather with runtime indexes and mismatched strides. diff --git a/include/xsimd/arch/xsimd_avx2.hpp b/include/xsimd/arch/xsimd_avx2.hpp index f41bdadac..e287d22b9 100644 --- a/include/xsimd/arch/xsimd_avx2.hpp +++ b/include/xsimd/arch/xsimd_avx2.hpp @@ -572,12 +572,23 @@ namespace xsimd return _mm256_i32gather_epi32(reinterpret_cast(src), index, sizeof(T)); } - template = 0, detail::enable_sized_integral_t = 0> + template = 0, detail::enable_integral_t = 0> XSIMD_INLINE batch gather(batch const&, T const* src, batch const& index, kernel::requires_arch) noexcept { - // scatter for this one is AVX512F+AVX512VL - return _mm256_i64gather_epi64(reinterpret_cast(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) + return _mm256_i32gather_epi64(reinterpret_cast(src), narrow, sizeof(T)); + else + return _mm256_i64gather_epi64(reinterpret_cast(src), _mm256_cvtepu32_epi64(narrow), sizeof(T)); + } + else + { + return _mm256_i64gather_epi64(reinterpret_cast(src), index, sizeof(T)); + } } template (src), sizeof(T)); } - template = 0, detail::enable_sized_integral_t = 0> + template = 0, detail::enable_integral_t = 0> XSIMD_INLINE batch gather(batch const&, T const* src, batch const& index, kernel::requires_arch) noexcept { - return _mm512_i64gather_epi64(index, static_cast(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) + return _mm512_i32gather_epi64(narrow, static_cast(src), sizeof(T)); + else + return _mm512_i64gather_epi64(_mm512_cvtepu32_epi64(narrow), static_cast(src), sizeof(T)); + } + else + { + return _mm512_i64gather_epi64(index, static_cast(src), sizeof(T)); + } } template = 0> diff --git a/include/xsimd/arch/xsimd_rvv.hpp b/include/xsimd/arch/xsimd_rvv.hpp index cf1d2092a..e2ed58cf2 100644 --- a/include/xsimd/arch/xsimd_rvv.hpp +++ b/include/xsimd/arch/xsimd_rvv.hpp @@ -553,15 +553,29 @@ namespace xsimd } // gather - template = 0> + template = 0> XSIMD_INLINE batch gather(batch const&, T const* src, batch const& index, kernel::requires_arch) noexcept { - using UU = as_unsigned_integer_t; - const auto uindex = detail_rvv::rvv_to_unsigned_batch(index); - auto const* base = reinterpret_cast 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, A>; + const auto wide = [&]() + { + if constexpr (std::is_signed_v) + 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::gather(src, indices(typename indices::register_type(wide))); + } + else + { + const auto uindex = detail_rvv::rvv_to_unsigned_batch(index); + auto const* base = reinterpret_cast const*>(src); + const auto bi = detail_rvv::rvvmul_splat(uindex, sizeof(T)); + return detail_rvv::rvvloxei(base, bi); + } } /************** diff --git a/include/xsimd/arch/xsimd_sve.hpp b/include/xsimd/arch/xsimd_sve.hpp index f017290aa..09ebf134b 100644 --- a/include/xsimd/arch/xsimd_sve.hpp +++ b/include/xsimd/arch/xsimd_sve.hpp @@ -204,10 +204,15 @@ namespace xsimd } // gather - template = 0> + template = 0> XSIMD_INLINE batch gather(batch const&, T const* src, batch const& index, kernel::requires_arch) noexcept { - return svld1_gather_index(detail_sve::ptrue(), src, index.data); + const auto* base = reinterpret_cast const*>(src); + if constexpr (sizeof(T) == sizeof(U)) + return svld1_gather_index(detail_sve::ptrue(), base, index.data); + else + return svld1_gather_index(detail_sve::ptrue(), base, svunpklo(index.data)); } /******************** diff --git a/include/xsimd/types/xsimd_batch.hpp b/include/xsimd/types/xsimd_batch.hpp index 448928f02..05eff5fe6 100644 --- a/include/xsimd/types/xsimd_batch.hpp +++ b/include/xsimd/types/xsimd_batch.hpp @@ -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. */ @@ -916,6 +919,9 @@ namespace xsimd { detail::static_check_supported_config(); static_assert(std::is_convertible_v, "Can't convert from src to this batch's type!"); + static_assert(batch::size == size + || (std::is_same_v && std::is_integral_v && sizeof(T) == 8 && std::is_integral_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 {}); } diff --git a/test/test_load_store.cpp b/test/test_load_store.cpp index cb9f20f9e..6409ad230 100644 --- a/test/test_load_store.cpp +++ b/test/test_load_store.cpp @@ -15,6 +15,7 @@ #include "test_utils.hpp" #include +#include #include #include @@ -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) + { + test_gather_indexed("int64_t"); + test_gather_indexed("uint64_t"); + test_gather_indexed("int32_t"); + test_gather_indexed("uint32_t"); + } } void test_scatter() @@ -402,6 +410,22 @@ struct load_store_test CHECK_BATCH_EQ(b, expected); } + template + void test_gather_indexed(const std::string& index_name) + { + using indices = xsimd::batch; + array_type data; + std::iota(data.begin(), data.end(), value_type(0x100000001LL)); + std::reverse_copy(data.cbegin(), data.cend(), expected.begin()); + std::array offsets {}; + const V origin = std::is_signed_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 void test_scatter_impl(const V& v, const std::string& name) {