Skip to content

Support 32-bit indices for 64-bit integer gathers - #1457

Merged
DiamonDinoia merged 1 commit into
xtensor-stack:masterfrom
LittlePyx:fix/avx2-i32-gather-i64
Nov 22, 2026
Merged

DiamonDinoia merged 1 commit into
xtensor-stack:masterfrom
LittlePyx:fix/avx2-i32-gather-i64

Conversation

@LittlePyx

@LittlePyx LittlePyx commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Gathering 64-bit integers with a same-architecture batch of 32-bit offsets fails on the base revision because the index batch has twice as many lanes as the result. This change lets the existing batch::gather use the first result_type::size offsets in lane order. The source and result must have the same 64-bit integer type. The remaining offsets cause no source reads. Loading the index batch itself still requires a full batch of readable offsets. Refs #751.

On AVX2, signed 32-bit offsets use vpgatherdq. Unsigned offsets are zero-extended before vpgatherqq so offsets with bit 31 set stay positive. The common fallback loads one element per result lane. SSE2 fallback measurements

For callers starting with an int32_t const*, the existing widening load also works: auto idx = xsimd::batch<int64_t>::load_unaligned(offsets); followed by xsimd::batch<int64_t>::gather(base, idx). I checked this on the base revision and with this patch. Passing an SSE index batch directly to an AVX2 result remains outside the scope of this PR. Velox example

Tests

From the repository root, set DOCTEST_INCLUDE to the directory containing doctest/doctest.h and run

for isa in '-msse2 -mno-avx' '-mavx2'; do
    g++ -std=c++17 -O0 $isa -Iinclude -I"$DOCTEST_INCLUDE" \
        test/main.cpp test/test_load_store.cpp -o /tmp/xsimd-load-store || exit 1
    /tmp/xsimd-load-store || exit 1
done
  • GCC 13.3 on x86-64 passed all 22 load/store cases in both builds. The new regression uses negative signed offsets and values above 32 bits, with signed and unsigned 32- and 64-bit index types. The same test fails to compile against the base revision on SSE2.
  • Existing gathers using function pointers, explicit template arguments, scalar index broadcasts and source conversion matched the base revision on SSE2 and AVX2. The widening-load example returned the scalar reference values on both.
  • Same-architecture runtime checks passed on AVX2 hardware, under Intel SDE for AVX-512F, and under QEMU for NEON, SVE-256 and RVV-256.

AI assistance

The contributor selected the use case, reviewed the scope with the maintainer and requested the compatibility checks. OpenAI Codex revised the code and tests, ran the checks and benchmarks, and drafted this description.

Comment thread test/test_load_store.cpp Outdated
SUBCASE("masked") { Test.test_masked(); }
}

#if XSIMD_WITH_AVX2

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Such API should be genral across all arch maybe with a bit_cast/compress fallback. Then each arch can optimize this with specific intrinsics.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated in e939403. The public overload now checks for one signed 32-bit index per 64-bit result lane. The common kernel accepts independent index and result architectures and reuses the existing per-lane gather. AVX2 still selects its intrinsic for the Velox index type.

The emulated<256> result / emulated<128> index regression failed to compile on the previous head and now passes all four lane checks, including the negative offset. The two AVX2 index types also pass, and the runtime-offset probe still emits vpgatherdq. I also cross-compiled the public call for a 256-bit SVE result with 128-bit NEON indices (compile-only).

@LittlePyx LittlePyx changed the title Gather four int64_t values with int32_t indices on AVX2 Support signed 32-bit indices for 64-bit integer gathers Nov 14, 2026

@DiamonDinoia DiamonDinoia left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will have to review the templating properly and possible pitfalls where the wrong overload is picked.

Some suggestions.

1.I would use if constexpr in the current gather kernels instead of sfinae
2. Optimized kernels per arch do not seem difficult to implement so we should do so to prevent providing a suboptimal api that catch the users by surprise
3. You could run locally using qemu if you compiled for different arch than your own. I will still double check locally and wait for ci to pass after that.
4. Please squash your commits. I think one commit is enough for this functionality.

