Skip to content

Commit dcb4364

Browse files
committed
Make some fonctions const for static luts
1 parent 6737353 commit dcb4364

File tree

3 files changed

+22
-20
lines changed

3 files changed

+22
-20
lines changed

src/lut.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,16 +61,18 @@ impl Lut {
6161

6262
/// Create a constant true Lut
6363
pub fn one(num_vars: usize) -> Lut {
64-
let mut ret = Lut::new(num_vars);
65-
fill_one(num_vars, ret.table.as_mut());
66-
ret
64+
Self {
65+
num_vars,
66+
table: vec![num_vars_mask(num_vars); table_size(num_vars)].into_boxed_slice(),
67+
}
6768
}
6869

6970
/// Create a constant false Lut
7071
pub fn zero(num_vars: usize) -> Lut {
71-
let mut ret = Lut::new(num_vars);
72-
fill_zero(num_vars, ret.table.as_mut());
73-
ret
72+
Self {
73+
num_vars,
74+
table: vec![0; table_size(num_vars)].into_boxed_slice(),
75+
}
7476
}
7577

7678
/// Create a Lut returning the value of one of its variables

src/operations.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,8 @@ pub const COUNT_MASKS: [u64; 7] = [
8383
];
8484

8585
/// u64 mask when the number of variables is smaller than 6
86-
pub fn num_vars_mask(num_vars: usize) -> u64 {
87-
NUM_VARS_MASK[std::cmp::min(num_vars, 6)]
86+
pub const fn num_vars_mask(num_vars: usize) -> u64 {
87+
NUM_VARS_MASK[if num_vars < 6 { num_vars } else { 6 }]
8888
}
8989

9090
/// Size of the lookup table, in u64

src/static_lut.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,25 +22,23 @@ pub struct StaticLut<const NUM_VARS: usize, const NUM_WORDS: usize> {
2222

2323
impl<const NUM_VARS: usize, const NUM_WORDS: usize> Default for StaticLut<NUM_VARS, NUM_WORDS> {
2424
fn default() -> Self {
25-
Self {
26-
table: [0u64; NUM_WORDS],
27-
}
25+
Self::zero()
2826
}
2927
}
3028

3129
impl<const NUM_VARS: usize, const NUM_WORDS: usize> StaticLut<NUM_VARS, NUM_WORDS> {
3230
/// Query the number of variables of the Lut
33-
pub fn num_vars(&self) -> usize {
31+
pub const fn num_vars(&self) -> usize {
3432
NUM_VARS
3533
}
3634

3735
/// Query the number of bits in the Lut
38-
pub fn num_bits(&self) -> usize {
36+
pub const fn num_bits(&self) -> usize {
3937
1 << NUM_VARS
4038
}
4139

4240
/// Query the number of 64-bit blocks in the Lut
43-
pub fn num_blocks(&self) -> usize {
41+
pub const fn num_blocks(&self) -> usize {
4442
table_size(NUM_VARS)
4543
}
4644

@@ -60,15 +58,17 @@ impl<const NUM_VARS: usize, const NUM_WORDS: usize> StaticLut<NUM_VARS, NUM_WORD
6058
}
6159

6260
/// Create a constant true Lut
63-
pub fn one() -> Self {
64-
let mut ret = Self::default();
65-
fill_one(NUM_VARS, ret.table.as_mut());
66-
ret
61+
pub const fn one() -> Self {
62+
Self {
63+
table: [num_vars_mask(NUM_VARS); NUM_WORDS],
64+
}
6765
}
6866

6967
/// Create a constant false Lut
70-
pub fn zero() -> Self {
71-
Self::default()
68+
pub const fn zero() -> Self {
69+
Self {
70+
table: [0; NUM_WORDS],
71+
}
7272
}
7373

7474
/// Create a Lut returning the value of one of its variables

0 commit comments

Comments
 (0)