summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/economy.cpp6
-rw-r--r--src/functions.h1
-rw-r--r--src/macros.h22
-rw-r--r--src/misc.cpp16
-rw-r--r--src/road_cmd.cpp17
-rw-r--r--src/roadveh_cmd.cpp2
-rw-r--r--src/vehicle_gui.cpp12
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
@@ -421,6 +421,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<typename T> 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
*
* This macro returns true if only one bit is set.
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);
}