diff --git a/gf256-macros/src/lfsr.rs b/gf256-macros/src/lfsr.rs index f3be726..f9f678c 100644 --- a/gf256-macros/src/lfsr.rs +++ b/gf256-macros/src/lfsr.rs @@ -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 @@ -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 @@ -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) diff --git a/src/crc.rs b/src/crc.rs index 60a0f4d..a18c3f4 100644 --- a/src/crc.rs +++ b/src/crc.rs @@ -338,7 +338,6 @@ /// # } /// ``` /// - pub use gf256_macros::crc; diff --git a/templates/crc.rs b/templates/crc.rs index 4fcc2d5..3e85ec4 100644 --- a/templates/crc.rs +++ b/templates/crc.rs @@ -36,11 +36,11 @@ 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>()); @@ -48,13 +48,13 @@ pub fn __crc(data: &[u8], crc: __u) -> __u { 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(); } @@ -62,23 +62,23 @@ pub fn __crc(data: &[u8], crc: __u) -> __u { 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); } } @@ -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; } @@ -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); } } @@ -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)]; } } } @@ -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); } } @@ -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; } @@ -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); } } @@ -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]; } } } @@ -186,9 +186,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); } } @@ -196,8 +196,8 @@ pub fn __crc(data: &[u8], crc: __u) -> __u { } 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 ) }; @@ -206,11 +206,11 @@ 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>()); @@ -218,36 +218,36 @@ pub fn __crc(data: &[u8], crc: __u) -> __u { 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); } } diff --git a/templates/gf.rs b/templates/gf.rs index b567b26..1eb3682 100644 --- a/templates/gf.rs +++ b/templates/gf.rs @@ -1,4 +1,4 @@ -///! Template for polynomial types +// Template for polynomial types use core::ops::*; use core::iter::*; @@ -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; @@ -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; @@ -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 ) }; @@ -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); @@ -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 // @@ -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 // @@ -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) } @@ -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!() diff --git a/templates/lfsr.rs b/templates/lfsr.rs index 141e25a..22eb07b 100644 --- a/templates/lfsr.rs +++ b/templates/lfsr.rs @@ -50,8 +50,8 @@ impl __lfsr { let mut i = 0; // TODO make this consistent in both gf and crc? while i < div_table.len() { - div_table[i] = __p2((i as __u2) << (8*size_of::<__u>())) - .naive_div(__p2(__polynomial << (8*size_of::<__u>()-__width))) + div_table[i] = __p2((i as __u2) << (__u::BITS)) + .naive_div(__p2(__polynomial << (__u::BITS-__width))) .0 as u8; i += 1; } @@ -62,8 +62,8 @@ impl __lfsr { let mut rem_table = [0; 256]; let mut i = 0; while i < rem_table.len() { - rem_table[i] = __p2((i as __u2) << (8*size_of::<__u>())) - .naive_rem(__p2(__polynomial << (8*size_of::<__u>()-__width))) + rem_table[i] = __p2((i as __u2) << (__u::BITS)) + .naive_rem(__p2(__polynomial << (__u::BITS-__width))) .0 as __u; i += 1; } @@ -78,13 +78,13 @@ impl __lfsr { while i < div_table.len() { cfg_if! { if #[cfg(__if(__table_barret))] { - div_table[i] = (__p2((i as __u2) << (8*size_of::<__u>())) - .naive_div(__p2(__inverse_polynomial << (8*size_of::<__u>()-__width))) + div_table[i] = (__p2((i as __u2) << (__u::BITS)) + .naive_div(__p2(__inverse_polynomial << (__u::BITS-__width))) .0 as u8) .reverse_bits(); } else { - div_table[i] = (__p2(((i as u8).reverse_bits() as __u2) << (8*size_of::<__u>())) - .naive_div(__p2(__inverse_polynomial << (8*size_of::<__u>()-__width))) + div_table[i] = (__p2(((i as u8).reverse_bits() as __u2) << (__u::BITS)) + .naive_div(__p2(__inverse_polynomial << (__u::BITS-__width))) .0 as u8) .reverse_bits(); } @@ -98,8 +98,8 @@ impl __lfsr { let mut rem_table = [0; 256]; let mut i = 0; while i < rem_table.len() { - rem_table[i] = (__p2(((i as u8).reverse_bits() as __u2) << (8*size_of::<__u>())) - .naive_rem(__p2(__inverse_polynomial << (8*size_of::<__u>()-__width))) + rem_table[i] = (__p2(((i as u8).reverse_bits() as __u2) << (__u::BITS)) + .naive_rem(__p2(__inverse_polynomial << (__u::BITS-__width))) .0 as __u) .reverse_bits(); i += 1; @@ -113,8 +113,8 @@ impl __lfsr { let mut div_table = [0; 16]; let mut i = 0; while i < div_table.len() { - div_table[i] = __p2((i as __u2) << (8*size_of::<__u>())) - .naive_div(__p2(__polynomial << (8*size_of::<__u>()-__width))) + div_table[i] = __p2((i as __u2) << (__u::BITS)) + .naive_div(__p2(__polynomial << (__u::BITS-__width))) .0 as u8; i += 1; } @@ -125,8 +125,8 @@ impl __lfsr { let mut rem_table = [0; 16]; let mut i = 0; while i < rem_table.len() { - rem_table[i] = __p2((i as __u2) << (8*size_of::<__u>())) - .naive_rem(__p2(__polynomial << (8*size_of::<__u>()-__width))) + rem_table[i] = __p2((i as __u2) << (__u::BITS)) + .naive_rem(__p2(__polynomial << (__u::BITS-__width))) .0 as __u; i += 1; } @@ -141,13 +141,13 @@ impl __lfsr { while i < div_table.len() { cfg_if! { if #[cfg(__if(__small_table_barret))] { - div_table[i] = (__p2((i as __u2) << (8*size_of::<__u>())) - .naive_div(__p2(__inverse_polynomial << (8*size_of::<__u>()-__width))) + div_table[i] = (__p2((i as __u2) << (__u::BITS)) + .naive_div(__p2(__inverse_polynomial << (__u::BITS-__width))) .0 as u8) .reverse_bits() >> 4; } else { - div_table[i] = (__p2((((i as u8).reverse_bits() >> 4) as __u2) << (8*size_of::<__u>())) - .naive_div(__p2(__inverse_polynomial << (8*size_of::<__u>()-__width))) + div_table[i] = (__p2((((i as u8).reverse_bits() >> 4) as __u2) << (__u::BITS)) + .naive_div(__p2(__inverse_polynomial << (__u::BITS-__width))) .0 as u8) .reverse_bits() >> 4; } @@ -161,8 +161,8 @@ impl __lfsr { let mut rem_table = [0; 16]; let mut i = 0; while i < rem_table.len() { - rem_table[i] = (__p2((((i as u8).reverse_bits() >> 4) as __u2) << (8*size_of::<__u>())) - .naive_rem(__p2(__inverse_polynomial << (8*size_of::<__u>()-__width))) + rem_table[i] = (__p2((((i as u8).reverse_bits() >> 4) as __u2) << (__u::BITS)) + .naive_rem(__p2(__inverse_polynomial << (__u::BITS-__width))) .0 as __u) .reverse_bits(); i += 1; @@ -174,16 +174,16 @@ impl __lfsr { #[cfg(__if(__barret || __table_barret || __small_table_barret || __barret_skip))] const BARRET_CONSTANT: __p = { __p( - __p2((__polynomial & __nonzeros) << (8*size_of::<__u>() + 8*size_of::<__u>()-__width)) - .naive_div(__p2(__polynomial << (8*size_of::<__u>()-__width))) + __p2((__polynomial & __nonzeros) << (__u::BITS + __u::BITS-__width)) + .naive_div(__p2(__polynomial << (__u::BITS-__width))) .0 as __u ) }; #[cfg(__if(__barret || __table_barret || __small_table_barret))] const INVERSE_BARRET_CONSTANT: __p = { __p( - __p2((__inverse_polynomial & __nonzeros) << (8*size_of::<__u>() + 8*size_of::<__u>()-__width)) - .naive_div(__p2(__inverse_polynomial << (8*size_of::<__u>()-__width))) + __p2((__inverse_polynomial & __nonzeros) << (__u::BITS + __u::BITS-__width)) + .naive_div(__p2(__inverse_polynomial << (__u::BITS-__width))) .0 as __u ) }; @@ -195,7 +195,7 @@ impl __lfsr { /// #[inline] pub const fn new(mut seed: __u) -> Self { - seed = seed & __nonzeros; + seed &= __nonzeros; // make sure seed does not equal zero! otherwise our rng would only // ever output zero! @@ -205,7 +205,7 @@ impl __lfsr { cfg_if! { if #[cfg(__if(__reflected))] { - seed = seed.reverse_bits() >> (8*size_of::<__u>()-__width); + seed = seed.reverse_bits() >> (__u::BITS-__width); } } @@ -226,7 +226,7 @@ impl __lfsr { #[inline] pub fn next(&mut self, bits: __u) -> __u { debug_assert!(bits <= __width); - let bits = bits as usize; + let bits = bits as u32; cfg_if! { if #[cfg(__if(__naive))] { // naive lfsr using bitshifts and xors @@ -245,94 +245,94 @@ impl __lfsr { self.0 = __nzu::try_from(x).unwrap(); } else if #[cfg(__if(__table))] { // lfsr with a per-byte division and remainder table - let mut x = __u::from(self.0) << (8*size_of::<__u>()-__width); + let mut x = __u::from(self.0) << (__u::BITS-__width); let mut q = 0; for i in (0..(bits+7)/8).rev() { let n = min(8, bits-8*i); - if n == 8*size_of::<__u>() { + if n == __u::BITS { q = __u::from(Self::DIV_TABLE[usize::try_from( - x >> (8*size_of::<__u>()-n)).unwrap()]); + x >> (__u::BITS-n)).unwrap()]); x = Self::REM_TABLE[usize::try_from( - x >> (8*size_of::<__u>()-n)).unwrap()]; + x >> (__u::BITS-n)).unwrap()]; } else { q = (q << n) | __u::from(Self::DIV_TABLE[usize::try_from( - x >> (8*size_of::<__u>()-n)).unwrap()]); + x >> (__u::BITS-n)).unwrap()]); x = (x << n) ^ Self::REM_TABLE[usize::try_from( - x >> (8*size_of::<__u>()-n)).unwrap()]; + x >> (__u::BITS-n)).unwrap()]; } } // adjust for alignment - x = x >> (8*size_of::<__u>()-__width); + x >>= (__u::BITS-__width); // update state self.0 = __nzu::try_from(x).unwrap(); } else if #[cfg(__if(__small_table))] { // lfsr with a per-nibble division and remainder table - let mut x = __u::from(self.0) << (8*size_of::<__u>()-__width); + let mut x = __u::from(self.0) << (__u::BITS-__width); let mut q = 0; for i in (0..(bits+3)/4).rev() { let n = min(4, bits-4*i); q = (q << n) | __u::from(Self::DIV_TABLE[usize::try_from( - x >> (8*size_of::<__u>()-n)).unwrap()]); + x >> (__u::BITS-n)).unwrap()]); x = (x << n) ^ Self::REM_TABLE[usize::try_from( - x >> (8*size_of::<__u>()-n)).unwrap()]; + x >> (__u::BITS-n)).unwrap()]; } // adjust for alignment - x = x >> (8*size_of::<__u>()-__width); + x >>= (__u::BITS-__width); // update state self.0 = __nzu::try_from(x).unwrap(); } else if #[cfg(__if(__barret))] { // lfsr using naive division with Barret-reduction - let x = __p2::from(__u::from(self.0)) << (bits + (8*size_of::<__u>()-__width)); - let q = x / __p2(__polynomial << (8*size_of::<__u>()-__width)); + let x = __p2::from(__u::from(self.0)) << (bits + (__u::BITS-__width)); + let q = x / __p2(__polynomial << (__u::BITS-__width)); let lo = __p::from_lossy(x); - let hi = __p::try_from(x >> (8*size_of::<__u>())).unwrap(); + let hi = __p::try_from(x >> (__u::BITS)).unwrap(); let mut x = lo + (hi.widening_mul(Self::BARRET_CONSTANT).1 + hi) - .wrapping_mul(__p((__polynomial & __nonzeros) << (8*size_of::<__u>()-__width))); + .wrapping_mul(__p((__polynomial & __nonzeros) << (__u::BITS-__width))); // adjust for alignment - x = x >> (8*size_of::<__u>()-__width); + x >>= (__u::BITS-__width); // update state self.0 = __nzu::try_from(__u::from(x)).unwrap(); let q = __u::try_from(q.0).unwrap(); } else if #[cfg(__if(__table_barret))] { // lfsr using a per-byte division table with Barret-reduction - let mut x = __p::from(__u::from(self.0)) << (8*size_of::<__u>()-__width); + let mut x = __p::from(__u::from(self.0)) << (__u::BITS-__width); let mut q = 0; for i in (0..(bits+7)/8).rev() { let n = min(8, bits-8*i); - if n == 8*size_of::<__u>() { + if n == __u::BITS { q = __u::from(Self::DIV_TABLE[usize::try_from( - x >> (8*size_of::<__u>()-n)).unwrap()]); - let hi = x >> (8*size_of::<__u>()-n); + x >> (__u::BITS-n)).unwrap()]); + let hi = x >> (__u::BITS-n); x = (hi.widening_mul(Self::BARRET_CONSTANT).1 + hi) - .wrapping_mul(__p((__polynomial & __nonzeros) << (8*size_of::<__u>()-__width))); + .wrapping_mul(__p((__polynomial & __nonzeros) << (__u::BITS-__width))); } else { q = (q << n) | __u::from(Self::DIV_TABLE[usize::try_from( - x >> (8*size_of::<__u>()-n)).unwrap()]); + x >> (__u::BITS-n)).unwrap()]); let lo = x << n; - let hi = x >> (8*size_of::<__u>()-n); + let hi = x >> (__u::BITS-n); x = lo + (hi.widening_mul(Self::BARRET_CONSTANT).1 + hi) - .wrapping_mul(__p((__polynomial & __nonzeros) << (8*size_of::<__u>()-__width))); + .wrapping_mul(__p((__polynomial & __nonzeros) << (__u::BITS-__width))); } } // adjust for alignment - x = x >> (8*size_of::<__u>()-__width); + x >>= (__u::BITS-__width); // update state self.0 = __nzu::try_from(__u::from(x)).unwrap(); } else if #[cfg(__if(__small_table_barret))] { // lfsr using a per-nibble division table with Barret-reduction - let mut x = __p::from(__u::from(self.0)) << (8*size_of::<__u>()-__width); + let mut x = __p::from(__u::from(self.0)) << (__u::BITS-__width); let mut q = 0; for i in (0..(bits+3)/4).rev() { let n = min(4, bits-4*i); q = (q << n) | __u::from(Self::DIV_TABLE[usize::try_from( - x >> (8*size_of::<__u>()-n)).unwrap()]); + x >> (__u::BITS-n)).unwrap()]); let lo = x << n; - let hi = x >> (8*size_of::<__u>()-n); + let hi = x >> (__u::BITS-n); x = lo + (hi.widening_mul(Self::BARRET_CONSTANT).1 + hi) - .wrapping_mul(__p((__polynomial & __nonzeros) << (8*size_of::<__u>()-__width))); + .wrapping_mul(__p((__polynomial & __nonzeros) << (__u::BITS-__width))); } // adjust for alignment - x = x >> (8*size_of::<__u>()-__width); + x >>= (__u::BITS-__width); // update state self.0 = __nzu::try_from(__u::from(x)).unwrap(); } @@ -340,7 +340,7 @@ impl __lfsr { cfg_if! { if #[cfg(__if(__reflected))] { - q.reverse_bits() >> (8*size_of::<__u>() - bits) + q.reverse_bits() >> (__u::BITS - bits) } else { q } @@ -362,7 +362,7 @@ impl __lfsr { #[inline] pub fn prev(&mut self, bits: __u) -> __u { debug_assert!(bits <= __width); - let bits = bits as usize; + let bits = bits as u32; cfg_if! { if #[cfg(__if(__naive))] { // naive lfsr using bitshifts and xors @@ -385,21 +385,21 @@ impl __lfsr { let mut q = 0; for i in (0..(bits+7)/8).rev() { let n = min(8, bits-8*i); - if n == 8*size_of::<__u>() { + if n == __u::BITS { q = __u::from(Self::INVERSE_DIV_TABLE[usize::try_from( x).unwrap()]); x = Self::INVERSE_REM_TABLE[usize::try_from( x).unwrap()]; } else { q = (q >> n) | (__u::from(Self::INVERSE_DIV_TABLE[usize::try_from( - (x << (8-n)) & 0xff).unwrap()]) << (8*size_of::<__u>()-8)); + (x << (8-n)) & 0xff).unwrap()]) << (__u::BITS-8)); x = (x >> n) ^ Self::INVERSE_REM_TABLE[usize::try_from( (x << (8-n)) & 0xff).unwrap()]; } } // update state self.0 = __nzu::try_from(x).unwrap(); - let q = q >> (8*size_of::<__u>() - bits); + let q = q >> (__u::BITS - bits); } else if #[cfg(__if(__small_table))] { // lfsr with a per-nibble division and remainder table let mut x = __u::from(self.0); @@ -407,54 +407,54 @@ impl __lfsr { for i in (0..(bits+3)/4).rev() { let n = min(4, bits-4*i); q = (q >> n) | (__u::from(Self::INVERSE_DIV_TABLE[usize::try_from( - (x << (4-n)) & 0xf).unwrap()]) << (8*size_of::<__u>()-4)); + (x << (4-n)) & 0xf).unwrap()]) << (__u::BITS-4)); x = (x >> n) ^ Self::INVERSE_REM_TABLE[usize::try_from( (x << (4-n)) & 0xf).unwrap()]; } // update state self.0 = __nzu::try_from(x).unwrap(); - let q = q >> (8*size_of::<__u>() - bits); + let q = q >> (__u::BITS - bits); } else if #[cfg(__if(__barret))] { // lfsr using naive division with Barret-reduction let x = __p2::from(__u::from(self.0).reverse_bits()) << bits; - let q = x / __p2(__inverse_polynomial << (8*size_of::<__u>()-__width)); + let q = x / __p2(__inverse_polynomial << (__u::BITS-__width)); let lo = __p::from_lossy(x); - let hi = __p::try_from(x >> (8*size_of::<__u>())).unwrap(); + let hi = __p::try_from(x >> (__u::BITS)).unwrap(); let mut x = lo + (hi.widening_mul(Self::INVERSE_BARRET_CONSTANT).1 + hi) - .wrapping_mul(__p((__inverse_polynomial & __nonzeros) << (8*size_of::<__u>()-__width))); + .wrapping_mul(__p((__inverse_polynomial & __nonzeros) << (__u::BITS-__width))); // adjust for alignment - x = x >> (8*size_of::<__u>()-__width); - x = x.reverse_bits() >> (8*size_of::<__u>()-__width); + x >>= (__u::BITS-__width); + x = x.reverse_bits() >> (__u::BITS-__width); // update state self.0 = __nzu::try_from(__u::from(x)).unwrap(); - let q = __u::try_from(q.0).unwrap().reverse_bits() >> (8*size_of::<__u>() - bits); + let q = __u::try_from(q.0).unwrap().reverse_bits() >> (__u::BITS - bits); } else if #[cfg(__if(__table_barret))] { // lfsr using a per-byte division table with Barret-reduction let mut x = __p::from(__u::from(self.0).reverse_bits()); let mut q = 0; for i in (0..(bits+7)/8).rev() { let n = min(8, bits-8*i); - if n == 8*size_of::<__u>() { + if n == __u::BITS { q = __u::from(Self::INVERSE_DIV_TABLE[usize::try_from( - x >> (8*size_of::<__u>()-n)).unwrap()]); - let hi = x >> (8*size_of::<__u>()-n); + x >> (__u::BITS-n)).unwrap()]); + let hi = x >> (__u::BITS-n); x = (hi.widening_mul(Self::INVERSE_BARRET_CONSTANT).1 + hi) - .wrapping_mul(__p((__inverse_polynomial & __nonzeros) << (8*size_of::<__u>()-__width))); + .wrapping_mul(__p((__inverse_polynomial & __nonzeros) << (__u::BITS-__width))); } else { q = (q >> n) | (__u::from(Self::INVERSE_DIV_TABLE[usize::try_from( - x >> (8*size_of::<__u>()-n)).unwrap()])) << (8*size_of::<__u>()-8); + x >> (__u::BITS-n)).unwrap()])) << (__u::BITS-8); let lo = x << n; - let hi = x >> (8*size_of::<__u>()-n); + let hi = x >> (__u::BITS-n); x = lo + (hi.widening_mul(Self::INVERSE_BARRET_CONSTANT).1 + hi) - .wrapping_mul(__p((__inverse_polynomial & __nonzeros) << (8*size_of::<__u>()-__width))); + .wrapping_mul(__p((__inverse_polynomial & __nonzeros) << (__u::BITS-__width))); } } // adjust for alignment - x = x >> (8*size_of::<__u>()-__width); - x = x.reverse_bits() >> (8*size_of::<__u>()-__width); + x >>= (__u::BITS-__width); + x = x.reverse_bits() >> (__u::BITS-__width); // update state self.0 = __nzu::try_from(__u::from(x)).unwrap(); - let q = q >> (8*size_of::<__u>() - bits); + let q = q >> (__u::BITS - bits); } else if #[cfg(__if(__small_table_barret))] { // lfsr using a per-nibble division table with Barret-reduction let mut x = __p::from(__u::from(self.0).reverse_bits()); @@ -462,24 +462,24 @@ impl __lfsr { for i in (0..(bits+3)/4).rev() { let n = min(4, bits-4*i); q = (q >> n) | (__u::from(Self::INVERSE_DIV_TABLE[usize::try_from( - x >> (8*size_of::<__u>()-n)).unwrap()])) << (8*size_of::<__u>()-4); + x >> (__u::BITS-n)).unwrap()])) << (__u::BITS-4); let lo = x << n; - let hi = x >> (8*size_of::<__u>()-n); + let hi = x >> (__u::BITS-n); x = lo + (hi.widening_mul(Self::INVERSE_BARRET_CONSTANT).1 + hi) - .wrapping_mul(__p((__inverse_polynomial & __nonzeros) << (8*size_of::<__u>()-__width))); + .wrapping_mul(__p((__inverse_polynomial & __nonzeros) << (__u::BITS-__width))); } // adjust for alignment - x = x >> (8*size_of::<__u>()-__width); - x = x.reverse_bits() >> (8*size_of::<__u>()-__width); + x >>= (__u::BITS-__width); + x = x.reverse_bits() >> (__u::BITS-__width); // update state self.0 = __nzu::try_from(__u::from(x)).unwrap(); - let q = q >> (8*size_of::<__u>() - bits); + let q = q >> (__u::BITS - bits); } } cfg_if! { if #[cfg(__if(__reflected))] { - q.reverse_bits() >> (8*size_of::<__u>() - bits) + q.reverse_bits() >> (__u::BITS - bits) } else { q } @@ -520,7 +520,7 @@ impl __lfsr { __p::try_from(x % __p2(__polynomial)).unwrap() } else if #[cfg(__if(__table_skip))] { // Galois-field multiplication with remainder table - let (lo, hi) = (a << (8*size_of::<__u>()-__width)) + let (lo, hi) = (a << (__u::BITS-__width)) .widening_mul(b); let mut x = 0; for b in hi.to_be_bytes() { @@ -530,30 +530,30 @@ impl __lfsr { u8::try_from(x).unwrap() ^ b)]; } else { x = (x << 8) ^ Self::REM_TABLE[usize::from( - u8::try_from(x >> (8*size_of::<__u>()-8)).unwrap() ^ b)]; + u8::try_from(x >> (__u::BITS-8)).unwrap() ^ b)]; } } } - (__p(x) + lo) >> (8*size_of::<__u>()-__width) + (__p(x) + lo) >> (__u::BITS-__width) } else if #[cfg(__if(__small_table_skip))] { // Galois-field multiplication with small remainder table - let (lo, hi) = (a << (8*size_of::<__u>()-__width)) + let (lo, hi) = (a << (__u::BITS-__width)) .widening_mul(b); let mut x = 0; for b in hi.to_be_bytes() { x = (x << 4) ^ Self::REM_TABLE[usize::from( - u8::try_from(x >> (8*size_of::<__u>()-4)).unwrap() ^ (b >> 4)) & 0xf]; + u8::try_from(x >> (__u::BITS-4)).unwrap() ^ (b >> 4)) & 0xf]; x = (x << 4) ^ Self::REM_TABLE[usize::from( - u8::try_from(x >> (8*size_of::<__u>()-4)).unwrap() ^ (b >> 0)) & 0xf]; + u8::try_from(x >> (__u::BITS-4)).unwrap() ^ (b >> 0)) & 0xf]; } - (__p(x) + lo) >> (8*size_of::<__u>()-__width) + (__p(x) + lo) >> (__u::BITS-__width) } else if #[cfg(__if(__barret_skip))] { // Galois-field multiplication with Barret-reduction - let (lo, hi) = (a << (8*size_of::<__u>()-__width)) + let (lo, hi) = (a << (__u::BITS-__width)) .widening_mul(b); let x = lo + (hi.widening_mul(Self::BARRET_CONSTANT).1 + hi) - .wrapping_mul(__p((__polynomial & __nonzeros) << (8*size_of::<__u>()-__width))); - x >> (8*size_of::<__u>()-__width) + .wrapping_mul(__p((__polynomial & __nonzeros) << (__u::BITS-__width))); + x >> (__u::BITS-__width) } } }; diff --git a/templates/p.rs b/templates/p.rs index 9ce1bd4..58dd80a 100644 --- a/templates/p.rs +++ b/templates/p.rs @@ -1,6 +1,5 @@ -///! Template for polynomial types +// Template for polynomial types -use core::mem::size_of; use core::ops::*; use core::iter::*; use core::num::TryFromIntError; @@ -3500,8 +3499,8 @@ impl FromStr for __p { /// 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<__p, ParseIntError> { - if s.starts_with("0x") { - Ok(__p(__u::from_str_radix(&s[2..], 16)?)) + if let Some(s) = s.strip_prefix("0x") { + Ok(__p(__u::from_str_radix(s, 16)?)) } else { "".parse::<__u>()?; unreachable!()