From 957eb6f784467825ab14c3a8f7dc67fbfe3b9d5c Mon Sep 17 00:00:00 2001 From: Ahmet Inan Date: Thu, 7 Mar 2024 22:04:09 +0100 Subject: [PATCH] use the new sorting functions --- polar_list_decoder.hh | 12 ++---------- polar_parity_aided.hh | 12 ++---------- simd_sort.hh | 20 ++++---------------- sort.hh | 8 ++++++-- tests/sort_regression_test.cc | 4 ++-- 5 files changed, 16 insertions(+), 40 deletions(-) diff --git a/polar_list_decoder.hh b/polar_list_decoder.hh index 9575681..07a1cb1 100644 --- a/polar_list_decoder.hh +++ b/polar_list_decoder.hh @@ -6,6 +6,7 @@ Copyright 2020 Ahmet Inan #pragma once +#include "sort.hh" #include "polar_helper.hh" namespace CODE { @@ -61,16 +62,7 @@ struct PolarListNode else fork[2*k+1] += sft.v[k]; int perm[2*TYPE::SIZE]; - perm[0] = 0; - for (int i = 1, j; i < 2*TYPE::SIZE; ++i) { - PATH t = fork[i]; - for (j = i; j > 0 && fork[j-1] > t; --j) { - fork[j] = fork[j-1]; - perm[j] = perm[j-1]; - } - fork[j] = t; - perm[j] = i; - } + CODE::insertion_sort(perm, fork, 2*TYPE::SIZE); for (int k = 0; k < TYPE::SIZE; ++k) metric[k] = fork[k]; MAP map; diff --git a/polar_parity_aided.hh b/polar_parity_aided.hh index 3268d37..f1230b0 100644 --- a/polar_parity_aided.hh +++ b/polar_parity_aided.hh @@ -6,6 +6,7 @@ Copyright 2023 Ahmet Inan #pragma once +#include "sort.hh" #include "polar_helper.hh" namespace CODE { @@ -118,16 +119,7 @@ struct PolarParityNode fork[2*k+1] = 1000000; } int perm[2*TYPE::SIZE]; - perm[0] = 0; - for (int i = 1, j; i < 2*TYPE::SIZE; ++i) { - PATH t = fork[i]; - for (j = i; j > 0 && fork[j-1] > t; --j) { - fork[j] = fork[j-1]; - perm[j] = perm[j-1]; - } - fork[j] = t; - perm[j] = i; - } + CODE::insertion_sort(perm, fork, 2*TYPE::SIZE); for (int k = 0; k < TYPE::SIZE; ++k) metric[k] = fork[k]; MAP map; diff --git a/simd_sort.hh b/simd_sort.hh index 1303658..1522c93 100644 --- a/simd_sort.hh +++ b/simd_sort.hh @@ -6,32 +6,20 @@ Copyright 2024 Ahmet Inan #pragma once +#include "sort.hh" + template static inline SIMD::uint_type, WIDTH> vorder(SIMD a) { SIMD::uint_type, WIDTH> p; - p.v[0] = 0; - for (int i = 1, j; i < WIDTH; ++i) { - TYPE t = a.v[i]; - for (j = i; j > 0 && a.v[j-1] > t; --j) { - a.v[j] = a.v[j-1]; - p.v[j] = p.v[j-1]; - } - a.v[j] = t; - p.v[j] = i; - } + CODE::insertion_sort(p.v, a.v, WIDTH); return p; } template static inline SIMD vsort(SIMD a) { - for (int i = 1, j; i < WIDTH; ++i) { - TYPE t = a.v[i]; - for (j = i; j > 0 && a.v[j-1] > t; --j) - a.v[j] = a.v[j-1]; - a.v[j] = t; - } + CODE::insertion_sort(a.v, WIDTH); return a; } diff --git a/sort.hh b/sort.hh index b1ea7f2..441773e 100644 --- a/sort.hh +++ b/sort.hh @@ -6,6 +6,8 @@ Copyright 2024 Ahmet Inan #pragma once +namespace CODE { + template static void insertion_sort(TYPE *a, int n) { @@ -17,8 +19,8 @@ static void insertion_sort(TYPE *a, int n) } } -template -static void insertion_sort(int *p, TYPE *a, int n) +template +static void insertion_sort(INDEX *p, TYPE *a, int n) { p[0] = 0; for (int i = 1, j; i < n; ++i) { @@ -32,3 +34,5 @@ static void insertion_sort(int *p, TYPE *a, int n) } } +} + diff --git a/tests/sort_regression_test.cc b/tests/sort_regression_test.cc index 449c859..38ea168 100644 --- a/tests/sort_regression_test.cc +++ b/tests/sort_regression_test.cc @@ -23,13 +23,13 @@ int main() for (int i = 0; i < size; ++i) a[i] = b[i] = c[i] = d[i] = rand(); std::sort(a, a+size); - insertion_sort(b, size); + CODE::insertion_sort(b, size); for (int i = 0; i < size; ++i) assert(a[i] == b[i]); for (int i = 0; i < size; ++i) e[i] = i; std::stable_sort(e, e+size, [c](int i, int j){ return c[i] < c[j]; }); - insertion_sort(f, d, size); + CODE::insertion_sort(f, d, size); for (int i = 0; i < size; ++i) assert(e[i] == f[i]); }