1
0
mirror of https://github.com/godotengine/godot.git synced 2025-11-04 12:00:25 +00:00

Fix potential integer underflow in rounded up divisions

A new `Math::division_round_up()` function was added, allowing for easy
and correct computation of integer divisions when the result needs to
be rounded up.

Fixes #80358.

Co-authored-by: Rémi Verschelde <rverschelde@gmail.com>
This commit is contained in:
EddieBreeg
2023-08-07 20:19:20 +02:00
committed by Rémi Verschelde
parent 13a0d6e9b2
commit 8747c67d9e
14 changed files with 81 additions and 41 deletions

View File

@@ -198,6 +198,22 @@ public:
#endif
}
// These methods assume (p_num + p_den) doesn't overflow.
static _ALWAYS_INLINE_ int32_t division_round_up(int32_t p_num, int32_t p_den) {
int32_t offset = (p_num < 0 && p_den < 0) ? 1 : -1;
return (p_num + p_den + offset) / p_den;
}
static _ALWAYS_INLINE_ uint32_t division_round_up(uint32_t p_num, uint32_t p_den) {
return (p_num + p_den - 1) / p_den;
}
static _ALWAYS_INLINE_ int64_t division_round_up(int64_t p_num, int64_t p_den) {
int32_t offset = (p_num < 0 && p_den < 0) ? 1 : -1;
return (p_num + p_den + offset) / p_den;
}
static _ALWAYS_INLINE_ uint64_t division_round_up(uint64_t p_num, uint64_t p_den) {
return (p_num + p_den - 1) / p_den;
}
static _ALWAYS_INLINE_ bool is_finite(double p_val) { return isfinite(p_val); }
static _ALWAYS_INLINE_ bool is_finite(float p_val) { return isfinite(p_val); }