Skip to content

exp2 op#394

Merged
thedavidmeister merged 1 commit into
mainfrom
2025-08-07-exp2
Aug 7, 2025
Merged

exp2 op#394
thedavidmeister merged 1 commit into
mainfrom
2025-08-07-exp2

Conversation

@thedavidmeister
Copy link
Copy Markdown
Contributor

@thedavidmeister thedavidmeister commented Aug 7, 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

    • Re-enabled support for the binary exponential (2^x) operation in the standard operations set.
    • The "exp2" operation is now available with updated documentation and metadata.
  • Refactor

    • Updated the "exp2" operation to use decimal floating-point arithmetic for improved precision and compatibility.
  • Tests

    • Enhanced and modernized tests for the "exp2" operation to reflect the new floating-point implementation and operand types.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Aug 7, 2025

Walkthrough

The changes re-enable and refactor the exp2 opcode in the codebase. The opcode is now fully integrated into the standard operations set, with its implementation switched from fixed-point to decimal floating-point arithmetic. Associated tests and metadata are updated to reflect this new floating-point approach.

Changes

Cohort / File(s) Change Summary
Standard Ops Integration
src/lib/op/LibAllStandardOps.sol
Re-enables the LibOpExp2 opcode: imports it, increments the opcode count, adds metadata, and updates function pointers for operand handling, integrity, and execution to include exp2.
Opcode Implementation Refactor
src/lib/op/math/LibOpExp2.sol
Refactors LibOpExp2 from fixed-point to decimal floating-point arithmetic using LibDecimalFloat, updates function signatures, removes legacy code, and changes stack operations to handle the new float types.
Test Suite Update
test/src/lib/op/math/LibOpExp2.t.sol
Updates and uncomments tests for LibOpExp2 to use decimal floating-point types and operand versioning, adapts all test logic and input/output handling to the new implementation, and maintains coverage for operand, integrity, and error scenarios.

Sequence Diagram(s)

sequenceDiagram
  participant User
  participant Interpreter
  participant LibOpExp2
  participant LibDecimalFloat

  User->>Interpreter: Executes "exp2" opcode
  Interpreter->>LibOpExp2: Calls run(state, operand, stackTop)
  LibOpExp2->>LibDecimalFloat: Computes 2^x using FLOAT_TWO.pow
  LibDecimalFloat-->>LibOpExp2: Returns result as Float
  LibOpExp2-->>Interpreter: Pushes result to stack
  Interpreter-->>User: Returns execution result
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

  • exp op #393: Updates the exp opcode to use decimal floating point, paralleling this PR's approach for exp2 and modifying similar files and integration points.
  • inv op #374: Refactors and re-enables the inv opcode with floating point arithmetic, following a similar integration process as this PR for exp2.
  • e constant #375: Integrates the e opcode using floating point arithmetic, structurally mirroring this PR's changes for exp2.

Note

⚡️ Unit Test Generation is now available in beta!

Learn more here, or try it out under "Finishing Touches" below.


📜 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 f7471f5 and 3395856.

⛔ 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/LibOpExp2.sol (1 hunks)
  • test/src/lib/op/math/LibOpExp2.t.sol (1 hunks)
🧰 Additional context used
🧠 Learnings (3)
📚 Learning: in the rain.interpreter codebase, when working with erc20 tokens that may not implement the optional...
Learnt from: thedavidmeister
PR: rainlanguage/rain.interpreter#360
File: src/lib/op/erc20/LibOpERC20Allowance.sol:35-36
Timestamp: 2025-07-15T11:31:10.098Z
Learning: In the rain.interpreter codebase, when working with ERC20 tokens that may not implement the optional decimals() function, the preference is to let the call fail explicitly rather than catching errors and defaulting to 18 decimals. This ensures correctness by avoiding potentially incorrect calculations with assumed decimal values.

Applied to files:

  • src/lib/op/math/LibOpExp2.sol
📚 Learning: in rain interpreter stack operations like libopevery, when the output position (stacktop) is set to ...
Learnt from: thedavidmeister
PR: rainlanguage/rain.interpreter#381
File: src/lib/op/logic/LibOpEvery.sol:24-46
Timestamp: 2025-07-27T22:56:57.928Z
Learning: In Rain interpreter stack operations like LibOpEvery, when the output position (stackTop) is set to coincide with an input item's position on the stack, explicit writing may not be needed if the desired output value is already at that position. The function can return the pointer to that position directly, leveraging the existing stack layout.

Applied to files:

  • src/lib/op/math/LibOpExp2.sol
📚 Learning: in multiplication overflow detection tests like libopuint256multest, when performing sequential mult...
Learnt from: thedavidmeister
PR: rainlanguage/rain.interpreter#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.

Applied to files:

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

80-80: Changes correctly re-enable the exp2 opcode

The re-integration of LibOpExp2 into the standard operations set is properly implemented:

  • Import statement correctly uncommented
  • ALL_STANDARD_OPS_LENGTH correctly incremented from 68 to 69
  • Metadata description "Binary exponential 2^x." is clear and accurate
  • Function pointers (operand handler, integrity, run) are consistently placed following the existing pattern

Also applies to: 115-115, 277-277, 477-478, 608-608, 719-719

src/lib/op/math/LibOpExp2.sol (1)

4-50: Decimal floating-point implementation looks correct

The refactoring from fixed-point to decimal floating-point arithmetic is well-implemented:

  • Proper use of LibDecimalFloat and Float types
  • Correct function signature updates (OperandV2, view modifier for LOG_TABLES_ADDRESS access)
  • Both run and referenceFn properly handle Float wrapping/unwrapping
  • Integrity function correctly specifies 1 input and 1 output
test/src/lib/op/math/LibOpExp2.t.sol (3)

11-44: Test updates correctly match the new decimal floating-point implementation

The test refactoring properly adapts to the new LibDecimalFloat implementation with appropriate use of OperandV2, Float, and StackItem types.


47-63: Standard test cases properly adapted

Bad input tests, operand disallowance, and output count validation tests are correctly implemented using the new types.


42-43: Decimal‐float precision is by design
The slight underflows (3.999 instead of 4.000, 7.998 instead of 8.000) come from LibDecimalFloat’s fixed three‐digit decimal mantissa. In other words, packLossless + pow intentionally rounds to 3 significant digits, so these “errors” are expected. No changes needed here.

✨ Finishing Touches
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch 2025-08-07-exp2

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.
  • 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.

Support

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

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 generate unit tests to generate unit tests for 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.

@thedavidmeister thedavidmeister merged commit f6284f7 into main Aug 7, 2025
11 checks passed
@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