Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions gf256-macros/src/lfsr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use std::collections::HashMap;
use quote::quote;
use std::iter::FromIterator;
use std::cmp::max;
use std::convert::TryFrom;
use crate::common::*;

// template files are relative to the current file
Expand Down Expand Up @@ -82,7 +81,7 @@ pub fn lfsr(
// default to 1 less than the width of the given polynomial, this
// is the only width that would really work
let polynomial = args.polynomial.0;
(128-usize::try_from(polynomial.leading_zeros()).unwrap()) - 1
(128-polynomial.leading_zeros()) - 1
};

// decide between div/rem modes
Expand Down Expand Up @@ -244,7 +243,7 @@ pub fn lfsr(
Literal::u128_unsuffixed(args.polynomial.0.reverse_bits() >> args.polynomial.0.leading_zeros())
)),
("__width".to_owned(), TokenTree::Literal(
Literal::usize_unsuffixed(width)
Literal::u32_unsuffixed(width)
)),
("__nonzeros".to_owned(), TokenTree::Literal(
Literal::u128_unsuffixed((1u128 << width) - 1)
Expand Down
1 change: 0 additions & 1 deletion src/crc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,6 @@
/// # }
/// ```
///

pub use gf256_macros::crc;


Expand Down
80 changes: 40 additions & 40 deletions templates/crc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,49 +36,49 @@ pub fn __crc(data: &[u8], crc: __u) -> __u {

cfg_if! {
if #[cfg(__if(__reflected))] {
crc = crc.reverse_bits() >> (8*size_of::<__u>()-__width);
crc = crc.reverse_bits() >> (__u::BITS-__width);
}
}

crc = crc << 8*size_of::<__u>()-__width;
crc <<= __u::BITS-__width;

// iterate over words
let mut words = data.chunks_exact(size_of::<__u>());
for word in &mut words {
let word = <[u8; size_of::<__u>()]>::try_from(word).unwrap();
cfg_if! {
if #[cfg(__if(__reflected))] {
crc = crc + __p::from_le_bytes(word).reverse_bits();
crc += __p::from_le_bytes(word).reverse_bits();
} else {
crc = crc + __p::from_be_bytes(word);
crc += __p::from_be_bytes(word);
}
}
crc = __p::try_from(
(__p2::from(crc) << 8*size_of::<__u>()) % __p2(__polynomial << (8*size_of::<__u>()-__width))
(__p2::from(crc) << __u::BITS) % __p2(__polynomial << (__u::BITS-__width))
).unwrap();
}

// handle remainder
for b in words.remainder() {
cfg_if! {
if #[cfg(__if(__reflected))] {
crc = crc + (__p::from(b.reverse_bits()) << (8*size_of::<__u>()-8));
crc += (__p::from(b.reverse_bits()) << (__u::BITS-8));
} else {
crc = crc + (__p::from(*b) << (8*size_of::<__u>()-8));
crc += (__p::from(*b) << (__u::BITS-8));
}
}
crc = __p::try_from(
(__p2::from(crc) << 8) % __p2(__polynomial << (8*size_of::<__u>()-__width))
(__p2::from(crc) << 8) % __p2(__polynomial << (__u::BITS-__width))
).unwrap();
}

// our division is always 8-bit aligned, so we need to do some
// finagling if our crc is not 8-bit aligned
crc = crc >> 8*size_of::<__u>()-__width;
crc >>= __u::BITS-__width;

cfg_if! {
if #[cfg(__if(__reflected))] {
crc = crc.reverse_bits() >> (8*size_of::<__u>()-__width);
crc = crc.reverse_bits() >> (__u::BITS-__width);
}
}

