diff options
author | smatz <smatz@openttd.org> | 2010-02-05 17:05:58 +0000 |
---|---|---|
committer | smatz <smatz@openttd.org> | 2010-02-05 17:05:58 +0000 |
commit | d62d0ac48997f9b72da0f554dcf5269694d5999e (patch) | |
tree | e849059133de10814a39a544a3060a9d7dcff672 | |
parent | a80eb0a1c00136ca3c4bbbe94842f386fdd2a334 (diff) | |
download | openttd-d62d0ac48997f9b72da0f554dcf5269694d5999e.tar.xz |
(svn r19019) -Codechange: use HasExactlyOneBit() and HasAtMostOneBit() instead of CountBits() where possible
-rw-r--r-- | src/ai/api/ai_station.cpp | 4 | ||||
-rw-r--r-- | src/ai/api/ai_tile.cpp | 2 | ||||
-rw-r--r-- | src/ai/api/ai_waypoint.cpp | 2 | ||||
-rw-r--r-- | src/core/bitmath_func.hpp | 24 | ||||
-rw-r--r-- | src/road.cpp | 2 | ||||
-rw-r--r-- | src/road_cmd.cpp | 8 | ||||
-rw-r--r-- | src/roadveh_cmd.cpp | 2 | ||||
-rw-r--r-- | src/vehicle_gui.cpp | 2 |
8 files changed, 35 insertions, 11 deletions
diff --git a/src/ai/api/ai_station.cpp b/src/ai/api/ai_station.cpp index 670355dff..5e25e5d8d 100644 --- a/src/ai/api/ai_station.cpp +++ b/src/ai/api/ai_station.cpp @@ -53,7 +53,7 @@ DEBUG(ai, 0, "GetCoverageRadius(): coverage radius of airports needs to be requested via AIAirport::GetAirportCoverageRadius(), as it requires AirportType"); return -1; } - if (CountBits(station_type) != 1) return -1; + if (!HasExactlyOneBit(station_type)) return -1; if (!_settings_game.station.modified_catchment) return CA_UNMODIFIED; switch (station_type) { @@ -89,7 +89,7 @@ /* static */ bool AIStation::HasStationType(StationID station_id, StationType station_type) { if (!IsValidStation(station_id)) return false; - if (CountBits(station_type) != 1) return false; + if (!HasExactlyOneBit(station_type)) return false; return (::Station::Get(station_id)->facilities & station_type) != 0; } diff --git a/src/ai/api/ai_tile.cpp b/src/ai/api/ai_tile.cpp index a90be1aa1..5ae15b83d 100644 --- a/src/ai/api/ai_tile.cpp +++ b/src/ai/api/ai_tile.cpp @@ -35,7 +35,7 @@ if (::GetRoadTypes(tile) != ROADTYPES_ROAD) return false; /* Depots and crossings aren't considered buildable */ if (::GetRoadTileType(tile) != ROAD_TILE_NORMAL) return false; - if (CountBits(::GetRoadBits(tile, ROADTYPE_ROAD)) != 1) return false; + if (!HasExactlyOneBit(::GetRoadBits(tile, ROADTYPE_ROAD))) return false; if (::IsRoadOwner(tile, ROADTYPE_ROAD, OWNER_TOWN)) return true; if (::IsRoadOwner(tile, ROADTYPE_ROAD, _current_company)) return true; return false; diff --git a/src/ai/api/ai_waypoint.cpp b/src/ai/api/ai_waypoint.cpp index f5badfbbb..a3845f89f 100644 --- a/src/ai/api/ai_waypoint.cpp +++ b/src/ai/api/ai_waypoint.cpp @@ -31,7 +31,7 @@ /* static */ bool AIWaypoint::HasWaypointType(StationID waypoint_id, WaypointType waypoint_type) { if (!IsValidWaypoint(waypoint_id)) return false; - if (CountBits(waypoint_type) != 1) return false; + if (!HasExactlyOneBit(waypoint_type)) return false; return (::Waypoint::Get(waypoint_id)->facilities & waypoint_type) != 0; } diff --git a/src/core/bitmath_func.hpp b/src/core/bitmath_func.hpp index 6ef67ca7f..85eaee6f9 100644 --- a/src/core/bitmath_func.hpp +++ b/src/core/bitmath_func.hpp @@ -255,6 +255,30 @@ static inline uint CountBits(T value) } /** + * Test whether \a value has exactly 1 bit set + * + * @param value the value to test. + * @return does \a value have exactly 1 bit set? + */ +template <typename T> +static FORCEINLINE bool HasExactlyOneBit(T value) +{ + return value != 0 && (value & (value - 1)) == 0; +} + +/** + * Test whether \a value has at most 1 bit set + * + * @param value the value to test. + * @return does \a value have at most 1 bit set? + */ +template <typename T> +static FORCEINLINE bool HasAtMostOneBit(T value) +{ + return (value & (value - 1)) == 0; +} + +/** * ROtate x Left by n * * @note Assumes a byte has 8 bits diff --git a/src/road.cpp b/src/road.cpp index 705270186..0da0d8ae0 100644 --- a/src/road.cpp +++ b/src/road.cpp @@ -64,7 +64,7 @@ RoadBits CleanUpRoadBits(const TileIndex tile, RoadBits org_rb) /* Accept only connective tiles */ connective = (neighbor_rb & mirrored_rb) || // Neighbor has got the fitting RoadBit - CountBits(neighbor_rb) == 1; // Neighbor has got only one Roadbit + HasExactlyOneBit(neighbor_rb); // Neighbor has got only one Roadbit } break; diff --git a/src/road_cmd.cpp b/src/road_cmd.cpp index 4b2f0957d..8b7643cc1 100644 --- a/src/road_cmd.cpp +++ b/src/road_cmd.cpp @@ -422,7 +422,7 @@ static CommandCost CheckRoadSlope(Slope tileh, RoadBits *pieces, RoadBits existi return CommandCost(); } } else { - if (CountBits(existing) == 1 && GetRoadFoundation(tileh, existing) == FOUNDATION_NONE) return CommandCost(EXPENSES_CONSTRUCTION, _price[PR_BUILD_FOUNDATION]); + if (HasExactlyOneBit(existing) && GetRoadFoundation(tileh, existing) == FOUNDATION_NONE) return CommandCost(EXPENSES_CONSTRUCTION, _price[PR_BUILD_FOUNDATION]); return CommandCost(); } } @@ -911,7 +911,7 @@ static CommandCost ClearTile_Road(TileIndex tile, DoCommandFlag flags) RoadBits b = GetAllRoadBits(tile); /* Clear the road if only one piece is on the tile OR we are not using the DC_AUTO flag */ - if ((CountBits(b) == 1 && GetRoadBits(tile, ROADTYPE_TRAM) == ROAD_NONE) || !(flags & DC_AUTO)) { + if ((HasExactlyOneBit(b) && GetRoadBits(tile, ROADTYPE_TRAM) == ROAD_NONE) || !(flags & DC_AUTO)) { RoadTypes rts = GetRoadTypes(tile); CommandCost ret(EXPENSES_CONSTRUCTION); for (RoadType rt = ROADTYPE_ROAD; rt < ROADTYPE_END; rt++) { @@ -1145,7 +1145,7 @@ static void DrawRoadBits(TileInfo *ti) } /* If there are no road bits, return, as there is nothing left to do */ - if (CountBits(road) < 2) return; + if (HasAtMostOneBit(road)) return; /* Draw extra details. */ for (const DrawRoadTileStruct *drts = _road_display_table[roadside][road | tram]; drts->image != 0; drts++) { @@ -1322,7 +1322,7 @@ static void TileLoop_Road(TileIndex tile) /* Show an animation to indicate road work */ if (t->road_build_months != 0 && (DistanceManhattan(t->xy, tile) < 8 || grp != HZB_TOWN_EDGE) && - IsNormalRoad(tile) && CountBits(GetAllRoadBits(tile)) > 1 ) { + IsNormalRoad(tile) && !HasAtMostOneBit(GetAllRoadBits(tile))) { if (GetFoundationSlope(tile, NULL) == SLOPE_FLAT && EnsureNoVehicleOnGround(tile) && Chance16(1, 40)) { StartRoadWorks(tile); diff --git a/src/roadveh_cmd.cpp b/src/roadveh_cmd.cpp index 5ff4816ad..b7dd6bb4c 100644 --- a/src/roadveh_cmd.cpp +++ b/src/roadveh_cmd.cpp @@ -1355,7 +1355,7 @@ again: uint turn_around_start_frame = RVC_TURN_AROUND_START_FRAME; RoadBits tram; - if (v->roadtype == ROADTYPE_TRAM && !IsRoadDepotTile(v->tile) && CountBits(tram = GetAnyRoadBits(v->tile, ROADTYPE_TRAM, true)) == 1) { + if (v->roadtype == ROADTYPE_TRAM && !IsRoadDepotTile(v->tile) && HasExactlyOneBit(tram = GetAnyRoadBits(v->tile, ROADTYPE_TRAM, true))) { /* * The tram is turning around with one tram 'roadbit'. This means that * it is using the 'big' corner 'drive data'. However, to support the diff --git a/src/vehicle_gui.cpp b/src/vehicle_gui.cpp index cb1c2c51b..152387c26 100644 --- a/src/vehicle_gui.cpp +++ b/src/vehicle_gui.cpp @@ -520,7 +520,7 @@ uint ShowRefitOptionsList(int left, int right, int y, EngineID engine) char *b = string; /* Draw nothing if the engine is not refittable */ - if (CountBits(cmask) <= 1) return y; + if (HasAtMostOneBit(cmask)) return y; b = InlineString(b, STR_PURCHASE_INFO_REFITTABLE_TO); |