-
Notifications
You must be signed in to change notification settings - Fork 2
sub #352
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
sub #352
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,79 +1,72 @@ | ||
| // SPDX-License-Identifier: CAL | ||
| pragma solidity ^0.8.18; | ||
|
|
||
| // import {OperandV2} from "rain.interpreter.interface/interface/unstable/IInterpreterV4.sol"; | ||
| // import {Pointer} from "rain.solmem/lib/LibPointer.sol"; | ||
| // import {IntegrityCheckState} from "../../integrity/LibIntegrityCheck.sol"; | ||
| // import {InterpreterState} from "../../state/LibInterpreterState.sol"; | ||
| // import {SaturatingMath} from "rain.math.saturating/SaturatingMath.sol"; | ||
| import {OperandV2} from "rain.interpreter.interface/interface/unstable/IInterpreterV4.sol"; | ||
| import {Pointer} from "rain.solmem/lib/LibPointer.sol"; | ||
| import {IntegrityCheckState} from "../../integrity/LibIntegrityCheck.sol"; | ||
| import {InterpreterState} from "../../state/LibInterpreterState.sol"; | ||
| import {Float, LibDecimalFloat} from "rain.math.float/lib/LibDecimalFloat.sol"; | ||
| import {StackItem} from "rain.interpreter.interface/interface/unstable/IInterpreterV4.sol"; | ||
|
|
||
| // /// @title LibOpSub | ||
| // /// @notice Opcode to subtract N integers. | ||
| // library LibOpSub { | ||
| // function integrity(IntegrityCheckState memory, Operand operand) internal pure returns (uint256, uint256) { | ||
| // // There must be at least two inputs. | ||
| // uint256 inputs = (Operand.unwrap(operand) >> 0x10) & 0x0F; | ||
| // inputs = inputs > 1 ? inputs : 2; | ||
| // return (inputs, 1); | ||
| // } | ||
| /// @title LibOpSub | ||
| /// @notice Opcode to subtract N integers. | ||
| library LibOpSub { | ||
| function integrity(IntegrityCheckState memory, OperandV2 operand) internal pure returns (uint256, uint256) { | ||
| // There must be at least two inputs. | ||
| uint256 inputs = uint256(OperandV2.unwrap(operand) >> 0x10) & 0x0F; | ||
| inputs = inputs > 1 ? inputs : 2; | ||
| return (inputs, 1); | ||
| } | ||
|
|
||
| // function sub(uint256 a, uint256 b) internal pure returns (uint256) { | ||
| // return a - b; | ||
| // } | ||
| /// sub | ||
| function run(InterpreterState memory, OperandV2 operand, Pointer stackTop) internal pure returns (Pointer) { | ||
| Float a; | ||
| Float b; | ||
| assembly ("memory-safe") { | ||
| a := mload(stackTop) | ||
| b := mload(add(stackTop, 0x20)) | ||
| stackTop := add(stackTop, 0x40) | ||
| } | ||
|
|
||
| // /// sub | ||
| // /// Subtraction with implied overflow checks from the Solidity 0.8.x compiler. | ||
| // function run(InterpreterState memory, Operand operand, Pointer stackTop) internal pure returns (Pointer) { | ||
| // uint256 a; | ||
| // uint256 b; | ||
| // uint256 saturate; | ||
| // assembly ("memory-safe") { | ||
| // a := mload(stackTop) | ||
| // b := mload(add(stackTop, 0x20)) | ||
| // stackTop := add(stackTop, 0x40) | ||
| // saturate := and(operand, 1) | ||
| // } | ||
| // function (uint256, uint256) internal pure returns (uint256) f = | ||
| // saturate > 0 ? SaturatingMath.saturatingSub : sub; | ||
| // a = f(a, b); | ||
| a = LibDecimalFloat.sub(a, b); | ||
|
|
||
| // { | ||
| // uint256 inputs = (Operand.unwrap(operand) >> 0x10) & 0x0F; | ||
| // uint256 i = 2; | ||
| // while (i < inputs) { | ||
| // assembly ("memory-safe") { | ||
| // b := mload(stackTop) | ||
| // stackTop := add(stackTop, 0x20) | ||
| // } | ||
| // a = f(a, b); | ||
| // unchecked { | ||
| // i++; | ||
| // } | ||
| // } | ||
| // } | ||
| { | ||
| uint256 inputs = uint256(OperandV2.unwrap(operand) >> 0x10) & 0x0F; | ||
| uint256 i = 2; | ||
| while (i < inputs) { | ||
| assembly ("memory-safe") { | ||
| b := mload(stackTop) | ||
| stackTop := add(stackTop, 0x20) | ||
| } | ||
| a = LibDecimalFloat.sub(a, b); | ||
| unchecked { | ||
| i++; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // assembly ("memory-safe") { | ||
| // stackTop := sub(stackTop, 0x20) | ||
| // mstore(stackTop, a) | ||
| // } | ||
| // return stackTop; | ||
| // } | ||
| assembly ("memory-safe") { | ||
| stackTop := sub(stackTop, 0x20) | ||
| mstore(stackTop, a) | ||
| } | ||
| return stackTop; | ||
| } | ||
|
|
||
| // /// Gas intensive reference implementation of subtraction for testing. | ||
| // function referenceFn(InterpreterState memory, Operand, uint256[] memory inputs) | ||
| // internal | ||
| // pure | ||
| // returns (uint256[] memory outputs) | ||
| // { | ||
| // // Unchecked so that when we assert that an overflow error is thrown, we | ||
| // // see the revert from the real function and not the reference function. | ||
| // unchecked { | ||
| // uint256 acc = inputs[0]; | ||
| // for (uint256 i = 1; i < inputs.length; i++) { | ||
| // acc -= inputs[i]; | ||
| // } | ||
| // outputs = new uint256[](1); | ||
| // outputs[0] = acc; | ||
| // } | ||
| // } | ||
| // } | ||
| /// Gas intensive reference implementation of subtraction for testing. | ||
| function referenceFn(InterpreterState memory, OperandV2, StackItem[] memory inputs) | ||
| internal | ||
| pure | ||
| returns (StackItem[] memory outputs) | ||
| { | ||
| // Unchecked so that when we assert that an overflow error is thrown, we | ||
| // see the revert from the real function and not the reference function. | ||
| unchecked { | ||
| Float acc = Float.wrap(StackItem.unwrap(inputs[0])); | ||
| for (uint256 i = 1; i < inputs.length; i++) { | ||
| acc = LibDecimalFloat.sub(acc, Float.wrap(StackItem.unwrap(inputs[i]))); | ||
| } | ||
| outputs = new StackItem[](1); | ||
| outputs[0] = StackItem.wrap(Float.unwrap(acc)); | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Update the documentation to reflect floating-point arithmetic.
The comment states "Opcode to subtract N integers" but the implementation actually performs floating-point subtraction using the
Floattype andLibDecimalFloatlibrary.Apply this diff to fix the documentation:
📝 Committable suggestion
🤖 Prompt for AI Agents