From 6f76bc2449d8296b4380e218be7b071dd14cb22c Mon Sep 17 00:00:00 2001 From: rubidium Date: Sat, 1 Sep 2007 08:31:36 +0000 Subject: (svn r11031) -Codechange: reduce the amount of duplication of bit counting functions. Based on patches by skidd13, SmatZ and Belugas. --- src/economy.cpp | 6 +++--- src/functions.h | 1 - src/macros.h | 22 ++++++++++++++++++++++ src/misc.cpp | 16 ---------------- src/road_cmd.cpp | 17 ++--------------- src/roadveh_cmd.cpp | 2 +- src/vehicle_gui.cpp | 12 ++---------- 7 files changed, 30 insertions(+), 46 deletions(-) diff --git a/src/economy.cpp b/src/economy.cpp index b46c37796..a9b0d9229 100644 --- a/src/economy.cpp +++ b/src/economy.cpp @@ -70,7 +70,7 @@ Money CalculateCompanyValue(const Player* p) uint num = 0; FOR_ALL_STATIONS(st) { - if (st->owner == owner) num += CountBitsSet(st->facilities); + if (st->owner == owner) num += COUNTBITS(st->facilities); } value.AddCost(num * _price.station_value * 25); @@ -146,7 +146,7 @@ int UpdateCompanyRatingAndValue(Player *p, bool update) const Station* st; FOR_ALL_STATIONS(st) { - if (st->owner == owner) num += CountBitsSet(st->facilities); + if (st->owner == owner) num += COUNTBITS(st->facilities); } _score_part[owner][SCORE_STATIONS] = num; } @@ -191,7 +191,7 @@ int UpdateCompanyRatingAndValue(Player *p, bool update) /* Generate score for variety of cargo */ { - uint num = CountBitsSet(p->cargo_types); + uint num = COUNTBITS(p->cargo_types); _score_part[owner][SCORE_CARGO] = num; if (update) p->cargo_types = 0; } diff --git a/src/functions.h b/src/functions.h index 2f1f41cbf..bcefb13c6 100644 --- a/src/functions.h +++ b/src/functions.h @@ -136,7 +136,6 @@ uint GetTownRadiusGroup(const Town* t, TileIndex tile); void ShowHighscoreTable(int difficulty, int8 rank); int FindFirstBit(uint32 x); -int CountBitsSet(uint32 value); void AfterLoadTown(); void UpdatePatches(); diff --git a/src/macros.h b/src/macros.h index e7944661a..4ff23c1df 100644 --- a/src/macros.h +++ b/src/macros.h @@ -420,6 +420,28 @@ static inline int KillFirstBit2x64(int value) } } +/** + * Counts the number of set bits in a variable. + * + * @param value the value to count the number of bits in. + * @return the number of bits. + */ +template static inline uint COUNTBITS(T value) +{ + uint num; + + /* This loop is only called once for every bit set by clearing the lowest + * bit in each loop. The number of bits is therefore equal to the number of + * times the loop was called. It was found at the following website: + * http://graphics.stanford.edu/~seander/bithacks.html */ + + for (num = 0; value != 0; num++) { + value &= (T)(value - 1); + } + + return num; +} + /** * Returns true if value a has only one bit set to 1 * diff --git a/src/misc.cpp b/src/misc.cpp index b7c47fc9a..2e3bd2bff 100644 --- a/src/misc.cpp +++ b/src/misc.cpp @@ -273,22 +273,6 @@ int FindFirstBit(uint32 value) return i; } -int CountBitsSet(uint32 value) -{ - int num; - - /* This loop is only called once for every bit set by clearing the lowest - * bit in each loop. The number of bits is therefore equal to the number of - * times the loop was called. It was found at the following website: - * http://graphics.stanford.edu/~seander/bithacks.html */ - - for (num = 0; value != 0; num++) { - value &= value - 1; - } - - return num; -} - static void Save_NAME() { int i; diff --git a/src/road_cmd.cpp b/src/road_cmd.cpp index 34163d16d..cf5015c8d 100644 --- a/src/road_cmd.cpp +++ b/src/road_cmd.cpp @@ -33,19 +33,6 @@ #include "tunnel_map.h" #include "misc/autoptr.hpp" - -static uint CountRoadBits(RoadBits r) -{ - uint count = 0; - - if (r & ROAD_NW) ++count; - if (r & ROAD_SW) ++count; - if (r & ROAD_SE) ++count; - if (r & ROAD_NE) ++count; - return count; -} - - bool CheckAllowRemoveRoad(TileIndex tile, RoadBits remove, Owner owner, bool *edge_road, RoadType rt) { RoadBits present; @@ -225,7 +212,7 @@ CommandCost CmdRemoveRoad(TileIndex tile, uint32 flags, uint32 p1, uint32 p2) MarkTileDirtyByTile(tile); } } - return CommandCost(CountRoadBits(c) * _price.remove_road); + return CommandCost(COUNTBITS(c) * _price.remove_road); } case ROAD_TILE_CROSSING: { @@ -487,7 +474,7 @@ do_clear:; pieces &= ComplementRoadBits(existing); } - cost.AddCost(CountRoadBits(pieces) * _price.build_road); + cost.AddCost(COUNTBITS(pieces) * _price.build_road); if (IsTileType(tile, MP_TUNNELBRIDGE)) { /* Pay for *every* tile of the bridge or tunnel */ cost.MultiplyCost(DistanceManhattan(IsTunnel(tile) ? GetOtherTunnelEnd(tile) : GetOtherBridgeEnd(tile), tile)); diff --git a/src/roadveh_cmd.cpp b/src/roadveh_cmd.cpp index 137ecd77e..4869ed714 100644 --- a/src/roadveh_cmd.cpp +++ b/src/roadveh_cmd.cpp @@ -1083,7 +1083,7 @@ static void RoadZPosAffectSpeed(Vehicle *v, byte old_z) static int PickRandomBit(uint bits) { uint i; - uint num = RandomRange(CountBitsSet(bits)); + uint num = RandomRange(COUNTBITS(bits)); for (i = 0; !(bits & 1) || (int)--num >= 0; bits >>= 1, i++) {} return i; diff --git a/src/vehicle_gui.cpp b/src/vehicle_gui.cpp index e2cd1d05c..da36d99f9 100644 --- a/src/vehicle_gui.cpp +++ b/src/vehicle_gui.cpp @@ -475,14 +475,6 @@ uint ShowAdditionalText(int x, int y, uint w, EngineID engine) return DrawStringMultiLine(x, y, STR_02BD, w); } -/** Count the number of bits that are set in a mask */ -static uint CountBits(uint32 mask) -{ - uint c = 0; - for (; mask != 0; mask >>= 1) if (HASBIT(mask, 0)) c++; - return c; -} - /** Display list of cargo types of the engine, for the purchase information window */ uint ShowRefitOptionsList(int x, int y, uint w, EngineID engine) { @@ -493,7 +485,7 @@ uint ShowRefitOptionsList(int x, int y, uint w, EngineID engine) char *b = _userstring; /* Draw nothing if the engine is not refittable */ - if (CountBits(cmask) <= 1) return 0; + if (COUNTBITS(cmask) <= 1) return 0; b = InlineString(b, STR_PURCHASE_INFO_REFITTABLE_TO); @@ -503,7 +495,7 @@ uint ShowRefitOptionsList(int x, int y, uint w, EngineID engine) } else { /* Check if we are able to refit to more cargo types and unable to. If * so, invert the cargo types to list those that we can't refit to. */ - if (CountBits(cmask ^ lmask) < CountBits(cmask)) { + if (COUNTBITS(cmask ^ lmask) < COUNTBITS(cmask)) { cmask ^= lmask; b = InlineString(b, STR_PURCHASE_INFO_ALL_BUT); } -- cgit v1.2.3-70-g09d2