Thanks for the contribution and addressing some initial comments. I would greatly appreciate if you could avoid using LLM in your replies as those response I prefer interacting with humans :)
I do not mind wrong grammar and typos slipping though. If it is a translation aid due to language barrier then I have no problem with it.

Comment thread test/test_load_store.cpp Outdated
CHECK_EQ(result.get(i), base[offsets[i]]);
}

#if XSIMD_WITH_AVX2

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If all architecture support this write the tests so that they don't require this macro.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In 59dec01, I removed the AVX2 guard from the tests. They now run whenever the required batch types are available. The four-lane case also passes with AVX2 disabled. I moved the dispatch logic into the existing gather kernels and retested the existing calls. I’ve squashed the PR into a single commit.

@LittlePyx
LittlePyx force-pushed the fix/avx2-i32-gather-i64 branch from e939403 to 59dec01 Compare November 15, 2026 02:34

@DiamonDinoia DiamonDinoia left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will have to think a bit more deeply about it. I am not sure the way is implemented is the simplest.

Definitely better this way. Though for the tests I would like to see the comination of T = [u]int64, V = [u]int32. As I am not sure that the current implementation does not break if there is no arch half the size of A. I am thinking of for example sse2 gather with 2 int 64 has not corresponding int32 batch.

template <typename T, typename A, typename V, typename IndexArch>
XSIMD_INLINE batch<T, A>
gather(batch<T, A> const&, T const* src, batch<V, A> const& index,
gather(batch<T, A> const&, T const* src, batch<V, IndexArch> const& index,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So you are changing the Arch because now using a type that is half the size of T now you can use a different architecture for this.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that’s why I used a separate IndexArch. Velox needs four 32-bit indices to gather four 64-bit values. The indices fit in an SSE2 register and the result fits in an AVX2 register.

In 4546e8c, I tested all four signed/unsigned combinations where both batch types are available. I also tried your SSE2-only example. There’s no native batch of two 32-bit indices there, so I tested the existing gather with two 64-bit indices. It returned the expected values for both int64_t and uint64_t.

@LittlePyx
LittlePyx force-pushed the fix/avx2-i32-gather-i64 branch from 59dec01 to 4546e8c Compare November 15, 2026 16:53
@LittlePyx LittlePyx changed the title Support signed 32-bit indices for 64-bit integer gathers Support 32-bit indices for 64-bit integer gathers Nov 15, 2026
@DiamonDinoia

DiamonDinoia commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

I am thinking of for example sse2 gather with 2 int 64 has not corresponding int32 batch.

For cases like these we can upcast to the bigger type and do a normal gather. Or we can do a scalar emulation. We shall check the performance of the two approach and pick the fastest each time. Othrwise the user might have code with default arch that does not compile depending on the compile flags.

Comment thread test/test_load_store.cpp Outdated
}

template <class T, class V, size_t N>
static void check_sized_integer_gather()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think think this is the way. We just need to do gather with T and T/2 if sizeof(t)==8. I do not want the std::is_void_v because gather with int32 might not compile with some archs but compile with other and code that ships with defaul arch compiled with something ike march=native might not compile on another CPU.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gahter<[u]int64,[u]int32> and Gahter<[u]int64,[u]int64> must compile on all archs

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a105381 removes the is_void_v check. The same test now passes with 32-bit and 64-bit indices on both SSE2 and AVX2, including the signed and unsigned cases. In your SSE2 example, the two 64-bit results use the first two 32-bit indices.

@LittlePyx
LittlePyx force-pushed the fix/avx2-i32-gather-i64 branch from 4546e8c to a105381 Compare November 16, 2026 05:28
@LittlePyx

Copy link
Copy Markdown
Contributor Author

a105381 keeps the scalar loads on SSE2. With GCC and an 8 KiB array, unsigned indices took 0.68 ns per gather with scalar loads and 0.90 ns with widening. Clang was about even on that case. The PR description has the larger-array timings too.

@DiamonDinoia

Copy link
Copy Markdown
Contributor

I do not know how to explain the changes I would like in code. But, I still think the code is a bit too complicated compared to what I would like, especially since I want humans to maintain this. I propose those changes:

diff --git a/include/xsimd/arch/common/xsimd_common_memory.hpp b/include/xsimd/arch/common/xsimd_common_memory.hpp
index 81eb999a..3f62fbc9 100644
--- a/include/xsimd/arch/common/xsimd_common_memory.hpp
+++ b/include/xsimd/arch/common/xsimd_common_memory.hpp
@@ -156,24 +156,12 @@ namespace xsimd
             }
         } // namespace detail
 
