arnaucube
arnaucube/poulpyPublic Self-hosted repository
poulpy/spqlios/lib/test/spqlios_vec_rnx_approxdecomp_tnxdbl_test.cpp
spqlios/lib/test/spqlios_vec_rnx_approxdecomp_tnxdbl_test.cppRaw
 1#include "gtest/gtest.h"
 2#include "spqlios/arithmetic/vec_rnx_arithmetic_private.h"
 3#include "testlib/vec_rnx_layout.h"
 4
 5static void test_rnx_approxdecomp(RNX_APPROXDECOMP_FROM_TNXDBL_F approxdec) {
 6  for (const uint64_t nn : {2, 4, 8, 32}) {
 7    MOD_RNX* module = new_rnx_module_info(nn, FFT64);
 8    for (const uint64_t ell : {1, 2, 7}) {
 9      for (const uint64_t k : {2, 5}) {
10        TNXDBL_APPROXDECOMP_GADGET* gadget = new_tnxdbl_approxdecomp_gadget(module, k, ell);
11        for (const uint64_t res_size : {ell, ell - 1, ell + 1}) {
12          const uint64_t res_sl = nn + uniform_u64_bits(2);
13          rnx_vec_f64_layout in(nn, 1, nn);
14          in.fill_random(3);
15          rnx_vec_f64_layout out(nn, res_size, res_sl);
16          approxdec(module, gadget, out.data(), res_size, res_sl, in.data());
17          // reconstruct the output
18          uint64_t msize = std::min(res_size, ell);
19          double err_bnd = msize == ell ? pow(2., -double(msize * k) - 1) : pow(2., -double(msize * k));
20          for (uint64_t j = 0; j < nn; ++j) {
21            double in_j = in.data()[j];
22            double out_j = 0;
23            for (uint64_t i = 0; i < res_size; ++i) {
24              out_j += out.get_copy(i).get_coeff(j) * pow(2., -double((i + 1) * k));
25            }
26            double err = out_j - in_j;
27            double err_abs = fabs(err - rint(err));
28            ASSERT_LE(err_abs, err_bnd);
29          }
30        }
31        delete_tnxdbl_approxdecomp_gadget(gadget);
32      }
33    }
34    delete_rnx_module_info(module);
35  }
36}
37
38TEST(vec_rnx, rnx_approxdecomp) { test_rnx_approxdecomp(rnx_approxdecomp_from_tnxdbl); }
39TEST(vec_rnx, rnx_approxdecomp_ref) { test_rnx_approxdecomp(rnx_approxdecomp_from_tnxdbl_ref); }
40#ifdef __x86_64__
41TEST(vec_rnx, rnx_approxdecomp_avx) { test_rnx_approxdecomp(rnx_approxdecomp_from_tnxdbl_avx); }
42#endif