Mercurial > hg > openttd
changeset 14460:bac88a0c21bc draft
(svn r19019) -Codechange: use HasExactlyOneBit() and HasAtMostOneBit() instead of CountBits() where possible
author | smatz <smatz@openttd.org> |
---|---|
date | Fri, 05 Feb 2010 17:05:58 +0000 |
parents | 8d0e19518a53 |
children | 09b9de9dd922 |
files | src/ai/api/ai_station.cpp src/ai/api/ai_tile.cpp src/ai/api/ai_waypoint.cpp src/core/bitmath_func.hpp src/road.cpp src/road_cmd.cpp src/roadveh_cmd.cpp src/vehicle_gui.cpp |
diffstat | 8 files changed, 35 insertions(+), 11 deletions(-) [+] |
line wrap: on
line diff
--- 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; }
--- 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;
--- 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; }
--- a/src/core/bitmath_func.hpp +++ b/src/core/bitmath_func.hpp @@ -255,6 +255,30 @@ } /** + * 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
--- a/src/road.cpp +++ b/src/road.cpp @@ -64,7 +64,7 @@ /* 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;
--- a/src/road_cmd.cpp +++ b/src/road_cmd.cpp @@ -422,7 +422,7 @@ 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 @@ 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 @@ } /* 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 @@ /* 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);
--- a/src/roadveh_cmd.cpp +++ b/src/roadveh_cmd.cpp @@ -1355,7 +1355,7 @@ 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