reed_solomon_simd/engine/engine_naive.rs
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 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
use crate::engine::{
tables::{self, Exp, Log, Skew},
utils, Engine, GfElement, ShardsRefMut, GF_MODULUS,
};
// ======================================================================
// Naive - PUBLIC
/// Simple reference implementation of [`Engine`].
///
/// - [`Naive`] is meant for those who want to study
/// the source code to understand [`Engine`].
/// - [`Naive`] also includes some debug assertions
/// which are not present in other implementations.
#[derive(Clone, Copy)]
pub struct Naive {
exp: &'static Exp,
log: &'static Log,
skew: &'static Skew,
}
impl Naive {
/// Creates new [`Naive`], initializing all [tables]
/// needed for encoding or decoding.
///
/// Currently only difference between encoding/decoding is
/// [`LogWalsh`] (128 kiB) which is only needed for decoding.
///
/// [`LogWalsh`]: crate::engine::tables::LogWalsh
pub fn new() -> Self {
let exp_log = &*tables::EXP_LOG;
let skew = &*tables::SKEW;
Self {
exp: &exp_log.exp,
log: &exp_log.log,
skew,
}
}
}
impl Engine for Naive {
fn fft(
&self,
data: &mut ShardsRefMut,
pos: usize,
size: usize,
truncated_size: usize,
skew_delta: usize,
) {
debug_assert!(size.is_power_of_two());
debug_assert!(truncated_size <= size);
let mut dist = size / 2;
while dist > 0 {
let mut r = 0;
while r < truncated_size {
let log_m = self.skew[r + dist + skew_delta - 1];
for i in r..r + dist {
let (a, b) = data.dist2_mut(pos + i, dist);
// FFT BUTTERFLY
if log_m != GF_MODULUS {
self.mul_add(a, b, log_m);
}
utils::xor(b, a);
}
r += dist * 2;
}
dist /= 2;
}
}
fn ifft(
&self,
data: &mut ShardsRefMut,
pos: usize,
size: usize,
truncated_size: usize,
skew_delta: usize,
) {
debug_assert!(size.is_power_of_two());
debug_assert!(truncated_size <= size);
let mut dist = 1;
while dist < size {
let mut r = 0;
while r < truncated_size {
let log_m = self.skew[r + dist + skew_delta - 1];
for i in r..r + dist {
let (a, b) = data.dist2_mut(pos + i, dist);
// IFFT BUTTERFLY
utils::xor(b, a);
if log_m != GF_MODULUS {
self.mul_add(a, b, log_m);
}
}
r += dist * 2;
}
dist *= 2;
}
}
fn mul(&self, x: &mut [[u8; 64]], log_m: GfElement) {
for chunk in x.iter_mut() {
for i in 0..32 {
let lo = GfElement::from(chunk[i]);
let hi = GfElement::from(chunk[i + 32]);
let prod = tables::mul(lo | (hi << 8), log_m, self.exp, self.log);
chunk[i] = prod as u8;
chunk[i + 32] = (prod >> 8) as u8;
}
}
}
}
// ======================================================================
// Naive - IMPL Default
impl Default for Naive {
fn default() -> Self {
Self::new()
}
}
// ======================================================================
// Naive - PRIVATE
impl Naive {
/// `x[] ^= y[] * log_m`
fn mul_add(&self, x: &mut [[u8; 64]], y: &[[u8; 64]], log_m: GfElement) {
debug_assert_eq!(x.len(), y.len());
for (x_chunk, y_chunk) in std::iter::zip(x.iter_mut(), y.iter()) {
for i in 0..32 {
let lo = GfElement::from(y_chunk[i]);
let hi = GfElement::from(y_chunk[i + 32]);
let prod = tables::mul(lo | (hi << 8), log_m, self.exp, self.log);
x_chunk[i] ^= prod as u8;
x_chunk[i + 32] ^= (prod >> 8) as u8;
}
}
}
}
// ======================================================================
// TESTS
// Engines are tested indirectly via roundtrip tests of HighRate and LowRate.