gnunet-svn
[Top][All Lists]
Advanced

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

[GNUnet-SVN] r9019 - gnunet/src/util


From: gnunet
Subject: [GNUnet-SVN] r9019 - gnunet/src/util
Date: Tue, 22 Sep 2009 22:14:06 -0600

Author: amatus
Date: 2009-09-22 22:14:06 -0600 (Tue, 22 Sep 2009)
New Revision: 9019

Modified:
   gnunet/src/util/container_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/container_heap.c
===================================================================
--- gnunet/src/util/container_heap.c    2009-09-22 22:11:53 UTC (rev 9018)
+++ gnunet/src/util/container_heap.c    2009-09-23 04:14:06 UTC (rev 9019)
@@ -87,6 +87,17 @@
   return heap->root->element;
 }
 
+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)
@@ -159,16 +170,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;
@@ -176,9 +185,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;
@@ -200,11 +209,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)
     {
@@ -213,9 +219,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]