Skip to content

Commit

Permalink
Optimize truncate_stack procedure (#1384)
Browse files Browse the repository at this point in the history
* refactor: optimize truncate_stack procedure

* refactor: update procedure docs, update changelog
  • Loading branch information
Fumuran authored Jul 8, 2024
1 parent 629ea82 commit d774b7f
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 21 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
- Added support for the `if.false` instruction, which can be used in the same manner as `if.true`
- Relaxed the parser to allow one branch of an `if.(true|false)` to be empty
- Added support for immediate values for `u32and`, `u32or`, `u32xor` and `u32not` bitwise instructions (#1362).
- Optimized `std::sys::truncate_stuck` procedure (#1384).

#### Changed

Expand Down
38 changes: 18 additions & 20 deletions stdlib/asm/sys.masm
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,27 @@
#! are removed in such a way that the top 16 elements of the stack remain unchanged. If the stack
#! would otherwise contain more than 16 elements at the end of execution, then adding a call to this
#! function at the end will reduce the size of the public inputs that are shared with the verifier.
#!
#! Input: Stack with 16 or more elements.
#! Output: Stack with only the original top 16 elements.
export.truncate_stack.4
loc_storew.0
dropw
loc_storew.1
dropw
loc_storew.2
dropw
loc_storew.3
dropw
sdepth
neq.16
#!
#! Cycles: 17 + 11 * overflow_words, where `overflow_words` is the number of words needed to drop.
export.truncate_stack.1
# save the first word to memory and bring elements to be dropped to the top of the stack
loc_storew.0 dropw movupw.3
# => [X, B, C, D, ...]

# until stack depth greater than 16, keep dropping extra elements
sdepth neq.16
while.true
dropw
sdepth
neq.16
dropw movupw.3
# => [X, B, C, D, ...]

sdepth neq.16
end
loc_loadw.3
swapw.3
loc_loadw.2
swapw.2
loc_loadw.1
swapw.1
# => [X, B, C, D, ...]

# bring the previously saved word back onto the stack
loc_loadw.0
# => [A, B, C, D, ...]
end
2 changes: 1 addition & 1 deletion stdlib/docs/sys.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Removes elements deep in the stack until the depth of the stack is exactly 16. The elements<br />are removed in such a way that the top 16 elements of the stack remain unchanged. If the stack<br />would otherwise contain more than 16 elements at the end of execution, then adding a call to this<br />function at the end will reduce the size of the public inputs that are shared with the verifier.<br />Input: Stack with 16 or more elements.<br />Output: Stack with only the original top 16 elements.<br />
Removes elements deep in the stack until the depth of the stack is exactly 16. The elements<br />are removed in such a way that the top 16 elements of the stack remain unchanged. If the stack<br />would otherwise contain more than 16 elements at the end of execution, then adding a call to this<br />function at the end will reduce the size of the public inputs that are shared with the verifier.<br /><br />Input: Stack with 16 or more elements.<br />Output: Stack with only the original top 16 elements.<br /><br />Cycles: 17 + 11 * overflow_words, where `overflow_words` is the number of words needed to drop.<br />
## std::sys
| Procedure | Description |
| ----------- | ------------- |

0 comments on commit d774b7f

Please sign in to comment.