added insertion sort and regression test

This commit is contained in:
Ahmet Inan 2024-03-05 11:46:33 +01:00
commit ae0a749d08
2 changed files with 70 additions and 101 deletions

119
sort.hh
View file

@ -6,107 +6,32 @@ Copyright 2024 Ahmet Inan <inan@aicodix.de>
#pragma once
#include <algorithm>
template <int WIDTH>
static inline SIMD<uint32_t, WIDTH> vorder(SIMD<float, WIDTH> a)
template <typename TYPE, int WIDTH>
static inline SIMD<typename SIMD<TYPE, WIDTH>::uint_type, WIDTH> vorder(SIMD<TYPE, WIDTH> a)
{
SIMD<uint32_t, WIDTH> tmp;
for (int i = 0; i < WIDTH; ++i)
tmp.v[i] = i;
std::stable_sort(tmp.v, tmp.v+WIDTH, [a](int i, int j){ return a.v[i] < a.v[j]; });
return tmp;
SIMD<typename SIMD<TYPE, WIDTH>::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;
}
return p;
}
template <int WIDTH>
static inline SIMD<uint64_t, WIDTH> vorder(SIMD<double, WIDTH> a)
template <typename TYPE, int WIDTH>
static inline SIMD<TYPE, WIDTH> vsort(SIMD<TYPE, WIDTH> a)
{
SIMD<uint64_t, WIDTH> tmp;
for (int i = 0; i < WIDTH; ++i)
tmp.v[i] = i;
std::stable_sort(tmp.v, tmp.v+WIDTH, [a](int i, int j){ return a.v[i] < a.v[j]; });
return tmp;
}
template <int WIDTH>
static inline SIMD<uint8_t, WIDTH> vorder(SIMD<int8_t, WIDTH> a)
{
SIMD<uint8_t, WIDTH> tmp;
for (int i = 0; i < WIDTH; ++i)
tmp.v[i] = i;
std::stable_sort(tmp.v, tmp.v+WIDTH, [a](int i, int j){ return a.v[i] < a.v[j]; });
return tmp;
}
template <int WIDTH>
static inline SIMD<uint16_t, WIDTH> vorder(SIMD<int16_t, WIDTH> a)
{
SIMD<uint16_t, WIDTH> tmp;
for (int i = 0; i < WIDTH; ++i)
tmp.v[i] = i;
std::stable_sort(tmp.v, tmp.v+WIDTH, [a](int i, int j){ return a.v[i] < a.v[j]; });
return tmp;
}
template <int WIDTH>
static inline SIMD<uint32_t, WIDTH> vorder(SIMD<int32_t, WIDTH> a)
{
SIMD<uint32_t, WIDTH> tmp;
for (int i = 0; i < WIDTH; ++i)
tmp.v[i] = i;
std::stable_sort(tmp.v, tmp.v+WIDTH, [a](int i, int j){ return a.v[i] < a.v[j]; });
return tmp;
}
template <int WIDTH>
static inline SIMD<uint64_t, WIDTH> vorder(SIMD<int64_t, WIDTH> a)
{
SIMD<uint64_t, WIDTH> tmp;
for (int i = 0; i < WIDTH; ++i)
tmp.v[i] = i;
std::stable_sort(tmp.v, tmp.v+WIDTH, [a](int i, int j){ return a.v[i] < a.v[j]; });
return tmp;
}
template <int WIDTH>
static inline SIMD<float, WIDTH> vsort(SIMD<float, WIDTH> a)
{
std::sort(a.v, a.v+WIDTH);
return a;
}
template <int WIDTH>
static inline SIMD<double, WIDTH> vsort(SIMD<double, WIDTH> a)
{
std::sort(a.v, a.v+WIDTH);
return a;
}
template <int WIDTH>
static inline SIMD<int8_t, WIDTH> vsort(SIMD<int8_t, WIDTH> a)
{
std::sort(a.v, a.v+WIDTH);
return a;
}
template <int WIDTH>
static inline SIMD<int16_t, WIDTH> vsort(SIMD<int16_t, WIDTH> a)
{
std::sort(a.v, a.v+WIDTH);
return a;
}
template <int WIDTH>
static inline SIMD<int32_t, WIDTH> vsort(SIMD<int32_t, WIDTH> a)
{
std::sort(a.v, a.v+WIDTH);
return a;
}
template <int WIDTH>
static inline SIMD<int64_t, WIDTH> vsort(SIMD<int64_t, WIDTH> a)
{
std::sort(a.v, a.v+WIDTH);
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;
}
return a;
}

View file

@ -0,0 +1,44 @@
/*
Regression Test for the SIMD wrappers sort
Copyright 2024 Ahmet Inan <inan@aicodix.de>
*/
#include <random>
#include <cassert>
#include <iostream>
#include <algorithm>
#include <functional>
#include "simd.hh"
#include "sort.hh"
int main()
{
const int WIDTH = 32;
typedef int32_t TYPE;
typedef SIMD<TYPE, WIDTH>::uint_type UINT;
std::random_device rd;
typedef std::default_random_engine generator;
typedef std::uniform_int_distribution<int> distribution;
auto rand = std::bind(distribution(0, WIDTH), generator(rd()));
for (int loop = 0; loop < 1000000; ++loop) {
SIMD<TYPE, WIDTH> a, b, c;
for (int i = 0; i < WIDTH; ++i)
a.v[i] = rand();
b = vsort(a);
c = a;
std::sort(c.v, c.v+WIDTH);
for (int i = 0; i < WIDTH; ++i)
assert(b.v[i] == c.v[i]);
SIMD<UINT, WIDTH> d, e;
d = vorder(a);
for (int i = 0; i < WIDTH; ++i)
e.v[i] = i;
std::stable_sort(e.v, e.v+WIDTH, [a](int i, int j){ return a.v[i] < a.v[j]; });
for (int i = 0; i < WIDTH; ++i)
assert(d.v[i] == e.v[i]);
}
std::cerr << "Sort regression test passed!" << std::endl;
return 0;
}