2020-06-15 14:18:57 +00:00
|
|
|
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
|
|
|
|
│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│
|
|
|
|
╞══════════════════════════════════════════════════════════════════════════════╡
|
|
|
|
│ Copyright 2020 Justine Alexandra Roberts Tunney │
|
|
|
|
│ │
|
2020-12-28 01:18:44 +00:00
|
|
|
│ Permission to use, copy, modify, and/or distribute this software for │
|
|
|
|
│ any purpose with or without fee is hereby granted, provided that the │
|
|
|
|
│ above copyright notice and this permission notice appear in all copies. │
|
2020-06-15 14:18:57 +00:00
|
|
|
│ │
|
2020-12-28 01:18:44 +00:00
|
|
|
│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │
|
|
|
|
│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │
|
|
|
|
│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │
|
|
|
|
│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │
|
|
|
|
│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │
|
|
|
|
│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │
|
|
|
|
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
|
|
|
│ PERFORMANCE OF THIS SOFTWARE. │
|
2020-06-15 14:18:57 +00:00
|
|
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
|
|
|
#include "dsp/tty/windex.h"
|
|
|
|
#include "libc/assert.h"
|
2022-08-11 19:13:18 +00:00
|
|
|
#include "libc/intrin/bits.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/limits.h"
|
2021-03-01 07:42:35 +00:00
|
|
|
#include "libc/macros.internal.h"
|
2022-11-08 19:39:50 +00:00
|
|
|
#include "libc/mem/gc.internal.h"
|
2021-02-01 11:33:13 +00:00
|
|
|
#include "libc/mem/mem.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/nexgen32e/x86feature.h"
|
|
|
|
#include "libc/str/str.h"
|
|
|
|
#include "libc/testlib/ezbench.h"
|
|
|
|
#include "libc/testlib/testlib.h"
|
|
|
|
|
2022-11-08 19:39:50 +00:00
|
|
|
unsigned windex_k8(short *, size_t) _Hide;
|
|
|
|
unsigned windex_avx2(short *, size_t) _Hide;
|
|
|
|
unsigned windex_sse4(short *, size_t) _Hide;
|
2020-06-15 14:18:57 +00:00
|
|
|
|
2020-12-05 20:20:41 +00:00
|
|
|
const short kW[64] forcealign(32) = {
|
2020-06-15 14:18:57 +00:00
|
|
|
8281, 3883, 1365, 1786, 9006, 3681, 5563, 8013, 5787, 9063, 2923,
|
|
|
|
3564, 6122, 32, 1436, 0741, 7957, 9219, 1320, 2083, 1904, 8905,
|
|
|
|
2465, 9122, 9563, 1290, 4474, 3988, 9920, 8325, 1088, 2915, 33,
|
|
|
|
1085, 7806, 3248, 1186, 1357, 6738, 1311, 1092, 6195, 7089, 6631,
|
|
|
|
1261, 1364, 9007, 8289, 1409, 1090, 8358, 1502, 7658, 2668, 3522,
|
|
|
|
1730, 2041, 7707, 5096, 6876, 1324, 1242, 5283, 0x7fff,
|
|
|
|
};
|
|
|
|
|
2020-12-05 20:20:41 +00:00
|
|
|
const short kW2[32] forcealign(32) = {
|
2020-06-15 14:18:57 +00:00
|
|
|
8281, 1, 1365, 1786, 9006, 3681, 5563, 8013, 5787, 9063, 2923,
|
|
|
|
3564, 6122, 32, 1436, 0741, 7957, 9219, 1320, 2083, 1904, 8905,
|
|
|
|
2465, 9122, 9563, 1290, 4474, 3988, 9920, 8325, 1088, 2915,
|
|
|
|
};
|
|
|
|
|
2020-12-05 20:20:41 +00:00
|
|
|
const short kW3[64] forcealign(32) = {
|
2020-06-15 14:18:57 +00:00
|
|
|
8281, 0x7fff, 1365, 1786, 9006, 3681, 5563, 8013, 5787, 9063, 2923,
|
|
|
|
3564, 6122, 32, 1436, 0741, 7957, 9219, 1320, 2083, 1904, 8905,
|
|
|
|
2465, 9122, 9563, 1290, 4474, 3988, 9920, 8325, 1088, 2915, 33,
|
|
|
|
1085, 7806, 3248, 1186, 1357, 6738, 1311, 1092, 6195, 7089, 6631,
|
|
|
|
1261, 1364, 9007, 8289, 1409, 1090, 8358, 1502, 7658, 2668, 3522,
|
|
|
|
1730, 2041, 7707, 5096, 6876, 1324, 1242, 7, 0x7fff,
|
|
|
|
};
|
|
|
|
|
2021-02-01 11:33:13 +00:00
|
|
|
#define TestIt(impl, index, value, n, array) \
|
|
|
|
({ \
|
|
|
|
short *a = memcpy(gc(memalign(32, n * 2)), array, n * 2); \
|
|
|
|
unsigned res = impl(array, n); \
|
|
|
|
ASSERT_EQ(index, res); \
|
|
|
|
ASSERT_EQ(value, a[res]); \
|
2020-06-15 14:18:57 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
TEST(windex, testRealWorldPicks) {
|
2020-12-05 20:20:41 +00:00
|
|
|
const short kPicks[96] forcealign(32) = {
|
2020-06-15 14:18:57 +00:00
|
|
|
103, 85, 145, 146, 121, 103, 145, 187, 146, 189,
|
|
|
|
121, 103, 139, 121, 63, 105, 105, 147, 60, 103,
|
|
|
|
103, 146, 121, 103, 139, 121, 139, 121, 157, 139,
|
|
|
|
199, 200, 163, 223, 164, 225, 81, 141, 105, 165,
|
|
|
|
78, 139, 103, 164, 61, 103, 103, 145, 79, 139,
|
|
|
|
103, 163, 21, 63, 21, 81, 63, 45, 105, 106,
|
|
|
|
106, 107, 102, 103, 103, 104, 64, 107, 107, 150,
|
|
|
|
82, 143, 107, 168, 108, 109, 109, 110, 21, 64,
|
|
|
|
21, 82, 105, 106, 64, 46, 106, 107, 0x7fff, 0x7fff,
|
|
|
|
0x7fff, 0x7fff, 0x7fff, 0x7fff, 0x7fff, 0x7fff,
|
|
|
|
};
|
|
|
|
/* multiple valid answers are fine if it's deterministic */
|
2021-02-06 08:24:35 +00:00
|
|
|
TestIt(windex_k8, 52, 21, ARRAYLEN(kPicks), kPicks);
|
|
|
|
if (X86_HAVE(AVX2)) TestIt(windex_avx2, 78, 21, ARRAYLEN(kPicks), kPicks);
|
|
|
|
if (X86_HAVE(SSE4_2)) TestIt(windex_sse4, 80, 21, ARRAYLEN(kPicks), kPicks);
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(windex, test) {
|
|
|
|
TestIt(windex, 13, 32, ARRAYLEN(kW), kW);
|
|
|
|
TestIt(windex, 1, 1, ARRAYLEN(kW2), kW2);
|
|
|
|
TestIt(windex, 62, 7, ARRAYLEN(kW3), kW3);
|
|
|
|
}
|
|
|
|
|
2021-02-06 08:24:35 +00:00
|
|
|
TEST(windex_avx2, test) {
|
2020-06-15 14:18:57 +00:00
|
|
|
if (X86_HAVE(AVX2)) {
|
2021-02-06 08:24:35 +00:00
|
|
|
TestIt(windex_avx2, 13, 32, ARRAYLEN(kW), kW);
|
|
|
|
TestIt(windex_avx2, 1, 1, ARRAYLEN(kW2), kW2);
|
|
|
|
TestIt(windex_avx2, 62, 7, ARRAYLEN(kW3), kW3);
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-06 08:24:35 +00:00
|
|
|
TEST(windex_sse4, test) {
|
2020-06-15 14:18:57 +00:00
|
|
|
if (X86_HAVE(SSE4_2)) {
|
2021-02-06 08:24:35 +00:00
|
|
|
TestIt(windex_sse4, 13, 32, ARRAYLEN(kW), kW);
|
|
|
|
TestIt(windex_sse4, 1, 1, ARRAYLEN(kW2), kW2);
|
|
|
|
TestIt(windex_sse4, 62, 7, ARRAYLEN(kW3), kW3);
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-06 08:24:35 +00:00
|
|
|
TEST(windex_k8, test) {
|
|
|
|
TestIt(windex_k8, 13, 32, ARRAYLEN(kW), kW);
|
|
|
|
TestIt(windex_k8, 1, 1, ARRAYLEN(kW2), kW2);
|
|
|
|
TestIt(windex_k8, 62, 7, ARRAYLEN(kW3), kW3);
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
BENCH(windex, bench) {
|
|
|
|
EZBENCH(donothing, windex(kW, ARRAYLEN(kW)));
|
|
|
|
}
|
2021-02-06 08:24:35 +00:00
|
|
|
BENCH(windex_k8, bench) {
|
|
|
|
EZBENCH(donothing, windex_k8(kW, ARRAYLEN(kW)));
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|
2021-02-06 08:24:35 +00:00
|
|
|
BENCH(windex_avx2, bench) {
|
2020-06-15 14:18:57 +00:00
|
|
|
if (X86_HAVE(AVX2)) {
|
2021-02-06 08:24:35 +00:00
|
|
|
EZBENCH(donothing, windex_avx2(kW, ARRAYLEN(kW)));
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|
|
|
|
}
|
2021-02-06 08:24:35 +00:00
|
|
|
BENCH(windex_sse4, bench) {
|
2020-06-15 14:18:57 +00:00
|
|
|
if (X86_HAVE(SSE4_2)) {
|
2021-02-06 08:24:35 +00:00
|
|
|
EZBENCH(donothing, windex_sse4(kW, ARRAYLEN(kW)));
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|
|
|
|
}
|