Expand All @@ -90,15 +90,15 @@ pub fn __crc(data: &[u8], crc: __u) -> __u {
while i < table.len() {
cfg_if! {
if #[cfg(__if(__reflected))] {
let x = ((i as u8).reverse_bits() as __u) << (8*size_of::<__u>()-8);
let x = ((i as u8).reverse_bits() as __u) << (__u::BITS-8);
let x = __p2((x as __u2) << 8)
.naive_rem(__p2(__polynomial << (8*size_of::<__u>()-__width))).0 as __u;
.naive_rem(__p2(__polynomial << (__u::BITS-__width))).0 as __u;
table[i] = x.reverse_bits();
i += 1;
} else {
let x = (i as __u) << (8*size_of::<__u>()-8);
let x = (i as __u) << (__u::BITS-8);
let x = __p2((x as __u2) << 8)
.naive_rem(__p2(__polynomial << (8*size_of::<__u>()-__width))).0 as __u;
.naive_rem(__p2(__polynomial << (__u::BITS-__width))).0 as __u;
table[i] = x;
i += 1;
}
Expand All @@ -111,7 +111,7 @@ pub fn __crc(data: &[u8], crc: __u) -> __u {
if #[cfg(__if(__reflected))] {
let mut crc = crc ^ __xor;
} else {
let mut crc = (crc ^ __xor) << (8*size_of::<__u>()-__width);
let mut crc = (crc ^ __xor) << (__u::BITS-__width);
}
}

Expand All @@ -122,7 +122,7 @@ pub fn __crc(data: &[u8], crc: __u) -> __u {
} else if #[cfg(__if(__reflected))] {
crc = (crc >> 8) ^ CRC_TABLE[usize::from((crc as u8) ^ b)];
} else {
crc = (crc << 8) ^ CRC_TABLE[usize::from(((crc >> (8*size_of::<__u>()-8)) as u8) ^ b)];
crc = (crc << 8) ^ CRC_TABLE[usize::from(((crc >> (__u::BITS-8)) as u8) ^ b)];
}
}
}
Expand All @@ -131,9 +131,9 @@ pub fn __crc(data: &[u8], crc: __u) -> __u {
// finagling if our crc is not 8-bit aligned
cfg_if! {
if #[cfg(__if(__reflected))] {
crc = crc & __nonzeros;
crc &= __nonzeros;
} else {
crc = crc >> (8*size_of::<__u>()-__width);
crc >>= (__u::BITS-__width);
}
}

Expand All @@ -145,15 +145,15 @@ pub fn __crc(data: &[u8], crc: __u) -> __u {
while i < table.len() {
cfg_if! {
if #[cfg(__if(__reflected))] {
let x = ((i as u8).reverse_bits() as __u) << (8*size_of::<__u>()-8);
let x = ((i as u8).reverse_bits() as __u) << (__u::BITS-8);
let x = __p2((x as __u2) << 4)
.naive_rem(__p2(__polynomial << (8*size_of::<__u>()-__width))).0 as __u;
.naive_rem(__p2(__polynomial << (__u::BITS-__width))).0 as __u;
table[i] = x.reverse_bits();
i += 1;
} else {
let x = (i as __u) << (8*size_of::<__u>()-4);
let x = (i as __u) << (__u::BITS-4);
let x = __p2((x as __u2) << 4)
.naive_rem(__p2(__polynomial << (8*size_of::<__u>()-__width))).0 as __u;
.naive_rem(__p2(__polynomial << (__u::BITS-__width))).0 as __u;
table[i] = x;
i += 1;
}
Expand All @@ -166,7 +166,7 @@ pub fn __crc(data: &[u8], crc: __u) -> __u {
if #[cfg(__if(__reflected))] {
let mut crc = crc ^ __xor;
} else {
let mut crc = (crc ^ __xor) << (8*size_of::<__u>()-__width);
let mut crc = (crc ^ __xor) << (__u::BITS-__width);
}
}

Expand All @@ -176,8 +176,8 @@ pub fn __crc(data: &[u8], crc: __u) -> __u {
crc = (crc >> 4) ^ CRC_TABLE[usize::from((crc as u8) ^ (b >> 0)) & 0xf];
crc = (crc >> 4) ^ CRC_TABLE[usize::from((crc as u8) ^ (b >> 4)) & 0xf];
} else {
crc = (crc << 4) ^ CRC_TABLE[usize::from(((crc >> (8*size_of::<__u>()-4)) as u8) ^ (b >> 4)) & 0xf];
crc = (crc << 4) ^ CRC_TABLE[usize::from(((crc >> (8*size_of::<__u>()-4)) as u8) ^ (b >> 0)) & 0xf];
crc = (crc << 4) ^ CRC_TABLE[usize::from(((crc >> (__u::BITS-4)) as u8) ^ (b >> 4)) & 0xf];
crc = (crc << 4) ^ CRC_TABLE[usize::from(((crc >> (__u::BITS-4)) as u8) ^ (b >> 0)) & 0xf];
}
}
}
Expand All @@ -186,18 +186,18 @@ pub fn __crc(data: &[u8], crc: __u) -> __u {
// finagling if our crc is not 8-bit aligned
cfg_if! {
if #[cfg(__if(__reflected))] {
crc = crc & __nonzeros;
crc &= __nonzeros;
} else {
crc = crc >> (8*size_of::<__u>()-__width);
crc >>= (__u::BITS-__width);
}
}

