Skip to content

Commit

Permalink
refactor: Use an excluded upper bound instead of max log width. (#451)
Browse files Browse the repository at this point in the history
More bikeshedding.

---------

Co-authored-by: Seyon Sivarajah <seyon.sivarajah@quantinuum.com>
  • Loading branch information
2 people authored and lmondada committed Aug 28, 2023
1 parent 72316e7 commit 8e19b72
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
15 changes: 9 additions & 6 deletions src/std_extensions/arithmetic/int_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,24 +32,27 @@ pub(super) fn int_type(width_arg: TypeArg) -> Type {

lazy_static! {
/// Array of valid integer types, indexed by log width of the integer.
pub static ref INT_TYPES: [Type; (MAX_LOG_WIDTH + 1) as usize] = (0..MAX_LOG_WIDTH + 1)
pub static ref INT_TYPES: [Type; LOG_WIDTH_BOUND as usize] = (0..LOG_WIDTH_BOUND)
.map(|i| int_type(TypeArg::BoundedNat(i as u64)))
.collect::<Vec<_>>()
.try_into()
.unwrap();
}

const fn is_valid_log_width(n: u8) -> bool {
n <= MAX_LOG_WIDTH
n < LOG_WIDTH_BOUND
}

/// The largest allowed log width.
pub const MAX_LOG_WIDTH: u8 = 7;
/// The smallest forbidden log width.
pub const LOG_WIDTH_BOUND: u8 = 8;

/// Type parameter for the log width of the integer.
// SAFETY: unsafe block should be ok as the value is definitely not zero.
pub const LOG_WIDTH_TYPE_PARAM: TypeParam =
TypeParam::bounded_nat(unsafe { NonZeroU64::new_unchecked(MAX_LOG_WIDTH as u64 + 1) });
#[allow(clippy::assertions_on_constants)]
pub const LOG_WIDTH_TYPE_PARAM: TypeParam = TypeParam::bounded_nat(unsafe {
assert!(LOG_WIDTH_BOUND > 0);
NonZeroU64::new_unchecked(LOG_WIDTH_BOUND as u64)
});

/// Get the log width of the specified type argument or error if the argument
/// is invalid.
Expand Down
4 changes: 2 additions & 2 deletions src/std_extensions/arithmetic/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ mod test {
types::type_param::TypeArg,
};

use super::int_types::MAX_LOG_WIDTH;
use super::int_types::LOG_WIDTH_BOUND;

#[test]
fn test_int_types() {
for i in 0..MAX_LOG_WIDTH + 1 {
for i in 0..LOG_WIDTH_BOUND {
assert_eq!(
INT_TYPES[i as usize],
int_type(TypeArg::BoundedNat(i as u64))
Expand Down

0 comments on commit 8e19b72

Please sign in to comment.