gnunet-svn
[Top][All Lists]
Advanced

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

[GNUnet-SVN] r8772 - GNUnet/src/util/containers


From: gnunet
Subject: [GNUnet-SVN] r8772 - GNUnet/src/util/containers
Date: Fri, 24 Jul 2009 11:56:30 -0600

Author: amatus
Date: 2009-07-24 11:56:28 -0600 (Fri, 24 Jul 2009)
New Revision: 8772

Modified:
   GNUnet/src/util/containers/heap.c
Log:
Remove use of log2(). FreeBSD doesn't have it and why use floating point when 
you don't have to?


Modified: GNUnet/src/util/containers/heap.c
===================================================================
--- GNUnet/src/util/containers/heap.c   2009-07-23 18:41:45 UTC (rev 8771)
+++ GNUnet/src/util/containers/heap.c   2009-07-24 17:56:28 UTC (rev 8772)
@@ -73,6 +73,18 @@
 
 };
 
+int
+next_power_of_2(int v)
+{
+  v |= v >> 1;
+  v |= v >> 2;
+  v |= v >> 4;
+  v |= v >> 8;
+  v |= v >> 16;
+  v++;
+  return v;
+}
+
 void
 internal_print (struct GNUNET_CONTAINER_heap_node *root)
 {
@@ -151,16 +163,14 @@
   struct GNUNET_CONTAINER_heap_node *ret;
   struct GNUNET_CONTAINER_heap_node *parent;
   int pos;
-  int depth;
   int i;
 
   ret = GNUNET_malloc (sizeof (struct GNUNET_CONTAINER_heap_node));
   pos = root->size + 1;
-  depth = (int) log2 (pos);
   ret->left_child = NULL;
   ret->right_child = NULL;
 
-  if (depth == 0)
+  if (0 == root->size)
     {
       ret->parent = NULL;
       root->root = ret;
@@ -168,9 +178,9 @@
   else
     {
       parent = root->root;
-      for (i = depth; i > 1; i--)
+      for (i = next_power_of_2(pos) >> 2; i > 1; i >>= 1)
         {
-          if (((pos / (1 << (i - 1))) % 2) == 0)
+          if (((pos / i) % 2) == 0)
             parent = parent->left_child;
           else
             parent = parent->right_child;
@@ -192,11 +202,8 @@
 getPos (struct GNUNET_CONTAINER_Heap *root, unsigned int pos)
 {
   struct GNUNET_CONTAINER_heap_node *ret;
-
-  int depth;
   int i;
 
-  depth = (int) log2 (pos);
   ret = NULL;
   if (pos > root->size)
     {
@@ -205,9 +212,9 @@
   else
     {
       ret = root->root;
-      for (i = depth; i > 0; i--)
+      for (i = next_power_of_2(pos) >> 2; i > 0; i >>= 1)
         {
-          if (((pos / (1 << (i - 1))) % 2) == 0)
+          if (((pos / i) % 2) == 0)
             ret = ret->left_child;
           else
             ret = ret->right_child;





reply via email to

[Prev in Thread] Current Thread [Next in Thread]