Skip to content

Commit

Permalink
apply cargo format (#32)
Browse files Browse the repository at this point in the history
* cargo format

Signed-off-by: alindima <[email protected]>

* remove unused file

---------

Signed-off-by: alindima <[email protected]>
  • Loading branch information
alindima authored Dec 12, 2023
1 parent fabd4c2 commit 15e129e
Show file tree
Hide file tree
Showing 7 changed files with 144 additions and 534 deletions.
2 changes: 1 addition & 1 deletion reed-solomon-benches/benches/criterion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ pub mod parameterized {
assert!(n > k);
#[cfg(feature = "avx")]
{
use reed_solomon_novelpoly::f2e16::{Additive8x};
use reed_solomon_novelpoly::f2e16::Additive8x;
assert_eq!(n % Additive8x::LANE, 0);
assert_eq!(k % Additive8x::LANE, 0);
}
Expand Down
8 changes: 6 additions & 2 deletions reed-solomon-novelpoly/inc_gen_field_tables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,12 @@ where
W: io::Write,
T: fmt::Debug,
{
writeln!(w, r###"#[allow(unused)]
pub(crate) static {}: {} = {:#?};"###, name, type_name, value)
writeln!(
w,
r###"#[allow(unused)]
pub(crate) static {}: {} = {:#?};"###,
name, type_name, value
)
}

/// Compute tables determined solely by the field, which never depend
Expand Down
373 changes: 0 additions & 373 deletions reed-solomon-novelpoly/src/field/gf16_tables.rs

This file was deleted.

106 changes: 47 additions & 59 deletions reed-solomon-novelpoly/src/field/inc_afft.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@

use static_init::dynamic;

#[dynamic(0)]
pub static AFFT: AdditiveFFT = AdditiveFFT::initalize();


/// Additive FFT and inverse in the "novel polynomial basis"
#[allow(non_snake_case)]
pub struct AdditiveFFT {
Expand Down Expand Up @@ -72,15 +70,13 @@ fn b_is_one() {
for i in (0..FIELD_SIZE).into_iter().step_by(256) {
let b = B[i >> 1];
if old_b != Some(b) {
test_b( Multiplier(ONEMASK) - b );
test_b( b );
test_b(Multiplier(ONEMASK) - b);
test_b(b);
old_b = Some(b);
}
}
}



// We want the low rate scheme given in
// https://www.citi.sinica.edu.tw/papers/whc/5524-F.pdf
// and https://github.com/catid/leopard/blob/master/docs/LowRateDecoder.pdf
Expand All @@ -91,45 +87,43 @@ fn b_is_one() {

/// Inverse additive FFT in the "novel polynomial basis"
pub fn inverse_afft(data: &mut [Additive], size: usize, index: usize) {
unsafe { &AFFT }.inverse_afft(data,size,index)
unsafe { &AFFT }.inverse_afft(data, size, index)
}

#[cfg(all(target_feature = "avx", feature = "avx"))]
pub fn inverse_afft_faster8(data: &mut [Additive], size: usize, index: usize) {
unsafe { &AFFT }.inverse_afft_faster8(data,size,index)
unsafe { &AFFT }.inverse_afft_faster8(data, size, index)
}

/// Additive FFT in the "novel polynomial basis"
pub fn afft(data: &mut [Additive], size: usize, index: usize) {
unsafe { &AFFT }.afft(data,size,index)
unsafe { &AFFT }.afft(data, size, index)
}

#[cfg(all(target_feature = "avx", feature = "avx"))]
/// Additive FFT in the "novel polynomial basis"
pub fn afft_faster8(data: &mut [Additive], size: usize, index: usize) {
unsafe { &AFFT }.afft_faster8(data,size,index)
unsafe { &AFFT }.afft_faster8(data, size, index)
}


impl AdditiveFFT {

/// `data[i + depart_no] ^= data[i];`
#[cfg(all(target_feature = "avx", feature = "avx"))]
#[inline(always)]
fn butterfly_down(data: &mut [Additive], i_8x: usize, depart_no_8x: usize) {
let rhs = Additive8x::load(&data[(i_8x * Additive8x::LANE) .. ][.. Additive8x::LANE]);
let dest = &mut data[((i_8x + depart_no_8x) * Additive8x::LANE) .. ][.. Additive8x::LANE];
let rhs = Additive8x::load(&data[(i_8x * Additive8x::LANE)..][..Additive8x::LANE]);
let dest = &mut data[((i_8x + depart_no_8x) * Additive8x::LANE)..][..Additive8x::LANE];
let mut lhs = Additive8x::load(dest);
lhs ^= rhs;
lhs.copy_to_slice(dest);
}

// `data[i] ^= data[i + depart_no].mul(skew)`;
#[cfg(all(target_feature = "avx", feature = "avx"))]
#[inline(always)]
fn butterfly_up(data: &mut [Additive], i_8x: usize, depart_no_8x: usize, skew: Multiplier) {
let rhs = Additive8x::load(&data[((i_8x + depart_no_8x) * Additive8x::LANE) .. ][.. Additive8x::LANE]).mul(skew);
let dest = &mut data[(i_8x * Additive8x::LANE) .. ][.. Additive8x::LANE];
let rhs = Additive8x::load(&data[((i_8x + depart_no_8x) * Additive8x::LANE)..][..Additive8x::LANE]).mul(skew);
let dest = &mut data[(i_8x * Additive8x::LANE)..][..Additive8x::LANE];
let mut lhs = Additive8x::load(dest);
lhs ^= rhs;
lhs.copy_to_slice(dest);
Expand All @@ -148,7 +142,6 @@ impl AdditiveFFT {
// and progress through FIELD_BITS-1 steps, obtaining \Psi_\beta(0,0).
let mut depart_no = 1_usize;
while depart_no < size {

// if depart_no >= 8 {
// println!("\n\n\nplain/Round depart_no={depart_no}");
// dbg!(&data);
Expand All @@ -172,9 +165,9 @@ impl AdditiveFFT {
// adding depart_no acts like the r+2^i superscript.

// if depart_no >= 8 && false{
// data[i + depart_no] ^= dbg!(data[dbg!(i)]);
// data[i + depart_no] ^= dbg!(data[dbg!(i)]);
// } else {
data[i + depart_no] ^= data[i];
data[i + depart_no] ^= data[i];
// }
}

Expand All @@ -198,7 +191,7 @@ impl AdditiveFFT {
// if depart_no >= 8 && false{
// data[i] ^= dbg!(dbg!(data[dbg!(i + depart_no)]).mul(skew));
// } else {
data[i] ^= data[i + depart_no].mul(skew);
data[i] ^= data[i + depart_no].mul(skew);
// }
}
}
Expand All @@ -210,11 +203,9 @@ impl AdditiveFFT {
// Increment by double depart_no in agreement with
// our updating 2*depart_no elements at this depth.
j += depart_no << 1;

}
depart_no <<= 1;
}

}

/// Inverse additive FFT in the "novel polynomial basis", but do 8 at once using available vector units
Expand All @@ -228,24 +219,20 @@ impl AdditiveFFT {
data[i + depart_no] ^= data[i];
}

let skew =
self.skews[j + index - 1]
;
let skew = self.skews[j + index - 1];
if skew.0 != ONEMASK {
for i in (j - depart_no)..j {
data[i] ^= data[i + depart_no].mul(skew);
}
}

j += depart_no << 1;

}
depart_no <<= 1;
}

assert!(depart_no >= Additive8x::LANE);


while depart_no < size {
let mut j = depart_no;
// println!("\n\n\nfaster8/Round depart_no={depart_no}");
Expand Down Expand Up @@ -360,7 +347,7 @@ impl AdditiveFFT {
}
depart_no >>= 1;
}

assert!(depart_no < Additive8x::LANE);

while depart_no > 0 {
Expand Down Expand Up @@ -418,7 +405,7 @@ impl AdditiveFFT {
// TODO: But why?
//
// let idx = mul_table(base[m], LOG_TABLE[(base[m] ^ 1_u16) as usize]);
let idx = Additive(base[m]).mul( Additive(base[m] ^ 1).to_multiplier() );
let idx = Additive(base[m]).mul(Additive(base[m] ^ 1).to_multiplier());
// WTF?!?
// base[m] = ONEMASK - LOG_TABLE[idx as usize];
base[m] = ONEMASK - idx.to_multiplier().0;
Expand Down Expand Up @@ -455,38 +442,34 @@ impl AdditiveFFT {
// TODO: How does this alter base?
base[0] = ONEMASK - base[0];
for i in 1..(FIELD_BITS - 1) {
base[i] = ( (
(ONEMASK as Wide) - (base[i] as Wide) + (base[i - 1] as Wide)
) % (ONEMASK as Wide) ) as Elt;
base[i] =
(((ONEMASK as Wide) - (base[i] as Wide) + (base[i - 1] as Wide)) % (ONEMASK as Wide)) as Elt;
}

// TODO: What is B anyways?
B[0] = Multiplier(0);
for i in 0..(FIELD_BITS - 1) {
let depart = 1 << i;
for j in 0..depart {
B[j + depart] = Multiplier( ((
B[j].to_wide() + (base[i] as Wide)
) % (ONEMASK as Wide)) as Elt);
B[j + depart] = Multiplier(((B[j].to_wide() + (base[i] as Wide)) % (ONEMASK as Wide)) as Elt);
}
}

B
}
},
}
}

}

#[cfg(any(feature = "mock", test))]
pub mod test_utils {
use super::*;
use rand::{Rng,SeedableRng};
use rand::{Rng, SeedableRng};

pub fn gen_plain<R: Rng + SeedableRng<Seed = [u8; 32]>>(size: usize) -> Vec<Additive> {
let rng = <R as SeedableRng>::from_seed(reed_solomon_tester::SMALL_RNG_SEED);
let dist = rand::distributions::Uniform::new_inclusive(Elt::MIN, Elt::MAX);

Vec::from_iter(rng.sample_iter::<Elt, _>(dist).take(size).map(Additive))
}

Expand All @@ -509,7 +492,6 @@ pub mod test_utils {

assert!(plain.eq(faster8));
}

}

#[cfg(test)]
Expand All @@ -530,11 +512,13 @@ mod afft_tests {
let mut data_faster8 = gen_faster8::<SmallRng>(size);
println!(">>>>");
unsafe { &AFFT }.afft(&mut data_plain, size, index);
println!(r#"
println!(
r#"
>>>>
"#);
"#
);
unsafe { &AFFT }.afft_faster8(&mut data_faster8, size, index);
println!(">>>>");
assert_plain_eq_faster8(data_plain, data_faster8);
Expand All @@ -549,11 +533,13 @@ mod afft_tests {
let mut data_faster8 = gen_faster8::<SmallRng>(size);
println!(">>>>");
unsafe { &AFFT }.afft(&mut data_plain, size, index);
println!(r#"
println!(
r#"
>>>>
"#);
"#
);
unsafe { &AFFT }.afft_faster8(&mut data_faster8, size, index);
println!(">>>>");
assert_plain_eq_faster8(data_plain, data_faster8);
Expand All @@ -568,16 +554,18 @@ mod afft_tests {
let mut data_plain = vec![Additive::zero(); size];
data_plain[0] = Additive((0x1234 & ONEMASK as u16) as Elt);
let mut data_faster8 = gen_faster8_from_plain(&data_plain);

assert_plain_eq_faster8(&data_plain, &data_faster8);

println!(">>>>");
unsafe { &AFFT }.afft(&mut data_plain, size, index);
println!(r#"
println!(
r#"
>>>>
"#);
"#
);
unsafe { &AFFT }.afft_faster8(&mut data_faster8, size, index);
println!(">>>>");
assert_plain_eq_faster8(data_plain, data_faster8);
Expand All @@ -594,11 +582,13 @@ mod afft_tests {

println!(">>>>");
unsafe { &AFFT }.inverse_afft(&mut data_plain, size, index);
println!(r#"
println!(
r#"
>>>>
"#);
"#
);
unsafe { &AFFT }.inverse_afft_faster8(&mut data_faster8, size, index);
println!(">>>>");
assert_plain_eq_faster8(data_plain, data_faster8);
Expand All @@ -613,26 +603,25 @@ mod afft_tests {
let mut data_faster8 = gen_faster8::<SmallRng>(size);
println!(">>>>");
unsafe { &AFFT }.inverse_afft(&mut data_plain, size, index);
println!(r#"
println!(
r#"
>>>>
"#);
"#
);
unsafe { &AFFT }.inverse_afft_faster8(&mut data_faster8, size, index);
println!(">>>>");
assert_plain_eq_faster8(data_plain, data_faster8);
}

}


#[cfg(b_is_not_one)]
use super::*;

#[cfg(b_is_not_one)]
#[test]
fn b_is_one() {

// This test ensure that b can be safely bypassed in tweaked_formal_derivative
let B = unsafe { &AFFT.B };
fn test_b(b: Multiplier) {
Expand All @@ -647,11 +636,10 @@ mod afft_tests {
for i in (0..FIELD_SIZE).into_iter().step_by(256) {
let b = B[i >> 1];
if old_b != Some(b) {
test_b( Multiplier(ONEMASK) - b );
test_b( b );
test_b(Multiplier(ONEMASK) - b);
test_b(b);
old_b = Some(b);
}
}
}

}
Loading

0 comments on commit 15e129e

Please sign in to comment.