-        template <typename T, typename A, typename V, typename IndexArch>
+        template <typename T, typename A, typename V>
         XSIMD_INLINE batch<T, A>
-        gather(batch<T, A> const&, T const* src, batch<V, IndexArch> const& index,
+        gather(batch<T, A> const&, T const* src, batch<V, A> const& index,
                kernel::requires_arch<common>) noexcept
         {
-            static_assert(detail::compatible_gather_sizes_v<T, A, V, IndexArch>, "Incompatible gather index size");
-
-            if constexpr (std::is_same_v<A, IndexArch>)
-            {
-                return detail::gather<batch<T, A>::size - 1, T, A>(src, index, {});
-            }
-            else
-            {
-                // Both signed and unsigned 32-bit offsets are representable in int64_t.
-                V offsets[batch<T, A>::size];
-                index.store_unaligned(offsets);
-                return batch<T, A>::gather(src, batch<int64_t, A>::load_unaligned(offsets));
-            }
+            return detail::gather<batch<T, A>::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 c09f3406..a86b021b 100644
--- a/include/xsimd/arch/xsimd_avx2.hpp
+++ b/include/xsimd/arch/xsimd_avx2.hpp
@@ -572,15 +572,14 @@ namespace xsimd
             return _mm256_i32gather_epi32(reinterpret_cast<const int*>(src), index, sizeof(T));
         }
 
-        template <class T, class A, class U, class IndexArch, 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, IndexArch> const& index,
+        template <class T, class A, class U, detail::enable_sized_integral_t<T, 8> = 0>
+        XSIMD_INLINE batch<T, A> gather(batch<T, A> const&, T const* src, batch<U, A> const& index,
                                         kernel::requires_arch<avx2>) noexcept
         {
-            static_assert(detail::compatible_gather_sizes_v<T, A, U, IndexArch>, "Incompatible gather index size");
             if constexpr (sizeof(U) == 4)
             {
-                __m128i narrow;
-                std::memcpy(&narrow, &index.data, sizeof(narrow));
+                // 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
diff --git a/include/xsimd/arch/xsimd_avx512f.hpp b/include/xsimd/arch/xsimd_avx512f.hpp
index ebee382b..4b025cb2 100644
--- a/include/xsimd/arch/xsimd_avx512f.hpp
+++ b/include/xsimd/arch/xsimd_avx512f.hpp
@@ -1190,15 +1190,14 @@ namespace xsimd
             return _mm512_i32gather_epi32(index, static_cast<const void*>(src), sizeof(T));
         }
 
-        template <class T, class A, class U, class IndexArch, 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, IndexArch> const& index,
+        template <class T, class A, class U, detail::enable_sized_integral_t<T, 8> = 0>
+        XSIMD_INLINE batch<T, A> gather(batch<T, A> const&, T const* src, batch<U, A> const& index,
                                         kernel::requires_arch<avx512f>) noexcept
         {
-            static_assert(detail::compatible_gather_sizes_v<T, A, U, IndexArch>, "Incompatible gather index size");
             if constexpr (sizeof(U) == 4)
             {
-                __m256i narrow;
-                std::memcpy(&narrow, &index.data, sizeof(narrow));
+                // 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
diff --git a/include/xsimd/arch/xsimd_rvv.hpp b/include/xsimd/arch/xsimd_rvv.hpp
index 977d1afc..e2ed58cf 100644
--- a/include/xsimd/arch/xsimd_rvv.hpp
+++ b/include/xsimd/arch/xsimd_rvv.hpp
@@ -553,46 +553,29 @@ namespace xsimd
         }
 
         // gather
-        template <class A, class T, class U, class IndexArch, 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, IndexArch> const& index, kernel::requires_arch<rvv>) noexcept
+        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
         {
-            constexpr auto lanes = batch<T, A>::size;
-            static_assert(lanes == batch<U, IndexArch>::size, "Index and destination sizes must match");
-            if constexpr (std::is_same_v<A, IndexArch>)
+            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);
             }
-            else
-            {
-                using indices = batch<widen_t<U>, A>;
-                map_to_sized_type_t<U> offsets[lanes];
-                index.store_unaligned(offsets);
-                const auto wide = [&]()
-                {
-                    if constexpr (std::is_signed_v<U>)
-                        return __riscv_vsext_vf2_i64m1(__riscv_vle32_v_i32mf2(offsets, lanes), lanes);
-                    else
-                        return __riscv_vzext_vf2_u64m1(__riscv_vle32_v_u32mf2(offsets, lanes), lanes);
-                }();
-                return batch<T, A>::gather(src, indices(typename indices::register_type(wide)));
-            }
-        }
-
-        template <class A, class T, class U, detail::enable_sized_integral_t<T, 8> = 0, detail::enable_sized_integral_t<U, 4> = 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 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)));
         }
 
         /**************
diff --git a/include/xsimd/arch/xsimd_sve.hpp b/include/xsimd/arch/xsimd_sve.hpp
index 5095debc..09ebf134 100644
--- a/include/xsimd/arch/xsimd_sve.hpp
+++ b/include/xsimd/arch/xsimd_sve.hpp
@@ -205,7 +205,7 @@ namespace xsimd
 
         // gather
         template <class A, class T, class U,
-                  std::enable_if_t<(sizeof(T) == 4 || sizeof(T) == 8) && detail::compatible_gather_sizes_v<T, A, U, A>, int> = 0>
+                  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
         {
             const auto* base = reinterpret_cast<map_to_sized_type_t<T> const*>(src);
diff --git a/include/xsimd/types/xsimd_batch.hpp b/include/xsimd/types/xsimd_batch.hpp
index a8ba8040..17c25454 100644
--- a/include/xsimd/types/xsimd_batch.hpp
+++ b/include/xsimd/types/xsimd_batch.hpp
@@ -26,13 +26,6 @@ namespace xsimd
 {
     namespace detail
     {
-        template <class T, class A, class V, class IndexArch>
-        using enable_narrow_gather_t = std::enable_if_t<
-            IndexArch::supported() && std::is_integral_v<T> && sizeof(T) == 8
-                && std::is_integral_v<V> && sizeof(V) == 4
-                && batch<T, A>::size == batch<V, IndexArch>::size,
-            int>;
-
         // Does batch<T, A> have a real predicated masked load/store, vs. the
         // emulated scalar `requires_arch<common>` fallback? Compile-time signal to
         // pick a masked single-loop over a vector+trailer loop. Size-keyed.
@@ -303,8 +296,6 @@ namespace xsimd
 
         template <class U, class V>
         [[nodiscard]] static XSIMD_INLINE batch gather(U const* src, batch<V, arch_type> const& index) noexcept;
-        template <class IndexArch, class V, detail::enable_narrow_gather_t<T, A, V, IndexArch> = 0>
-        [[nodiscard]] static XSIMD_INLINE batch gather(T const* src, batch<V, IndexArch> const& index) noexcept;
         template <class U, class V>
         XSIMD_INLINE void scatter(U* dst, batch<V, arch_type> const& index) const noexcept;
 
@@ -928,14 +919,8 @@ 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!");
-        return kernel::gather(batch {}, src, index, A {});
-    }
-
-    /** Gather 64-bit integers using one 32-bit integer element offset per result lane. */
-    template <class T, class A>
-    template <class IndexArch, class V, detail::enable_narrow_gather_t<T, A, V, IndexArch>>
-    XSIMD_INLINE batch<T, A> batch<T, A>::gather(T const* src, batch<V, IndexArch> const& index) noexcept
-    {
+        static_assert(batch<V, A>::size == batch<T, A>::size || (sizeof(T) == 8 && sizeof(V) == 4),
+                      "gather rule: the index batch needs one offset per result lane, or 32-bit offsets for 64-bit elements (the low size offsets are used)");
         return kernel::gather(batch {}, src, index, A {});
     }
 
