You've already forked godot
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:
committed by
Rémi Verschelde
parent
13a0d6e9b2
commit
8747c67d9e
@@ -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); }
|
||||
|
||||
|
||||
Reference in New Issue
Block a user