arnaucube
arnaucube/poulpyPublic Self-hosted repository
poulpy/spqlios/lib/test/spqlios_zn_approxdecomp_test.cpp
spqlios/lib/test/spqlios_zn_approxdecomp_test.cppRaw
 1#include "gtest/gtest.h"
 2#include "spqlios/arithmetic/zn_arithmetic_private.h"
 3#include "testlib/test_commons.h"
 4
 5template <typename INTTYPE>
 6static void test_tndbl_approxdecomp(                                                                                //
 7    void (*approxdec)(const MOD_Z*, const TNDBL_APPROXDECOMP_GADGET*, INTTYPE*, uint64_t, const double*, uint64_t)  //
 8) {
 9  for (const uint64_t nn : {1, 3, 8, 51}) {
10    MOD_Z* module = new_z_module_info(DEFAULT);
11    for (const uint64_t ell : {1, 2, 7}) {
12      for (const uint64_t k : {2, 5}) {
13        TNDBL_APPROXDECOMP_GADGET* gadget = new_tndbl_approxdecomp_gadget(module, k, ell);
14        for (const uint64_t res_size : {ell * nn}) {
15          std::vector<double> in(nn);
16          std::vector<INTTYPE> out(res_size);
17          for (double& x : in) x = uniform_f64_bounds(-10, 10);
18          approxdec(module, gadget, out.data(), res_size, in.data(), nn);
19          // reconstruct the output
20          double err_bnd = pow(2., -double(ell * k) - 1);
21          for (uint64_t j = 0; j < nn; ++j) {
22            double in_j = in[j];
23            double out_j = 0;
24            for (uint64_t i = 0; i < ell; ++i) {
25              out_j += out[ell * j + i] * pow(2., -double((i + 1) * k));
26            }
27            double err = out_j - in_j;
28            double err_abs = fabs(err - rint(err));
29            ASSERT_LE(err_abs, err_bnd);
30          }
31        }
32        delete_tndbl_approxdecomp_gadget(gadget);
33      }
34    }
35    delete_z_module_info(module);
36  }
37}
38
39TEST(vec_rnx, i8_tndbl_rnx_approxdecomp) { test_tndbl_approxdecomp(i8_approxdecomp_from_tndbl); }
40TEST(vec_rnx, default_i8_tndbl_rnx_approxdecomp) { test_tndbl_approxdecomp(default_i8_approxdecomp_from_tndbl_ref); }
41
42TEST(vec_rnx, i16_tndbl_rnx_approxdecomp) { test_tndbl_approxdecomp(i16_approxdecomp_from_tndbl); }
43TEST(vec_rnx, default_i16_tndbl_rnx_approxdecomp) { test_tndbl_approxdecomp(default_i16_approxdecomp_from_tndbl_ref); }
44
45TEST(vec_rnx, i32_tndbl_rnx_approxdecomp) { test_tndbl_approxdecomp(i32_approxdecomp_from_tndbl); }
46TEST(vec_rnx, default_i32_tndbl_rnx_approxdecomp) { test_tndbl_approxdecomp(default_i32_approxdecomp_from_tndbl_ref); }