Skip to content

Commit

Permalink
Remove use of BITS associated const
Browse files Browse the repository at this point in the history
These were only introduced in Rust 1.53.0 so they would bump the MSRV
unnecessarily.
  • Loading branch information
bartelsielski authored and akhilles committed Nov 4, 2021
1 parent 4a9a15b commit f0e62a3
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 42 deletions.
8 changes: 4 additions & 4 deletions src/crc128.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ impl Crc<u128> {

const fn init(&self) -> u128 {
if self.algorithm.refin {
self.algorithm.init.reverse_bits() >> (u128::BITS as u8 - self.algorithm.width)
self.algorithm.init.reverse_bits() >> (128u8 - self.algorithm.width)
} else {
self.algorithm.init << (u128::BITS as u8 - self.algorithm.width)
self.algorithm.init << (128u8 - self.algorithm.width)
}
}

Expand All @@ -35,7 +35,7 @@ impl Crc<u128> {
}
} else {
while i < bytes.len() {
let table_index = (crc >> (u128::BITS - 8)) ^ bytes[i] as u128;
let table_index = (crc >> 120) ^ bytes[i] as u128;
crc = self.table_entry(table_index) ^ (crc << 8);
i += 1;
}
Expand All @@ -48,7 +48,7 @@ impl Crc<u128> {
crc = crc.reverse_bits();
}
if !self.algorithm.refout {
crc >>= u128::BITS as u8 - self.algorithm.width;
crc >>= 128u8 - self.algorithm.width;
}
crc ^ self.algorithm.xorout
}
Expand Down
8 changes: 4 additions & 4 deletions src/crc16.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ impl Crc<u16> {

const fn init(&self) -> u16 {
if self.algorithm.refin {
self.algorithm.init.reverse_bits() >> (u16::BITS as u8 - self.algorithm.width)
self.algorithm.init.reverse_bits() >> (16u8 - self.algorithm.width)
} else {
self.algorithm.init << (u16::BITS as u8 - self.algorithm.width)
self.algorithm.init << (16u8 - self.algorithm.width)
}
}

Expand All @@ -35,7 +35,7 @@ impl Crc<u16> {
}
} else {
while i < bytes.len() {
let table_index = (crc >> (u16::BITS - 8)) ^ bytes[i] as u16;
let table_index = (crc >> 8) ^ bytes[i] as u16;
crc = self.table_entry(table_index) ^ (crc << 8);
i += 1;
}
Expand All @@ -48,7 +48,7 @@ impl Crc<u16> {
crc = crc.reverse_bits();
}
if !self.algorithm.refout {
crc >>= u16::BITS as u8 - self.algorithm.width;
crc >>= 16u8 - self.algorithm.width;
}
crc ^ self.algorithm.xorout
}
Expand Down
8 changes: 4 additions & 4 deletions src/crc32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ impl Crc<u32> {

const fn init(&self) -> u32 {
if self.algorithm.refin {
self.algorithm.init.reverse_bits() >> (u32::BITS as u8 - self.algorithm.width)
self.algorithm.init.reverse_bits() >> (32u8 - self.algorithm.width)
} else {
self.algorithm.init << (u32::BITS as u8 - self.algorithm.width)
self.algorithm.init << (32u8 - self.algorithm.width)
}
}

Expand All @@ -35,7 +35,7 @@ impl Crc<u32> {
}
} else {
while i < bytes.len() {
let table_index = (crc >> (u32::BITS - 8)) ^ bytes[i] as u32;
let table_index = (crc >> 24) ^ bytes[i] as u32;
crc = self.table_entry(table_index) ^ (crc << 8);
i += 1;
}
Expand All @@ -48,7 +48,7 @@ impl Crc<u32> {
crc = crc.reverse_bits();
}
if !self.algorithm.refout {
crc >>= u32::BITS as u8 - self.algorithm.width;
crc >>= 32u8 - self.algorithm.width;
}
crc ^ self.algorithm.xorout
}
Expand Down
8 changes: 4 additions & 4 deletions src/crc64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ impl Crc<u64> {

const fn init(&self) -> u64 {
if self.algorithm.refin {
self.algorithm.init.reverse_bits() >> (u64::BITS as u8 - self.algorithm.width)
self.algorithm.init.reverse_bits() >> (64u8 - self.algorithm.width)
} else {
self.algorithm.init << (u64::BITS as u8 - self.algorithm.width)
self.algorithm.init << (64u8 - self.algorithm.width)
}
}

Expand All @@ -35,7 +35,7 @@ impl Crc<u64> {
}
} else {
while i < bytes.len() {
let table_index = (crc >> (u64::BITS - 8)) ^ bytes[i] as u64;
let table_index = (crc >> 56) ^ bytes[i] as u64;
crc = self.table_entry(table_index) ^ (crc << 8);
i += 1;
}
Expand All @@ -48,7 +48,7 @@ impl Crc<u64> {
crc = crc.reverse_bits();
}
if !self.algorithm.refout {
crc >>= u64::BITS as u8 - self.algorithm.width;
crc >>= 64u8 - self.algorithm.width;
}
crc ^ self.algorithm.xorout
}
Expand Down
6 changes: 3 additions & 3 deletions src/crc8.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ impl Crc<u8> {

const fn init(&self) -> u8 {
if self.algorithm.refin {
self.algorithm.init.reverse_bits() >> (u8::BITS as u8 - self.algorithm.width)
self.algorithm.init.reverse_bits() >> (8u8 - self.algorithm.width)
} else {
self.algorithm.init << (u8::BITS as u8 - self.algorithm.width)
self.algorithm.init << (8u8 - self.algorithm.width)
}
}

Expand All @@ -41,7 +41,7 @@ impl Crc<u8> {
crc = crc.reverse_bits();
}
if !self.algorithm.refout {
crc >>= u8::BITS as u8 - self.algorithm.width;
crc >>= 8u8 - self.algorithm.width;
}
crc ^ self.algorithm.xorout
}
Expand Down
20 changes: 10 additions & 10 deletions src/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ use crate::util::*;
pub(crate) const fn crc8_table(width: u8, poly: u8, reflect: bool) -> [u8; 256] {
let poly = if reflect {
let poly = poly.reverse_bits();
poly >> (u8::BITS as u8 - width)
poly >> (8u8 - width)
} else {
poly << (u8::BITS as u8 - width)
poly << (8u8 - width)
};

let mut table = [0u8; 256];
Expand All @@ -20,9 +20,9 @@ pub(crate) const fn crc8_table(width: u8, poly: u8, reflect: bool) -> [u8; 256]
pub(crate) const fn crc16_table(width: u8, poly: u16, reflect: bool) -> [u16; 256] {
let poly = if reflect {
let poly = poly.reverse_bits();
poly >> (u16::BITS as u8 - width)
poly >> (16u8 - width)
} else {
poly << (u16::BITS as u8 - width)
poly << (16u8 - width)
};

let mut table = [0u16; 256];
Expand All @@ -37,9 +37,9 @@ pub(crate) const fn crc16_table(width: u8, poly: u16, reflect: bool) -> [u16; 25
pub(crate) const fn crc32_table(width: u8, poly: u32, reflect: bool) -> [u32; 256] {
let poly = if reflect {
let poly = poly.reverse_bits();
poly >> (u32::BITS as u8 - width)
poly >> (32u8 - width)
} else {
poly << (u32::BITS as u8 - width)
poly << (32u8 - width)
};

let mut table = [0u32; 256];
Expand All @@ -54,9 +54,9 @@ pub(crate) const fn crc32_table(width: u8, poly: u32, reflect: bool) -> [u32; 25
pub(crate) const fn crc64_table(width: u8, poly: u64, reflect: bool) -> [u64; 256] {
let poly = if reflect {
let poly = poly.reverse_bits();
poly >> (u64::BITS as u8 - width)
poly >> (64u8 - width)
} else {
poly << (u64::BITS as u8 - width)
poly << (64u8 - width)
};

let mut table = [0u64; 256];
Expand All @@ -71,9 +71,9 @@ pub(crate) const fn crc64_table(width: u8, poly: u64, reflect: bool) -> [u64; 25
pub(crate) const fn crc128_table(width: u8, poly: u128, reflect: bool) -> [u128; 256] {
let poly = if reflect {
let poly = poly.reverse_bits();
poly >> (u128::BITS as u8 - width)
poly >> (128u8 - width)
} else {
poly << (u128::BITS as u8 - width)
poly << (128u8 - width)
};

let mut table = [0u128; 256];
Expand Down
23 changes: 10 additions & 13 deletions src/util.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
pub(crate) const fn crc8(poly: u8, reflect: bool, mut value: u8) -> u8 {
let mut i = 0;
if reflect {
let mut i = 0;
while i < 8 {
value = (value >> 1) ^ ((value & 1) * poly);
i += 1;
}
} else {
value <<= u8::BITS - 8;

let mut i = 0;
while i < 8 {
value = (value << 1) ^ (((value >> (u8::BITS - 1)) & 1) * poly);
value = (value << 1) ^ (((value >> 7) & 1) * poly);
i += 1;
}
}
Expand All @@ -25,11 +22,11 @@ pub(crate) const fn crc16(poly: u16, reflect: bool, mut value: u16) -> u16 {
i += 1;
}
} else {
value <<= u16::BITS - 8;
value <<= 8;

let mut i = 0;
while i < 8 {
value = (value << 1) ^ (((value >> (u16::BITS - 1)) & 1) * poly);
value = (value << 1) ^ (((value >> 15) & 1) * poly);
i += 1;
}
}
Expand All @@ -44,11 +41,11 @@ pub(crate) const fn crc32(poly: u32, reflect: bool, mut value: u32) -> u32 {
i += 1;
}
} else {
value <<= u32::BITS - 8;
value <<= 24;

let mut i = 0;
while i < 8 {
value = (value << 1) ^ (((value >> (u32::BITS - 1)) & 1) * poly);
value = (value << 1) ^ (((value >> 31) & 1) * poly);
i += 1;
}
}
Expand All @@ -63,11 +60,11 @@ pub(crate) const fn crc64(poly: u64, reflect: bool, mut value: u64) -> u64 {
i += 1;
}
} else {
value <<= u64::BITS - 8;
value <<= 56;

let mut i = 0;
while i < 8 {
value = (value << 1) ^ (((value >> (u64::BITS - 1)) & 1) * poly);
value = (value << 1) ^ (((value >> 63) & 1) * poly);
i += 1;
}
}
Expand All @@ -82,11 +79,11 @@ pub(crate) const fn crc128(poly: u128, reflect: bool, mut value: u128) -> u128 {
i += 1;
}
} else {
value <<= u128::BITS - 8;
value <<= 120;

let mut i = 0;
while i < 8 {
value = (value << 1) ^ (((value >> (u128::BITS - 1)) & 1) * poly);
value = (value << 1) ^ (((value >> 127) & 1) * poly);
i += 1;
}
}
Expand Down

0 comments on commit f0e62a3

Please sign in to comment.