10#ifndef INCLUDED_DVBS2RX_GF_UTIL_H
11#define INCLUDED_DVBS2RX_GF_UTIL_H
35 return (
static_cast<T
>(1) << n_bits) - 1;
46 for (
int i = 0; i < n_bits; i++)
60inline uint8_t
get_byte(
const T& value, uint32_t byte_index)
62 return (value >> (byte_index * 8)) & 0xFF;
73 for (uint32_t i = byte_index * 8; i < (byte_index + 1) * 8; i++)
74 byte |= value[i] << (i - byte_index * 8);
91inline uint8_t
get_msby(
const T& value, uint32_t lsb_index)
93 return value >> lsb_index;
104 for (uint32_t i = lsb_index; i < (lsb_index + 8); i++)
105 byte |= value[i] << (i - lsb_index);
121 if (n_bytes >
sizeof(T))
122 throw std::invalid_argument(
"n_bytes too large for type T");
124 for (
int i = n_bytes - 1; i >= 0; i--) {
141 if (size >
sizeof(T))
142 throw std::invalid_argument(
"u8 array too large for type T");
144 for (
size_t i = 0; i < size; i++) {
145 val |=
static_cast<T
>(in[i]) << ((size - 1 - i) * 8);
192 const unsigned int n_leak_bytes =
sizeof(T) - 1;
196 if (x.
degree() >
static_cast<int>(n_leak_bytes) * 8)
197 throw std::runtime_error(
"Failed to compute remainder LUT. Type T is too small.");
199 std::array<T, 256> table;
200 for (
int i = 0; i < 256; i++) {
201 T padded_in_byte =
static_cast<T
>(i) << (n_leak_bytes * 8);
203 table[i] = (y % x).get_poly();
223 const std::array<T, 256>& x_lut)
225 static constexpr int n_leak_bytes =
sizeof(T) - 1;
226 static constexpr uint32_t bytes_after_msby = n_leak_bytes - 1;
227 static constexpr uint32_t bits_after_msby = bytes_after_msby * 8;
228 const T leak_mask =
bitmask<T>(n_leak_bytes * 8);
233 for (
int i = 0; i < y_size - n_leak_bytes; i++) {
238 uint8_t in_byte_plus_leak = y[i] ^
get_msby(leak, bits_after_msby);
239 T leak_carried_forward = (leak_mask & (leak << 8));
240 leak = leak_carried_forward ^ x_lut[in_byte_plus_leak];
255 unsigned int n_last_bytes = std::min(n_leak_bytes, y_size);
259 y_last_bytes ^= leak;
Polynomial over GF(2).
Definition gf.h:203
int degree() const
Get the polynomial degree.
Definition gf.h:267
unsigned char * u8_ptr_t
Definition gf_util.h:23
class DVBS2RX_API gf2_poly
Definition gf.h:29
std::bitset< 256 > bitset256_t
Definition gf.h:26
std::array< T, 256 > build_gf2_poly_rem_lut(const gf2_poly< T > &x)
Build LUT to assist with GF(2) polynomial remainder computation.
Definition gf_util.h:178
T from_u8_vector(const u8_vector_t &vec)
Convert u8 vector in network byte order (big-endian) to type.
Definition gf_util.h:158
std::vector< unsigned char > u8_vector_t
Definition gf_util.h:22
uint8_t get_msby(const T &value, uint32_t lsb_index)
Get the most significant byte of a given value.
Definition gf_util.h:91
const unsigned char * u8_cptr_t
Definition gf_util.h:24
T bitmask(int n_bits)
Get bitmask for the least significant bits of a type T.
Definition gf_util.h:33
T from_u8_array(u8_cptr_t in, size_t size)
Convert u8 array in network byte order (big-endian) to type.
Definition gf_util.h:139
u8_vector_t to_u8_vector(T val, size_t n_bytes=sizeof(T))
Convert type to u8 vector in network byte order (big-endian)
Definition gf_util.h:119
gf2_poly< T > gf2_poly_rem(u8_cptr_t y, const int y_size, const gf2_poly< T > &x, const std::array< T, 256 > &x_lut)
Compute the remainder "y % x" of GF2 polynomials y and x using a LUT.
Definition gf_util.h:220
uint8_t get_byte(const T &value, uint32_t byte_index)
Get the byte at a given index of a type T value.
Definition gf_util.h:60
Fixed-length double-ended queue with contiguous volk-aligned elements.
Definition gr_bch.h:22