Skip to content

Fixing casting issue in fixed-point.hh#1941

Merged
nicolasnoble merged 1 commit intomainfrom
fixed-point-cast
Jun 8, 2025
Merged

Fixing casting issue in fixed-point.hh#1941
nicolasnoble merged 1 commit intomainfrom
fixed-point-cast

Conversation

@nicolasnoble
Copy link
Copy Markdown
Member

The integer() overload with a different type had inconsistent codepath between signed and unsigned, resulting in precision problems.

The `integer()` overload with a different type had inconsistent codepath between signed and unsigned, resulting in precision problems.
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Jun 8, 2025

Walkthrough

The change updates the return expression in the templated integer() method of a fixed-point arithmetic class. The order of operations is modified so that integer division occurs before type conversion, which may affect rounding behavior when extracting integers from fixed-point values. No other logic or interface changes are made.

Changes

File(s) Change Summary
src/mips/psyqo/fixed-point.hh Modified the integer() method to perform integer division before type conversion, altering rounding behavior.

Sequence Diagram(s)

sequenceDiagram
    participant Caller
    participant FixedPoint

    Caller->>FixedPoint: integer<U>()
    Note right of FixedPoint: Compute (value + scale/2) / scale
    FixedPoint-->>Caller: Return result as type U
Loading

Possibly related PRs

Poem

A hop, a skip, a clever tweak,
Now rounding’s done just as we seek.
Division first, then cast anew,
Fixed-point math is crisp and true!
With every bit and byte aligned,
The rabbit’s code is redefined. 🐇

✨ Finishing Touches
  • 📝 Generate Docstrings

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.

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

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR fixes a precision inconsistency in the integer() overload for unsigned types by performing the division before casting, matching the signed path’s behavior.

  • Adjusted the return expression to divide before converting to U for unsigned rounding.
  • Ensures consistent rounding logic between signed and unsigned overloads.
Comments suppressed due to low confidence (2)

src/mips/psyqo/fixed-point.hh:175

  • There should be unit tests covering edge cases around rounding for this unsigned path (e.g., values just below and above scale/2) to prevent regressions.
return U((value + scale / 2) / scale);

src/mips/psyqo/fixed-point.hh:178

  • The doc comment for integer() should explicitly mention that both signed and unsigned overloads now use the same rounding-to-nearest behavior.
/**

}
}
return U(value + scale / 2) / U(scale);
return U((value + scale / 2) / scale);
Copy link

Copilot AI Jun 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The rounding logic is duplicated between signed and unsigned integer() overloads. Consider extracting a common helper to centralize this behavior and reduce duplication.

Copilot uses AI. Check for mistakes.
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.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between a067701 and 429bf83.

📒 Files selected for processing (1)
  • src/mips/psyqo/fixed-point.hh (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (13)
  • GitHub Check: pcsx-redux (aarch64-linux)
  • GitHub Check: pcsx-redux (x86_64-linux)
  • GitHub Check: pcsx-redux (x86_64-linux)
  • GitHub Check: pcsx-redux (aarch64-linux)
  • GitHub Check: Codacy Static Code Analysis
  • GitHub Check: coverage
  • GitHub Check: build
  • GitHub Check: macos-build-and-test-toolchain
  • GitHub Check: build-openbios
  • GitHub Check: aur-build
  • GitHub Check: toolchain
  • GitHub Check: asan
  • GitHub Check: cross-arm64

}
}
return U(value + scale / 2) / U(scale);
return U((value + scale / 2) / scale);
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.

🛠️ Refactor suggestion

LGTM! This change correctly fixes the casting precision issue.

The modification ensures that integer division occurs before type conversion, which prevents potential precision loss and inconsistent behavior when converting between signed and unsigned types. By performing (value + scale / 2) / scale in the native type T first, then casting the result to U, the function avoids intermediate casting issues that could occur when T and U have different signedness.

However, this change creates an inconsistency with other similar methods in the class. Consider applying the same pattern to the floor() and ceil() methods for consistency:

Current floor() method (lines 193, 196):

return U(value - scale + 1) / U(scale);  // and
return U(value) / U(scale);

Current ceil() method (lines 214, 217):

return U(value) / U(scale);  // and  
return U(value + scale - 1) / U(scale);

Suggested refactor for consistency:

# In floor() method:
-return U(value - scale + 1) / U(scale);
+return U((value - scale + 1) / scale);
-return U(value) / U(scale);
+return U(value / scale);

# In ceil() method:  
-return U(value) / U(scale);
+return U(value / scale);
-return U(value + scale - 1) / U(scale);
+return U((value + scale - 1) / scale);

This would ensure all conversion methods use the same "divide first, then cast" approach for better consistency and to prevent similar casting issues.

🤖 Prompt for AI Agents
In src/mips/psyqo/fixed-point.hh around lines 193 to 196 and 214 to 217,
refactor the floor() and ceil() methods to perform the division in the native
type T before casting to U, similar to the fix applied at line 175. This means
adjusting the expressions so that all arithmetic operations happen first in type
T, then the final result is cast to U, ensuring consistent handling of
signedness and precision across all conversion methods.

@nicolasnoble nicolasnoble merged commit 2af96b5 into main Jun 8, 2025
25 checks passed
@nicolasnoble nicolasnoble deleted the fixed-point-cast branch June 8, 2025 03:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants