psyqo: make fixed-point iDiv/dDiv constexpr#2048
Conversation
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>
📝 WalkthroughWalkthroughThe ChangesFixed-point division helper relocation
Estimated code review effort: 3 (Moderate) | ~20 minutes 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
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
📒 Files selected for processing (2)
src/mips/psyqo/fixed-point.hhsrc/mips/psyqo/src/fixed-point.cpp
💤 Files with no reviewable changes (1)
- src/mips/psyqo/src/fixed-point.cpp
| 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; | ||
| } |
There was a problem hiding this comment.
🎯 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.hhRepository: 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.hhRepository: 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.hhRepository: 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.
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.