Skip to content

Commit

Permalink
Use BITS from core.
Browse files Browse the repository at this point in the history
  • Loading branch information
tczajka committed Sep 7, 2023
1 parent 80641a7 commit d6284be
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
8 changes: 4 additions & 4 deletions src/math.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ use crate::{arch::word::Word, primitive::PrimitiveUnsigned};
/// 0 for 0.
#[inline]
pub(crate) fn bit_len<T: PrimitiveUnsigned>(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).
Expand Down Expand Up @@ -73,7 +73,7 @@ where
if n == 0 {
T::from(0u8)
} else {
T::MAX >> (T::BIT_SIZE - n)
T::MAX >> (T::BITS - n)
}
}

Expand All @@ -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)
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/modular/pow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
};
Expand Down Expand Up @@ -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;
Expand Down
3 changes: 1 addition & 2 deletions src/pow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

use crate::{
ibig::IBig,
primitive::PrimitiveUnsigned,
sign::Sign::*,
ubig::{Repr::*, UBig},
};
Expand Down Expand Up @@ -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 {
Expand Down
14 changes: 7 additions & 7 deletions src/primitive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ where
Self: Shr<u32, Output = Self>,
{
const BYTE_SIZE: usize = mem::size_of::<Self>();
const BIT_SIZE: u32 = 8 * Self::BYTE_SIZE as u32;
const BITS: u32;
const MAX: Self;
type ByteRepr: AsRef<[u8]> + AsMut<[u8]>;

Expand All @@ -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::<Self>()];
type ByteRepr = [u8; Self::BYTE_SIZE];
const BITS: u32 = Self::BITS;
const MAX: Self = Self::MAX;

#[inline]
Expand Down Expand Up @@ -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;

Expand All @@ -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);
}
Expand All @@ -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));
}

Expand Down

0 comments on commit d6284be

Please sign in to comment.