Skip to content

inv op#374

Merged
thedavidmeister merged 2 commits into
mainfrom
2025-07-21-inv
Jul 21, 2025
Merged

inv op#374
thedavidmeister merged 2 commits into
mainfrom
2025-07-21-inv

Conversation

@thedavidmeister
Copy link
Copy Markdown
Contributor

@thedavidmeister thedavidmeister commented Jul 21, 2025

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 the "inv" (inverse) operation as part of the standard opcode set, allowing users to compute the mathematical inverse (1/x) using floating-point arithmetic.
  • Bug Fixes

    • Updated handling to prevent division by zero when using the inverse operation.
  • Tests

    • Revised tests to validate the new floating-point implementation of the inverse operation and ensure correct input/output requirements.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Jul 21, 2025

"""

Walkthrough

The changes re-enable and update the "inv" (inverse) opcode across the standard operations library, shifting its implementation from fixed-point to floating-point arithmetic. This involves updating imports, function signatures, and logic in both the opcode library and its corresponding tests, as well as incrementing the opcode count and restoring related metadata and function pointers.

Changes

File(s) Change Summary
src/lib/op/LibAllStandardOps.sol Re-enabled "inv" opcode: uncommented import, metadata, and function pointers; incremented opcode count from 51 to 52.
src/lib/op/math/LibOpInv.sol Migrated "inv" opcode implementation from fixed-point to floating-point; updated types, imports, and function signatures.
test/src/lib/op/math/LibOpInv.t.sol Updated tests for "inv" opcode to use floating-point types and new operand version; adjusted input handling and assertions.

Sequence Diagram(s)

sequenceDiagram
    participant Caller
    participant LibAllStandardOps
    participant LibOpInv
    participant LibDecimalFloat

    Caller->>LibAllStandardOps: Request execution of "inv" opcode
    LibAllStandardOps->>LibOpInv: Call run() with OperandV2, stackTop
    LibOpInv->>LibDecimalFloat: Call inv() with Float value
    LibDecimalFloat-->>LibOpInv: Return inverse Float
    LibOpInv-->>LibAllStandardOps: Return updated stackTop
    LibAllStandardOps-->>Caller: Return result
Loading

Estimated code review effort

3 (30–60 minutes)

Possibly related PRs

  • 2025 07 12 mul #355: Re-enables and updates the "mul" opcode integration and implementation, modifying similar functions and patterns as this PR but for a different opcode.
  • gt op #356: Re-enables and integrates the "greater-than" opcode using floating-point implementation, updating similar files and functions for another opcode.
  • 2025 07 09 div #351: Re-enables and integrates the "div" opcode with floating-point arithmetic, performing analogous changes for a different math opcode.
    """

📜 Recent review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 05afb15 and 541ebbf.

📒 Files selected for processing (1)
  • test/src/lib/op/math/LibOpInv.t.sol (1 hunks)
🧰 Additional context used
🧠 Learnings (2)
📓 Common learnings
Learnt from: thedavidmeister
PR: rainlanguage/rain.interpreter#360
File: src/lib/op/erc20/LibOpERC20Allowance.sol:0-0
Timestamp: 2025-07-15T11:31:28.010Z
Learning: In the rainlanguage/rain.interpreter project, forge (Foundry's formatting tool) handles code formatting automatically, so formatting-related suggestions are not actionable.
test/src/lib/op/math/LibOpInv.t.sol (1)

Learnt from: thedavidmeister
PR: #368
File: test/src/lib/op/math/uint256/LibOpUint256Mul.t.sol:56-69
Timestamp: 2025-07-17T14:15:14.886Z
Learning: In multiplication overflow detection tests like LibOpUint256MulTest, when performing sequential multiplication (a * b * c * d...), encountering a zero value means the final result will always be zero regardless of subsequent values. Since zero multiplied by any value (including MAX_UINT256) cannot overflow, it's safe and correct to break out of the overflow detection loop early when zero is encountered.

⏰ 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). (2)
  • GitHub Check: rainix (ubuntu-latest, rainix-rs-artifacts)
  • GitHub Check: git-clean
🔇 Additional comments (5)
test/src/lib/op/math/LibOpInv.t.sol (5)

4-8: LGTM! Imports updated correctly for floating-point transition.

The imports have been properly updated to support the transition from fixed-point to floating-point arithmetic, including the necessary floating-point types and upgraded operand version.


11-17: LGTM! Integrity test properly updated for OperandV2.

The integrity test correctly validates that the inverse operation takes exactly 1 input and produces exactly 1 output, with the operand type properly upgraded to OperandV2.


19-32: LGTM! Runtime test well-implemented for floating-point arithmetic.

The runtime test correctly handles the transition to floating-point arithmetic with proper bounds checking, zero exclusion to prevent division by zero, and appropriate use of the new StackItem wrapping mechanism.


34-44: LGTM! Evaluation tests are mathematically correct.

