From d6284beca7430df850dbed9ee9c867d546de8b78 Mon Sep 17 00:00:00 2001 From: Tomek Czajka Date: Thu, 7 Sep 2023 18:37:44 +0200 Subject: [PATCH] Use BITS from core. --- src/convert.rs | 2 +- src/math.rs | 8 ++++---- src/modular/pow.rs | 6 +++--- src/pow.rs | 3 +-- src/primitive.rs | 14 +++++++------- 5 files changed, 16 insertions(+), 17 deletions(-) diff --git a/src/convert.rs b/src/convert.rs index 86d2c1c..41c986b 100644 --- a/src/convert.rs +++ b/src/convert.rs @@ -583,7 +583,7 @@ where Err(OutOfBoundsError) } else { assert!( - T::BIT_SIZE % WORD_BITS == 0, + T::BITS % WORD_BITS == 0, "A large primitive type not a multiple of word size." ); let mut repr = T::default().to_le_bytes(); diff --git a/src/math.rs b/src/math.rs index e9a03a8..65845e6 100644 --- a/src/math.rs +++ b/src/math.rs @@ -6,14 +6,14 @@ use crate::{arch::word::Word, primitive::PrimitiveUnsigned}; /// 0 for 0. #[inline] pub(crate) fn bit_len(x: T) -> u32 { - T::BIT_SIZE - x.leading_zeros() + T::BITS - x.leading_zeros() } /// The length of an integer in bits. /// 0 for 0. #[inline] pub(crate) const fn bit_len_word(x: Word) -> u32 { - Word::BIT_SIZE - x.leading_zeros() + Word::BITS - x.leading_zeros() } /// Ceiling of log_2(x). @@ -73,7 +73,7 @@ where if n == 0 { T::from(0u8) } else { - T::MAX >> (T::BIT_SIZE - n) + T::MAX >> (T::BITS - n) } } @@ -83,7 +83,7 @@ pub(crate) const fn ones_word(n: u32) -> Word { if n == 0 { 0 } else { - Word::MAX >> (Word::BIT_SIZE - n) + Word::MAX >> (Word::BITS - n) } } diff --git a/src/modular/pow.rs b/src/modular/pow.rs index c60ff76..29b55ac 100644 --- a/src/modular/pow.rs +++ b/src/modular/pow.rs @@ -7,7 +7,7 @@ use crate::{ modulo::{Modulo, ModuloLarge, ModuloRepr, ModuloSmall, ModuloSmallRaw}, modulo_ring::ModuloRingSmall, }, - primitive::{double_word, split_double_word, PrimitiveUnsigned, WORD_BITS, WORD_BITS_USIZE}, + primitive::{double_word, split_double_word, WORD_BITS, WORD_BITS_USIZE}, sign::Sign::*, ubig::{Repr::*, UBig}, }; @@ -226,14 +226,14 @@ impl<'a> ModuloLarge<'a> { } /// Choose the optimal window size for n-bit exponents. - /// 1 <= window_size < min(WORD_BITS, usize::BIT_SIZE) inclusive. + /// 1 <= window_size < min(WORD_BITS, usize::BITS) inclusive. fn choose_pow_window_len(n: usize) -> u32 { // This won't overflow because cost(3) is already approximately usize::MAX / 4 // and it can only grow by a factor of 2. let cost = |window_size| (1usize << (window_size - 1)) - 1 + n / (window_size as usize + 1); let mut window_size = 1; let mut c = cost(window_size); - while window_size + 1 < WORD_BITS.min(usize::BIT_SIZE) { + while window_size + 1 < WORD_BITS.min(usize::BITS) { let c2 = cost(window_size + 1); if c <= c2 { break; diff --git a/src/pow.rs b/src/pow.rs index 6f79b24..515378f 100644 --- a/src/pow.rs +++ b/src/pow.rs @@ -2,7 +2,6 @@ use crate::{ ibig::IBig, - primitive::PrimitiveUnsigned, sign::Sign::*, ubig::{Repr::*, UBig}, }; @@ -34,7 +33,7 @@ impl UBig { } _ => {} } - let mut p = usize::BIT_SIZE - 2 - exp.leading_zeros(); + let mut p = usize::BITS - 2 - exp.leading_zeros(); let mut res = self * self; loop { if exp & (1 << p) != 0 { diff --git a/src/primitive.rs b/src/primitive.rs index d93b0f7..0f8df73 100644 --- a/src/primitive.rs +++ b/src/primitive.rs @@ -46,7 +46,7 @@ where Self: Shr, { const BYTE_SIZE: usize = mem::size_of::(); - const BIT_SIZE: u32 = 8 * Self::BYTE_SIZE as u32; + const BITS: u32; const MAX: Self; type ByteRepr: AsRef<[u8]> + AsMut<[u8]>; @@ -72,8 +72,8 @@ where macro_rules! impl_primitive_unsigned { ($t:ty) => { impl PrimitiveUnsigned for $t { - // LEGACY: In Rust 1.49 this could be [u8; Self::BYTE_SIZE]. - type ByteRepr = [u8; mem::size_of::()]; + type ByteRepr = [u8; Self::BYTE_SIZE]; + const BITS: u32 = Self::BITS; const MAX: Self = Self::MAX; #[inline] @@ -143,7 +143,7 @@ impl_primitive_signed!(i64, u64); impl_primitive_signed!(i128, u128); impl_primitive_signed!(isize, usize); -pub(crate) const WORD_BITS: u32 = Word::BIT_SIZE; +pub(crate) const WORD_BITS: u32 = Word::BITS; pub(crate) const WORD_BITS_USIZE: usize = WORD_BITS as usize; pub(crate) const WORD_BYTES: usize = Word::BYTE_SIZE; @@ -167,8 +167,8 @@ mod tests { #[test] fn test_bits_bytes() { - assert_eq!(u8::BIT_SIZE, 8); - assert_eq!(u64::BIT_SIZE, 64); + assert_eq!(u8::BITS, 8); + assert_eq!(u64::BITS, 64); assert_eq!(u8::BYTE_SIZE, 1); assert_eq!(u64::BYTE_SIZE, 8); } @@ -185,7 +185,7 @@ mod tests { #[test] fn test_double_word() { - assert_eq!(DoubleWord::BIT_SIZE, 2 * WORD_BITS); + assert_eq!(DoubleWord::BITS, 2 * WORD_BITS); assert_eq!(split_double_word(double_word(3, 4)), (3, 4)); }