Skip to content

psyqo: make fixed-point iDiv/dDiv constexpr#2048

Open
nicolasnoble wants to merge 1 commit into
grumpycoders:mainfrom
nicolasnoble:psyqo-fixedpoint-constexpr
Open

psyqo: make fixed-point iDiv/dDiv constexpr#2048
nicolasnoble wants to merge 1 commit into
grumpycoders:mainfrom
nicolasnoble:psyqo-fixedpoint-constexpr

Conversation

@nicolasnoble

Copy link
Copy Markdown
Member

Move the FixedPointInternals iDiv/dDiv division helpers out of fixed-point.cpp
into the header as constexpr, so fixed-point division can be evaluated at
compile time. The out-of-line definitions are removed; the bodies are
unchanged. printInt stays in the .cpp.

Move the FixedPointInternals iDiv/dDiv division helpers out of fixed-point.cpp
into the header as constexpr, so fixed-point division can be evaluated at
compile time. The out-of-line definitions are removed; the bodies are
unchanged. printInt stays in the .cpp.

Co-authored-by: Elias Daler <eliasdaler@protonmail.com>
Signed-off-by: Nicolas 'Pixel' Noble <nicolas@nobis-crew.org>
@coderabbitai

coderabbitai Bot commented Jul 8, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

The iDiv and dDiv fixed-point division helper functions are relocated from src/mips/psyqo/src/fixed-point.cpp into src/mips/psyqo/fixed-point.hh as inline constexpr definitions, replacing their prior forward declarations and removing the corresponding implementations from the cpp file.

Changes

Fixed-point division helper relocation

Layer / File(s) Summary
Inline constexpr division implementation
src/mips/psyqo/fixed-point.hh, src/mips/psyqo/src/fixed-point.cpp
iDiv and dDiv are now defined inline as constexpr functions in the header (implementing shift/subtract division and signed division via absolute values), while their prior implementations are removed from the cpp file.

Estimated code review effort: 3 (Moderate) | ~20 minutes

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately summarizes the main change: making the fixed-point division helpers constexpr in the header.
Description check ✅ Passed The description matches the changeset by stating the helpers moved to the header as constexpr and printInt stayed in the .cpp.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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

Comment @coderabbitai help to get the list of available commands.

@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

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/mips/psyqo/fixed-point.hh`:
- Around line 72-83: The dDiv helper in fixed-point.hh negates signed inputs
directly, which still breaks when a or b is INT32_MIN. Update dDiv (used by
FixedPoint::operator/ and FixedPoint::operator/=) to guard the minimum 32-bit
value before negation, either by widening to a larger temporary type or by
special-casing INT32_MIN so constant evaluation remains valid.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: d4eebe76-e534-43e3-a8dc-f94077b4a052

📥 Commits

Reviewing files that changed from the base of the PR and between e909cc3 and fa42b7b.

📒 Files selected for processing (2)
  • src/mips/psyqo/fixed-point.hh
  • src/mips/psyqo/src/fixed-point.cpp
💤 Files with no reviewable changes (1)
  • src/mips/psyqo/src/fixed-point.cpp

Comment on lines +72 to +83
constexpr int32_t dDiv(int32_t a, int32_t b, unsigned scale) {
int s = 1;
if (a < 0) {
a = -a;
s = -1;
}
if (b < 0) {
b = -b;
s = -s;
}
return iDiv(a, b, scale) * s;
}

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.

🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# Find compile-time (constexpr/consteval) usages that could feed dDiv with signed division.
rg -nP -C3 '\bdDiv\s*\(' src/mips/psyqo
# Look for constexpr/consteval contexts invoking FixedPoint division on signed 32-bit types.
rg -nP -C3 'operator/|operator/=' src/mips/psyqo/fixed-point.hh

Repository: grumpycoders/pcsx-redux

Length of output: 3784


🏁 Script executed:

#!/bin/bash
set -euo pipefail

# Inspect the relevant parts of fixed-point.hh around dDiv and the signed operators.
sed -n '1,140p' src/mips/psyqo/fixed-point.hh
printf '\n---\n'
sed -n '300,430p' src/mips/psyqo/fixed-point.hh
printf '\n---\n'
sed -n '470,520p' src/mips/psyqo/fixed-point.hh

Repository: grumpycoders/pcsx-redux

Length of output: 10563


🏁 Script executed:

#!/bin/bash
set -euo pipefail

# Find constructors, raw accessors, and any direct uses of INT32_MIN / numeric_limits in the fixed-point implementation.
rg -n -C2 'raw\(|INT32_MIN|numeric_limits|FixedPoint\s*\(|constexpr\s+FixedPoint' src/mips/psyqo/fixed-point.hh

Repository: grumpycoders/pcsx-redux

Length of output: 6974


Guard INT32_MIN before negating in dDiv

FixedPoint::operator/ and operator/= for signed 32-bit values call this directly, so a raw INT32_MIN still makes constant evaluation ill-formed when a = -a; or b = -b;. Widen the temporary or special-case the minimum value.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/mips/psyqo/fixed-point.hh` around lines 72 - 83, The dDiv helper in
fixed-point.hh negates signed inputs directly, which still breaks when a or b is
INT32_MIN. Update dDiv (used by FixedPoint::operator/ and
FixedPoint::operator/=) to guard the minimum 32-bit value before negation,
either by widening to a larger temporary type or by special-casing INT32_MIN so
constant evaluation remains valid.

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.

1 participant