Skip to content
Merged
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
1,390 changes: 707 additions & 683 deletions .gas-snapshot

Large diffs are not rendered by default.

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(0x1ed65c8c3d3ffc05de391f627229478134e639737bdd866422cd8df0abe08657);
bytes32 constant BYTECODE_HASH = bytes32(0x74f4ab876f43b52d924dd67b52b3ca05890425f855a8a777e9814f4246011d05);

/// @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"07fc082e085209de0aa70ab90acb0ae40b080b3c0b4d0b5e0c000c1f0cdd0d8d0e110f5310860cdd117f123112d3134b135c136d136d137e13e914f41573158c15a015ff16181631166a169516ae16c716ee1701176317b117ff184d189b18e919371968197619c419f51a431a741ac21b101c06";
hex"08050837085b09e70ab00ac20ad40aed0b110b450b560b670c090c280ce60d960e1a0f5c108f0ce61188123a12dc1354136513761376138713f214fd157c159515a916081621163a1673169e16b716d016f7170a176c17ba1808185618a418f219401971197f198d19db1a0c1a5a1a8b1ad91b271c1d";
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(0xf2207cd3afbd9ca7533c86525c77d0d2b740db0becbe048a274555b9a6a2c12a);
bytes32 constant BYTECODE_HASH = bytes32(0xbed2d4ac5d5461f524b74c1685829181a4869af0e88d8acce1c56f4fa26b53b5);

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

/// @dev The function pointers for the integrity check fns.
bytes constant INTEGRITY_FUNCTION_POINTERS =
hex"0e880f060f6a10e410ee10ee10f81101111c11c211c2121e129612a310ee10f812a310ee10f810ee10ee10ee10f810e410e410e410e412ad12d212ec10ee10ee12ad10ee10ee12a310f810ee10ee12a310e412f612f612f612f612f612f6131010e410f812f610e412f610e41310131010f812ec";
hex"0e900f0e0f7210ec10f610f611001109112411ca11ca1226129e12ab10f6110012ab10f6110010f610f610f6110010ec10ec10ec10ec12b512da12f410f610f612b510f610f612ab110010f610f612ab10ec12fe12fe12fe12fe12fe12fe131810ec1100110012fe10ec12fe10ec13181318110012f4";
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(0x6ffd9e7d346f2c3f3aa8e40939dcd8b27f15e0b57a639fb5b8674df19c36bb64);
bytes32 constant BYTECODE_HASH = bytes32(0x5394e29197b822127aea4c685666b83280e3d4d2e05754107709bec524e5cb32);

/// @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(0x6ffd9e7d346f2c3f3aa8e40939dcd8b27f15e
/// 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"0278ac80600029b010910908a00a7c48c001280030060500180000200cc2200408e400000000000000000000000000000000000000000000000000200000000000000027fe9a970c95d83823c0361a3848145c3934b3f333d587e204afc8f01f95a26007b9a51f0d5e6f2803ce312215fae0a12b1233de118cfd53051c784d29a55ade3512233f1681ac9625ac6dfc34a4e5e9215fc32a0e92b2401ebbd837100ef76b00cd8814225902e62e9714ec133135922d0d473b203ddb3124f880501427634f1c69b5340f23b4970ac0e53d0b895f991ac908be0983724f12c080bc369d7b7d2c757aea313a7b862a6f62e02f5c0ab5086d2a7b2690a6b319bcb24402af7d4506414b8117b04fb31db2182f18ef532928c301e31b9314ad37fa5d8001336a5932ab2461308fa388";
hex"02120580000068a0a65201249030011040048204660b2023215058008c06080120210000000000000000000000000000000000080000000000000000000000000000402d8199df009d67d22c1d0f37036e3e2e0e7d8c5707d7fdec21100100143de58736ae590b3905464e09fa0f6d29e133af31ac46712e06de8018f697c502a7d9d3259988671deafd991c6afdc512aa165d057d9d9d19be782a30f7cb9a0f11ccad2a42ee8e267c88391191db7333ac16ad0a15ce750b56f94f0464fdb31ba3783a100f9a7f1ed9827e0c53405c3522c7531a79fcd03a90ed6506c827b92f19ae1324e4865308f7e23532ad38972b2a49ad1f17f5b838bb726f2881231d34e365890d1d863d0150056620b2eeff2772e4921742d7773722824116f1989e2354b15415a57077135ab9fa22782c84";

/// @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"1a6d1a6d1a6d1b421c591c591c591b421b421a6d1a6d1a6d1c591c591c591c591c591c591c591c591c591c591c591c591c591c591c591c591c591c591c591c591c591c591c591c591c591c591c591c591c591c591c591c591c591c591c591c591c591c591c591c591c591c591c591a6d1c591c59";
hex"1a751a751a751b4a1c611c611c611b4a1b4a1a751a751a751c611c611c611c611c611c611c611c611c611c611c611c611c611c611c611c611c611c611c611c611c611c611c611c611c611c611c611c611c611c611c611c611c611c611c611c611c611c611c611c611c611c611c611c611a751c611c61";

