(svn r19019) -Codechange: use HasExactlyOneBit() and HasAtMostOneBit() instead of CountBits() where possible
This commit is contained in:
		@@ -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;
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -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;
 | 
			
		||||
 
 | 
			
		||||
@@ -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;
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -254,6 +254,30 @@ static inline uint CountBits(T value)
 | 
			
		||||
	return num;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * 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
 | 
			
		||||
 *
 | 
			
		||||
 
 | 
			
		||||
@@ -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;
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -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);
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -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
 | 
			
		||||
 
 | 
			
		||||
@@ -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);
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user