-
-
Notifications
You must be signed in to change notification settings - Fork 21.5k
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
Fix integer underflows when rounding up and input is 0 #81277
Fix integer underflows when rounding up and input is 0 #81277
Conversation
core/math/math_funcs.h
Outdated
// 65 / 64 = 2 | ||
// | ||
// Will not work correctly if numerator + denominator - 1u results in an overflow. | ||
static _ALWAYS_INLINE_ uint32_t divide_round_up(uint32_t numerator, uint32_t denominator) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
static _ALWAYS_INLINE_ uint32_t divide_round_up(uint32_t numerator, uint32_t denominator) { | |
static _ALWAYS_INLINE_ uint32_t divide_round_up(uint32_t p_numerator, uint32_t p_denominator) { |
Prefix arguments with p_
, same everywhere
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Arg!
My bad.
Also what is the difference between this and: Which already does this |
Fixes godotengine#80358 Whenever the code wants to round up, it performs x = (x - 1) / y + 1 when it should be doing x = (x + y - 1) / y The original code when the input is 0 is wrong for both unsigned (produces a very large value) and signed (produces 1 instead of 0). This version handles 0 properly. (x / y = 0). Mathematically they're the same: = (x - 1) / y + 1 = (x - 1) / y + y / y = (x + y - 1) / y However in terms of computer science, they're not
a437278
to
e826b64
Compare
It's very simple: I looked for it and I didn't see it. 🤦♂️ I'm closing this one then. |
No worries, thank you for your contribution nontheless! In the future check the area to the right on an issue, it lists the PRs linked with this :) |
Fixes #80358
Whenever the code wants to round up, it performs x = (x - 1) / y + 1 when it should be doing x = (x + y - 1) / y
The original code when the input is 0 is wrong for both unsigned (produces a very large value) and signed (produces 1 instead of 0).
This version handles 0 properly. (x / y = 0).
Mathematically they're the same:
= (x - 1) / y + 1
= (x - 1) / y + y / y
= (x + y - 1) / y
However in terms of computer science, they're not