diff --git a/include/xsimd/types/xsimd_utils.hpp b/include/xsimd/types/xsimd_utils.hpp
index ed3b38f5..f339c5de 100644
--- a/include/xsimd/types/xsimd_utils.hpp
+++ b/include/xsimd/types/xsimd_utils.hpp
@@ -243,11 +243,6 @@ namespace xsimd
              * Matching & mismatching sizes *
              ********************************/
 
-            template <class T, class A, class V, class IndexArch>
-            inline constexpr bool compatible_gather_sizes_v = batch<T, A>::size == batch<V, IndexArch>::size
-                || (std::is_same_v<A, IndexArch> && std::is_integral_v<T> && sizeof(T) == 8
-                    && std::is_integral_v<V> && sizeof(V) == 4);
-
             template <class T, class U, class B = int>
             using sizes_match_t = std::enable_if_t<sizeof(T) == sizeof(U), B>;
 
diff --git a/test/test_load_store.cpp b/test/test_load_store.cpp
index d032e4e6..2ad9b6f9 100644
--- a/test/test_load_store.cpp
+++ b/test/test_load_store.cpp
@@ -15,6 +15,7 @@
 #include "test_utils.hpp"
 
 #include <algorithm>
+#include <numeric>
 #include <random>
 #include <type_traits>
 
