summaryrefslogtreecommitdiff
path: root/src/core/bitmath_func.cpp
diff options
context:
space:
mode:
authorskidd13 <skidd13@openttd.org>2007-11-26 17:50:22 +0000
committerskidd13 <skidd13@openttd.org>2007-11-26 17:50:22 +0000
commit71c10f7df7c1ec20bbc0c23580041a2e337b2500 (patch)
tree2e39f6768332621cf233165bcce1d8d2ee07f273 /src/core/bitmath_func.cpp
parent734b22e070ca1dac717780d0a4d1ca962c4ddfbf (diff)
downloadopenttd-71c10f7df7c1ec20bbc0c23580041a2e337b2500.tar.xz
(svn r11527) -Codechange: Split the bitmath functions of to their own files
Diffstat (limited to 'src/core/bitmath_func.cpp')
-rw-r--r--src/core/bitmath_func.cpp45
1 files changed, 45 insertions, 0 deletions
diff --git a/src/core/bitmath_func.cpp b/src/core/bitmath_func.cpp
new file mode 100644
index 000000000..9f36ed9a8
--- /dev/null
+++ b/src/core/bitmath_func.cpp
@@ -0,0 +1,45 @@
+/* $Id$ */
+
+/** @file bitmath_func.cpp */
+
+#include "../stdafx.h"
+#include "bitmath_func.hpp"
+
+const uint8 _ffb_64[64] = {
+ 0, 0, 1, 0, 2, 0, 1, 0,
+ 3, 0, 1, 0, 2, 0, 1, 0,
+ 4, 0, 1, 0, 2, 0, 1, 0,
+ 3, 0, 1, 0, 2, 0, 1, 0,
+ 5, 0, 1, 0, 2, 0, 1, 0,
+ 3, 0, 1, 0, 2, 0, 1, 0,
+ 4, 0, 1, 0, 2, 0, 1, 0,
+ 3, 0, 1, 0, 2, 0, 1, 0,
+};
+
+/**
+ * Search the first set bit in a 32 bit variable.
+ *
+ * This algorithm is a static implementation of a log
+ * conguence search algorithm. It checks the first half
+ * if there is a bit set search there further. And this
+ * way further. If no bit is set return 0.
+ *
+ * @param x The value to search
+ * @param The position of the first bit set
+ */
+uint8 FindFirstBit(uint32 x)
+{
+ if (x == 0) return 0;
+ /* The macro FIND_FIRST_BIT is better to use when your x is
+ not more than 128. */
+
+ uint8 pos = 0;
+
+ if ((x & 0x0000ffff) == 0) { x >>= 16; pos += 16; }
+ if ((x & 0x000000ff) == 0) { x >>= 8; pos += 8; }
+ if ((x & 0x0000000f) == 0) { x >>= 4; pos += 4; }
+ if ((x & 0x00000003) == 0) { x >>= 2; pos += 2; }
+ if ((x & 0x00000001) == 0) { pos += 1; }
+
+ return pos;
+}