Skip to content

Optimized _DivideHLBC, fixed incorrect behavior for INT_MIN inputs #596

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 27, 2025

Conversation

calc84maniac
Copy link
Contributor

Inspired by #595, I took a look at optimizing GraphX's rounded-down signed division routine. Since it wasn't initially designed to round down and the rounding fixup was added later, I thought about ways to make it round down from the start. Eventually, I came up with the idea of transforming the signed division into unsigned, which always rounds down. So, after conditionally negating both the dividend and divisor such that the divisor is positive, the unsigned division can be done as follows:

If x >= 0: Return x / y
If x < 0: Return ((y * 2^24) + x) / y

This setup can be done via an optimized first iteration where the remainder register is initialized to either 0 or y-1 based on the sign bit of the dividend, and that sign bit of the dividend is directly output into the sign bit of the result, allowing the rest of the division to be calculated in 23 iterations.

The one exceptional case is that when the dividend is INT_MIN and gets negated, its sign bit is not actually a sign, so it can't use the same setup. In that case, it simply uses 24 iterations of unsigned division instead.

@mateoconlechuga mateoconlechuga merged commit 1923f31 into master Apr 27, 2025
9 checks passed
@mateoconlechuga mateoconlechuga deleted the opt_graphx_divhlbc branch April 27, 2025 01:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

3 participants