/// @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"15b517e7182a18c8";
bytes constant LITERAL_PARSER_FUNCTION_POINTERS = hex"15bd17ef183218d0";
14 changes: 7 additions & 7 deletions src/lib/op/LibAllStandardOps.sol
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ import {LibOpE} from "./math/LibOpE.sol";
// import {LibOpExp} from "./math/LibOpExp.sol";
// import {LibOpExp2} from "./math/LibOpExp2.sol";
// import {LibOpFloor} from "./math/LibOpFloor.sol";
// import {LibOpFrac} from "./math/LibOpFrac.sol";
import {LibOpFrac} from "./math/LibOpFrac.sol";
// import {LibOpGm} from "./math/LibOpGm.sol";
// import {LibOpHeadroom} from "./math/LibOpHeadroom.sol";
import {LibOpInv} from "./math/LibOpInv.sol";
Expand Down Expand Up @@ -109,7 +109,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 = 58;
uint256 constant ALL_STANDARD_OPS_LENGTH = 59;

/// @title LibAllStandardOps
/// @notice Every opcode available from the core repository laid out as a single
Expand Down Expand Up @@ -272,7 +272,7 @@ library LibAllStandardOps {
// AuthoringMetaV2("exp", "Natural exponential e^x. Errors if the exponentiation exceeds `max-value()`."),
// AuthoringMetaV2("exp2", "Binary exponential 2^x where x. Errors if the exponentiation exceeds `max-value()`."),
// AuthoringMetaV2("floor", "Floor of a number."),
// AuthoringMetaV2("frac", "Fractional part of a number."),
AuthoringMetaV2("frac", "Fractional part of a number."),
// AuthoringMetaV2("gm", "Geometric mean of all numbers. Errors if any number is zero."),
// AuthoringMetaV2(
// "headroom",
Expand Down Expand Up @@ -467,8 +467,8 @@ library LibAllStandardOps {
// LibParseOperand.handleOperandDisallowed,
// // floor
// LibParseOperand.handleOperandDisallowed,
// // frac
// LibParseOperand.handleOperandDisallowed,
// frac
LibParseOperand.handleOperandDisallowed,
// // gm
// LibParseOperand.handleOperandDisallowed,
// // headroom
Expand Down Expand Up @@ -591,7 +591,7 @@ library LibAllStandardOps {
// LibOpExp.integrity,
// LibOpExp2.integrity,
// LibOpFloor.integrity,
// LibOpFrac.integrity,
LibOpFrac.integrity,
// LibOpGm.integrity,
// LibOpHeadroom.integrity,
LibOpInv.integrity,
Expand Down Expand Up @@ -699,7 +699,7 @@ library LibAllStandardOps {
// LibOpExp.run,
// LibOpExp2.run,
// LibOpFloor.run,
// LibOpFrac.run,
LibOpFrac.run,
// LibOpGm.run,
// LibOpHeadroom.run,
LibOpInv.run,
Expand Down
75 changes: 39 additions & 36 deletions src/lib/op/math/LibOpFrac.sol
Original file line number Diff line number Diff line change
@@ -1,43 +1,46 @@
// SPDX-License-Identifier: CAL
pragma solidity ^0.8.18;

// import {UD60x18, frac} 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 {StackItem} from "rain.interpreter.interface/interface/unstable/IInterpreterV4.sol";
import {Float, LibDecimalFloat} from "rain.math.float/lib/LibDecimalFloat.sol";

// /// @title LibOpFrac
// /// @notice Opcode for the frac of an decimal 18 fixed point number.
// library LibOpFrac {
// function integrity(IntegrityCheckState memory, Operand) internal pure returns (uint256, uint256) {
// // There must be one input and one output.
// return (1, 1);
// }
/// @title LibOpFrac
/// @notice Opcode for the frac of a decimal floating point number.
library LibOpFrac {
using LibDecimalFloat for Float;

// /// frac
// /// 18 decimal fixed point frac 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(frac(UD60x18.wrap(a)));
function integrity(IntegrityCheckState memory, OperandV2) internal pure returns (uint256, uint256) {
// There must be one input and one output.
return (1, 1);
}

// assembly ("memory-safe") {
// mstore(stackTop, a)
// }
// return stackTop;
// }
/// frac
/// decimal floating point frac of a number.
function run(InterpreterState memory, OperandV2, Pointer stackTop) internal pure returns (Pointer) {
Float a;
assembly ("memory-safe") {
a := mload(stackTop)
}
a = a.frac();

// /// Gas intensive reference implementation of frac 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(frac(UD60x18.wrap(inputs[0])));
// return outputs;
// }
// }
assembly ("memory-safe") {
mstore(stackTop, a)
}
return stackTop;
}

/// Gas intensive reference implementation of frac 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(Float.wrap(StackItem.unwrap(inputs[0])).frac()));
return outputs;
}
}
117 changes: 60 additions & 57 deletions test/src/lib/op/math/LibOpFrac.t.sol
Original file line number Diff line number Diff line change
@@ -1,60 +1,63 @@
// SPDX-License-Identifier: CAL
pragma solidity =0.8.25;

// import {OpTest, IntegrityCheckState, Operand, InterpreterState, UnexpectedOperand} from "test/abstract/OpTest.sol";
// import {LibOpFrac} from "src/lib/op/math/LibOpFrac.sol";
// import {LibOperand} from "test/lib/operand/LibOperand.sol";

// contract LibOpFracTest is OpTest {
// /// Directly test the integrity logic of LibOpFrac.
// /// Inputs are always 1, outputs are always 1.
// function testOpFracIntegrity(IntegrityCheckState memory state, Operand operand) external pure {
// (uint256 calcInputs, uint256 calcOutputs) = LibOpFrac.integrity(state, operand);
// assertEq(calcInputs, 1);
// assertEq(calcOutputs, 1);
// }

// /// Directly test the runtime logic of LibOpFrac.
// function testOpFracRun(uint256 a, uint16 operandData) public view {
// a = bound(a, 0, 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, LibOpFrac.referenceFn, LibOpFrac.integrity, LibOpFrac.run, inputs);
// }

// /// Test the eval of `frac`.
// function testOpFracEval() external view {
// checkHappy("_: frac(0);", 0, "0");
// checkHappy("_: frac(1);", 0, "1");
// checkHappy("_: frac(0.5);", 0.5e18, "0.5");
// checkHappy("_: frac(2);", 0, "2");
// checkHappy("_: frac(3);", 0, "3");
// checkHappy("_: frac(3.8);", 0.8e18, "3.8");
// }

// /// Test the eval of `frac` for bad inputs.
// function testOpFracZeroInputs() external {
// checkBadInputs("_: frac();", 0, 1, 0);
// }

// function testOpFracTwoInputs() external {
// checkBadInputs("_: frac(1 1);", 2, 1, 2);
// }

// function testOpFracZeroOutputs() external {
// checkBadOutputs(": frac(1);", 1, 1, 0);
// }

// function testOpFracTwoOutputs() external {
// checkBadOutputs("_ _: frac(1);", 1, 1, 2);
// }

// /// Test that operand is disallowed.
// function testOpFracEvalOperandDisallowed() external {
// checkUnhappyParse("_: frac<0>(1);", abi.encodeWithSelector(UnexpectedOperand.selector));
// }
// }
import {OpTest, IntegrityCheckState, OperandV2, InterpreterState, UnexpectedOperand} from "test/abstract/OpTest.sol";
import {LibOpFrac} from "src/lib/op/math/LibOpFrac.sol";
import {LibOperand} from "test/lib/operand/LibOperand.sol";
import {LibDecimalFloat, Float} from "rain.math.float/lib/LibDecimalFloat.sol";
import {StackItem} from "rain.interpreter.interface/interface/unstable/IInterpreterV4.sol";

contract LibOpFracTest is OpTest {
/// Directly test the integrity logic of LibOpFrac.
/// Inputs are always 1, outputs are always 1.
function testOpFracIntegrity(IntegrityCheckState memory state, OperandV2 operand) external pure {
(uint256 calcInputs, uint256 calcOutputs) = LibOpFrac.integrity(state, operand);
assertEq(calcInputs, 1);
assertEq(calcOutputs, 1);
}

/// Directly test the runtime logic of LibOpFrac.
function testOpFracRun(Float a, uint16 operandData) public view {
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, LibOpFrac.referenceFn, LibOpFrac.integrity, LibOpFrac.run, inputs);
}

/// Test the eval of `frac`.
function testOpFracEval() external view {
checkHappy("_: frac(0);", 0, "0");
checkHappy("_: frac(1);", 0, "1");
checkHappy("_: frac(0.5);", Float.unwrap(LibDecimalFloat.packLossless(0.5e1, -1)), "0.5");
checkHappy("_: frac(2);", 0, "2");
checkHappy("_: frac(3);", 0, "3");
checkHappy("_: frac(3.8);", Float.unwrap(LibDecimalFloat.packLossless(0.8e1, -1)), "3.8");
checkHappy("_: frac(-0.5);", Float.unwrap(LibDecimalFloat.packLossless(-0.5e1, -1)), "-0.5");
checkHappy("_: frac(1.5e10);", Float.unwrap(LibDecimalFloat.packLossless(0, 9)), "1.5e10");
}

/// Test the eval of `frac` for bad inputs.
function testOpFracZeroInputs() external {
checkBadInputs("_: frac();", 0, 1, 0);
}

function testOpFracTwoInputs() external {
checkBadInputs("_: frac(1 1);", 2, 1, 2);
}

function testOpFracZeroOutputs() external {
checkBadOutputs(": frac(1);", 1, 1, 0);
}

function testOpFracTwoOutputs() external {
checkBadOutputs("_ _: frac(1);", 1, 1, 2);
}

/// Test that operand is disallowed.
function testOpFracEvalOperandDisallowed() external {
checkUnhappyParse("_: frac<0>(1);", abi.encodeWithSelector(UnexpectedOperand.selector));
}
}
Loading