Skip to content

Commit

Permalink
refactor!(leading_zeroes): Impl BasicSnippet
Browse files Browse the repository at this point in the history
Remove the implementations of `DeprecatedSnippet`, implement
`BasicSnippet` directly.
  • Loading branch information
jan-ferdinand committed Jan 6, 2025
1 parent 821d7ca commit 9ba83df
Show file tree
Hide file tree
Showing 11 changed files with 202 additions and 347 deletions.
24 changes: 24 additions & 0 deletions tasm-lib/benchmarks/tasmlib_arithmetic_u32_leading_zeros.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
[
{
"name": "tasmlib_arithmetic_u32_leading_zeros",
"benchmark_result": {
"clock_cycle_count": 12,
"hash_table_height": 12,
"u32_table_height": 17,
"op_stack_table_height": 4,
"ram_table_height": 0
},
"case": "CommonCase"
},
{
"name": "tasmlib_arithmetic_u32_leading_zeros",
"benchmark_result": {
"clock_cycle_count": 12,
"hash_table_height": 12,
"u32_table_height": 33,
"op_stack_table_height": 4,
"ram_table_height": 0
},
"case": "WorstCase"
}
]
24 changes: 0 additions & 24 deletions tasm-lib/benchmarks/tasmlib_arithmetic_u32_leadingzeros.json

This file was deleted.

4 changes: 2 additions & 2 deletions tasm-lib/benchmarks/tasmlib_arithmetic_u64_div_mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
{
"name": "tasmlib_arithmetic_u64_div_mod",
"benchmark_result": {
"clock_cycle_count": 8020,
"clock_cycle_count": 8016,
"hash_table_height": 522,
"u32_table_height": 10526,
"op_stack_table_height": 5390,
"op_stack_table_height": 5382,
"ram_table_height": 142
},
"case": "WorstCase"
Expand Down
8 changes: 4 additions & 4 deletions tasm-lib/benchmarks/tasmlib_arithmetic_u64_leading_zeros.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@
{
"name": "tasmlib_arithmetic_u64_leading_zeros",
"benchmark_result": {
"clock_cycle_count": 36,
"clock_cycle_count": 32,
"hash_table_height": 30,
"u32_table_height": 33,
"op_stack_table_height": 21,
"op_stack_table_height": 13,
"ram_table_height": 0
},
"case": "CommonCase"
},
{
"name": "tasmlib_arithmetic_u64_leading_zeros",
"benchmark_result": {
"clock_cycle_count": 23,
"clock_cycle_count": 21,
"hash_table_height": 30,
"u32_table_height": 32,
"op_stack_table_height": 13,
"op_stack_table_height": 9,
"ram_table_height": 0
},
"case": "WorstCase"
Expand Down
12 changes: 6 additions & 6 deletions tasm-lib/benchmarks/tasmlib_verifier_xfe_ntt.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@
{
"name": "tasmlib_verifier_xfe_ntt",
"benchmark_result": {
"clock_cycle_count": 116763,
"hash_table_height": 222,
"clock_cycle_count": 116761,
"hash_table_height": 216,
"u32_table_height": 10855,
"op_stack_table_height": 111550,
"op_stack_table_height": 111546,
"ram_table_height": 13729
},
"case": "CommonCase"
},
{
"name": "tasmlib_verifier_xfe_ntt",
"benchmark_result": {
"clock_cycle_count": 257114,
"hash_table_height": 222,
"clock_cycle_count": 257112,
"hash_table_height": 216,
"u32_table_height": 24122,
"op_stack_table_height": 245974,
"op_stack_table_height": 245970,
"ram_table_height": 30529
},
"case": "WorstCase"
Expand Down
2 changes: 1 addition & 1 deletion tasm-lib/src/arithmetic/u32.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
pub mod isodd;
pub mod isu32;
pub mod leadingzeros;
pub mod leading_zeros;
pub mod next_power_of_two;
pub mod or;
pub mod overflowingadd;
Expand Down
92 changes: 92 additions & 0 deletions tasm-lib/src/arithmetic/u32/leading_zeros.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
use triton_vm::prelude::*;

use crate::prelude::*;

#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
pub struct LeadingZeros;

impl BasicSnippet for LeadingZeros {
fn inputs(&self) -> Vec<(DataType, String)> {
vec![(DataType::U32, "arg".to_string())]
}

fn outputs(&self) -> Vec<(DataType, String)> {
vec![(DataType::U32, "leading_zeros(arg)".to_string())]
}

fn entrypoint(&self) -> String {
"tasmlib_arithmetic_u32_leading_zeros".to_string()
}

fn code(&self, _: &mut Library) -> Vec<LabelledInstruction> {
let entrypoint = self.entrypoint();
let non_zero_label = format!("{entrypoint}_non_zero");

triton_asm! {
// BEFORE: _ value
// AFTER: _ (leading zeros in value)
{entrypoint}:
dup 0
skiz
call {non_zero_label}

push -1
mul
addi 32

return

{non_zero_label}:
log_2_floor
addi 1
return
}
}
}

#[cfg(test)]
mod tests {
use super::*;
use crate::test_prelude::*;

impl Closure for LeadingZeros {
type Args = u32;

fn rust_shadow(&self, stack: &mut Vec<BFieldElement>) {
let arg = pop_encodable::<Self::Args>(stack);
push_encodable(stack, &arg.leading_zeros());
}

fn pseudorandom_args(
&self,
seed: [u8; 32],
bench_case: Option<BenchmarkCase>,
) -> Self::Args {
match bench_case {
Some(BenchmarkCase::CommonCase) => 1 << 15,
Some(BenchmarkCase::WorstCase) => u32::MAX,
None => StdRng::from_seed(seed).gen(),
}
}

fn corner_case_args(&self) -> Vec<Self::Args> {
vec![0, 1, 2, 3, 1 << 28, 1 << 29, 1 << 30, 1 << 31, u32::MAX]
}
}

#[test]
fn unit() {
ShadowedClosure::new(LeadingZeros).test();
}
}

#[cfg(test)]
mod benches {
use super::*;
use crate::test_prelude::*;

#[test]
fn benchmark() {
ShadowedClosure::new(LeadingZeros).bench();
}
}
158 changes: 0 additions & 158 deletions tasm-lib/src/arithmetic/u32/leadingzeros.rs

This file was deleted.

Loading

0 comments on commit 9ba83df

Please sign in to comment.