summaryrefslogtreecommitdiff
path: root/src/core
diff options
context:
space:
mode:
authorrubidium <rubidium@openttd.org>2007-12-10 22:26:24 +0000
committerrubidium <rubidium@openttd.org>2007-12-10 22:26:24 +0000
commita1dbb10c26763574d7cdd66d0e9522382164a773 (patch)
treec567c5687d1d60dfa3feffb7d149f848ed838bf2 /src/core
parentac7c79f039f43aed145c327b06ea9edadfbbe7f3 (diff)
downloadopenttd-a1dbb10c26763574d7cdd66d0e9522382164a773.tar.xz
(svn r11616) -Fix [FS#1526]: sometimes large values could go off the chart.
Diffstat (limited to 'src/core')
-rw-r--r--src/core/bitmath_func.cpp15
-rw-r--r--src/core/bitmath_func.hpp2
2 files changed, 9 insertions, 8 deletions
diff --git a/src/core/bitmath_func.cpp b/src/core/bitmath_func.cpp
index bbfad72ff..614caa53f 100644
--- a/src/core/bitmath_func.cpp
+++ b/src/core/bitmath_func.cpp
@@ -45,7 +45,7 @@ uint8 FindFirstBit(uint32 x)
}
/**
- * Search the last set bit in a 32 bit variable.
+ * Search the last set bit in a 64 bit variable.
*
* This algorithm is a static implementation of a log
* conguence search algorithm. It checks the second half
@@ -55,17 +55,18 @@ uint8 FindFirstBit(uint32 x)
* @param x The value to search
* @return The position of the last bit set
*/
-uint8 FindLastBit(uint32 x)
+uint8 FindLastBit(uint64 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; }
+ if ((x & 0xffffffff00000000ULL) != 0) { x >>= 32; pos += 32; }
+ if ((x & 0x00000000ffff0000ULL) != 0) { x >>= 16; pos += 16; }
+ if ((x & 0x000000000000ff00ULL) != 0) { x >>= 8; pos += 8; }
+ if ((x & 0x00000000000000f0ULL) != 0) { x >>= 4; pos += 4; }
+ if ((x & 0x000000000000000cULL) != 0) { x >>= 2; pos += 2; }
+ if ((x & 0x0000000000000002ULL) != 0) { pos += 1; }
return pos;
}
diff --git a/src/core/bitmath_func.hpp b/src/core/bitmath_func.hpp
index f4c6ff0bc..357a52212 100644
--- a/src/core/bitmath_func.hpp
+++ b/src/core/bitmath_func.hpp
@@ -211,7 +211,7 @@ static inline uint8 FindFirstBit2x64(const int value)
}
uint8 FindFirstBit(uint32 x);
-uint8 FindLastBit(uint32 x);
+uint8 FindLastBit(uint64 x);
/**
* Clear the first bit in an integer.