[glib: 1/2] Optimize g_nearest_pow() function in glib/garray.c



commit 2f8c61314c15abd0c97197e6f819b8d0e47dfcc2
Author: Jeffrey Stedfast <fejj gnome org>
Date:   Fri Aug 9 14:16:51 2019 +0200

    Optimize g_nearest_pow() function in glib/garray.c
    
    Closes issue #83

 glib/garray.c | 16 ++++++++++++----
 1 file changed, 12 insertions(+), 4 deletions(-)
---
diff --git a/glib/garray.c b/glib/garray.c
index 569cff892..d2c8cd8a5 100644
--- a/glib/garray.c
+++ b/glib/garray.c
@@ -874,12 +874,20 @@ g_array_binary_search (GArray        *array,
 static guint
 g_nearest_pow (guint num)
 {
-  guint n = 1;
+  guint n = num - 1;
 
-  while (n < num && n > 0)
-    n <<= 1;
+  g_assert (num > 0);
 
-  return n ? n : num;
+  n |= n >> 1;
+  n |= n >> 2;
+  n |= n >> 4;
+  n |= n >> 8;
+  n |= n >> 16;
+#if GLIB_SIZEOF_INT == 8
+  n |= n >> 32;
+#endif
+
+  return n + 1;
 }
 
 static void


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]