Skip to content
Merged

sub #352

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/generated/Rainterpreter.pointers.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
pragma solidity =0.8.25;

/// @dev Hash of the known bytecode.
bytes32 constant BYTECODE_HASH = bytes32(0x1d115d8ef0c1335daaafcea1dfa2f935ac4df052b3f183a474a07ea5aa7ff08b);
bytes32 constant BYTECODE_HASH = bytes32(0x7844dd6794cf33b630c441de28241af557bf1cecb3463e8dd2ba51da7b32fa96);

/// @dev The function pointers known to the interpreter for dynamic dispatch.
/// By setting these as a constant they can be inlined into the interpreter
/// and loaded at eval time for very low gas (~100) due to the compiler
/// optimising it to a single `codecopy` to build the in memory bytes array.
bytes constant OPCODE_FUNCTION_POINTERS =
hex"06ef0721074508d1099a09ac09be09d709fb0a2f0a400a510af30b120bc10c450c560c670c670c780d050d1e0d320d450da70df50e260f0d";
hex"06f8072a074e08da09a309b509c709e00a040a380a490a5a0afc0b1b0bca0c4e0c5f0c700c700c810d0e0d270d3b0d4e0db00dfe0e2f0e7d0f64";
6 changes: 3 additions & 3 deletions src/generated/RainterpreterExpressionDeployer.pointers.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
pragma solidity =0.8.25;

/// @dev Hash of the known bytecode.
bytes32 constant BYTECODE_HASH = bytes32(0xb40c9e9fb4ae97dd069e91127d58129634bf95c07fcf2b9d120d0f8796785f6e);
bytes32 constant BYTECODE_HASH = bytes32(0x02455b61a2b9ddfcc272f62e4a9baffdfc9277cb25fdee9de1103210d24ff15e);

/// @dev The hash of the meta that describes the contract.
bytes32 constant DESCRIBED_BY_META_HASH = bytes32(0xf14fc5b96da7a0b73a39431663afad57fb831c10c0da7a7ec0af5d491b9e7400);
bytes32 constant DESCRIBED_BY_META_HASH = bytes32(0x0fe1540732ed38e2456cd4c64af90025053a1def08aa2623d8708f4173c7771b);

/// @dev The function pointers for the integrity check fns.
bytes constant INTEGRITY_FUNCTION_POINTERS =
hex"0d980e160e7a0ff40ffe0ffe10081011102c10d210d2112e11a60ffe10080ff40ff40ff40ff411b30ffe0ffe0ff411bd11e50ff4100811b3";
hex"0da00e1e0e820ffc1006100610101019103410da10da113611ae100610100ffc0ffc0ffc0ffc11bb100610060ffc11c511ed0ffc11ed101011bb";
8 changes: 4 additions & 4 deletions src/generated/RainterpreterParser.pointers.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
pragma solidity =0.8.25;

/// @dev Hash of the known bytecode.
bytes32 constant BYTECODE_HASH = bytes32(0x3a3ed07d70c78fed723b124334291bea8f78f30132a8b95c991894aaff2cf618);
bytes32 constant BYTECODE_HASH = bytes32(0x5ae79f118873b18e79dd113453e372bf50fea0bab70b88a50cdf6e33a57c1653);

