[Bf-blender-cvs] [990515a5a72] master: Math Lib: add divide_floor_i

Campbell Barton noreply at git.blender.org
Mon Sep 18 05:12:41 CEST 2017


Commit: 990515a5a72692e7ee93c68d393352cad375171c
Author: Campbell Barton
Date:   Mon Sep 18 13:14:58 2017 +1000
Branches: master
https://developer.blender.org/rB990515a5a72692e7ee93c68d393352cad375171c

Math Lib: add divide_floor_i

Integer division that floors on negative output (like Python's).

===================================================================

M	source/blender/blenlib/intern/math_base_inline.c

===================================================================

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
@@ -194,6 +194,17 @@ MINLINE int divide_round_i(int a, int 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.
  */
 MINLINE int mod_i(int i, int n)



More information about the Bf-blender-cvs mailing list