diff options
author | maedhros <maedhros@openttd.org> | 2007-01-14 19:06:11 +0000 |
---|---|---|
committer | maedhros <maedhros@openttd.org> | 2007-01-14 19:06:11 +0000 |
commit | c1364599db5ef7bbf8cf6a17fee2079651c4c355 (patch) | |
tree | f7a2ab466d8e492c406f5a6e7e47f73fb2e77c27 /src | |
parent | 238ccd26fbfad7889586ce0022acc4529d317df8 (diff) | |
download | openttd-c1364599db5ef7bbf8cf6a17fee2079651c4c355.tar.xz |
(svn r8123) -Fix (r117): FindFirstBit now really returns the first bit. Fixes bug FS#538.
Diffstat (limited to 'src')
-rw-r--r-- | src/misc.cpp | 20 |
1 files changed, 11 insertions, 9 deletions
diff --git a/src/misc.cpp b/src/misc.cpp index 39714bb55..921e267b1 100644 --- a/src/misc.cpp +++ b/src/misc.cpp @@ -266,16 +266,18 @@ void InitializeLandscapeVariables(bool only_constants) int FindFirstBit(uint32 value) { - // This is much faster than the one that was before here. - // Created by Darkvater.. blame him if it is wrong ;) - // Btw, the macro FINDFIRSTBIT is better to use when your value is - // not more than 128. + // The macro FIND_FIRST_BIT is better to use when your value is + // not more than 128. byte i = 0; - if (value & 0xffff0000) { value >>= 16; i += 16; } - if (value & 0x0000ff00) { value >>= 8; i += 8; } - if (value & 0x000000f0) { value >>= 4; i += 4; } - if (value & 0x0000000c) { value >>= 2; i += 2; } - if (value & 0x00000002) { i += 1; } + + if (value == 0) return 0; + + if ((value & 0x0000ffff) == 0) { value >>= 16; i += 16; } + if ((value & 0x000000ff) == 0) { value >>= 8; i += 8; } + if ((value & 0x0000000f) == 0) { value >>= 4; i += 4; } + if ((value & 0x00000003) == 0) { value >>= 2; i += 2; } + if ((value & 0x00000001) == 0) { i += 1; } + return i; } |