/// @dev The parse meta that is used to lookup word definitions.
/// The structure of the parse meta is:
Expand All @@ -29,7 +29,7 @@ bytes32 constant BYTECODE_HASH = bytes32(0x3a3ed07d70c78fed723b124334291bea8f78f
/// bit count of the previous bloom filter. If we reach the end of the bloom
/// filters then we have a miss.
bytes constant PARSE_META =
hex"01092004900810001008201088000400e0010080000800001028010100080800090002cd70471a14fbe600f861d118d0e20c0e8be82d1b7c7f8004e5ab37174d06d00d67df8415fedd510602c61f08bcae760bcd088610f1c17805ac4b9f0a09bc09160ce432091ef7b011dfb72601a91367121faf890c2b9f4819bd68ce14811fdb034a067c0fffb07907743c4a13edf10f";
hex"01092404900810001008201088000400e0010080000800001028010100080800090002cd70471b14fbe600f861d118d0e20c0e8be82d1c7c7f8004e5ab37174d06d00d67df8415fedd510602c61f08bcae760bcd088610f1c17805ac4b9f0a09bc09160ce432091ef7b011dfb72601a91367121faf890c2b9f4819bd68ce14811fdb034a067c0fffb07907743c4a1a15024513edf10f";

/// @dev The build depth of the parser meta.

Expand All @@ -39,11 +39,11 @@ uint8 constant PARSE_META_BUILD_DEPTH = 2;
/// These positional indexes all map to the same indexes looked up in the parse
/// meta.
bytes constant OPERAND_HANDLER_FUNCTION_POINTERS =
hex"197a197a197a1a4f1b661b661b661a4f1a4f197a197a197a1b661b661b661b661b661b661b661b661b661b661b661b661b661b661b661b66";
hex"1982198219821a571b6e1b6e1b6e1a571a571982198219821b6e1b6e1b6e1b6e1b6e1b6e1b6e1b6e1b6e1b6e1b6e1b6e1b6e1b6e19821b6e1b6e";

/// @dev Every two bytes is a function pointer for a literal parser.
/// Literal dispatches are determined by the first byte(s) of the literal
/// rather than a full word lookup, and are done with simple conditional
/// jumps as the possibilities are limited compared to the number of words we
/// have.
bytes constant LITERAL_PARSER_FUNCTION_POINTERS = hex"14c216f4173717d5";
bytes constant LITERAL_PARSER_FUNCTION_POINTERS = hex"14ca16fc173f17dd";
17 changes: 7 additions & 10 deletions src/lib/op/LibAllStandardOps.sol
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ import {LibOpMod} from "./math/LibOpMod.sol";
// import {LibOpScaleN} from "./math/LibOpScaleN.sol";
// import {LibOpSnapToUnit} from "./math/LibOpSnapToUnit.sol";
// import {LibOpSqrt} from "./math/LibOpSqrt.sol";
// import {LibOpSub} from "./math/LibOpSub.sol";
import {LibOpSub} from "./math/LibOpSub.sol";

import {LibOpGet} from "./store/LibOpGet.sol";
import {LibOpSet} from "./store/LibOpSet.sol";
Expand All @@ -105,7 +105,7 @@ import {LibParseLiteralHex} from "../parse/literal/LibParseLiteralHex.sol";
import {LibParseLiteralSubParseable} from "../parse/literal/LibParseLiteralSubParseable.sol";

/// @dev Number of ops currently provided by `AllStandardOps`.
uint256 constant ALL_STANDARD_OPS_LENGTH = 28;
uint256 constant ALL_STANDARD_OPS_LENGTH = 29;

/// @title LibAllStandardOps
/// @notice Every opcode available from the core repository laid out as a single
Expand Down Expand Up @@ -305,10 +305,7 @@ library LibAllStandardOps {
// "Rounds a number to the nearest whole number if it is within the threshold distance from that whole number. The first input is the threshold and the second is the value to snap to the nearest unit."
// ),
// AuthoringMetaV2("sqrt", "Calculates the square root of the input. Errors if the input is negative."),
// AuthoringMetaV2(
// "sub",
// "Subtracts all numbers from the first number. The optional operand controls whether subtraction will saturate at 0. The default behaviour, and what will happen if the operand is 0, is that negative values are an error. If the operand is 1, the word will saturate at 0 (e.g. 1-2=0)."
// ),
AuthoringMetaV2("sub", "Subtracts all numbers from the first number."),
// AuthoringMetaV2("saturating-sub", "Subtracts all numbers from the first number. Saturates at 0 (e.g. 1-2=0)."),
AuthoringMetaV2("get", "Gets a value from storage. The first operand is the key to lookup."),
AuthoringMetaV2(
Expand Down Expand Up @@ -509,8 +506,8 @@ library LibAllStandardOps {
// LibParseOperand.handleOperandDisallowed,
// // sqrt
// LibParseOperand.handleOperandDisallowed,
// // sub
// LibParseOperand.handleOperandSingleFull,
// sub
LibParseOperand.handleOperandSingleFull,
// // saturating-sub
// LibParseOperand.handleOperandDisallowedAlwaysOne,
// get
Expand Down Expand Up @@ -623,7 +620,7 @@ library LibAllStandardOps {
// LibOpScaleNDynamic.integrity,
// LibOpSnapToUnit.integrity,
// LibOpSqrt.integrity,
// LibOpSub.integrity,
LibOpSub.integrity,
// // saturating-sub is a repeat of sub.
// LibOpSub.integrity,
LibOpGet.integrity,
Expand Down Expand Up @@ -737,7 +734,7 @@ library LibAllStandardOps {
// LibOpScaleNDynamic.run,
// LibOpSnapToUnit.run,
// LibOpSqrt.run,
// LibOpSub.run,
LibOpSub.run,
// // saturating-sub is a repeat of sub.
// LibOpSub.run,
LibOpGet.run,
Expand Down
133 changes: 63 additions & 70 deletions src/lib/op/math/LibOpSub.sol
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.
Comment on lines +11 to +12
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

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 Float type and LibDecimalFloat library.

Apply this diff to fix the documentation:

-/// @title LibOpSub
-/// @notice Opcode to subtract N integers.
+/// @title LibOpSub
+/// @notice Opcode to subtract N floating-point numbers.
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
/// @title LibOpSub
/// @notice Opcode to subtract N integers.
/// @title LibOpSub
/// @notice Opcode to subtract N floating-point numbers.
🤖 Prompt for AI Agents
In src/lib/op/math/LibOpSub.sol around lines 11 to 12, update the documentation
comment to accurately describe the operation as performing floating-point
subtraction using the Float type and LibDecimalFloat library instead of
subtracting integers. Replace "Opcode to subtract N integers" with a description
that reflects floating-point arithmetic.

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));
}
}
}
Loading
Loading