Skip to content

Commit

Permalink
regs: add convenience accessor methods for s registers
Browse files Browse the repository at this point in the history
  • Loading branch information
dr-orlovsky committed Oct 18, 2024
1 parent 573ffc5 commit 1da568b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
8 changes: 4 additions & 4 deletions src/isa/exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1418,7 +1418,7 @@ impl InstructionSet for DigestOp {
let none;
match self {
DigestOp::Ripemd(src, dst) => {
let s = regs.get_s(*src);
let s = regs.s16(*src);

Check warning on line 1421 in src/isa/exec.rs

View check run for this annotation

Codecov / codecov/patch

src/isa/exec.rs#L1421

Added line #L1421 was not covered by tests
none = s.is_none();
let hash = s.map(|s| {
let mut hash: [u8; 20] = ripemd::Ripemd160::digest(s.as_ref()).into();
Expand All @@ -1429,19 +1429,19 @@ impl InstructionSet for DigestOp {
regs.set_n(RegR::R160, dst, hash);
}
DigestOp::Sha256(src, dst) => {
let s = regs.get_s(*src);
let s = regs.s16(*src);

Check warning on line 1432 in src/isa/exec.rs

View check run for this annotation

Codecov / codecov/patch

src/isa/exec.rs#L1432

Added line #L1432 was not covered by tests
none = s.is_none();
let hash: Option<[u8; 32]> = s.map(|s| sha2::Sha256::digest(s.as_ref()).into());
regs.set_n(RegR::R256, dst, hash);
}
DigestOp::Blake3(src, dst) => {
let s = regs.get_s(*src);
let s = regs.s16(*src);

Check warning on line 1438 in src/isa/exec.rs

View check run for this annotation

Codecov / codecov/patch

src/isa/exec.rs#L1438

Added line #L1438 was not covered by tests
none = s.is_none();
let hash: Option<[u8; 32]> = s.map(|s| blake3::hash(s.as_ref()).into());
regs.set_n(RegR::R256, dst, hash);
}
DigestOp::Sha512(src, dst) => {
let s = regs.get_s(*src);
let s = regs.s16(*src);

Check warning on line 1444 in src/isa/exec.rs

View check run for this annotation

Codecov / codecov/patch

src/isa/exec.rs#L1444

Added line #L1444 was not covered by tests
none = s.is_none();
let hash: Option<[u8; 64]> = s.map(|s| sha2::Sha512::digest(s.as_ref()).into());
regs.set_n(RegR::R512, dst, hash);
Expand Down
14 changes: 9 additions & 5 deletions src/reg/core_regs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ impl CoreRegs {
Reg::A(reg, index) => self.get_n(reg, index).into(),
Reg::F(reg, index) => self.get_n(reg, index).into(),
Reg::R(reg, index) => self.get_n(reg, index).into(),
Reg::S(reg) => self.get_s(reg).cloned().into(),
Reg::S(reg) => self.s16(reg).cloned().into(),

Check warning on line 288 in src/reg/core_regs.rs

View check run for this annotation

Codecov / codecov/patch

src/reg/core_regs.rs#L288

Added line #L288 was not covered by tests
}
}

Expand Down Expand Up @@ -366,6 +366,7 @@ impl CoreRegs {

/// Returns value from one of `S`-registers
#[inline]
#[deprecated(since = "0.11.0-beta.9", note = "use `s16` method")]
pub fn get_s(&self, index: impl Into<RegS>) -> Option<&ByteStr> {
self.s16[index.into().as_usize()].as_ref()
}
Expand All @@ -391,7 +392,7 @@ impl CoreRegs {
idx1: impl Into<RegS>,
idx2: impl Into<RegS>,
) -> Option<(&ByteStr, &ByteStr)> {
self.get_s(idx1).and_then(|val1| self.get_s(idx2).map(|val2| (val1, val2)))
self.s16(idx1).and_then(|val1| self.s16(idx2).map(|val2| (val1, val2)))

Check warning on line 395 in src/reg/core_regs.rs

View check run for this annotation

Codecov / codecov/patch

src/reg/core_regs.rs#L395

Added line #L395 was not covered by tests
}

/// Assigns the provided value to the register bit-wise. Silently discards most significant bits
Expand Down Expand Up @@ -464,6 +465,7 @@ impl CoreRegs {
/// Assigns the provided value to the string register.
///
/// Returns `true` if the value was not `None`.
#[deprecated(since = "0.11.0-beta.9", note = "use `set_s16` method")]
pub fn set_s(&mut self, index: impl Into<RegS>, value: Option<impl Into<ByteStr>>) -> bool {
let reg = &mut self.s16[index.into().as_usize()];
let was_set = reg.is_some();
Expand All @@ -474,17 +476,19 @@ impl CoreRegs {
/// Assigns the provided value to the string register if the register is not initialized.
///
/// Returns `false` if the register is initialized and the value is not `None`.
#[deprecated(since = "0.11.0-beta.9")]
pub fn set_s_if(&mut self, index: impl Into<RegS>, value: Option<impl Into<ByteStr>>) -> bool {
let index = index.into();
if self.get_s(index).is_none() {
if self.s16(index).is_none() {

Check warning on line 482 in src/reg/core_regs.rs

View check run for this annotation

Codecov / codecov/patch

src/reg/core_regs.rs#L482

Added line #L482 was not covered by tests
#[allow(deprecated)]
self.set_s(index, value)

Check warning on line 484 in src/reg/core_regs.rs

View check run for this annotation

Codecov / codecov/patch

src/reg/core_regs.rs#L484

Added line #L484 was not covered by tests
} else {
value.is_none()

Check warning on line 486 in src/reg/core_regs.rs

View check run for this annotation

Codecov / codecov/patch

src/reg/core_regs.rs#L486

Added line #L486 was not covered by tests
}
}

/// Executes provided operation (as callback function) if and only if all the provided registers
/// contain a value (initialized). Otherwise, sets destination to `None` and does not calls the
/// contain a value (initialized). Otherwise, sets destination to `None` and does not call the
/// callback.
#[inline]
#[allow(clippy::too_many_arguments)]
Expand Down Expand Up @@ -900,7 +904,7 @@ mod test {
}

for idx in 0u8..16 {
regs.set_s(u4::with(idx), Some(ByteStr::with(format!("string index {idx}"))));
regs.set_s16(u4::with(idx), ByteStr::with(format!("string index {idx}")));
}

eprintln!("{regs:#?}");
Expand Down

0 comments on commit 1da568b

Please sign in to comment.