Skip to content

Commit

Permalink
a tiny patch for the width check (#1971)
Browse files Browse the repository at this point in the history
* a tiny patch for the width check

* all hail clippy

* little documentation
  • Loading branch information
EclecticGriffin authored Mar 14, 2024
1 parent 67305da commit b1f7d44
Showing 1 changed file with 6 additions and 7 deletions.
13 changes: 6 additions & 7 deletions calyx-ir/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use crate::{self as ir, LibrarySignatures, Nothing, RRC, WRC};
use calyx_frontend::BoolAttr;
use calyx_utils::CalyxResult;
use std::{cmp, rc::Rc};
use std::rc::Rc;

use super::{CellType, PortDef};

Expand Down Expand Up @@ -163,12 +163,11 @@ impl<'a> Builder<'a> {
pub fn add_constant(&mut self, val: u64, width: u64) -> RRC<ir::Cell> {
// Ensure that the value can fit within the width
assert!(
val < match width.cmp(&64) {
cmp::Ordering::Less => 1 << width,
cmp::Ordering::Equal => u64::MAX,
cmp::Ordering::Greater =>
panic!("Widths greater than 64 are not supported."),
},
// This calculates the position of the most significant 1 bit which
// tells us the minimum number of bits required to represent the
// constant. Note that this will not work for constants that require
// more than 64 bits as those currently cannot be parsed
(64 - val.leading_zeros()) as u64 <= width,
"Constant value {} cannot fit in {} bits",
val,
width
Expand Down

0 comments on commit b1f7d44

Please sign in to comment.