-
Notifications
You must be signed in to change notification settings - Fork 0
/
gf2_polynomial.h
64 lines (51 loc) · 1.73 KB
/
gf2_polynomial.h
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
#pragma once
#include "polynomial.h"
#include <utility>
namespace mr {
template<unsigned max_order>
struct gf2_polynomial : polynomial<bit_t, max_order>
{
using base = polynomial<bit_t, max_order>;
template<typename...Powers>
constexpr gf2_polynomial(unsigned power, Powers &&...powers)
: gf2_polynomial::gf2_polynomial(std::forward<Powers>(powers)...)
{
base::coeffs[power] = 1;
}
private:
using base::base; // hide the inherited ctor
};
template<unsigned...Pwrs>
struct static_gf2_polynomial : gf2_polynomial<select_last_unsigned<Pwrs...>::value>
{
constexpr static const unsigned max_order = select_last_unsigned<Pwrs...>::value;
using base = gf2_polynomial<max_order>;
constexpr static_gf2_polynomial()
: base(Pwrs...) {} // represented value fixed by template args
private:
using base::base; // hide the inherited ctor
};
// detect 128 bit builtin types
#ifdef __SIZEOF_INT128__
using int128_t = __int128_t;
using uint128_t = __uint128_t;
// specialize polynomials with coeffs in GF(2) with casting as uint128_t
template<unsigned max_order>
constexpr void gf2_polynomial_to_decimal(const polynomial<bit_t, max_order> &p, uint128_t &d)
{
const auto deg = p.degree();
d = 0;
for(auto i=0; i<=deg; i++)
d |= (p[i] << i);
}
template<unsigned max_order>
constexpr void decimal_to_gf2_polynomial(const uint128_t &d, polynomial<bit_t, max_order> &p)
{
// degree not known without clz
for(auto i=0; i<max_order; i++) {
const auto dbg = d & (1U << i);
p[i] = dbg;
}
}
#endif
}