The evaluation tests properly validate the inverse operation with correct mathematical results and appropriate use of floating-point encoding with explicit exponent scaling.


46-61: LGTM! Comprehensive bad input validation tests.

The test functions properly validate input/output constraints for the inverse operation, ensuring exactly 1 input and 1 output are required.


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.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

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 c086ff1 and 05afb15.

⛔ 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/LibOpInv.sol (1 hunks)
  • test/src/lib/op/math/LibOpInv.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, rainix-rs-static)
  • GitHub Check: rainix (ubuntu-latest, rainix-sol-artifacts)
  • GitHub Check: rainix (ubuntu-latest, test-wasm-build)
  • GitHub Check: rainix (macos-latest, rainix-rs-artifacts)
  • GitHub Check: rainix (ubuntu-latest, rainix-sol-test)
  • GitHub Check: rainix (ubuntu-latest, rainix-sol-static)
  • GitHub Check: rainix (macos-latest, rainix-rs-test)
  • GitHub Check: rainix (ubuntu-latest, rainix-rs-test)
  • GitHub Check: rainix (ubuntu-latest, rainix-rs-artifacts)
  • GitHub Check: git-clean
🔇 Additional comments (12)
src/lib/op/LibAllStandardOps.sol (3)

84-84: LGTM! Systematic re-integration of the inv opcode.

The import and opcode count increment are correctly aligned with re-enabling the inverse operation.

Also applies to: 111-111


280-280: LGTM! Clear and accurate metadata description.

The AuthoringMetaV2 entry appropriately describes the inverse operation and its error condition.


495-496: LGTM! Consistent function pointer integration.

All necessary function pointer arrays (operand handler, integrity, and runtime execution) are correctly updated to include the inv opcode handlers.

Also applies to: 626-626, 741-741

src/lib/op/math/LibOpInv.sol (4)

4-9: LGTM! Proper imports for floating-point migration.

The imports correctly include all necessary types for the floating-point implementation.


14-17: LGTM! Correct integrity function signature update.

The function signature is properly updated to use OperandV2 while maintaining the correct 1-input, 1-output semantics.


21-32: LGTM! Clean floating-point implementation.

The run function correctly:

  • Loads the float value from the stack
  • Computes the inverse using LibDecimalFloat.inv
  • Stores the result back to the stack
  • Returns the stack pointer

The implementation is clean and follows the expected pattern.


35-43: LGTM! Proper reference function implementation.

The reference function correctly handles the new StackItem array types and Float wrapping/unwrapping for testing purposes.

test/src/lib/op/math/LibOpInv.t.sol (5)

4-8: LGTM! Proper imports for updated test implementation.

The imports correctly include all necessary types for testing the floating-point inverse operation.


13-17: LGTM! Correct integrity test signature update.

The function signature is properly updated to use OperandV2 while maintaining the same test logic.


20-30: LGTM! Improved runtime test implementation.

The updated runtime test correctly:

  • Uses Float input type instead of uint256
  • Uses vm.assume to exclude zero inputs (better than previous bounds checking)
  • Properly wraps inputs in StackItem arrays
  • Maintains the same test coverage

34-42: LGTM! Comprehensive eval tests with proper floating-point values.

The test cases correctly verify the inverse operation for various inputs:

  • inv(1) = 1
  • inv(0.5) = 2
  • inv(2) = 0.5
  • inv(3) ≈ 0.333...

The use of LibDecimalFloat.packLossless with appropriate scaling is correct.


45-65: LGTM! Comprehensive error condition testing.

The tests properly verify all error conditions:

  • Zero inputs (should require 1)
  • Two inputs (should require exactly 1)
  • Zero outputs (should produce 1)
  • Two outputs (should produce exactly 1)
  • Operand disallowed validation

}

/// Test that operand is disallowed.
function testOpExpEvalOperandDisallowed() external {
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)

Fix function name to match the operation being tested.

The function name suggests it's testing exponential operation, but it's actually testing the inverse operation's operand validation.

-    function testOpExpEvalOperandDisallowed() external {
+    function testOpInvEvalOperandDisallowed() external {
📝 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
function testOpExpEvalOperandDisallowed() external {
function testOpInvEvalOperandDisallowed() external {
🤖 Prompt for AI Agents
In test/src/lib/op/math/LibOpInv.t.sol at line 62, the function name
testOpExpEvalOperandDisallowed does not match the operation being tested, which
is the inverse operation's operand validation. Rename the function to reflect
that it tests the inverse operation, such as testOpInvEvalOperandDisallowed, to
improve clarity and correctness.

@thedavidmeister thedavidmeister merged commit 69cb2e3 into main Jul 21, 2025
11 checks passed
This was referenced Jul 22, 2025
This was referenced Jul 30, 2025
@coderabbitai coderabbitai Bot mentioned this pull request Aug 7, 2025
4 tasks
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