@@ -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<xsimd::batch<int64_t, typename B::arch_type>>("int64_t");
+            test_gather_indexed<xsimd::batch<uint64_t, typename B::arch_type>>("uint64_t");
+            test_gather_indexed<xsimd::batch<int32_t, typename B::arch_type>>("int32_t");
+            test_gather_indexed<xsimd::batch<uint32_t, typename B::arch_type>>("uint32_t");
+        }
     }
 
     void test_scatter()
@@ -402,6 +410,22 @@ private:
         CHECK_BATCH_EQ(b, expected);
     }
 
+    template <class I>
+    void test_gather_indexed(const std::string& index_name)
+    {
+        using index_value_type = typename I::value_type;
+        array_type data;
+        std::iota(data.begin(), data.end(), value_type(0));
+        std::reverse_copy(data.cbegin(), data.cend(), expected.begin());
+        std::array<index_value_type, I::size> offsets { }; // unused 32-bit lanes stay 0: catch a gather that reads the wrong index lanes
+        const index_value_type start = std::is_signed_v<index_value_type> ? index_value_type(0) : index_value_type(size - 1);
+        const value_type* base = std::is_signed_v<index_value_type> ? data.data() + size - 1 : data.data(); // negative offsets: catch a zero-extended index
+        for (std::size_t i = 0; i < size; ++i)
+            offsets[i] = start - static_cast<index_value_type>(i); // offsets count down: a plain load cannot match
+        INFO("gather 64-bit integers with ", index_name, " index");
+        CHECK_BATCH_EQ(batch_type::gather(base, I::load_unaligned(offsets.data())), expected);
+    }
+
     template <class V>
     void test_scatter_impl(const V& v, const std::string& name)
     {
@@ -461,46 +485,6 @@ TEST_CASE_TEMPLATE("[load store]", B, BATCH_TYPES)
     SUBCASE("masked") { Test.test_masked(); }
 }
 