crc ^ __xor
} else if #[cfg(__if(__barret))] {
const BARRET_CONSTANT: __p = {
__p(
__p2((__polynomial & __nonzeros) << ((8*size_of::<__u>()-__width) + 8*size_of::<__u>()))
.naive_div(__p2(__polynomial << (8*size_of::<__u>()-__width)))
__p2((__polynomial & __nonzeros) << ((__u::BITS-__width) + __u::BITS))
.naive_div(__p2(__polynomial << (__u::BITS-__width)))
.0 as __u
)
};
Expand All @@ -206,48 +206,48 @@ pub fn __crc(data: &[u8], crc: __u) -> __u {

cfg_if! {
if #[cfg(__if(__reflected))] {
crc = crc.reverse_bits() >> (8*size_of::<__u>()-__width);
crc = crc.reverse_bits() >> (__u::BITS-__width);
}
}

crc = crc << 8*size_of::<__u>()-__width;
crc <<= __u::BITS-__width;

// iterate over words
let mut words = data.chunks_exact(size_of::<__u>());
for word in &mut words {
let word = <[u8; size_of::<__u>()]>::try_from(word).unwrap();
cfg_if! {
if #[cfg(__if(__reflected))] {
crc = crc + __p::from_le_bytes(word).reverse_bits();
crc += __p::from_le_bytes(word).reverse_bits();
} else {
crc = crc + __p::from_be_bytes(word);
crc += __p::from_be_bytes(word);
}
}
crc = (crc.widening_mul(BARRET_CONSTANT).1 + crc)
.wrapping_mul(__p((__polynomial & __nonzeros) << (8*size_of::<__u>()-__width)));
.wrapping_mul(__p((__polynomial & __nonzeros) << (__u::BITS-__width)));
}

// handle remainder
for b in words.remainder() {
cfg_if! {
if #[cfg(__if(__reflected))] {
crc = crc + (__p::from(b.reverse_bits()) << (8*size_of::<__u>()-8));
crc += (__p::from(b.reverse_bits()) << (__u::BITS-8));
} else {
crc = crc + (__p::from(*b) << (8*size_of::<__u>()-8));
crc += (__p::from(*b) << (__u::BITS-8));
}
}
crc = (crc << 8)
+ ((crc >> (8*size_of::<__u>()-8)).widening_mul(BARRET_CONSTANT).1 + (crc >> (8*size_of::<__u>()-8)))
.wrapping_mul(__p((__polynomial & __nonzeros) << (8*size_of::<__u>()-__width)));
+ ((crc >> (__u::BITS-8)).widening_mul(BARRET_CONSTANT).1 + (crc >> (__u::BITS-8)))
.wrapping_mul(__p((__polynomial & __nonzeros) << (__u::BITS-__width)));
}

// our division is always 8-bit aligned, so we need to do some
// finagling if our crc is not 8-bit aligned
crc = crc >> (8*size_of::<__u>()-__width);
crc >>= (__u::BITS-__width);

cfg_if! {
if #[cfg(__if(__reflected))] {
crc = crc.reverse_bits() >> (8*size_of::<__u>()-__width);
crc = crc.reverse_bits() >> (__u::BITS-__width);
}
}

Expand Down
40 changes: 20 additions & 20 deletions templates/gf.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
///! Template for polynomial types
// Template for polynomial types

