From 990515a5a72692e7ee93c68d393352cad375171c Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Mon, 18 Sep 2017 13:14:58 +1000 Subject: Math Lib: add divide_floor_i Integer division that floors on negative output (like Python's). --- source/blender/blenlib/intern/math_base_inline.c | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/source/blender/blenlib/intern/math_base_inline.c b/source/blender/blenlib/intern/math_base_inline.c index 6574c001a23..5ae2b1a70a7 100644 --- a/source/blender/blenlib/intern/math_base_inline.c +++ b/source/blender/blenlib/intern/math_base_inline.c @@ -193,6 +193,17 @@ MINLINE int divide_round_i(int a, int b) return (2 * a + b) / (2 * b); } +/** + * Integer division that floors negative result. + * \note This works like Python's int division. + */ +MINLINE int divide_floor_i(int a, int b) +{ + int d = a / b; + int r = a % b; /* Optimizes into a single division. */ + return r ? d - ((a < 0) ^ (b < 0)) : d; +} + /** * modulo that handles negative numbers, works the same as Python's. */ -- cgit v1.2.3