poulpy/spqlios/lib/test/spqlios_zn_vmp_test.cpp
spqlios/lib/test/spqlios_zn_vmp_test.cppRaw
1#include "gtest/gtest.h"
2#include "spqlios/arithmetic/zn_arithmetic_private.h"
3#include "testlib/zn_layouts.h"
4
5static void test_zn_vmp_prepare(ZN32_VMP_PREPARE_CONTIGUOUS_F prep) {
6 MOD_Z* module = new_z_module_info(DEFAULT);
7 for (uint64_t nrows : {1, 2, 5, 15}) {
8 for (uint64_t ncols : {1, 2, 32, 42, 67}) {
9 std::vector<int32_t> src(nrows * ncols);
10 zn32_pmat_layout out(nrows, ncols);
11 for (int32_t& x : src) x = uniform_i64_bits(32);
12 prep(module, out.data, src.data(), nrows, ncols);
13 for (uint64_t i = 0; i < nrows; ++i) {
14 for (uint64_t j = 0; j < ncols; ++j) {
15 int32_t in = src[i * ncols + j];
16 int32_t actual = out.get(i, j);
17 ASSERT_EQ(actual, in);
18 }
19 }
20 }
21 }
22 delete_z_module_info(module);
23}
24
25TEST(zn, zn32_vmp_prepare_contiguous) { test_zn_vmp_prepare(zn32_vmp_prepare_contiguous); }
26TEST(zn, default_zn32_vmp_prepare_contiguous_ref) { test_zn_vmp_prepare(default_zn32_vmp_prepare_contiguous_ref); }
27
28template <typename INTTYPE>
29static void test_zn_vmp_apply(void (*apply)(const MOD_Z*, int32_t*, uint64_t, const INTTYPE*, uint64_t,
30 const ZN32_VMP_PMAT*, uint64_t, uint64_t)) {
31 MOD_Z* module = new_z_module_info(DEFAULT);
32 for (uint64_t nrows : {1, 2, 5, 15}) {
33 for (uint64_t ncols : {1, 2, 32, 42, 67}) {
34 for (uint64_t a_size : {1, 2, 5, 15}) {
35 for (uint64_t res_size : {1, 2, 32, 42, 67}) {
36 std::vector<INTTYPE> a(a_size);
37 zn32_pmat_layout out(nrows, ncols);
38 std::vector<int32_t> res(res_size);
39 for (INTTYPE& x : a) x = uniform_i64_bits(32);
40 out.fill_random();
41 std::vector<int32_t> expect = vmp_product(a.data(), a_size, res_size, out);
42 apply(module, res.data(), res_size, a.data(), a_size, out.data, nrows, ncols);
43 for (uint64_t i = 0; i < res_size; ++i) {
44 int32_t exp = expect[i];
45 int32_t actual = res[i];
46 ASSERT_EQ(actual, exp);
47 }
48 }
49 }
50 }
51 }
52 delete_z_module_info(module);
53}
54
55TEST(zn, zn32_vmp_apply_i32) { test_zn_vmp_apply(zn32_vmp_apply_i32); }
56TEST(zn, zn32_vmp_apply_i16) { test_zn_vmp_apply(zn32_vmp_apply_i16); }
57TEST(zn, zn32_vmp_apply_i8) { test_zn_vmp_apply(zn32_vmp_apply_i8); }
58
59TEST(zn, default_zn32_vmp_apply_i32_ref) { test_zn_vmp_apply(default_zn32_vmp_apply_i32_ref); }
60TEST(zn, default_zn32_vmp_apply_i16_ref) { test_zn_vmp_apply(default_zn32_vmp_apply_i16_ref); }
61TEST(zn, default_zn32_vmp_apply_i8_ref) { test_zn_vmp_apply(default_zn32_vmp_apply_i8_ref); }
62
63#ifdef __x86_64__
64TEST(zn, default_zn32_vmp_apply_i32_avx) { test_zn_vmp_apply(default_zn32_vmp_apply_i32_avx); }
65TEST(zn, default_zn32_vmp_apply_i16_avx) { test_zn_vmp_apply(default_zn32_vmp_apply_i16_avx); }
66TEST(zn, default_zn32_vmp_apply_i8_avx) { test_zn_vmp_apply(default_zn32_vmp_apply_i8_avx); }
67#endif