summaryrefslogtreecommitdiffstats
path: root/core/math
diff options
context:
space:
mode:
authorEddieBreeg <eddiebreeg0@protonmail.com>2023-08-07 20:19:20 +0200
committerRémi Verschelde <rverschelde@gmail.com>2024-01-02 14:14:47 +0100
commit8747c67d9e549e9b2cf5a93201be105b9c8d9291 (patch)
tree448bf57e5f613c3111c746b4b6bd2dcc35e7c21d /core/math
parent13a0d6e9b253654f5cc2a44f3d0b3cae10440443 (diff)
downloadredot-engine-8747c67d9e549e9b2cf5a93201be105b9c8d9291.tar.gz
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>
Diffstat (limited to 'core/math')
-rw-r--r--core/math/math_funcs.h16
1 files changed, 16 insertions, 0 deletions
diff --git a/core/math/math_funcs.h b/core/math/math_funcs.h
index 366ccca4cb..3060f31970 100644
--- a/core/math/math_funcs.h
+++ b/core/math/math_funcs.h
@@ -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); }