Skip to content

Commit

Permalink
Fix OpenRCT2#13567: Cure guests of severe gluttony
Browse files Browse the repository at this point in the history
Incorrect assumption that bitscanforward itertated over 64 bits meant that food that was previously within the ExtraItemFlags would never get removed from the peeps inventory. bitscanforward function has been replaced with a 64bit version
  • Loading branch information
duncanspumpkin committed Dec 16, 2020
1 parent 8fc167a commit 7dc2b99
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
26 changes: 24 additions & 2 deletions src/openrct2/util/Util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,12 +172,34 @@ int32_t bitscanforward(int32_t source)
int32_t success = __builtin_ffs(source);
return success - 1;
#else
# pragma message "Falling back to iterative bitscan forward, consider using intrinsics"
# pragma message("Falling back to iterative bitscan forward, consider using intrinsics")
// This is a low-hanging optimisation boost, check if your compiler offers
// any intrinsic.
// cf. https://github.com/OpenRCT2/OpenRCT2/pull/2093
for (int32_t i = 0; i < 32; i++)
if (source & (1u << i))
if (source & (1ull << i))
return i;

return -1;
#endif
}

int32_t bitscanforward(int64_t source)
{
#if defined(_MSC_VER) && (_MSC_VER >= 1400) && defined(_M_X64) // Visual Studio 2005
DWORD i;
uint8_t success = _BitScanForward64(&i, static_cast<uint64_t>(source));
return success != 0 ? i : -1;
#elif defined(__GNUC__)
int32_t success = __builtin_ffsll(source);
return success - 1;
#else
# pragma message("Falling back to iterative bitscan forward, consider using intrinsics")
// This is a low-hanging optimisation boost, check if your compiler offers
// any intrinsic.
// cf. https://github.com/OpenRCT2/OpenRCT2/pull/2093
for (int32_t i = 0; i < 64; i++)
if (source & (1ull << i))
return i;

return -1;
Expand Down
1 change: 1 addition & 0 deletions src/openrct2/util/Util.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ bool sse41_available();
bool avx2_available();

int32_t bitscanforward(int32_t source);
int32_t bitscanforward(int64_t source);
void bitcount_init();
int32_t bitcount(uint32_t source);
int32_t strcicmp(char const* a, char const* b);
Expand Down

0 comments on commit 7dc2b99

Please sign in to comment.