From 2e7fce3b850947fc5bfedb52dd245928e6af9aa2 Mon Sep 17 00:00:00 2001 From: kojix2 <2xijok@gmail.com> Date: Wed, 10 Sep 2025 11:45:36 +0900 Subject: [PATCH 1/2] Add test for sort_index correctness --- test/narray_test.rb | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/test/narray_test.rb b/test/narray_test.rb index 3a1e2a21..08065ea0 100644 --- a/test/narray_test.rb +++ b/test/narray_test.rb @@ -649,4 +649,10 @@ class NArrayTest < Test::Unit::TestCase assert_equal(Numo::DFloat[1, Float::NAN, 3].format_to_a, Numo::DFloat.cast(Numo::RObject[1, nil, 3]).format_to_a) end + + test "sort_index correctness" do + a = Numo::SFloat.new(10).rand_norm + idx = a.sort_index + assert { a[idx].to_a == a[idx].to_a.sort } + end end From 3271e12683397899e0f8bf6c7b81eab5be8b0c4c Mon Sep 17 00:00:00 2001 From: kojix2 <2xijok@gmail.com> Date: Wed, 10 Sep 2025 14:35:10 +0900 Subject: [PATCH 2/2] Fix sporadic misordering in sort_index on GCC 13+ by forcing byte-wise swap for elements whose size >= sizeof(void*), avoiding unsafe word reinterpret swaps --- ext/numo/narray/gen/tmpl/qsort.c | 1 + 1 file changed, 1 insertion(+) diff --git a/ext/numo/narray/gen/tmpl/qsort.c b/ext/numo/narray/gen/tmpl/qsort.c index 7d51ec41..dffda343 100644 --- a/ext/numo/narray/gen/tmpl/qsort.c +++ b/ext/numo/narray/gen/tmpl/qsort.c @@ -128,6 +128,7 @@ static void presorted; loop:SWAPINIT(a, es); + if (es >= sizeof(void*)) swaptype = 2; if (n < 7) { for (pm = (char *) a + es; pm < (char *) a + n * es; pm += es)