diff --git a/src/c_wrapper/bitlog.cpp b/src/c_wrapper/bitlog.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..67b82511f309987e8c87e1a6d1f3d98d1d21aa85
--- /dev/null
+++ b/src/c_wrapper/bitlog.cpp
@@ -0,0 +1,26 @@
+#include "bitlog.hpp"
+
+
+
+/* from http://graphics.stanford.edu/~seander/bithacks.html */
+const char pyopencl::log_table_8[] =
+{
+  0, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3,
+  4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
+  5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
+  5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
+  6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
+  6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
+  6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
+  6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
+  7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
+  7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
+  7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
+  7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
+  7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
+  7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
+  7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
+  7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7
+};
+
+
diff --git a/src/c_wrapper/bitlog.hpp b/src/c_wrapper/bitlog.hpp
new file mode 100644
index 0000000000000000000000000000000000000000..86e1171089bdd579d4d9e29ee3805a1452d94aab
--- /dev/null
+++ b/src/c_wrapper/bitlog.hpp
@@ -0,0 +1,50 @@
+// Base-2 logarithm bithack.
+
+
+
+
+#ifndef _AFJDFJSDFSD_PYOPENCL_HEADER_SEEN_BITLOG_HPP
+#define _AFJDFJSDFSD_PYOPENCL_HEADER_SEEN_BITLOG_HPP
+
+
+
+
+#include <climits>
+#include <stdint.h>
+
+namespace pyopencl
+{
+  extern const char log_table_8[];
+
+  inline unsigned bitlog2_16(uint16_t v)
+  {
+    if (unsigned long t = v >> 8)
+      return 8+log_table_8[t];
+    else 
+      return log_table_8[v];
+  }
+
+  inline unsigned bitlog2_32(uint32_t v)
+  {
+    if (uint16_t t = v >> 16)
+      return 16+bitlog2_16(t);
+    else 
+      return bitlog2_16(v);
+  }
+
+  inline unsigned bitlog2(unsigned long v)
+  {
+#if (ULONG_MAX != 4294967295)
+    if (uint32_t t = v >> 32)
+      return 32+bitlog2_32(t);
+    else 
+#endif
+      return bitlog2_32(v);
+  }
+}
+
+
+
+
+
+#endif