-template <class Values, class Indices>
-static void check_integer_gather()
-{
-    using T = typename Values::value_type;
-    using V = typename Indices::value_type;
-    static_assert(Indices::size >= Values::size);
-    const T data[] = { T(0x100000001), T(0), T(-0x200000004), T(0x300000006) };
-    const V active_offsets[] = { 2, 0, 2, 1 };
-    V offsets[Indices::size];
-    for (std::size_t i = 0; i < Indices::size; ++i)
-    {
-        // Extra index lanes point at a different value from every active lane.
-        offsets[i] = i < Values::size ? active_offsets[i % 4] : V(3);
-    }
-    auto index = Indices::load_unaligned(offsets);
-    auto base = data;
-    if constexpr (std::is_signed_v<V>)
-    {
-        base += 2;
-        index -= Indices(2);
-    }
-    const auto result = Values::gather(base, index);
-    for (std::size_t i = 0; i < Values::size; ++i)
-        CHECK_EQ(result.get(i), data[offsets[i]]);
-}
-
-TEST_CASE_TEMPLATE("Gather 64-bit integers", T, int64_t, uint64_t)
-{
-    using B = xsimd::batch<T>;
-    check_integer_gather<B, xsimd::batch<int32_t>>();
-    check_integer_gather<B, xsimd::batch<uint32_t>>();
-    check_integer_gather<B, xsimd::batch<int64_t>>();
-    check_integer_gather<B, xsimd::batch<uint64_t>>();
-#if XSIMD_WITH_EMULATED
-    using values = xsimd::batch<T, xsimd::emulated<128>>;
-    check_integer_gather<values, xsimd::batch<int32_t, xsimd::emulated<64>>>();
-    check_integer_gather<values, xsimd::batch<uint32_t, xsimd::emulated<64>>>();
-#endif
-}
-
 template <class B>
 struct masked_load_store_test
 {

@LittlePyx
LittlePyx force-pushed the fix/avx2-i32-gather-i64 branch from a105381 to 16e5ec8 Compare November 20, 2026 03:38
@LittlePyx

Copy link
Copy Markdown
Contributor Author

@DiamonDinoia, I used your same-arch gather and load/store test changes in 16e5ec8. I kept one integer-only overload because Velox passes four 32-bit offsets in an SSE register directly to the AVX2 gather. That call fails to compile with just the same-arch overload.

@DiamonDinoia DiamonDinoia left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi,

  1. Remove the second gather overload in xsimd_batch.hpp, the one that takes batch<V, IndexArch>. I still do not understand where you need this.
  2. Remove detail::enable_avx2_half_gather_t. I do not see where it is used.
  3. Remove the avx2 kernel overload that widens SSE indices with _mm256_castsi128_si256 and forwards to the same-arch kernel.
  4. In test_load_store.cpp, remove the AVX2-only branch with the two xsimd::sse2 index cases. Also remove the arch template parameter of test_gather_indexed, so the helper uses typename B::arch_type.

@DiamonDinoia

Copy link
Copy Markdown
Contributor

Why Velox can't do:

// offsets is an int32_t const*
auto idx = xsimd::batch<int64_t>::load_unaligned(offsets);
auto r = xsimd::batch<int64_t>::gather(base, idx);

After the changes I suggested.

@LittlePyx
LittlePyx force-pushed the fix/avx2-i32-gather-i64 branch from 16e5ec8 to eaf147a Compare November 21, 2026 16:18
@LittlePyx

Copy link
Copy Markdown
Contributor Author

@DiamonDinoia, I tried your int32_t const* example on SSE2 and AVX2. Loading the offsets into batch<int64_t> and then calling gather worked on both. I removed the half-width overload in eaf147a, so this version only takes same-arch indices.

@DiamonDinoia DiamonDinoia left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like how this looks now both code and tests I understand.

Sorry for the back and forth but I do not merge code I do not understand.

If CI passes this is mergeable. Otherwise, could you fix the issues?

@LittlePyx

Copy link
Copy Markdown
Contributor Author

Thanks for the review, @DiamonDinoia. I checked the Windows build on eaf147a. It uses Clang 20.1.8, but the MSVC standard library on the runner requires Clang 22 or newer, so the build failed before any tests ran. Build log

@DiamonDinoia
DiamonDinoia merged commit 475c804 into xtensor-stack:master Nov 22, 2026
96 of 98 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants