summaryrefslogtreecommitdiff
path: root/src/core/bitmath_func.cpp
diff options
context:
space:
mode:
authorrubidium <rubidium@openttd.org>2007-12-02 19:21:56 +0000
committerrubidium <rubidium@openttd.org>2007-12-02 19:21:56 +0000
commitfaf096f506c27b61bf2fd4286090ec7eed0dde6f (patch)
treef1ddd90df6ac5ff2f2ff45161e825956f9bff97a /src/core/bitmath_func.cpp
parentb5a902703e26e01e92af33083a4ec2e977ad27be (diff)
downloadopenttd-faf096f506c27b61bf2fd4286090ec7eed0dde6f.tar.xz
(svn r11559) -Fix [FS#1505]: overflow when drawing graphics with high company values.
Diffstat (limited to 'src/core/bitmath_func.cpp')
-rw-r--r--src/core/bitmath_func.cpp28
1 files changed, 27 insertions, 1 deletions
diff --git a/src/core/bitmath_func.cpp b/src/core/bitmath_func.cpp
index 9f36ed9a8..53139308a 100644
--- a/src/core/bitmath_func.cpp
+++ b/src/core/bitmath_func.cpp
@@ -25,7 +25,7 @@ const uint8 _ffb_64[64] = {
* way further. If no bit is set return 0.
*
* @param x The value to search
- * @param The position of the first bit set
+ * @return The position of the first bit set
*/
uint8 FindFirstBit(uint32 x)
{
@@ -43,3 +43,29 @@ uint8 FindFirstBit(uint32 x)
return pos;
}
+
+/**
+ * Search the last 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
+ * @return The position of the last bit set
+ */
+uint8 FindLastBit(uint32 x)
+{
+ if (x == 0) return 0;
+
+ uint8 pos = 0;
+
+ if ((x & 0xffff0000) != 0) { x >>= 16; pos += 16; }
+ if ((x & 0x0000ff00) != 0) { x >>= 8; pos += 8; }
+ if ((x & 0x000000f0) != 0) { x >>= 4; pos += 4; }
+ if ((x & 0x0000000c) != 0) { x >>= 2; pos += 2; }
+ if ((x & 0x00000002) != 0) { pos += 1; }
+
+ return pos;
+}