aboutsummaryrefslogtreecommitdiffstats
path: root/test/low_test.cpp
blob: 2dbeae1c65b7c65dc1c4d8088d53d89431c03c95 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#ifndef MCL_USE_LLVM
    #define MCL_USE_LLVM
#endif
#include <cybozu/test.hpp>
#include <cybozu/xorshift.hpp>
#include <cybozu/itoa.hpp>
#include "../src/fp_proto.hpp"
#include <cybozu/benchmark.hpp>

cybozu::XorShift rg;

extern "C" void add_test(mcl::fp::Unit *z, const mcl::fp::Unit *x, const mcl::fp::Unit *y);

template<size_t N>
void addNC(mcl::fp::Unit *z, const mcl::fp::Unit *x, const mcl::fp::Unit *y);

template<size_t N>
void subNC(mcl::fp::Unit *z, const mcl::fp::Unit *x, const mcl::fp::Unit *y);

#define DEF_FUNC(BIT) \
    template<> void addNC<BIT>(mcl::fp::Unit *z, const mcl::fp::Unit *x, const mcl::fp::Unit *y) { mcl_fp_addNC ## BIT(z, x, y); } \
    template<> void subNC<BIT>(mcl::fp::Unit *z, const mcl::fp::Unit *x, const mcl::fp::Unit *y) { mcl_fp_subNC ## BIT(z, x, y); }

DEF_FUNC(64)
DEF_FUNC(128)
DEF_FUNC(192)
DEF_FUNC(256)
DEF_FUNC(320)
DEF_FUNC(384)
DEF_FUNC(448)
DEF_FUNC(512)
//DEF_FUNC(96)
//DEF_FUNC(160)
//DEF_FUNC(224)

template<size_t bit>
void bench()
{
    using namespace mcl::fp;
    const size_t N = bit / UnitBitSize;
    Unit x[N], y[N];
    for (int i = 0; i < 10; i++) {
        Unit z[N];
        Unit w[N];
        rg.read(x, N);
        rg.read(y, N);
        low_addNC_G<N>(z, x, y);
        addNC<bit>(w, x, y);
        CYBOZU_TEST_EQUAL_ARRAY(z, w, N);

        low_subNC_G<N>(z, x, y);
        subNC<bit>(w, x, y);
        CYBOZU_TEST_EQUAL_ARRAY(z, w, N);
    }
    const std::string bitS = cybozu::itoa(bit);
    std::string name;
    name = "add" + bitS; CYBOZU_BENCH(name.c_str(), addNC<bit>, x, x, y);
    name = "sub" + bitS; CYBOZU_BENCH(name.c_str(), subNC<bit>, x, x, y);
}

CYBOZU_TEST_AUTO(addNC64) { bench<64>(); }
CYBOZU_TEST_AUTO(addNC128) { bench<128>(); }
CYBOZU_TEST_AUTO(addNC192) { bench<192>(); }
CYBOZU_TEST_AUTO(addNC256) { bench<256>(); }
CYBOZU_TEST_AUTO(addNC320) { bench<320>(); }
CYBOZU_TEST_AUTO(addNC384) { bench<384>(); }
CYBOZU_TEST_AUTO(addNC448) { bench<448>(); }
CYBOZU_TEST_AUTO(addNC512) { bench<512>(); }
//CYBOZU_TEST_AUTO(addNC96) { bench<96>(); }
//CYBOZU_TEST_AUTO(addNC160) { bench<160>(); }
//CYBOZU_TEST_AUTO(addNC224) { bench<224>(); }
#if 0
CYBOZU_TEST_AUTO(addNC)
{
    using namespace mcl::fp;
    const size_t bit = 128;
    const size_t N = bit / UnitBitSize;
    Unit x[N], y[N];
    for (int i = 0; i < 10; i++) {
        Unit z[N];
        Unit w[N];
        rg.read(x, N);
        rg.read(y, N);
        low_addNC_G<N>(z, x, y);
        addNC<bit>(w, x, y);
        CYBOZU_TEST_EQUAL_ARRAY(z, w, N);
        add_test(w, x, y);
        CYBOZU_TEST_EQUAL_ARRAY(z, w, N);
    }
    std::string name = "add" + cybozu::itoa(bit);
    CYBOZU_BENCH(name.c_str(), addNC<bit>, x, x, y);
    CYBOZU_BENCH("add", add_test, x, x, y);
}
#endif