-
Notifications
You must be signed in to change notification settings - Fork 2
inv op #374
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
inv op #374
Changes from all commits
Commits
Show all changes
2 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,43 +1,44 @@ | ||
| // SPDX-License-Identifier: CAL | ||
| pragma solidity ^0.8.18; | ||
|
|
||
| // import {UD60x18, inv} from "prb-math/UD60x18.sol"; | ||
| // import {OperandV2} from "rain.interpreter.interface/interface/unstable/IInterpreterV4.sol"; | ||
| // import {Pointer} from "rain.solmem/lib/LibPointer.sol"; | ||
| // import {InterpreterState} from "../../state/LibInterpreterState.sol"; | ||
| // import {IntegrityCheckState} from "../../integrity/LibIntegrityCheck.sol"; | ||
| import {OperandV2} from "rain.interpreter.interface/interface/unstable/IInterpreterV4.sol"; | ||
| import {Pointer} from "rain.solmem/lib/LibPointer.sol"; | ||
| import {InterpreterState} from "../../state/LibInterpreterState.sol"; | ||
| import {IntegrityCheckState} from "../../integrity/LibIntegrityCheck.sol"; | ||
| import {Float, LibDecimalFloat} from "rain.math.float/lib/LibDecimalFloat.sol"; | ||
| import {StackItem} from "rain.interpreter.interface/interface/unstable/IInterpreterV4.sol"; | ||
|
|
||
| // /// @title LibOpInv | ||
| // /// @notice Opcode for the inverse 1 / x of an decimal 18 fixed point number. | ||
| // library LibOpInv { | ||
| // function integrity(IntegrityCheckState memory, Operand) internal pure returns (uint256, uint256) { | ||
| // // There must be one inputs and one output. | ||
| // return (1, 1); | ||
| // } | ||
| /// @title LibOpInv | ||
| /// @notice Opcode for the inverse 1 / x of a floating point number. | ||
| library LibOpInv { | ||
| function integrity(IntegrityCheckState memory, OperandV2) internal pure returns (uint256, uint256) { | ||
| // There must be one inputs and one output. | ||
| return (1, 1); | ||
| } | ||
|
|
||
| // /// inv | ||
| // /// 18 decimal fixed point inverse of a number. | ||
| // function run(InterpreterState memory, Operand, Pointer stackTop) internal pure returns (Pointer) { | ||
| // uint256 a; | ||
| // assembly ("memory-safe") { | ||
| // a := mload(stackTop) | ||
| // } | ||
| // a = UD60x18.unwrap(inv(UD60x18.wrap(a))); | ||
| /// inv | ||
| /// floating point inverse of a number. | ||
| function run(InterpreterState memory, OperandV2, Pointer stackTop) internal pure returns (Pointer) { | ||
| Float a; | ||
| assembly ("memory-safe") { | ||
| a := mload(stackTop) | ||
| } | ||
| a = LibDecimalFloat.inv(a); | ||
|
|
||
| // assembly ("memory-safe") { | ||
| // mstore(stackTop, a) | ||
| // } | ||
| // return stackTop; | ||
| // } | ||
| assembly ("memory-safe") { | ||
| mstore(stackTop, a) | ||
| } | ||
| return stackTop; | ||
| } | ||
|
|
||
| // /// Gas intensive reference implementation of inv for testing. | ||
| // function referenceFn(InterpreterState memory, Operand, uint256[] memory inputs) | ||
| // internal | ||
| // pure | ||
| // returns (uint256[] memory) | ||
| // { | ||
| // uint256[] memory outputs = new uint256[](1); | ||
| // outputs[0] = UD60x18.unwrap(inv(UD60x18.wrap(inputs[0]))); | ||
| // return outputs; | ||
| // } | ||
| // } | ||
| /// Gas intensive reference implementation of inv for testing. | ||
| function referenceFn(InterpreterState memory, OperandV2, StackItem[] memory inputs) | ||
| internal | ||
| pure | ||
| returns (StackItem[] memory) | ||
| { | ||
| StackItem[] memory outputs = new StackItem[](1); | ||
| outputs[0] = StackItem.wrap(Float.unwrap((LibDecimalFloat.inv(Float.wrap(StackItem.unwrap(inputs[0])))))); | ||
| return outputs; | ||
| } | ||
| } |
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,59 +1,67 @@ | ||
| // SPDX-License-Identifier: CAL | ||
| pragma solidity =0.8.25; | ||
|
|
||
| // import {OpTest, IntegrityCheckState, Operand, InterpreterState, UnexpectedOperand} from "test/abstract/OpTest.sol"; | ||
| // import {LibOpInv} from "src/lib/op/math/LibOpInv.sol"; | ||
| // import {LibOperand} from "test/lib/operand/LibOperand.sol"; | ||
|
|
||
| // contract LibOpInvTest is OpTest { | ||
| // /// Directly test the integrity logic of LibOpInv. | ||
| // /// Inputs are always 1, outputs are always 1. | ||
| // function testOpInvIntegrity(IntegrityCheckState memory state, Operand operand) external pure { | ||
| // (uint256 calcInputs, uint256 calcOutputs) = LibOpInv.integrity(state, operand); | ||
| // assertEq(calcInputs, 1); | ||
| // assertEq(calcOutputs, 1); | ||
| // } | ||
|
|
||
| // /// Directly test the runtime logic of LibOpInv. | ||
| // function testOpInvRun(uint256 a, uint16 operandData) public view { | ||
| // // 0 is division by 0. | ||
| // a = bound(a, 1, type(uint64).max - 1e18); | ||
| // InterpreterState memory state = opTestDefaultInterpreterState(); | ||
|
|
||
| // Operand operand = LibOperand.build(1, 1, operandData); | ||
| // uint256[] memory inputs = new uint256[](1); | ||
| // inputs[0] = a; | ||
|
|
||
| // opReferenceCheck(state, operand, LibOpInv.referenceFn, LibOpInv.integrity, LibOpInv.run, inputs); | ||
| // } | ||
|
|
||
| // /// Test the eval of `inv`. | ||
| // function testOpInvEval() external view { | ||
| // checkHappy("_: inv(1);", 1e18, "1"); | ||
| // checkHappy("_: inv(0.5);", 2e18, "0.5"); | ||
| // checkHappy("_: inv(2);", 0.5e18, "2"); | ||
| // checkHappy("_: inv(3);", 333333333333333333, "3"); | ||
| // } | ||
|
|
||
| // /// Test the eval of `inv` for bad inputs. | ||
| // function testOpInvZeroInputs() external { | ||
| // checkBadInputs("_: inv();", 0, 1, 0); | ||
| // } | ||
|
|
||
| // function testOpInvTwoInputs() external { | ||
| // checkBadInputs("_: inv(1 1);", 2, 1, 2); | ||
| // } | ||
|
|
||
| // function testOpInvZeroOutputs() external { | ||
| // checkBadOutputs(": inv(1);", 1, 1, 0); | ||
| // } | ||
|
|
||
| // function testOpInvTwoOutputs() external { | ||
| // checkBadOutputs("_ _: inv(1);", 1, 1, 2); | ||
| // } | ||
|
|
||
| // /// Test that operand is disallowed. | ||
| // function testOpExpEvalOperandDisallowed() external { | ||
| // checkUnhappyParse("_: inv<0>(1);", abi.encodeWithSelector(UnexpectedOperand.selector)); | ||
| // } | ||
| // } | ||
| import {OpTest, IntegrityCheckState, OperandV2, InterpreterState, UnexpectedOperand} from "test/abstract/OpTest.sol"; | ||
| import {LibDecimalFloat, Float} from "rain.math.float/lib/LibDecimalFloat.sol"; | ||
| import {LibOpInv} from "src/lib/op/math/LibOpInv.sol"; | ||
| import {LibOperand} from "test/lib/operand/LibOperand.sol"; | ||
| import {StackItem} from "rain.interpreter.interface/interface/unstable/IInterpreterV4.sol"; | ||
|
|
||
| contract LibOpInvTest is OpTest { | ||
| /// Directly test the integrity logic of LibOpInv. | ||
| /// Inputs are always 1, outputs are always 1. | ||
| function testOpInvIntegrity(IntegrityCheckState memory state, OperandV2 operand) external pure { | ||
| (uint256 calcInputs, uint256 calcOutputs) = LibOpInv.integrity(state, operand); | ||
| assertEq(calcInputs, 1); | ||
| assertEq(calcOutputs, 1); | ||
| } | ||
|
|
||
| /// Directly test the runtime logic of LibOpInv. | ||
| function testOpInvRun(int224 signedCoefficient, int32 exponent, uint16 operandData) public view { | ||
| exponent = int32(bound(exponent, type(int16).min, type(int16).max)); | ||
| Float a = LibDecimalFloat.packLossless(signedCoefficient, exponent); | ||
| // 0 is division by 0. | ||
| vm.assume(!LibDecimalFloat.isZero(a)); | ||
| InterpreterState memory state = opTestDefaultInterpreterState(); | ||
|
|
||
| OperandV2 operand = LibOperand.build(1, 1, operandData); | ||
| StackItem[] memory inputs = new StackItem[](1); | ||
| inputs[0] = StackItem.wrap(Float.unwrap(a)); | ||
|
|
||
| opReferenceCheck(state, operand, LibOpInv.referenceFn, LibOpInv.integrity, LibOpInv.run, inputs); | ||
| } | ||
|
|
||
| /// Test the eval of `inv`. | ||
| function testOpInvEval() external view { | ||
| checkHappy("_: inv(1);", Float.unwrap(LibDecimalFloat.packLossless(1e38, -38)), "1"); | ||
| checkHappy("_: inv(0.5);", Float.unwrap(LibDecimalFloat.packLossless(2e37, -37)), "0.5"); | ||
| checkHappy("_: inv(2);", Float.unwrap(LibDecimalFloat.packLossless(0.5e38, -38)), "2"); | ||
| checkHappy( | ||
| "_: inv(3);", | ||
| Float.unwrap(LibDecimalFloat.packLossless(0.33333333333333333333333333333333333333e38, -38)), | ||
| "3" | ||
| ); | ||
| } | ||
|
|
||
| /// Test the eval of `inv` for bad inputs. | ||
| function testOpInvZeroInputs() external { | ||
| checkBadInputs("_: inv();", 0, 1, 0); | ||
| } | ||
|
|
||
| function testOpInvTwoInputs() external { | ||
| checkBadInputs("_: inv(1 1);", 2, 1, 2); | ||
| } | ||
|
|
||
| function testOpInvZeroOutputs() external { | ||
| checkBadOutputs(": inv(1);", 1, 1, 0); | ||
| } | ||
|
|
||
| function testOpInvTwoOutputs() external { | ||
| checkBadOutputs("_ _: inv(1);", 1, 1, 2); | ||
| } | ||
|
|
||
| /// Test that operand is disallowed. | ||
| function testOpExpEvalOperandDisallowed() external { | ||
| checkUnhappyParse("_: inv<0>(1);", abi.encodeWithSelector(UnexpectedOperand.selector)); | ||
| } | ||
| } | ||
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.
🧹 Nitpick (assertive)
Fix function name to match the operation being tested.
The function name suggests it's testing exponential operation, but it's actually testing the inverse operation's operand validation.
📝 Committable suggestion
🤖 Prompt for AI Agents