[Bf-blender-cvs] [5e28b71457e] master: math utils: Add utilities to scan bit and clear it
Sergey Sharybin
noreply at git.blender.org
Fri Dec 15 16:59:20 CET 2017
Commit: 5e28b71457e8c8ce20e19a28ce44c9d9b5f47844
Author: Sergey Sharybin
Date: Fri Dec 15 16:41:31 2017 +0100
Branches: master
https://developer.blender.org/rB5e28b71457e8c8ce20e19a28ce44c9d9b5f47844
math utils: Add utilities to scan bit and clear it
===================================================================
M source/blender/blenlib/BLI_math_bits.h
M source/blender/blenlib/intern/math_bits_inline.c
===================================================================
diff --git a/source/blender/blenlib/BLI_math_bits.h b/source/blender/blenlib/BLI_math_bits.h
index 248b615e3f8..86213dc271f 100644
--- a/source/blender/blenlib/BLI_math_bits.h
+++ b/source/blender/blenlib/BLI_math_bits.h
@@ -35,10 +35,18 @@ extern "C" {
MINLINE int bitscan_forward_i(int a);
MINLINE unsigned int bitscan_forward_uint(unsigned int a);
+/* Similar to above, but also clears the bit. */
+MINLINE int bitscan_forward_clear_i(int *a);
+MINLINE unsigned int bitscan_forward_clear_uint(unsigned int *a);
+
/* Search the value from MSB to LSB for a set bit. Returns index of this bit. */
MINLINE int bitscan_reverse_i(int a);
MINLINE unsigned int bitscan_reverse_uint(unsigned int a);
+/* Similar to above, but also clears the bit. */
+MINLINE int bitscan_reverse_clear_i(int *a);
+MINLINE unsigned int bitscan_reverse_clear_uint(unsigned int *a);
+
/* NOTE: Those functions returns 2 to the power of index of highest order bit. */
MINLINE unsigned int highest_order_bit_uint(unsigned int n);
MINLINE unsigned short highest_order_bit_s(unsigned short n);
diff --git a/source/blender/blenlib/intern/math_bits_inline.c b/source/blender/blenlib/intern/math_bits_inline.c
index e967ecbb3a3..d96cf969dae 100644
--- a/source/blender/blenlib/intern/math_bits_inline.c
+++ b/source/blender/blenlib/intern/math_bits_inline.c
@@ -44,6 +44,18 @@ MINLINE unsigned int bitscan_forward_uint(unsigned int a)
return (unsigned int)bitscan_forward_i((int)a);
}
+MINLINE int bitscan_forward_clear_i(int *a)
+{
+ int i = bitscan_forward_i(*a);
+ *a &= (*a) - 1;
+ return i;
+}
+
+MINLINE unsigned int bitscan_forward_clear_uint(unsigned int *a)
+{
+ return (unsigned int)bitscan_forward_clear_i((int *)a);
+}
+
MINLINE int bitscan_reverse_i(int a)
{
BLI_assert(a != 0);
@@ -61,6 +73,19 @@ MINLINE unsigned int bitscan_reverse_uint(unsigned int a)
return (unsigned int)bitscan_reverse_i((int)a);
}
+MINLINE int bitscan_reverse_clear_i(int *a)
+{
+ int i = bitscan_reverse_i(*a);
+ /* TODO(sergey): This could probably be optimized. */
+ *a &= ~(1 << (sizeof(int) * 8 - i - 1));
+ return i;
+}
+
+MINLINE unsigned int bitscan_reverse_clear_uint(unsigned int *a)
+{
+ return (unsigned int)bitscan_reverse_clear_i((int *)a);
+}
+
MINLINE unsigned int highest_order_bit_uint(unsigned int n)
{
if (n == 0) {
More information about the Bf-blender-cvs
mailing list