[glib: 1/2] Merging tests/bit-test.c into glib/tests/utils.c
- From: Philip Withnall <pwithnall src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [glib: 1/2] Merging tests/bit-test.c into glib/tests/utils.c
- Date: Thu, 2 Dec 2021 09:45:38 +0000 (UTC)
commit dd47df80e19d7356c30ac05cb41a0abecdef8a47
Author: Emmanuel Fleury <emmanuel fleury gmail com>
Date: Tue Nov 30 18:30:45 2021 +0100
Merging tests/bit-test.c into glib/tests/utils.c
glib/tests/utils.c | 126 ++++++++++++++++++++++++++++++++++++++++++++++
tests/bit-test.c | 145 -----------------------------------------------------
tests/meson.build | 1 -
3 files changed, 126 insertions(+), 146 deletions(-)
---
diff --git a/glib/tests/utils.c b/glib/tests/utils.c
index 2ded21df7..bd22cc612 100644
--- a/glib/tests/utils.c
+++ b/glib/tests/utils.c
@@ -211,6 +211,131 @@ test_tmpdir (void)
g_assert_cmpstr (g_get_tmp_dir (), !=, "");
}
+#if defined(__GNUC__) && (__GNUC__ >= 4)
+#define TEST_BUILTINS 1
+#else
+#define TEST_BUILTINS 0
+#endif
+
+#if TEST_BUILTINS
+static gint
+builtin_bit_nth_lsf1 (gulong mask, gint nth_bit)
+{
+ if (nth_bit >= 0)
+ {
+ if (G_LIKELY (nth_bit < GLIB_SIZEOF_LONG * 8 - 1))
+ mask &= -(1UL << (nth_bit + 1));
+ else
+ mask = 0;
+ }
+ return __builtin_ffsl (mask) - 1;
+}
+
+static gint
+builtin_bit_nth_lsf2 (gulong mask, gint nth_bit)
+{
+ if (nth_bit >= 0)
+ {
+ if (G_LIKELY (nth_bit < GLIB_SIZEOF_LONG * 8 - 1))
+ mask &= -(1UL << (nth_bit + 1));
+ else
+ mask = 0;
+ }
+ return mask ? __builtin_ctzl (mask) : -1;
+}
+
+static gint
+builtin_bit_nth_msf (gulong mask, gint nth_bit)
+{
+ if (nth_bit >= 0 && nth_bit < GLIB_SIZEOF_LONG * 8)
+ mask &= (1UL << nth_bit) - 1;
+ return mask ? GLIB_SIZEOF_LONG * 8 - 1 - __builtin_clzl (mask) : -1;
+}
+
+static guint
+builtin_bit_storage (gulong number)
+{
+ return number ? GLIB_SIZEOF_LONG * 8 - __builtin_clzl (number) : 1;
+}
+#endif
+
+static gint
+naive_bit_nth_lsf (gulong mask, gint nth_bit)
+{
+ if (G_UNLIKELY (nth_bit < -1))
+ nth_bit = -1;
+ while (nth_bit < ((GLIB_SIZEOF_LONG * 8) - 1))
+ {
+ nth_bit++;
+ if (mask & (1UL << nth_bit))
+ return nth_bit;
+ }
+ return -1;
+}
+
+static gint
+naive_bit_nth_msf (gulong mask, gint nth_bit)
+{
+ if (nth_bit < 0 || G_UNLIKELY (nth_bit > GLIB_SIZEOF_LONG * 8))
+ nth_bit = GLIB_SIZEOF_LONG * 8;
+ while (nth_bit > 0)
+ {
+ nth_bit--;
+ if (mask & (1UL << nth_bit))
+ return nth_bit;
+ }
+ return -1;
+}
+
+static guint
+naive_bit_storage (gulong number)
+{
+ guint n_bits = 0;
+
+ do
+ {
+ n_bits++;
+ number >>= 1;
+ }
+ while (number);
+ return n_bits;
+}
+
+static void
+test_basic_bits (void)
+{
+ gulong i;
+ gint nth_bit;
+
+ /* we loop like this: 0, -1, 1, -2, 2, -3, 3, ... */
+ for (i = 0; (glong) i < 1500; i = -(i + ((glong) i >= 0)))
+ {
+#if TEST_BUILTINS
+ g_assert_cmpint (naive_bit_storage (i), ==, builtin_bit_storage (i));
+#endif
+ g_assert_cmpint (naive_bit_storage (i), ==, g_bit_storage (i));
+
+ for (nth_bit = -3; nth_bit <= 2 + GLIB_SIZEOF_LONG * 8; nth_bit++)
+ {
+#if TEST_BUILTINS
+ g_assert_cmpint (naive_bit_nth_lsf (i, nth_bit), ==,
+ builtin_bit_nth_lsf1 (i, nth_bit));
+ g_assert_cmpint (naive_bit_nth_lsf (i, nth_bit), ==,
+ builtin_bit_nth_lsf2 (i, nth_bit));
+#endif
+ g_assert_cmpint (naive_bit_nth_lsf (i, nth_bit), ==,
+ g_bit_nth_lsf (i, nth_bit));
+
+#if TEST_BUILTINS
+ g_assert_cmpint (naive_bit_nth_msf (i, nth_bit), ==,
+ builtin_bit_nth_msf (i, nth_bit));
+#endif
+ g_assert_cmpint (naive_bit_nth_msf (i, nth_bit), ==,
+ g_bit_nth_msf (i, nth_bit));
+ }
+ }
+}
+
static void
test_bits (void)
{
@@ -908,6 +1033,7 @@ main (int argc,
g_test_add_func ("/utils/appname", test_appname);
g_test_add_func ("/utils/prgname-thread-safety", test_prgname_thread_safety);
g_test_add_func ("/utils/tmpdir", test_tmpdir);
+ g_test_add_func ("/utils/basic_bits", test_basic_bits);
g_test_add_func ("/utils/bits", test_bits);
g_test_add_func ("/utils/swap", test_swap);
g_test_add_func ("/utils/find-program", test_find_program);
diff --git a/tests/meson.build b/tests/meson.build
index d1ebdb43f..6236080d6 100644
--- a/tests/meson.build
+++ b/tests/meson.build
@@ -19,7 +19,6 @@ tests = {
'testglib' : {'tap' : true},
'testgdate' : {},
'datetime' : {},
- 'bit-test' : {},
'child-test' : {},
'completion-test' : {},
'dirname-test' : {},
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]