use core::ops::*;
use core::iter::*;
Expand Down Expand Up @@ -90,8 +90,8 @@ impl __gf {
let mut i = 0;
while i < rem_table.len() {
rem_table[i] = __p(
__p2((i as __u2) << 8*size_of::<__u>())
.naive_rem(__p2(__polynomial << (8*size_of::<__u>()-__width)))
__p2((i as __u2) << __u::BITS)
.naive_rem(__p2(__polynomial << (__u::BITS-__width)))
.0 as __u
);
i += 1;
Expand All @@ -109,8 +109,8 @@ impl __gf {
let mut i = 0;
while i < rem_table.len() {
rem_table[i] = __p(
__p2((i as __u2) << 8*size_of::<__u>())
.naive_rem(__p2(__polynomial << (8*size_of::<__u>()-__width)))
__p2((i as __u2) << __u::BITS)
.naive_rem(__p2(__polynomial << (__u::BITS-__width)))
.0 as __u
);
i += 1;
Expand Down Expand Up @@ -140,8 +140,8 @@ impl __gf {
// leaving 2 xmuls and 2 xors.
//
__p(
__p2((__polynomial & __nonzeros) << ((8*size_of::<__u>()-__width) + 8*size_of::<__u>()))
.naive_div(__p2(__polynomial << (8*size_of::<__u>()-__width)))
__p2((__polynomial & __nonzeros) << ((__u::BITS-__width) + __u::BITS))
.naive_div(__p2(__polynomial << (__u::BITS-__width)))
.0 as __u
)
};
Expand Down Expand Up @@ -459,7 +459,7 @@ impl __gf {
}
} else if #[cfg(__if(__rem_table))] {
// multiplication with a per-byte remainder table
let (mut lo, mut hi) = __p(self.0 << (8*size_of::<__u>()-__width))
let (mut lo, mut hi) = __p(self.0 << (__u::BITS-__width))
.widening_mul(__p(other.0));

let mut x = __p(0);
Expand All @@ -470,25 +470,25 @@ impl __gf {
x.0 ^ b)) };
} else {
x = (x << 8) ^ unsafe { *Self::REM_TABLE.get_unchecked(usize::from(
((x >> (8*size_of::<__u>()-8)).0 as u8) ^ b)) };
((x >> (__u::BITS-8)).0 as u8) ^ b)) };
}
}
}

__gf((x + lo).0 >> (8*size_of::<__u>()-__width))
__gf((x + lo).0 >> (__u::BITS-__width))
} else if #[cfg(__if(__small_rem_table))] {
// multiplication with a per-nibble remainder table
let (mut lo, mut hi) = __p(self.0 << (8*size_of::<__u>()-__width)).widening_mul(__p(other.0));
let (mut lo, mut hi) = __p(self.0 << (__u::BITS-__width)).widening_mul(__p(other.0));

let mut x = __p(0);
for b in hi.to_be_bytes() {
x = (x << 4) ^ unsafe { *Self::REM_TABLE.get_unchecked(usize::from(
(((x >> (8*size_of::<__u>()-4)).0 as u8) ^ (b >> 4)) & 0xf)) };
(((x >> (__u::BITS-4)).0 as u8) ^ (b >> 4)) & 0xf)) };
x = (x << 4) ^ unsafe { *Self::REM_TABLE.get_unchecked(usize::from(
(((x >> (8*size_of::<__u>()-4)).0 as u8) ^ (b >> 0)) & 0xf)) };
(((x >> (__u::BITS-4)).0 as u8) ^ (b >> 0)) & 0xf)) };
}

__gf((x + lo).0 >> (8*size_of::<__u>()-__width))
__gf((x + lo).0 >> (__u::BITS-__width))
} else if #[cfg(__if(__barret))] {
// multiplication using Barret reduction
//
Expand All @@ -497,11 +497,11 @@ impl __gf {
// useful here if we have hardware xmul instructions, though
// it may be more expensive if xmul is naive.
//
let (lo, hi) = __p(self.0 << (8*size_of::<__u>()-__width))
let (lo, hi) = __p(self.0 << (__u::BITS-__width))
.widening_mul(__p(other.0));
let x = lo + (hi.widening_mul(Self::BARRET_CONSTANT).1 + hi)
.wrapping_mul(__p((__polynomial & __nonzeros) << (8*size_of::<__u>()-__width)));
__gf(x.0 >> (8*size_of::<__u>()-__width))
.wrapping_mul(__p((__polynomial & __nonzeros) << (__u::BITS-__width)));
__gf(x.0 >> (__u::BITS-__width))
} else {
// fallback to naive multiplication
//
Expand All @@ -510,7 +510,7 @@ impl __gf {
// accelerated
//
let (lo, hi) = __p(self.0).widening_mul(__p(other.0));
let x = __p2(((hi.0 as __u2) << (8*size_of::<__u>())) | (lo.0 as __u2))
let x = __p2(((hi.0 as __u2) << __u::BITS) | (lo.0 as __u2))
% __p2(__polynomial);
__gf(x.0 as __u)
}
Expand Down Expand Up @@ -3879,8 +3879,8 @@ impl FromStr for __gf {
/// hexadecimal strings starting with `0x`. If you need a different radix
/// there is [`from_str_radix`](#method.from_str_radix).
fn from_str(s: &str) -> Result<__gf, ParseIntError> {
if s.starts_with("0x") {
Ok(__gf(__u::from_str_radix(&s[2..], 16)?))
if let Some(s) = s.strip_prefix("0x") {
Ok(__gf(__u::from_str_radix(s, 16)?))
} else {
"".parse::<__u>()?;
unreachable!()
Expand Down
Loading