From aff8b4ab65fffc30f7ab42927b247bcfe20d2168 Mon Sep 17 00:00:00 2001 From: thedavidmeister Date: Fri, 23 Aug 2024 20:42:21 +0400 Subject: [PATCH] wip on operand --- lib/rain.math.float | 2 +- .../RainterpreterReferenceExternNPE2.sol | 13 +- src/lib/op/LibAllStandardOpsNP.sol | 16 +- src/lib/op/bitwise/LibOpCtPopNP.sol | 4 +- src/lib/op/erc20/LibOpERC20Allowance.sol | 120 +++++++-------- src/lib/op/erc20/LibOpERC20BalanceOf.sol | 120 +++++++-------- src/lib/op/erc20/LibOpERC20TotalSupply.sol | 126 +++++++-------- src/lib/op/evm/LibOpBlockNumberNP.sol | 5 +- src/lib/op/evm/LibOpChainIdNP.sol | 5 +- src/lib/op/evm/LibOpTimestampNP.sol | 5 +- src/lib/op/math/LibOpE.sol | 60 ++++---- src/lib/op/math/LibOpScale18.sol | 70 ++++----- src/lib/op/math/LibOpScale18Dynamic.sol | 94 ++++++------ src/lib/op/math/LibOpScaleN.sol | 70 ++++----- src/lib/op/math/LibOpScaleNDynamic.sol | 94 ++++++------ src/lib/parse/LibParseOperand.sol | 32 ++-- .../parse/literal/LibParseLiteralDecimal.sol | 144 +++++++++--------- 17 files changed, 492 insertions(+), 488 deletions(-) diff --git a/lib/rain.math.float b/lib/rain.math.float index 455a0f447..9b63eb73e 160000 --- a/lib/rain.math.float +++ b/lib/rain.math.float @@ -1 +1 @@ -Subproject commit 455a0f4472c7084b751ae062f2a243e6e18cb958 +Subproject commit 9b63eb73e147ddee00465e1147c8d6fbaa2d2f6c diff --git a/src/concrete/extern/RainterpreterReferenceExternNPE2.sol b/src/concrete/extern/RainterpreterReferenceExternNPE2.sol index f326cd140..e6cc78db3 100644 --- a/src/concrete/extern/RainterpreterReferenceExternNPE2.sol +++ b/src/concrete/extern/RainterpreterReferenceExternNPE2.sol @@ -35,6 +35,7 @@ import { INTEGRITY_FUNCTION_POINTERS, OPCODE_FUNCTION_POINTERS } from "../../generated/RainterpreterReferenceExternNPE2.pointers.sol"; +import {LibDecimalFloat} from "rain.math.float/src/lib/LibDecimalFloat.sol"; /// @dev The number of subparser functions available to the parser. This is NOT /// 1:1 with the number of opcodes provided by the extern component of this @@ -246,17 +247,17 @@ contract RainterpreterReferenceExternNPE2 is BaseRainterpreterSubParserNPE2, Bas ParseState memory state = LibParseState.newState("", "", "", ""); // If we have a match on the keyword then the next chars MUST // be a decimal, otherwise it's an error. - uint256 value; - (cursor, value) = LibParseLiteralDecimal.parseDecimal( + int256 signedCoefficient; + int256 exponent; + (cursor, signedCoefficient, exponent) = LibParseLiteralDecimal.parseDecimalFloat( state, cursor + SUB_PARSER_LITERAL_REPEAT_KEYWORD_BYTES_LENGTH, end ); - value = LibFixedPointDecimalScale.scaleToIntegerLossless(value); // We can only repeat a single digit. - if (value > 9) { - revert InvalidRepeatCount(value); + if (LibDecimalFloat.lt(signedCoefficient, exponent, 9, 0)) { + revert InvalidRepeatCount(); } - return (true, SUB_PARSER_LITERAL_REPEAT_INDEX, value); + return (true, SUB_PARSER_LITERAL_REPEAT_INDEX, LibDecimalFloat.pack(signedCoefficient, exponent)); } else { return (false, 0, 0); } diff --git a/src/lib/op/LibAllStandardOpsNP.sol b/src/lib/op/LibAllStandardOpsNP.sol index 747057bd8..725d6cd3c 100644 --- a/src/lib/op/LibAllStandardOpsNP.sol +++ b/src/lib/op/LibAllStandardOpsNP.sol @@ -32,9 +32,9 @@ import {LibOpUint256ERC20Allowance} from "./erc20/uint256/LibOpUint256ERC20Allow import {LibOpUint256ERC20BalanceOf} from "./erc20/uint256/LibOpUint256ERC20BalanceOf.sol"; import {LibOpUint256ERC20TotalSupply} from "./erc20/uint256/LibOpUint256ERC20TotalSupply.sol"; -import {LibOpERC20Allowance} from "./erc20/LibOpERC20Allowance.sol"; -import {LibOpERC20BalanceOf} from "./erc20/LibOpERC20BalanceOf.sol"; -import {LibOpERC20TotalSupply} from "./erc20/LibOpERC20TotalSupply.sol"; +// import {LibOpERC20Allowance} from "./erc20/LibOpERC20Allowance.sol"; +// import {LibOpERC20BalanceOf} from "./erc20/LibOpERC20BalanceOf.sol"; +// import {LibOpERC20TotalSupply} from "./erc20/LibOpERC20TotalSupply.sol"; import {LibOpUint256ERC721BalanceOf} from "./erc721/uint256/LibOpUint256ERC721BalanceOf.sol"; import {LibOpERC721OwnerOf} from "./erc721/LibOpERC721OwnerOf.sol"; @@ -70,7 +70,7 @@ import {LibOpAdd} from "./math/LibOpAdd.sol"; // import {LibOpCeil} from "./math/LibOpCeil.sol"; // import {LibOpMul} from "./math/LibOpMul.sol"; // import {LibOpDiv} from "./math/LibOpDiv.sol"; -import {LibOpE} from "./math/LibOpE.sol"; +// import {LibOpE} from "./math/LibOpE.sol"; // import {LibOpExp} from "./math/LibOpExp.sol"; // import {LibOpExp2} from "./math/LibOpExp2.sol"; // import {LibOpFloor} from "./math/LibOpFloor.sol"; @@ -85,10 +85,10 @@ import {LibOpMin} from "./math/LibOpMin.sol"; import {LibOpMod} from "./math/LibOpMod.sol"; // import {LibOpLog2} from "./math/LibOpLog2.sol"; // import {LibOpPow} from "./math/LibOpPow.sol"; -import {LibOpScale18Dynamic} from "./math/LibOpScale18Dynamic.sol"; -import {LibOpScale18} from "./math/LibOpScale18.sol"; -import {LibOpScaleNDynamic} from "./math/LibOpScaleNDynamic.sol"; -import {LibOpScaleN} from "./math/LibOpScaleN.sol"; +// import {LibOpScale18Dynamic} from "./math/LibOpScale18Dynamic.sol"; +// import {LibOpScale18} from "./math/LibOpScale18.sol"; +// import {LibOpScaleNDynamic} from "./math/LibOpScaleNDynamic.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"; diff --git a/src/lib/op/bitwise/LibOpCtPopNP.sol b/src/lib/op/bitwise/LibOpCtPopNP.sol index e77605ca5..8261423da 100644 --- a/src/lib/op/bitwise/LibOpCtPopNP.sol +++ b/src/lib/op/bitwise/LibOpCtPopNP.sol @@ -28,7 +28,7 @@ library LibOpCtPopNP { value := mload(stackTop) } unchecked { - value = LibCtPop.ctpop(value) * FIXED_POINT_ONE; + value = LibCtPop.ctpop(value); } assembly ("memory-safe") { mstore(stackTop, value) @@ -42,7 +42,7 @@ library LibOpCtPopNP { pure returns (uint256[] memory) { - inputs[0] = LibCtPop.ctpopSlow(inputs[0]) * FIXED_POINT_ONE; + inputs[0] = LibCtPop.ctpopSlow(inputs[0]); return inputs; } } diff --git a/src/lib/op/erc20/LibOpERC20Allowance.sol b/src/lib/op/erc20/LibOpERC20Allowance.sol index ac6f76f8e..4917e06ff 100644 --- a/src/lib/op/erc20/LibOpERC20Allowance.sol +++ b/src/lib/op/erc20/LibOpERC20Allowance.sol @@ -1,68 +1,68 @@ -// SPDX-License-Identifier: CAL -pragma solidity ^0.8.18; +// // SPDX-License-Identifier: CAL +// pragma solidity ^0.8.18; -import {IERC20} from "openzeppelin-contracts/contracts/token/ERC20/IERC20.sol"; -import {Pointer} from "rain.solmem/lib/LibPointer.sol"; -import {IntegrityCheckStateNP} from "../../integrity/LibIntegrityCheckNP.sol"; -import {Operand} from "rain.interpreter.interface/interface/unstable/IInterpreterV4.sol"; -import {InterpreterStateNP} from "../../state/LibInterpreterStateNP.sol"; -import {IERC20Metadata} from "openzeppelin-contracts/contracts/token/ERC20/extensions/IERC20Metadata.sol"; +// import {IERC20} from "openzeppelin-contracts/contracts/token/ERC20/IERC20.sol"; +// import {Pointer} from "rain.solmem/lib/LibPointer.sol"; +// import {IntegrityCheckStateNP} from "../../integrity/LibIntegrityCheckNP.sol"; +// import {Operand} from "rain.interpreter.interface/interface/unstable/IInterpreterV4.sol"; +// import {InterpreterStateNP} from "../../state/LibInterpreterStateNP.sol"; +// import {IERC20Metadata} from "openzeppelin-contracts/contracts/token/ERC20/extensions/IERC20Metadata.sol"; -/// @title LibOpERC20Allowance -/// @notice Opcode for getting the current erc20 allowance of an account. -library LibOpERC20Allowance { - function integrity(IntegrityCheckStateNP memory, Operand) internal pure returns (uint256, uint256) { - // Always 3 inputs, the token, the owner and the spender. - // Always 1 output, the allowance. - return (3, 1); - } +// /// @title LibOpERC20Allowance +// /// @notice Opcode for getting the current erc20 allowance of an account. +// library LibOpERC20Allowance { +// function integrity(IntegrityCheckStateNP memory, Operand) internal pure returns (uint256, uint256) { +// // Always 3 inputs, the token, the owner and the spender. +// // Always 1 output, the allowance. +// return (3, 1); +// } - function run(InterpreterStateNP memory, Operand, Pointer stackTop) internal view returns (Pointer) { - uint256 token; - uint256 owner; - uint256 spender; - assembly ("memory-safe") { - token := mload(stackTop) - owner := mload(add(stackTop, 0x20)) - stackTop := add(stackTop, 0x40) - spender := mload(stackTop) - } - uint256 tokenAllowance = - IERC20(address(uint160(token))).allowance(address(uint160(owner)), address(uint160(spender))); +// function run(InterpreterStateNP memory, Operand, Pointer stackTop) internal view returns (Pointer) { +// uint256 token; +// uint256 owner; +// uint256 spender; +// assembly ("memory-safe") { +// token := mload(stackTop) +// owner := mload(add(stackTop, 0x20)) +// stackTop := add(stackTop, 0x40) +// spender := mload(stackTop) +// } +// uint256 tokenAllowance = +// IERC20(address(uint160(token))).allowance(address(uint160(owner)), address(uint160(spender))); - // This can fail as `decimals` is an OPTIONAL part of the ERC20 standard. - uint256 tokenDecimals = IERC20Metadata(address(uint160(token))).decimals(); - tokenAllowance = LibFixedPointDecimalScale.scale18( - tokenAllowance, - tokenDecimals, - // Saturate scaling as "infinite approve" is a fairly common pattern - // so erroring would make a lot of contracts unusable in practise. - // Rounding down is the default. - FLAG_SATURATE - ); +// // This can fail as `decimals` is an OPTIONAL part of the ERC20 standard. +// uint256 tokenDecimals = IERC20Metadata(address(uint160(token))).decimals(); +// tokenAllowance = LibFixedPointDecimalScale.scale18( +// tokenAllowance, +// tokenDecimals, +// // Saturate scaling as "infinite approve" is a fairly common pattern +// // so erroring would make a lot of contracts unusable in practise. +// // Rounding down is the default. +// FLAG_SATURATE +// ); - assembly ("memory-safe") { - mstore(stackTop, tokenAllowance) - } - return stackTop; - } +// assembly ("memory-safe") { +// mstore(stackTop, tokenAllowance) +// } +// return stackTop; +// } - function referenceFn(InterpreterStateNP memory, Operand, uint256[] memory inputs) - internal - view - returns (uint256[] memory) - { - uint256 token = inputs[0]; - uint256 owner = inputs[1]; - uint256 spender = inputs[2]; +// function referenceFn(InterpreterStateNP memory, Operand, uint256[] memory inputs) +// internal +// view +// returns (uint256[] memory) +// { +// uint256 token = inputs[0]; +// uint256 owner = inputs[1]; +// uint256 spender = inputs[2]; - uint256 tokenDecimals = IERC20Metadata(address(uint160(token))).decimals(); - uint256 tokenAllowance = - IERC20(address(uint160(token))).allowance(address(uint160(owner)), address(uint160(spender))); - tokenAllowance = LibFixedPointDecimalScale.scale18(tokenAllowance, tokenDecimals, FLAG_SATURATE); +// uint256 tokenDecimals = IERC20Metadata(address(uint160(token))).decimals(); +// uint256 tokenAllowance = +// IERC20(address(uint160(token))).allowance(address(uint160(owner)), address(uint160(spender))); +// tokenAllowance = LibFixedPointDecimalScale.scale18(tokenAllowance, tokenDecimals, FLAG_SATURATE); - uint256[] memory outputs = new uint256[](1); - outputs[0] = tokenAllowance; - return outputs; - } -} +// uint256[] memory outputs = new uint256[](1); +// outputs[0] = tokenAllowance; +// return outputs; +// } +// } diff --git a/src/lib/op/erc20/LibOpERC20BalanceOf.sol b/src/lib/op/erc20/LibOpERC20BalanceOf.sol index 9190afe4f..6fa02dfb4 100644 --- a/src/lib/op/erc20/LibOpERC20BalanceOf.sol +++ b/src/lib/op/erc20/LibOpERC20BalanceOf.sol @@ -1,69 +1,69 @@ -// SPDX-License-Identifier: CAL -pragma solidity ^0.8.18; +// // SPDX-License-Identifier: CAL +// pragma solidity ^0.8.18; -import {IERC20} from "openzeppelin-contracts/contracts/token/ERC20/IERC20.sol"; -import {Pointer} from "rain.solmem/lib/LibPointer.sol"; -import {IntegrityCheckStateNP} from "../../integrity/LibIntegrityCheckNP.sol"; -import {Operand} from "rain.interpreter.interface/interface/unstable/IInterpreterV4.sol"; -import {InterpreterStateNP} from "../../state/LibInterpreterStateNP.sol"; -import {IERC20Metadata} from "openzeppelin-contracts/contracts/token/ERC20/extensions/IERC20Metadata.sol"; +// import {IERC20} from "openzeppelin-contracts/contracts/token/ERC20/IERC20.sol"; +// import {Pointer} from "rain.solmem/lib/LibPointer.sol"; +// import {IntegrityCheckStateNP} from "../../integrity/LibIntegrityCheckNP.sol"; +// import {Operand} from "rain.interpreter.interface/interface/unstable/IInterpreterV4.sol"; +// import {InterpreterStateNP} from "../../state/LibInterpreterStateNP.sol"; +// import {IERC20Metadata} from "openzeppelin-contracts/contracts/token/ERC20/extensions/IERC20Metadata.sol"; -/// @title LibOpERC20BalanceOf -/// @notice Opcode for getting the current erc20 balance of an account. -library LibOpERC20BalanceOf { - function integrity(IntegrityCheckStateNP memory, Operand) internal pure returns (uint256, uint256) { - // Always 2 inputs, the token and the account. - // Always 1 output, the balance. - return (2, 1); - } +// /// @title LibOpERC20BalanceOf +// /// @notice Opcode for getting the current erc20 balance of an account. +// library LibOpERC20BalanceOf { +// function integrity(IntegrityCheckStateNP memory, Operand) internal pure returns (uint256, uint256) { +// // Always 2 inputs, the token and the account. +// // Always 1 output, the balance. +// return (2, 1); +// } - function run(InterpreterStateNP memory, Operand, Pointer stackTop) internal view returns (Pointer) { - uint256 token; - uint256 account; - assembly ("memory-safe") { - token := mload(stackTop) - stackTop := add(stackTop, 0x20) - account := mload(stackTop) - } - uint256 tokenBalance = IERC20(address(uint160(token))).balanceOf(address(uint160(account))); +// function run(InterpreterStateNP memory, Operand, Pointer stackTop) internal view returns (Pointer) { +// uint256 token; +// uint256 account; +// assembly ("memory-safe") { +// token := mload(stackTop) +// stackTop := add(stackTop, 0x20) +// account := mload(stackTop) +// } +// uint256 tokenBalance = IERC20(address(uint160(token))).balanceOf(address(uint160(account))); - // This can fail as `decimals` is an OPTIONAL part of the ERC20 standard. - uint256 tokenDecimals = IERC20Metadata(address(uint160(token))).decimals(); - tokenBalance = LibFixedPointDecimalScale.scale18( - tokenBalance, - tokenDecimals, - // Error on overflow as balance is a critical value. - // Rounding down is the default. - 0 - ); +// // This can fail as `decimals` is an OPTIONAL part of the ERC20 standard. +// uint256 tokenDecimals = IERC20Metadata(address(uint160(token))).decimals(); +// tokenBalance = LibFixedPointDecimalScale.scale18( +// tokenBalance, +// tokenDecimals, +// // Error on overflow as balance is a critical value. +// // Rounding down is the default. +// 0 +// ); - assembly ("memory-safe") { - mstore(stackTop, tokenBalance) - } - return stackTop; - } +// assembly ("memory-safe") { +// mstore(stackTop, tokenBalance) +// } +// return stackTop; +// } - function referenceFn(InterpreterStateNP memory, Operand, uint256[] memory inputs) - internal - view - returns (uint256[] memory) - { - uint256 token = inputs[0]; - uint256 account = inputs[1]; +// function referenceFn(InterpreterStateNP memory, Operand, uint256[] memory inputs) +// internal +// view +// returns (uint256[] memory) +// { +// uint256 token = inputs[0]; +// uint256 account = inputs[1]; - uint256 tokenBalance = IERC20(address(uint160(token))).balanceOf(address(uint160(account))); +// uint256 tokenBalance = IERC20(address(uint160(token))).balanceOf(address(uint160(account))); - uint256 tokenDecimals = IERC20Metadata(address(uint160(token))).decimals(); - tokenBalance = LibFixedPointDecimalScale.scale18( - tokenBalance, - tokenDecimals, - // Error on overflow as balance is a critical value. - // Rounding down is the default. - 0 - ); +// uint256 tokenDecimals = IERC20Metadata(address(uint160(token))).decimals(); +// tokenBalance = LibFixedPointDecimalScale.scale18( +// tokenBalance, +// tokenDecimals, +// // Error on overflow as balance is a critical value. +// // Rounding down is the default. +// 0 +// ); - uint256[] memory outputs = new uint256[](1); - outputs[0] = tokenBalance; - return outputs; - } -} +// uint256[] memory outputs = new uint256[](1); +// outputs[0] = tokenBalance; +// return outputs; +// } +// } diff --git a/src/lib/op/erc20/LibOpERC20TotalSupply.sol b/src/lib/op/erc20/LibOpERC20TotalSupply.sol index 56a0161b9..5a0cd4c49 100644 --- a/src/lib/op/erc20/LibOpERC20TotalSupply.sol +++ b/src/lib/op/erc20/LibOpERC20TotalSupply.sol @@ -1,63 +1,63 @@ -// SPDX-License-Identifier: CAL -pragma solidity ^0.8.25; - -import {IERC20} from "openzeppelin-contracts/contracts/interfaces/IERC20.sol"; -import {Pointer} from "rain.solmem/lib/LibPointer.sol"; -import {IntegrityCheckStateNP} from "../../integrity/LibIntegrityCheckNP.sol"; -import {Operand} from "rain.interpreter.interface/interface/unstable/IInterpreterV4.sol"; -import {InterpreterStateNP} from "../../state/LibInterpreterStateNP.sol"; -import {IERC20Metadata} from "openzeppelin-contracts/contracts/token/ERC20/extensions/IERC20Metadata.sol"; - -/// @title LibOpERC20TotalSupply -/// @notice Opcode for ERC20 `totalSupply`. -library LibOpERC20TotalSupply { - function integrity(IntegrityCheckStateNP memory, Operand) internal pure returns (uint256, uint256) { - // Always 1 input, the contract. - // Always 1 output, the total supply. - return (1, 1); - } - - function run(InterpreterStateNP memory, Operand, Pointer stackTop) internal view returns (Pointer) { - uint256 token; - assembly ("memory-safe") { - token := mload(stackTop) - } - uint256 totalSupply = IERC20(address(uint160(token))).totalSupply(); - - uint256 tokenDecimals = IERC20Metadata(address(uint160(token))).decimals(); - totalSupply = LibFixedPointDecimalScale.scale18( - totalSupply, - tokenDecimals, - // Error on overflow as total supply is a critical value. - // Rounding down is the default. - 0 - ); - - assembly ("memory-safe") { - mstore(stackTop, totalSupply) - } - return stackTop; - } - - function referenceFn(InterpreterStateNP memory, Operand, uint256[] memory inputs) - internal - view - returns (uint256[] memory) - { - uint256 account = inputs[0]; - uint256 totalSupply = IERC20(address(uint160(account))).totalSupply(); - - uint256 tokenDecimals = IERC20Metadata(address(uint160(account))).decimals(); - totalSupply = LibFixedPointDecimalScale.scale18( - totalSupply, - tokenDecimals, - // Error on overflow as total supply is a critical value. - // Rounding down is the default. - 0 - ); - - uint256[] memory outputs = new uint256[](1); - outputs[0] = totalSupply; - return outputs; - } -} +// // SPDX-License-Identifier: CAL +// pragma solidity ^0.8.25; + +// import {IERC20} from "openzeppelin-contracts/contracts/interfaces/IERC20.sol"; +// import {Pointer} from "rain.solmem/lib/LibPointer.sol"; +// import {IntegrityCheckStateNP} from "../../integrity/LibIntegrityCheckNP.sol"; +// import {Operand} from "rain.interpreter.interface/interface/unstable/IInterpreterV4.sol"; +// import {InterpreterStateNP} from "../../state/LibInterpreterStateNP.sol"; +// import {IERC20Metadata} from "openzeppelin-contracts/contracts/token/ERC20/extensions/IERC20Metadata.sol"; + +// /// @title LibOpERC20TotalSupply +// /// @notice Opcode for ERC20 `totalSupply`. +// library LibOpERC20TotalSupply { +// function integrity(IntegrityCheckStateNP memory, Operand) internal pure returns (uint256, uint256) { +// // Always 1 input, the contract. +// // Always 1 output, the total supply. +// return (1, 1); +// } + +// function run(InterpreterStateNP memory, Operand, Pointer stackTop) internal view returns (Pointer) { +// uint256 token; +// assembly ("memory-safe") { +// token := mload(stackTop) +// } +// uint256 totalSupply = IERC20(address(uint160(token))).totalSupply(); + +// uint256 tokenDecimals = IERC20Metadata(address(uint160(token))).decimals(); +// totalSupply = LibFixedPointDecimalScale.scale18( +// totalSupply, +// tokenDecimals, +// // Error on overflow as total supply is a critical value. +// // Rounding down is the default. +// 0 +// ); + +// assembly ("memory-safe") { +// mstore(stackTop, totalSupply) +// } +// return stackTop; +// } + +// function referenceFn(InterpreterStateNP memory, Operand, uint256[] memory inputs) +// internal +// view +// returns (uint256[] memory) +// { +// uint256 account = inputs[0]; +// uint256 totalSupply = IERC20(address(uint160(account))).totalSupply(); + +// uint256 tokenDecimals = IERC20Metadata(address(uint160(account))).decimals(); +// totalSupply = LibFixedPointDecimalScale.scale18( +// totalSupply, +// tokenDecimals, +// // Error on overflow as total supply is a critical value. +// // Rounding down is the default. +// 0 +// ); + +// uint256[] memory outputs = new uint256[](1); +// outputs[0] = totalSupply; +// return outputs; +// } +// } diff --git a/src/lib/op/evm/LibOpBlockNumberNP.sol b/src/lib/op/evm/LibOpBlockNumberNP.sol index b6905f58d..4503f22fe 100644 --- a/src/lib/op/evm/LibOpBlockNumberNP.sol +++ b/src/lib/op/evm/LibOpBlockNumberNP.sol @@ -14,10 +14,9 @@ library LibOpBlockNumberNP { } function run(InterpreterStateNP memory, Operand, Pointer stackTop) internal view returns (Pointer) { - uint256 decimalOne = FIXED_POINT_ONE; assembly ("memory-safe") { stackTop := sub(stackTop, 0x20) - mstore(stackTop, mul(number(), decimalOne)) + mstore(stackTop, number()) } return stackTop; } @@ -28,7 +27,7 @@ library LibOpBlockNumberNP { returns (uint256[] memory) { uint256[] memory outputs = new uint256[](1); - outputs[0] = block.number * FIXED_POINT_ONE; + outputs[0] = block.number; return outputs; } } diff --git a/src/lib/op/evm/LibOpChainIdNP.sol b/src/lib/op/evm/LibOpChainIdNP.sol index ff3b502fa..28e7c0cec 100644 --- a/src/lib/op/evm/LibOpChainIdNP.sol +++ b/src/lib/op/evm/LibOpChainIdNP.sol @@ -14,10 +14,9 @@ library LibOpChainIdNP { } function run(InterpreterStateNP memory, Operand, Pointer stackTop) internal view returns (Pointer) { - uint256 decimalOne = FIXED_POINT_ONE; assembly ("memory-safe") { stackTop := sub(stackTop, 0x20) - mstore(stackTop, mul(chainid(), decimalOne)) + mstore(stackTop, chainid()) } return stackTop; } @@ -28,7 +27,7 @@ library LibOpChainIdNP { returns (uint256[] memory) { uint256[] memory outputs = new uint256[](1); - outputs[0] = block.chainid * FIXED_POINT_ONE; + outputs[0] = block.chainid; return outputs; } } diff --git a/src/lib/op/evm/LibOpTimestampNP.sol b/src/lib/op/evm/LibOpTimestampNP.sol index a1769b484..65c5b2017 100644 --- a/src/lib/op/evm/LibOpTimestampNP.sol +++ b/src/lib/op/evm/LibOpTimestampNP.sol @@ -14,10 +14,9 @@ library LibOpTimestampNP { } function run(InterpreterStateNP memory, Operand, Pointer stackTop) internal view returns (Pointer) { - uint256 decimalOne = FIXED_POINT_ONE; assembly ("memory-safe") { stackTop := sub(stackTop, 0x20) - mstore(stackTop, mul(timestamp(), decimalOne)) + mstore(stackTop, timestamp()) } return stackTop; } @@ -28,7 +27,7 @@ library LibOpTimestampNP { returns (uint256[] memory) { uint256[] memory outputs = new uint256[](1); - outputs[0] = block.timestamp * FIXED_POINT_ONE; + outputs[0] = block.timestamp; return outputs; } } diff --git a/src/lib/op/math/LibOpE.sol b/src/lib/op/math/LibOpE.sol index f1c17ee39..55626a9d8 100644 --- a/src/lib/op/math/LibOpE.sol +++ b/src/lib/op/math/LibOpE.sol @@ -1,34 +1,34 @@ -// SPDX-License-Identifier: CAL -pragma solidity ^0.8.18; +// // SPDX-License-Identifier: CAL +// pragma solidity ^0.8.18; -import {Pointer} from "rain.solmem/lib/LibPointer.sol"; -import {Operand} from "rain.interpreter.interface/interface/unstable/IInterpreterV4.sol"; -import {InterpreterStateNP} from "../../state/LibInterpreterStateNP.sol"; -import {IntegrityCheckStateNP} from "../../integrity/LibIntegrityCheckNP.sol"; +// import {Pointer} from "rain.solmem/lib/LibPointer.sol"; +// import {Operand} from "rain.interpreter.interface/interface/unstable/IInterpreterV4.sol"; +// import {InterpreterStateNP} from "../../state/LibInterpreterStateNP.sol"; +// import {IntegrityCheckStateNP} from "../../integrity/LibIntegrityCheckNP.sol"; -/// @title LibOpE -/// Stacks the mathematical constant e. -library LibOpE { - function integrity(IntegrityCheckStateNP memory, Operand) internal pure returns (uint256, uint256) { - return (0, 1); - } +// /// @title LibOpE +// /// Stacks the mathematical constant e. +// library LibOpE { +// function integrity(IntegrityCheckStateNP memory, Operand) internal pure returns (uint256, uint256) { +// return (0, 1); +// } - function run(InterpreterStateNP memory, Operand, Pointer stackTop) internal pure returns (Pointer) { - uint256 e = FIXED_POINT_E; - assembly ("memory-safe") { - stackTop := sub(stackTop, 0x20) - mstore(stackTop, e) - } - return stackTop; - } +// function run(InterpreterStateNP memory, Operand, Pointer stackTop) internal pure returns (Pointer) { +// uint256 e = FIXED_POINT_E; +// assembly ("memory-safe") { +// stackTop := sub(stackTop, 0x20) +// mstore(stackTop, e) +// } +// return stackTop; +// } - function referenceFn(InterpreterStateNP memory, Operand, uint256[] memory) - internal - pure - returns (uint256[] memory) - { - uint256[] memory outputs = new uint256[](1); - outputs[0] = FIXED_POINT_E; - return outputs; - } -} +// function referenceFn(InterpreterStateNP memory, Operand, uint256[] memory) +// internal +// pure +// returns (uint256[] memory) +// { +// uint256[] memory outputs = new uint256[](1); +// outputs[0] = FIXED_POINT_E; +// return outputs; +// } +// } diff --git a/src/lib/op/math/LibOpScale18.sol b/src/lib/op/math/LibOpScale18.sol index 9e889bc03..473b346eb 100644 --- a/src/lib/op/math/LibOpScale18.sol +++ b/src/lib/op/math/LibOpScale18.sol @@ -1,40 +1,40 @@ -// SPDX-License-Identifier: CAL -pragma solidity ^0.8.18; +// // SPDX-License-Identifier: CAL +// pragma solidity ^0.8.18; -import {Operand} from "rain.interpreter.interface/interface/unstable/IInterpreterV4.sol"; -import {InterpreterStateNP} from "../../state/LibInterpreterStateNP.sol"; -import {IntegrityCheckStateNP} from "../../integrity/LibIntegrityCheckNP.sol"; -import {Pointer} from "rain.solmem/lib/LibPointer.sol"; +// import {Operand} from "rain.interpreter.interface/interface/unstable/IInterpreterV4.sol"; +// import {InterpreterStateNP} from "../../state/LibInterpreterStateNP.sol"; +// import {IntegrityCheckStateNP} from "../../integrity/LibIntegrityCheckNP.sol"; +// import {Pointer} from "rain.solmem/lib/LibPointer.sol"; -/// @title LibOpScale18 -/// @notice Opcode for scaling a number to 18 decimal fixed point. -library LibOpScale18 { - using LibFixedPointDecimalScale for uint256; +// /// @title LibOpScale18 +// /// @notice Opcode for scaling a number to 18 decimal fixed point. +// library LibOpScale18 { +// using LibFixedPointDecimalScale for uint256; - function integrity(IntegrityCheckStateNP memory, Operand) internal pure returns (uint256, uint256) { - return (1, 1); - } +// function integrity(IntegrityCheckStateNP memory, Operand) internal pure returns (uint256, uint256) { +// return (1, 1); +// } - /// scale-18 - /// 18 decimal fixed point scaling. - function run(InterpreterStateNP memory, Operand operand, Pointer stackTop) internal pure returns (Pointer) { - uint256 a; - assembly ("memory-safe") { - a := mload(stackTop) - } - a = a.scale18(Operand.unwrap(operand) & 0xFF, Operand.unwrap(operand) >> 8); - assembly ("memory-safe") { - mstore(stackTop, a) - } - return stackTop; - } +// /// scale-18 +// /// 18 decimal fixed point scaling. +// function run(InterpreterStateNP memory, Operand operand, Pointer stackTop) internal pure returns (Pointer) { +// uint256 a; +// assembly ("memory-safe") { +// a := mload(stackTop) +// } +// a = a.scale18(Operand.unwrap(operand) & 0xFF, Operand.unwrap(operand) >> 8); +// assembly ("memory-safe") { +// mstore(stackTop, a) +// } +// return stackTop; +// } - function referenceFn(InterpreterStateNP memory, Operand operand, uint256[] memory inputs) - internal - pure - returns (uint256[] memory outputs) - { - outputs = new uint256[](1); - outputs[0] = inputs[0].scale18(Operand.unwrap(operand) & 0xFF, Operand.unwrap(operand) >> 8); - } -} +// function referenceFn(InterpreterStateNP memory, Operand operand, uint256[] memory inputs) +// internal +// pure +// returns (uint256[] memory outputs) +// { +// outputs = new uint256[](1); +// outputs[0] = inputs[0].scale18(Operand.unwrap(operand) & 0xFF, Operand.unwrap(operand) >> 8); +// } +// } diff --git a/src/lib/op/math/LibOpScale18Dynamic.sol b/src/lib/op/math/LibOpScale18Dynamic.sol index ed0beedaf..d64f3dc4a 100644 --- a/src/lib/op/math/LibOpScale18Dynamic.sol +++ b/src/lib/op/math/LibOpScale18Dynamic.sol @@ -1,52 +1,52 @@ -// SPDX-License-Identifier: CAL -pragma solidity ^0.8.18; +// // SPDX-License-Identifier: CAL +// pragma solidity ^0.8.18; -import {MASK_2BIT} from "sol.lib.binmaskflag/Binary.sol"; -import {Operand} from "rain.interpreter.interface/interface/unstable/IInterpreterV4.sol"; -import {Pointer} from "rain.solmem/lib/LibPointer.sol"; -import {IntegrityCheckStateNP} from "../../integrity/LibIntegrityCheckNP.sol"; -import {InterpreterStateNP} from "../../state/LibInterpreterStateNP.sol"; -import {LibParseLiteral} from "../../parse/literal/LibParseLiteral.sol"; +// import {MASK_2BIT} from "sol.lib.binmaskflag/Binary.sol"; +// import {Operand} from "rain.interpreter.interface/interface/unstable/IInterpreterV4.sol"; +// import {Pointer} from "rain.solmem/lib/LibPointer.sol"; +// import {IntegrityCheckStateNP} from "../../integrity/LibIntegrityCheckNP.sol"; +// import {InterpreterStateNP} from "../../state/LibInterpreterStateNP.sol"; +// import {LibParseLiteral} from "../../parse/literal/LibParseLiteral.sol"; -/// @title LibOpScale18Dynamic -/// @notice Opcode for scaling a number to 18 decimal fixed point based on -/// runtime scale input. -library LibOpScale18Dynamic { - using LibFixedPointDecimalScale for uint256; +// /// @title LibOpScale18Dynamic +// /// @notice Opcode for scaling a number to 18 decimal fixed point based on +// /// runtime scale input. +// library LibOpScale18Dynamic { +// using LibFixedPointDecimalScale for uint256; - function integrity(IntegrityCheckStateNP memory, Operand) internal pure returns (uint256, uint256) { - return (2, 1); - } +// function integrity(IntegrityCheckStateNP memory, Operand) internal pure returns (uint256, uint256) { +// return (2, 1); +// } - /// scale-18-dynamic - /// 18 decimal fixed point scaling from runtime value. - function run(InterpreterStateNP memory, Operand operand, Pointer stackTop) internal pure returns (Pointer) { - uint256 a; - uint256 scale; - assembly ("memory-safe") { - scale := mload(stackTop) - stackTop := add(stackTop, 0x20) - a := mload(stackTop) - } - // There's no upper bound because we might be saturating all the way to - // infinity. `scale18` will handle catching such things. - scale = LibFixedPointDecimalScale.decimalOrIntToInt(scale, DECIMAL_MAX_SAFE_INT); - a = a.scale18(scale, Operand.unwrap(operand)); - assembly ("memory-safe") { - mstore(stackTop, a) - } - return stackTop; - } +// /// scale-18-dynamic +// /// 18 decimal fixed point scaling from runtime value. +// function run(InterpreterStateNP memory, Operand operand, Pointer stackTop) internal pure returns (Pointer) { +// uint256 a; +// uint256 scale; +// assembly ("memory-safe") { +// scale := mload(stackTop) +// stackTop := add(stackTop, 0x20) +// a := mload(stackTop) +// } +// // There's no upper bound because we might be saturating all the way to +// // infinity. `scale18` will handle catching such things. +// scale = LibFixedPointDecimalScale.decimalOrIntToInt(scale, DECIMAL_MAX_SAFE_INT); +// a = a.scale18(scale, Operand.unwrap(operand)); +// assembly ("memory-safe") { +// mstore(stackTop, a) +// } +// return stackTop; +// } - function referenceFn(InterpreterStateNP memory, Operand operand, uint256[] memory inputs) - internal - pure - returns (uint256[] memory outputs) - { - outputs = new uint256[](1); - outputs[0] = inputs[1].scale18( - LibFixedPointDecimalScale.decimalOrIntToInt(inputs[0], DECIMAL_MAX_SAFE_INT), - Operand.unwrap(operand) & MASK_2BIT - ); - } -} +// function referenceFn(InterpreterStateNP memory, Operand operand, uint256[] memory inputs) +// internal +// pure +// returns (uint256[] memory outputs) +// { +// outputs = new uint256[](1); +// outputs[0] = inputs[1].scale18( +// LibFixedPointDecimalScale.decimalOrIntToInt(inputs[0], DECIMAL_MAX_SAFE_INT), +// Operand.unwrap(operand) & MASK_2BIT +// ); +// } +// } diff --git a/src/lib/op/math/LibOpScaleN.sol b/src/lib/op/math/LibOpScaleN.sol index 11f4f2bda..905c73848 100644 --- a/src/lib/op/math/LibOpScaleN.sol +++ b/src/lib/op/math/LibOpScaleN.sol @@ -1,40 +1,40 @@ -// SPDX-License-Identifier: CAL -pragma solidity ^0.8.18; +// // SPDX-License-Identifier: CAL +// pragma solidity ^0.8.18; -import {Operand} from "rain.interpreter.interface/interface/unstable/IInterpreterV4.sol"; -import {InterpreterStateNP} from "../../state/LibInterpreterStateNP.sol"; -import {IntegrityCheckStateNP} from "../../integrity/LibIntegrityCheckNP.sol"; -import {Pointer} from "rain.solmem/lib/LibPointer.sol"; +// import {Operand} from "rain.interpreter.interface/interface/unstable/IInterpreterV4.sol"; +// import {InterpreterStateNP} from "../../state/LibInterpreterStateNP.sol"; +// import {IntegrityCheckStateNP} from "../../integrity/LibIntegrityCheckNP.sol"; +// import {Pointer} from "rain.solmem/lib/LibPointer.sol"; -/// @title LibOpScaleN -/// @notice Opcode for scaling a decimal18 number to some other scale N. -library LibOpScaleN { - using LibFixedPointDecimalScale for uint256; +// /// @title LibOpScaleN +// /// @notice Opcode for scaling a decimal18 number to some other scale N. +// library LibOpScaleN { +// using LibFixedPointDecimalScale for uint256; - function integrity(IntegrityCheckStateNP memory, Operand) internal pure returns (uint256, uint256) { - return (1, 1); - } +// function integrity(IntegrityCheckStateNP memory, Operand) internal pure returns (uint256, uint256) { +// return (1, 1); +// } - /// scale-n - /// Scale from 18 decimal to n decimal. - function run(InterpreterStateNP memory, Operand operand, Pointer stackTop) internal pure returns (Pointer) { - uint256 a; - assembly ("memory-safe") { - a := mload(stackTop) - } - a = a.scaleN(Operand.unwrap(operand) & 0xFF, Operand.unwrap(operand) >> 8); - assembly ("memory-safe") { - mstore(stackTop, a) - } - return stackTop; - } +// /// scale-n +// /// Scale from 18 decimal to n decimal. +// function run(InterpreterStateNP memory, Operand operand, Pointer stackTop) internal pure returns (Pointer) { +// uint256 a; +// assembly ("memory-safe") { +// a := mload(stackTop) +// } +// a = a.scaleN(Operand.unwrap(operand) & 0xFF, Operand.unwrap(operand) >> 8); +// assembly ("memory-safe") { +// mstore(stackTop, a) +// } +// return stackTop; +// } - function referenceFn(InterpreterStateNP memory, Operand operand, uint256[] memory inputs) - internal - pure - returns (uint256[] memory outputs) - { - outputs = new uint256[](1); - outputs[0] = inputs[0].scaleN(Operand.unwrap(operand) & 0xFF, Operand.unwrap(operand) >> 8); - } -} +// function referenceFn(InterpreterStateNP memory, Operand operand, uint256[] memory inputs) +// internal +// pure +// returns (uint256[] memory outputs) +// { +// outputs = new uint256[](1); +// outputs[0] = inputs[0].scaleN(Operand.unwrap(operand) & 0xFF, Operand.unwrap(operand) >> 8); +// } +// } diff --git a/src/lib/op/math/LibOpScaleNDynamic.sol b/src/lib/op/math/LibOpScaleNDynamic.sol index f56d92258..56a2aeeba 100644 --- a/src/lib/op/math/LibOpScaleNDynamic.sol +++ b/src/lib/op/math/LibOpScaleNDynamic.sol @@ -1,52 +1,52 @@ -// SPDX-License-Identifier: CAL -pragma solidity ^0.8.18; +// // SPDX-License-Identifier: CAL +// pragma solidity ^0.8.18; -import {Operand} from "rain.interpreter.interface/interface/unstable/IInterpreterV4.sol"; -import {IntegrityCheckStateNP} from "../../integrity/LibIntegrityCheckNP.sol"; -import {InterpreterStateNP} from "../../state/LibInterpreterStateNP.sol"; -import {Pointer} from "rain.solmem/lib/LibPointer.sol"; -import {MASK_2BIT} from "sol.lib.binmaskflag/Binary.sol"; -import {LibParseLiteral} from "../../parse/literal/LibParseLiteral.sol"; +// import {Operand} from "rain.interpreter.interface/interface/unstable/IInterpreterV4.sol"; +// import {IntegrityCheckStateNP} from "../../integrity/LibIntegrityCheckNP.sol"; +// import {InterpreterStateNP} from "../../state/LibInterpreterStateNP.sol"; +// import {Pointer} from "rain.solmem/lib/LibPointer.sol"; +// import {MASK_2BIT} from "sol.lib.binmaskflag/Binary.sol"; +// import {LibParseLiteral} from "../../parse/literal/LibParseLiteral.sol"; -/// @title LibOpScaleNDynamic -/// @notice Opcode for scaling a number from 18 decimal fixed point based on -/// runtime scale input. -library LibOpScaleNDynamic { - using LibFixedPointDecimalScale for uint256; +// /// @title LibOpScaleNDynamic +// /// @notice Opcode for scaling a number from 18 decimal fixed point based on +// /// runtime scale input. +// library LibOpScaleNDynamic { +// using LibFixedPointDecimalScale for uint256; - function integrity(IntegrityCheckStateNP memory, Operand) internal pure returns (uint256, uint256) { - return (2, 1); - } +// function integrity(IntegrityCheckStateNP memory, Operand) internal pure returns (uint256, uint256) { +// return (2, 1); +// } - /// scaleN-dynamic - /// 18 decimal fixed point scaling from runtime value. - function run(InterpreterStateNP memory, Operand operand, Pointer stackTop) internal pure returns (Pointer) { - uint256 a; - uint256 scale; - assembly ("memory-safe") { - scale := mload(stackTop) - stackTop := add(stackTop, 0x20) - a := mload(stackTop) - } - a = a.scaleN( - LibFixedPointDecimalScale.decimalOrIntToInt(scale, DECIMAL_MAX_SAFE_INT), - Operand.unwrap(operand) & MASK_2BIT - ); - assembly ("memory-safe") { - mstore(stackTop, a) - } - return stackTop; - } +// /// scaleN-dynamic +// /// 18 decimal fixed point scaling from runtime value. +// function run(InterpreterStateNP memory, Operand operand, Pointer stackTop) internal pure returns (Pointer) { +// uint256 a; +// uint256 scale; +// assembly ("memory-safe") { +// scale := mload(stackTop) +// stackTop := add(stackTop, 0x20) +// a := mload(stackTop) +// } +// a = a.scaleN( +// LibFixedPointDecimalScale.decimalOrIntToInt(scale, DECIMAL_MAX_SAFE_INT), +// Operand.unwrap(operand) & MASK_2BIT +// ); +// assembly ("memory-safe") { +// mstore(stackTop, a) +// } +// return stackTop; +// } - function referenceFn(InterpreterStateNP memory, Operand operand, uint256[] memory inputs) - internal - pure - returns (uint256[] memory outputs) - { - outputs = new uint256[](1); - outputs[0] = inputs[1].scaleN( - LibFixedPointDecimalScale.decimalOrIntToInt(inputs[0], DECIMAL_MAX_SAFE_INT), - Operand.unwrap(operand) & MASK_2BIT - ); - } -} +// function referenceFn(InterpreterStateNP memory, Operand operand, uint256[] memory inputs) +// internal +// pure +// returns (uint256[] memory outputs) +// { +// outputs = new uint256[](1); +// outputs[0] = inputs[1].scaleN( +// LibFixedPointDecimalScale.decimalOrIntToInt(inputs[0], DECIMAL_MAX_SAFE_INT), +// Operand.unwrap(operand) & MASK_2BIT +// ); +// } +// } diff --git a/src/lib/parse/LibParseOperand.sol b/src/lib/parse/LibParseOperand.sol index 2a39f623c..51ec8d768 100644 --- a/src/lib/parse/LibParseOperand.sol +++ b/src/lib/parse/LibParseOperand.sol @@ -15,6 +15,7 @@ import {CMASK_OPERAND_END, CMASK_WHITESPACE, CMASK_OPERAND_START} from "./LibPar import {ParseState, OPERAND_VALUES_LENGTH, FSM_YANG_MASK} from "./LibParseState.sol"; import {LibParseError} from "./LibParseError.sol"; import {LibParseInterstitial} from "./LibParseInterstitial.sol"; +import {LibDecimalFloat} from "rain.math.float/src/lib/LibDecimalFloat.sol"; library LibParseOperand { using LibParseError for ParseState; @@ -156,9 +157,8 @@ library LibParseOperand { assembly ("memory-safe") { operand := mload(add(values, 0x20)) } - operand = Operand.wrap( - LibFixedPointDecimalScale.decimalOrIntToInt(Operand.unwrap(operand), uint256(type(uint16).max)) - ); + (int256 signedCoefficient, int256 exponent) = LibDecimalFloat.unpack(operand); + operand = Operand.wrap(LibDecimalFloat.toFixedDecimalLossless(signedCoefficient, exponent, 0)); } else if (values.length == 0) { operand = Operand.wrap(0); } else { @@ -173,9 +173,8 @@ library LibParseOperand { assembly ("memory-safe") { operand := mload(add(values, 0x20)) } - operand = Operand.wrap( - LibFixedPointDecimalScale.decimalOrIntToInt(Operand.unwrap(operand), uint256(type(uint16).max)) - ); + (int256 signedCoefficient, int256 exponent) = LibDecimalFloat.unpack(Operand.unwrap(operand)); + operand = Operand.wrap(LibDecimalFloat.toFixedDecimalLossless(signedCoefficient, exponent, 0)); } else if (values.length == 0) { revert ExpectedOperand(); } else { @@ -194,8 +193,10 @@ library LibParseOperand { a := mload(add(values, 0x20)) b := mload(add(values, 0x40)) } - a = LibFixedPointDecimalScale.decimalOrIntToInt(a, type(uint8).max); - b = LibFixedPointDecimalScale.decimalOrIntToInt(b, type(uint8).max); + (int256 signedCoefficient, int256 exponent) = LibDecimalFloat.unpack(a); + a = LibDecimalFloat.toFixedDecimalLossless(signedCoefficient, exponent, 0); + (signedCoefficient, exponent) = LibDecimalFloat.unpack(b); + b = LibDecimalFloat.toFixedDecimalLossless(signedCoefficient, exponent, 0); operand = Operand.wrap(a | (b << 8)); } else if (values.length < 2) { @@ -234,9 +235,12 @@ library LibParseOperand { c = 0; } - a = LibFixedPointDecimalScale.decimalOrIntToInt(a, type(uint8).max); - b = LibFixedPointDecimalScale.decimalOrIntToInt(b, 1); - c = LibFixedPointDecimalScale.decimalOrIntToInt(c, 1); + (int256 signedCoefficient, int256 exponent) = LibDecimalFloat.unpack(a); + a = LibDecimalFloat.toFixedDecimalLossless(signedCoefficient, exponent, 0); + (signedCoefficient, exponent) = LibDecimalFloat.unpack(b); + b = LibDecimalFloat.toFixedDecimalLossless(signedCoefficient, exponent, 0); + (signedCoefficient, exponent) = LibDecimalFloat.unpack(c); + c = LibDecimalFloat.toFixedDecimalLossless(signedCoefficient, exponent, 0); operand = Operand.wrap(a | (b << 8) | (c << 9)); } else if (length == 0) { @@ -270,8 +274,10 @@ library LibParseOperand { b = 0; } - a = LibFixedPointDecimalScale.decimalOrIntToInt(a, 1); - b = LibFixedPointDecimalScale.decimalOrIntToInt(b, 1); + (int256 signedCoefficient, int256 exponent) = LibDecimalFloat.unpack(a); + a = LibDecimalFloat.toFixedDecimalLossless(signedCoefficient, exponent, 0); + (signedCoefficient, exponent) = LibDecimalFloat.unpack(b); + b = LibDecimalFloat.toFixedDecimalLossless(signedCoefficient, exponent, 0); operand = Operand.wrap(a | (b << 1)); } else { diff --git a/src/lib/parse/literal/LibParseLiteralDecimal.sol b/src/lib/parse/literal/LibParseLiteralDecimal.sol index 5b7634580..cd755a619 100644 --- a/src/lib/parse/literal/LibParseLiteralDecimal.sol +++ b/src/lib/parse/literal/LibParseLiteralDecimal.sol @@ -207,86 +207,86 @@ library LibParseLiteralDecimal { } } - /// Returns cursor after, value - function parseDecimal(ParseState memory state, uint256 cursor, uint256 end) - internal - pure - returns (uint256, uint256) - { - uint256 fracOffset = 0; - uint256 fracValue = 0; - uint256 scale = 0; - uint256 intValue = 0; + // /// Returns cursor after, value + // function parseDecimal(ParseState memory state, uint256 cursor, uint256 end) + // internal + // pure + // returns (uint256, uint256) + // { + // uint256 fracOffset = 0; + // uint256 fracValue = 0; + // uint256 scale = 0; + // uint256 intValue = 0; - unchecked { - { - uint256 start = cursor; - cursor = LibParse.skipMask(cursor, end, CMASK_NUMERIC_0_9); - if (cursor == start) { - revert ZeroLengthDecimal(state.parseErrorOffset(cursor)); - } - intValue = state.unsafeStrToInt(start, cursor); - } + // unchecked { + // { + // uint256 start = cursor; + // cursor = LibParse.skipMask(cursor, end, CMASK_NUMERIC_0_9); + // if (cursor == start) { + // revert ZeroLengthDecimal(state.parseErrorOffset(cursor)); + // } + // intValue = state.unsafeStrToInt(start, cursor); + // } - uint256 isFrac = LibParse.isMask(cursor, end, CMASK_DECIMAL_POINT); - if (isFrac == 1) { - cursor++; - uint256 fracStart = cursor; - cursor = LibParse.skipMask(cursor, end, CMASK_NUMERIC_0_9); - if (cursor == fracStart) { - revert MalformedDecimalPoint(state.parseErrorOffset(cursor)); - } + // uint256 isFrac = LibParse.isMask(cursor, end, CMASK_DECIMAL_POINT); + // if (isFrac == 1) { + // cursor++; + // uint256 fracStart = cursor; + // cursor = LibParse.skipMask(cursor, end, CMASK_NUMERIC_0_9); + // if (cursor == fracStart) { + // revert MalformedDecimalPoint(state.parseErrorOffset(cursor)); + // } - // Trailing zeros are allowed in fractional literals but should - // not be counted in the precision. - uint256 nonZeroCursor = cursor; - while (LibParse.isMask(nonZeroCursor - 1, end, CMASK_ZERO) == 1) { - nonZeroCursor--; - } + // // Trailing zeros are allowed in fractional literals but should + // // not be counted in the precision. + // uint256 nonZeroCursor = cursor; + // while (LibParse.isMask(nonZeroCursor - 1, end, CMASK_ZERO) == 1) { + // nonZeroCursor--; + // } - fracValue = state.unsafeStrToInt(fracStart, nonZeroCursor); - fracOffset = nonZeroCursor - fracStart; - } + // fracValue = state.unsafeStrToInt(fracStart, nonZeroCursor); + // fracOffset = nonZeroCursor - fracStart; + // } - uint256 eValue = 0; - uint256 eNeg = 0; - if (LibParse.isMask(cursor, end, CMASK_E_NOTATION) > 0) { - cursor++; - eNeg = LibParse.isMask(cursor, end, CMASK_NEGATIVE_SIGN); - cursor += eNeg; + // uint256 eValue = 0; + // uint256 eNeg = 0; + // if (LibParse.isMask(cursor, end, CMASK_E_NOTATION) > 0) { + // cursor++; + // eNeg = LibParse.isMask(cursor, end, CMASK_NEGATIVE_SIGN); + // cursor += eNeg; - uint256 eStart = cursor; - cursor = LibParse.skipMask(cursor, end, CMASK_NUMERIC_0_9); - if (cursor == eStart) { - revert MalformedExponentDigits(state.parseErrorOffset(cursor)); - } - eValue = state.unsafeStrToInt(eStart, cursor); - } + // uint256 eStart = cursor; + // cursor = LibParse.skipMask(cursor, end, CMASK_NUMERIC_0_9); + // if (cursor == eStart) { + // revert MalformedExponentDigits(state.parseErrorOffset(cursor)); + // } + // eValue = state.unsafeStrToInt(eStart, cursor); + // } - { - // If this is a fractional number, then we need to scale it up to - // 1e18 being "one". Otherwise we treat as an integer. - if (eNeg > 0) { - if (DECIMAL_SCALE < eValue) { - revert DecimalLiteralPrecisionLoss(state.parseErrorOffset(cursor)); - } - scale = DECIMAL_SCALE - eValue; - } else { - scale = DECIMAL_SCALE + eValue; - } - } + // { + // // If this is a fractional number, then we need to scale it up to + // // 1e18 being "one". Otherwise we treat as an integer. + // if (eNeg > 0) { + // if (DECIMAL_SCALE < eValue) { + // revert DecimalLiteralPrecisionLoss(state.parseErrorOffset(cursor)); + // } + // scale = DECIMAL_SCALE - eValue; + // } else { + // scale = DECIMAL_SCALE + eValue; + // } + // } - if (scale >= 77) { - revert DecimalLiteralOverflow(state.parseErrorOffset(cursor)); - } + // if (scale >= 77) { + // revert DecimalLiteralOverflow(state.parseErrorOffset(cursor)); + // } - if (scale < fracOffset) { - revert DecimalLiteralPrecisionLoss(state.parseErrorOffset(cursor)); - } - } + // if (scale < fracOffset) { + // revert DecimalLiteralPrecisionLoss(state.parseErrorOffset(cursor)); + // } + // } - // Do this bit with checked math in case we missed an edge case above - // that causes overflow. - return (cursor, intValue * (10 ** scale) + fracValue * (10 ** (scale - fracOffset))); - } + // // Do this bit with checked math in case we missed an edge case above + // // that causes overflow. + // return (cursor, intValue * (10 ** scale) + fracValue * (10 ** (scale - fracOffset))); + // } }