Skip to content

2025 07 12 mul - #355

Merged
thedavidmeister merged 7 commits into
mainfrom
2025-07-12-mul
Jul 12, 2025
Merged

2025 07 12 mul#355
thedavidmeister merged 7 commits into
mainfrom
2025-07-12-mul

Conversation

@thedavidmeister

@thedavidmeister thedavidmeister commented Jul 12, 2025

Copy link
Copy Markdown
Contributor

Motivation

Solution

Checks

By submitting this for review, I'm confirming I've done the following:

  • made this PR as small as possible
  • unit-tested any new functionality
  • linked any relevant issues or PRs
  • included screenshots (if this involves a front-end change)

Summary by CodeRabbit

  • New Features

    • Enabled multiplication ("mul") as a standard operation, allowing users to multiply multiple floating point numbers.
  • Refactor

    • Updated multiplication logic to use floating point arithmetic instead of fixed point, improving flexibility and accuracy for a wider range of values.
  • Tests

    • Enhanced and expanded tests to cover the new floating point multiplication, including overflow scenarios and operand handling.

@coderabbitai

coderabbitai Bot commented Jul 12, 2025

Copy link
Copy Markdown
Contributor

Important

Review skipped

Review was skipped due to path filters

⛔ Files ignored due to path filters (1)
  • src/generated/RainterpreterExpressionDeployer.pointers.sol is excluded by !**/generated/**

CodeRabbit blocks several paths by default. You can override this behavior by explicitly including those paths in the path filters. For example, including **/dist/** will override the default block on the dist directory, by removing the pattern from both the lists.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Walkthrough

The changes enable and fully integrate the multiplication opcode (LibOpMul) into the standard operations set, updating the implementation to use decimal floating-point arithmetic instead of fixed-point. Associated tests are refactored to match the new floating-point logic and error handling. No other opcodes or logic are affected.

Changes

File(s) Change Summary
src/lib/op/LibAllStandardOps.sol Enabled import and integration of LibOpMul into standard ops: incremented ops count, added "mul" to metadata, and activated all relevant function pointers for parsing, integrity, and execution.
src/lib/op/math/LibOpMul.sol Refactored multiplication opcode to use decimal floating-point arithmetic; updated all logic, function signatures, and error handling to match new math type; removed fixed-point dependencies.
test/src/lib/op/math/LibOpMul.t.sol Uncommented and refactored tests for LibOpMul to use floating-point types, updated for new error handling, and adjusted evaluation logic to match floating-point implementation.

Sequence Diagram(s)

sequenceDiagram
    participant Interpreter as Interpreter
    participant LibAllStandardOps as LibAllStandardOps
    participant LibOpMul as LibOpMul
    participant Stack as Stack

    Interpreter->>LibAllStandardOps: Request "mul" opcode execution
    LibAllStandardOps->>LibOpMul: Call integrity() for operand validation
    LibAllStandardOps->>LibOpMul: Call run() with operand and stack pointer
    LibOpMul->>Stack: Load N floating-point values
    LibOpMul->>LibOpMul: Multiply all values using floating-point math
    LibOpMul->>Stack: Store result back on stack
    LibAllStandardOps->>Interpreter: Return result
Loading

Possibly related PRs

  • rainlanguage/rain.interpreter#352: Enables and integrates the subtraction opcode (LibOpSub) with floating-point arithmetic, following a parallel pattern for a different arithmetic operation.
  • rainlanguage/rain.interpreter#351: Enables and integrates the division opcode (LibOpDiv), updating standard ops and function pointers similarly for division.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@coderabbitai coderabbitai Bot left a comment

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.

Actionable comments posted: 1

📜 Review details

Configuration used: CodeRabbit UI
Review profile: ASSERTIVE
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 89c23d2 and 2cd35c6.

⛔ Files ignored due to path filters (3)
  • src/generated/Rainterpreter.pointers.sol is excluded by !**/generated/**
  • src/generated/RainterpreterExpressionDeployer.pointers.sol is excluded by !**/generated/**
  • src/generated/RainterpreterParser.pointers.sol is excluded by !**/generated/**
📒 Files selected for processing (3)
  • src/lib/op/LibAllStandardOps.sol (6 hunks)
  • src/lib/op/math/LibOpMul.sol (1 hunks)
  • test/src/lib/op/math/LibOpMul.t.sol (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (10)
  • GitHub Check: rainix (ubuntu-latest, test-wasm-build)
  • GitHub Check: rainix (ubuntu-latest, rainix-sol-test)
  • GitHub Check: rainix (ubuntu-latest, rainix-sol-artifacts)
  • GitHub Check: rainix (ubuntu-latest, rainix-rs-static)
  • GitHub Check: rainix (macos-latest, rainix-rs-artifacts)
  • GitHub Check: rainix (ubuntu-latest, rainix-sol-static)
  • GitHub Check: rainix (ubuntu-latest, rainix-rs-test)
  • GitHub Check: rainix (ubuntu-latest, rainix-rs-artifacts)
  • GitHub Check: rainix (macos-latest, rainix-rs-test)
  • GitHub Check: git-clean
🔇 Additional comments (4)
src/lib/op/LibAllStandardOps.sol (1)

72-72: Complete and correct integration of multiplication opcode.

The multiplication opcode has been properly integrated into the standard operations set with all required components:

  • Import statement uncommented
  • ALL_STANDARD_OPS_LENGTH correctly incremented to 30
  • Authoring metadata added with appropriate description
  • Operand handler, integrity, and run function pointers properly configured

The alphabetical ordering is maintained across all sections.

Also applies to: 108-108, 274-274, 488-489, 608-608, 720-720

src/lib/op/math/LibOpMul.sol (2)

15-20: Integrity function correctly handles operand parsing.

The function properly extracts the input count from the operand and enforces a minimum of 2 inputs, which is appropriate for multiplication. The bit manipulation and output configuration are correct.


22-52: Efficient implementation of floating-point multiplication.

The run function correctly implements variable-input multiplication using decimal floating-point arithmetic. The assembly blocks are properly marked as memory-safe and efficiently handle stack operations. Overflow handling is appropriately delegated to the LibDecimalFloat.mul function.

test/src/lib/op/math/LibOpMul.t.sol (1)

12-163: Comprehensive test coverage for floating-point multiplication.

The test suite thoroughly covers all aspects of the multiplication opcode:

  • Integrity checks for input validation
  • Runtime behavior with proper overflow error handling
  • Evaluation tests covering edge cases and normal operations
  • Verification that operands are correctly disallowed

The tests properly use the new floating-point types and error selectors (CoefficientOverflow and ExponentOverflow).

Comment on lines +54 to +98
/// Gas intensive reference implementation of multiplication 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 a;
uint256 overflows = 0;
(int256 signedCoefficientA, int256 exponentA) =
LibDecimalFloat.unpack(Float.wrap(StackItem.unwrap(inputs[0])));
if (int32(exponentA) != exponentA) {
overflows++;
}
for (uint256 i = 1; i < inputs.length; i++) {
(int256 signedCoefficientB, int256 exponentB) =
LibDecimalFloat.unpack(Float.wrap(StackItem.unwrap(inputs[i])));
if (int32(exponentB) != exponentB) {
overflows++;
break;
}

(signedCoefficientA, exponentA) =
LibDecimalFloatImplementation.mul(signedCoefficientA, exponentA, signedCoefficientB, exponentB);

if (int32(exponentA) != exponentA) {
overflows++;
break;
}
}
outputs = new StackItem[](1);

if (overflows > 0) {
a = Float.wrap(keccak256(abi.encodePacked("overflow sentinel")));
} else {
a = LibDecimalFloat.packLossless(signedCoefficientA, exponentA);
}

outputs[0] = StackItem.wrap(Float.unwrap(a));

return outputs;
}
}

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.

🧹 Nitpick (assertive)

Well-structured reference implementation with explicit overflow detection.

The reference function provides a comprehensive test implementation with explicit overflow checking for exponents that exceed int32 bounds. The unchecked block is appropriate for testing scenarios.

Consider using a constant for the overflow sentinel instead of the magic string:

+    bytes32 constant OVERFLOW_SENTINEL = keccak256("overflow sentinel");
     
     function referenceFn(InterpreterState memory, OperandV2, StackItem[] memory inputs)
         internal
         pure
         returns (StackItem[] memory outputs)
     {
         // ... existing code ...
         
         if (overflows > 0) {
-            a = Float.wrap(keccak256(abi.encodePacked("overflow sentinel")));
+            a = Float.wrap(OVERFLOW_SENTINEL);
         } else {
📝 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
/// Gas intensive reference implementation of multiplication 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 a;
uint256 overflows = 0;
(int256 signedCoefficientA, int256 exponentA) =
LibDecimalFloat.unpack(Float.wrap(StackItem.unwrap(inputs[0])));
if (int32(exponentA) != exponentA) {
overflows++;
}
for (uint256 i = 1; i < inputs.length; i++) {
(int256 signedCoefficientB, int256 exponentB) =
LibDecimalFloat.unpack(Float.wrap(StackItem.unwrap(inputs[i])));
if (int32(exponentB) != exponentB) {
overflows++;
break;
}
(signedCoefficientA, exponentA) =
LibDecimalFloatImplementation.mul(signedCoefficientA, exponentA, signedCoefficientB, exponentB);
if (int32(exponentA) != exponentA) {
overflows++;
break;
}
}
outputs = new StackItem[](1);
if (overflows > 0) {
a = Float.wrap(keccak256(abi.encodePacked("overflow sentinel")));
} else {
a = LibDecimalFloat.packLossless(signedCoefficientA, exponentA);
}
outputs[0] = StackItem.wrap(Float.unwrap(a));
return outputs;
}
}
// Add at contract scope, above referenceFn
bytes32 constant OVERFLOW_SENTINEL = keccak256("overflow sentinel");
/// Gas intensive reference implementation of multiplication 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 a;
uint256 overflows = 0;
(int256 signedCoefficientA, int256 exponentA) =
LibDecimalFloat.unpack(Float.wrap(StackItem.unwrap(inputs[0])));
if (int32(exponentA) != exponentA) {
overflows++;
}
for (uint256 i = 1; i < inputs.length; i++) {
(int256 signedCoefficientB, int256 exponentB) =
LibDecimalFloat.unpack(Float.wrap(StackItem.unwrap(inputs[i])));
if (int32(exponentB) != exponentB) {
overflows++;
break;
}
(signedCoefficientA, exponentA) =
LibDecimalFloatImplementation.mul(signedCoefficientA, exponentA, signedCoefficientB, exponentB);
if (int32(exponentA) != exponentA) {
overflows++;
break;
}
}
outputs = new StackItem[](1);
if (overflows > 0) {
- a = Float.wrap(keccak256(abi.encodePacked("overflow sentinel")));
+ a = Float.wrap(OVERFLOW_SENTINEL);
} else {
a = LibDecimalFloat.packLossless(signedCoefficientA, exponentA);
}
outputs[0] = StackItem.wrap(Float.unwrap(a));
return outputs;
}
}
🤖 Prompt for AI Agents
In src/lib/op/math/LibOpMul.sol between lines 54 and 98, replace the magic
string "overflow sentinel" used in keccak256 for the overflow sentinel with a
named constant defined at the contract or library level. Define a constant
bytes32 variable to hold the keccak256 hash of the overflow sentinel string and
use that constant in the referenceFn function to improve code clarity and
maintainability.

@thedavidmeister
thedavidmeister merged commit 50f80bc into main Jul 12, 2025
11 checks passed
This was referenced Jul 17, 2025
@coderabbitai coderabbitai Bot mentioned this pull request Jul 28, 2025
4 tasks
This was referenced Aug 7, 2025
This was referenced Sep 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant