Changeset - r27271:e0993573a0ba
[Not reviewed]
master
0 10 0
Rubidium - 17 months ago 2023-04-29 07:25:51
rubidium@openttd.org
Codechange: replace ClampToI32/U16 with ClampTo<int32_t/uint16_t>
10 files changed with 67 insertions and 98 deletions:
0 comments (0 inline, 0 general)
src/build_vehicle_gui.cpp
Show inline comments
 
@@ -194,7 +194,7 @@ static bool EngineCostSorter(const GUIEn
 
{
 
	Money va = Engine::Get(a.engine_id)->GetCost();
 
	Money vb = Engine::Get(b.engine_id)->GetCost();
 
	int r = ClampToI32(va - vb);
 
	int r = ClampTo<int32_t>(va - vb);
 

	
 
	/* Use EngineID to sort instead since we want consistent sorting */
 
	if (r == 0) return EngineNumberSorter(a, b);
 
@@ -262,7 +262,7 @@ static bool EngineRunningCostSorter(cons
 
{
 
	Money va = Engine::Get(a.engine_id)->GetRunningCost();
 
	Money vb = Engine::Get(b.engine_id)->GetRunningCost();
 
	int r = ClampToI32(va - vb);
 
	int r = ClampTo<int32_t>(va - vb);
 

	
 
	/* Use EngineID to sort instead since we want consistent sorting */
 
	if (r == 0) return EngineNumberSorter(a, b);
src/core/math_func.hpp
Show inline comments
 
@@ -190,37 +190,6 @@ constexpr To ClampTo(From value)
 
}
 

	
 
/**
 
 * Reduce a signed 64-bit int to a signed 32-bit one
 
 *
 
 * This function clamps a 64-bit integer to a 32-bit integer.
 
 * If the 64-bit value is smaller than the smallest 32-bit integer
 
 * value 0x80000000 this value is returned (the left one bit is the sign bit).
 
 * If the 64-bit value is greater than the greatest 32-bit integer value 0x7FFFFFFF
 
 * this value is returned. In all other cases the 64-bit value 'fits' in a
 
 * 32-bits integer field and so the value is casted to int32 and returned.
 
 *
 
 * @param a The 64-bit value to clamps
 
 * @return The 64-bit value reduced to a 32-bit value
 
 * @see Clamp(int, int, int)
 
 */
 
static inline int32 ClampToI32(const int64 a)
 
{
 
	return ClampTo<int32>(a);
 
}
 

	
 
/**
 
 * Reduce an unsigned 64-bit int to an unsigned 16-bit one
 
 *
 
 * @param a The 64-bit value to clamp
 
 * @return The 64-bit value reduced to a 16-bit value
 
 * @see ClampU(uint, uint, uint)
 
 */
 
static inline uint16 ClampToU16(const uint64 a)
 
{
 
	return ClampTo<uint16>(a);
 
}
 

	
 
/**
 
 * Returns the (absolute) difference between two (scalar) variables
 
 *
 
 * @param a The first scalar
src/ground_vehicle.cpp
Show inline comments
 
@@ -175,10 +175,10 @@ int GroundVehicle<T, Type>::GetAccelerat
 
		 * down hill will never slow down enough, and a vehicle that came up
 
		 * a hill will never speed up enough to (eventually) get back to the
 
		 * same (maximum) speed. */
 
		int accel = ClampToI32((force - resistance) / (mass * 4));
 
		int accel = ClampTo<int32_t>((force - resistance) / (mass * 4));
 
		return force < resistance ? std::min(-1, accel) : std::max(1, accel);
 
	} else {
 
		return ClampToI32(std::min<int64>(-force - resistance, -10000) / mass);
 
		return ClampTo<int32_t>(std::min<int64>(-force - resistance, -10000) / mass);
 
	}
 
}
 

	
src/newgrf_engine.cpp
Show inline comments
 
@@ -519,7 +519,7 @@ static uint32 VehicleGetVariable(Vehicle
 
			{
 
				const Vehicle *w = v->Next();
 
				assert(w != nullptr);
 
				uint16 altitude = ClampToU16(v->z_pos - w->z_pos); // Aircraft height - shadow height
 
				uint16 altitude = ClampTo<uint16_t>(v->z_pos - w->z_pos); // Aircraft height - shadow height
 
				byte airporttype = ATP_TTDP_LARGE;
 

	
 
				const Station *st = GetTargetAirportIfValid(Aircraft::From(v));
 
@@ -820,14 +820,14 @@ static uint32 VehicleGetVariable(Vehicle
 
		case 0x39: return v->cargo_type;
 
		case 0x3A: return v->cargo_cap;
 
		case 0x3B: return GB(v->cargo_cap, 8, 8);
 
		case 0x3C: return ClampToU16(v->cargo.StoredCount());
 
		case 0x3D: return GB(ClampToU16(v->cargo.StoredCount()), 8, 8);
 
		case 0x3C: return ClampTo<uint16_t>(v->cargo.StoredCount());
 
		case 0x3D: return GB(ClampTo<uint16_t>(v->cargo.StoredCount()), 8, 8);
 
		case 0x3E: return v->cargo.Source();
 
		case 0x3F: return ClampU(v->cargo.DaysInTransit(), 0, 0xFF);
 
		case 0x40: return ClampToU16(v->age);
 
		case 0x41: return GB(ClampToU16(v->age), 8, 8);
 
		case 0x42: return ClampToU16(v->max_age);
 
		case 0x43: return GB(ClampToU16(v->max_age), 8, 8);
 
		case 0x40: return ClampTo<uint16_t>(v->age);
 
		case 0x41: return GB(ClampTo<uint16_t>(v->age), 8, 8);
 
		case 0x42: return ClampTo<uint16_t>(v->max_age);
 
		case 0x43: return GB(ClampTo<uint16_t>(v->max_age), 8, 8);
 
		case 0x44: return Clamp(v->build_year, ORIGINAL_BASE_YEAR, ORIGINAL_MAX_YEAR) - ORIGINAL_BASE_YEAR;
 
		case 0x45: return v->unitnumber;
 
		case 0x46: return v->GetEngine()->grf_prop.local_id;
 
@@ -845,20 +845,20 @@ static uint32 VehicleGetVariable(Vehicle
 
		case 0x4F: return GB(v->reliability, 8, 8);
 
		case 0x50: return v->reliability_spd_dec;
 
		case 0x51: return GB(v->reliability_spd_dec, 8, 8);
 
		case 0x52: return ClampToI32(v->GetDisplayProfitThisYear());
 
		case 0x53: return GB(ClampToI32(v->GetDisplayProfitThisYear()),  8, 24);
 
		case 0x54: return GB(ClampToI32(v->GetDisplayProfitThisYear()), 16, 16);
 
		case 0x55: return GB(ClampToI32(v->GetDisplayProfitThisYear()), 24,  8);
 
		case 0x56: return ClampToI32(v->GetDisplayProfitLastYear());
 
		case 0x57: return GB(ClampToI32(v->GetDisplayProfitLastYear()),  8, 24);
 
		case 0x58: return GB(ClampToI32(v->GetDisplayProfitLastYear()), 16, 16);
 
		case 0x59: return GB(ClampToI32(v->GetDisplayProfitLastYear()), 24,  8);
 
		case 0x52: return ClampTo<int32_t>(v->GetDisplayProfitThisYear());
 
		case 0x53: return GB(ClampTo<int32_t>(v->GetDisplayProfitThisYear()),  8, 24);
 
		case 0x54: return GB(ClampTo<int32_t>(v->GetDisplayProfitThisYear()), 16, 16);
 
		case 0x55: return GB(ClampTo<int32_t>(v->GetDisplayProfitThisYear()), 24,  8);
 
		case 0x56: return ClampTo<int32_t>(v->GetDisplayProfitLastYear());
 
		case 0x57: return GB(ClampTo<int32_t>(v->GetDisplayProfitLastYear()),  8, 24);
 
		case 0x58: return GB(ClampTo<int32_t>(v->GetDisplayProfitLastYear()), 16, 16);
 
		case 0x59: return GB(ClampTo<int32_t>(v->GetDisplayProfitLastYear()), 24,  8);
 
		case 0x5A: return v->Next() == nullptr ? INVALID_VEHICLE : v->Next()->index;
 
		case 0x5B: break; // not implemented
 
		case 0x5C: return ClampToI32(v->value);
 
		case 0x5D: return GB(ClampToI32(v->value),  8, 24);
 
		case 0x5E: return GB(ClampToI32(v->value), 16, 16);
 
		case 0x5F: return GB(ClampToI32(v->value), 24,  8);
 
		case 0x5C: return ClampTo<int32_t>(v->value);
 
		case 0x5D: return GB(ClampTo<int32_t>(v->value),  8, 24);
 
		case 0x5E: return GB(ClampTo<int32_t>(v->value), 16, 16);
 
		case 0x5F: return GB(ClampTo<int32_t>(v->value), 24,  8);
 
		case 0x60: break; // not implemented
 
		case 0x61: break; // not implemented
 
		case 0x62: break; // vehicle specific, see below
src/newgrf_town.cpp
Show inline comments
 
@@ -46,21 +46,21 @@
 
		/* Town properties */
 
		case 0x80: return this->t->xy;
 
		case 0x81: return GB(this->t->xy, 8, 8);
 
		case 0x82: return ClampToU16(this->t->cache.population);
 
		case 0x83: return GB(ClampToU16(this->t->cache.population), 8, 8);
 
		case 0x82: return ClampTo<uint16_t>(this->t->cache.population);
 
		case 0x83: return GB(ClampTo<uint16_t>(this->t->cache.population), 8, 8);
 
		case 0x8A: return this->t->grow_counter / TOWN_GROWTH_TICKS;
 
		case 0x92: return this->t->flags;  // In original game, 0x92 and 0x93 are really one word. Since flags is a byte, this is to adjust
 
		case 0x93: return 0;
 
		case 0x94: return ClampToU16(this->t->cache.squared_town_zone_radius[0]);
 
		case 0x95: return GB(ClampToU16(this->t->cache.squared_town_zone_radius[0]), 8, 8);
 
		case 0x96: return ClampToU16(this->t->cache.squared_town_zone_radius[1]);
 
		case 0x97: return GB(ClampToU16(this->t->cache.squared_town_zone_radius[1]), 8, 8);
 
		case 0x98: return ClampToU16(this->t->cache.squared_town_zone_radius[2]);
 
		case 0x99: return GB(ClampToU16(this->t->cache.squared_town_zone_radius[2]), 8, 8);
 
		case 0x9A: return ClampToU16(this->t->cache.squared_town_zone_radius[3]);
 
		case 0x9B: return GB(ClampToU16(this->t->cache.squared_town_zone_radius[3]), 8, 8);
 
		case 0x9C: return ClampToU16(this->t->cache.squared_town_zone_radius[4]);
 
		case 0x9D: return GB(ClampToU16(this->t->cache.squared_town_zone_radius[4]), 8, 8);
 
		case 0x94: return ClampTo<uint16_t>(this->t->cache.squared_town_zone_radius[0]);
 
		case 0x95: return GB(ClampTo<uint16_t>(this->t->cache.squared_town_zone_radius[0]), 8, 8);
 
		case 0x96: return ClampTo<uint16_t>(this->t->cache.squared_town_zone_radius[1]);
 
		case 0x97: return GB(ClampTo<uint16_t>(this->t->cache.squared_town_zone_radius[1]), 8, 8);
 
		case 0x98: return ClampTo<uint16_t>(this->t->cache.squared_town_zone_radius[2]);
 
		case 0x99: return GB(ClampTo<uint16_t>(this->t->cache.squared_town_zone_radius[2]), 8, 8);
 
		case 0x9A: return ClampTo<uint16_t>(this->t->cache.squared_town_zone_radius[3]);
 
		case 0x9B: return GB(ClampTo<uint16_t>(this->t->cache.squared_town_zone_radius[3]), 8, 8);
 
		case 0x9C: return ClampTo<uint16_t>(this->t->cache.squared_town_zone_radius[4]);
 
		case 0x9D: return GB(ClampTo<uint16_t>(this->t->cache.squared_town_zone_radius[4]), 8, 8);
 
		case 0x9E: return this->t->ratings[0];
 
		case 0x9F: return GB(this->t->ratings[0], 8, 8);
 
		case 0xA0: return this->t->ratings[1];
 
@@ -79,24 +79,24 @@
 
		case 0xAD: return GB(this->t->ratings[7], 8, 8);
 
		case 0xAE: return this->t->have_ratings;
 
		case 0xB2: return this->t->statues;
 
		case 0xB6: return ClampToU16(this->t->cache.num_houses);
 
		case 0xB6: return ClampTo<uint16_t>(this->t->cache.num_houses);
 
		case 0xB9: return this->t->growth_rate / TOWN_GROWTH_TICKS;
 
		case 0xBA: return ClampToU16(this->t->supplied[CT_PASSENGERS].new_max);
 
		case 0xBB: return GB(ClampToU16(this->t->supplied[CT_PASSENGERS].new_max), 8, 8);
 
		case 0xBC: return ClampToU16(this->t->supplied[CT_MAIL].new_max);
 
		case 0xBD: return GB(ClampToU16(this->t->supplied[CT_MAIL].new_max), 8, 8);
 
		case 0xBE: return ClampToU16(this->t->supplied[CT_PASSENGERS].new_act);
 
		case 0xBF: return GB(ClampToU16(this->t->supplied[CT_PASSENGERS].new_act), 8, 8);
 
		case 0xC0: return ClampToU16(this->t->supplied[CT_MAIL].new_act);
 
		case 0xC1: return GB(ClampToU16(this->t->supplied[CT_MAIL].new_act), 8, 8);
 
		case 0xC2: return ClampToU16(this->t->supplied[CT_PASSENGERS].old_max);
 
		case 0xC3: return GB(ClampToU16(this->t->supplied[CT_PASSENGERS].old_max), 8, 8);
 
		case 0xC4: return ClampToU16(this->t->supplied[CT_MAIL].old_max);
 
		case 0xC5: return GB(ClampToU16(this->t->supplied[CT_MAIL].old_max), 8, 8);
 
		case 0xC6: return ClampToU16(this->t->supplied[CT_PASSENGERS].old_act);
 
		case 0xC7: return GB(ClampToU16(this->t->supplied[CT_PASSENGERS].old_act), 8, 8);
 
		case 0xC8: return ClampToU16(this->t->supplied[CT_MAIL].old_act);
 
		case 0xC9: return GB(ClampToU16(this->t->supplied[CT_MAIL].old_act), 8, 8);
 
		case 0xBA: return ClampTo<uint16_t>(this->t->supplied[CT_PASSENGERS].new_max);
 
		case 0xBB: return GB(ClampTo<uint16_t>(this->t->supplied[CT_PASSENGERS].new_max), 8, 8);
 
		case 0xBC: return ClampTo<uint16_t>(this->t->supplied[CT_MAIL].new_max);
 
		case 0xBD: return GB(ClampTo<uint16_t>(this->t->supplied[CT_MAIL].new_max), 8, 8);
 
		case 0xBE: return ClampTo<uint16_t>(this->t->supplied[CT_PASSENGERS].new_act);
 
		case 0xBF: return GB(ClampTo<uint16_t>(this->t->supplied[CT_PASSENGERS].new_act), 8, 8);
 
		case 0xC0: return ClampTo<uint16_t>(this->t->supplied[CT_MAIL].new_act);
 
		case 0xC1: return GB(ClampTo<uint16_t>(this->t->supplied[CT_MAIL].new_act), 8, 8);
 
		case 0xC2: return ClampTo<uint16_t>(this->t->supplied[CT_PASSENGERS].old_max);
 
		case 0xC3: return GB(ClampTo<uint16_t>(this->t->supplied[CT_PASSENGERS].old_max), 8, 8);
 
		case 0xC4: return ClampTo<uint16_t>(this->t->supplied[CT_MAIL].old_max);
 
		case 0xC5: return GB(ClampTo<uint16_t>(this->t->supplied[CT_MAIL].old_max), 8, 8);
 
		case 0xC6: return ClampTo<uint16_t>(this->t->supplied[CT_PASSENGERS].old_act);
 
		case 0xC7: return GB(ClampTo<uint16_t>(this->t->supplied[CT_PASSENGERS].old_act), 8, 8);
 
		case 0xC8: return ClampTo<uint16_t>(this->t->supplied[CT_MAIL].old_act);
 
		case 0xC9: return GB(ClampTo<uint16_t>(this->t->supplied[CT_MAIL].old_act), 8, 8);
 
		case 0xCA: return this->t->GetPercentTransported(CT_PASSENGERS);
 
		case 0xCB: return this->t->GetPercentTransported(CT_MAIL);
 
		case 0xCC: return this->t->received[TE_FOOD].new_act;
src/script/script_info.cpp
Show inline comments
 
@@ -146,42 +146,42 @@ SQInteger ScriptInfo::AddSetting(HSQUIRR
 
		} else if (strcmp(key, "min_value") == 0) {
 
			SQInteger res;
 
			if (SQ_FAILED(sq_getinteger(vm, -1, &res))) return SQ_ERROR;
 
			config.min_value = ClampToI32(res);
 
			config.min_value = ClampTo<int32_t>(res);
 
			items |= 0x004;
 
		} else if (strcmp(key, "max_value") == 0) {
 
			SQInteger res;
 
			if (SQ_FAILED(sq_getinteger(vm, -1, &res))) return SQ_ERROR;
 
			config.max_value = ClampToI32(res);
 
			config.max_value = ClampTo<int32_t>(res);
 
			items |= 0x008;
 
		} else if (strcmp(key, "easy_value") == 0) {
 
			SQInteger res;
 
			if (SQ_FAILED(sq_getinteger(vm, -1, &res))) return SQ_ERROR;
 
			config.easy_value = ClampToI32(res);
 
			config.easy_value = ClampTo<int32_t>(res);
 
			items |= 0x010;
 
		} else if (strcmp(key, "medium_value") == 0) {
 
			SQInteger res;
 
			if (SQ_FAILED(sq_getinteger(vm, -1, &res))) return SQ_ERROR;
 
			config.medium_value = ClampToI32(res);
 
			config.medium_value = ClampTo<int32_t>(res);
 
			items |= 0x020;
 
		} else if (strcmp(key, "hard_value") == 0) {
 
			SQInteger res;
 
			if (SQ_FAILED(sq_getinteger(vm, -1, &res))) return SQ_ERROR;
 
			config.hard_value = ClampToI32(res);
 
			config.hard_value = ClampTo<int32_t>(res);
 
			items |= 0x040;
 
		} else if (strcmp(key, "random_deviation") == 0) {
 
			SQInteger res;
 
			if (SQ_FAILED(sq_getinteger(vm, -1, &res))) return SQ_ERROR;
 
			config.random_deviation = ClampToI32(abs(res));
 
			config.random_deviation = ClampTo<int32_t>(abs(res));
 
			items |= 0x200;
 
		} else if (strcmp(key, "custom_value") == 0) {
 
			SQInteger res;
 
			if (SQ_FAILED(sq_getinteger(vm, -1, &res))) return SQ_ERROR;
 
			config.custom_value = ClampToI32(res);
 
			config.custom_value = ClampTo<int32_t>(res);
 
			items |= 0x080;
 
		} else if (strcmp(key, "step_size") == 0) {
 
			SQInteger res;
 
			if (SQ_FAILED(sq_getinteger(vm, -1, &res))) return SQ_ERROR;
 
			config.step_size = ClampToI32(res);
 
			config.step_size = ClampTo<int32_t>(res);
 
		} else if (strcmp(key, "flags") == 0) {
 
			SQInteger res;
 
			if (SQ_FAILED(sq_getinteger(vm, -1, &res))) return SQ_ERROR;
src/settings_gui.cpp
Show inline comments
 
@@ -2434,7 +2434,7 @@ struct GameSettingsWindow : Window {
 
			/* Save the correct currency-translated value */
 
			if (sd->flags & SF_GUI_CURRENCY) llvalue /= _currency->rate;
 

	
 
			value = (int32)ClampToI32(llvalue);
 
			value = ClampTo<int32_t>(llvalue);
 
		} else {
 
			value = sd->def;
 
		}
src/strgen/strgen.cpp
Show inline comments
 
@@ -99,7 +99,7 @@ struct FileStringReader : StringReader {
 

	
 
	char *ReadLine(char *buffer, const char *last) override
 
	{
 
		return fgets(buffer, ClampToU16(last - buffer + 1), this->fh);
 
		return fgets(buffer, ClampTo<uint16_t>(last - buffer + 1), this->fh);
 
	}
 

	
 
	void HandlePragma(char *str) override;
src/town_cmd.cpp
Show inline comments
 
@@ -2957,7 +2957,7 @@ CommandCost CmdExpandTown(DoCommandFlag 
 
	if (flags & DC_EXEC) {
 
		/* The more houses, the faster we grow */
 
		if (grow_amount == 0) {
 
			uint amount = RandomRange(ClampToU16(t->cache.num_houses / 10)) + 3;
 
			uint amount = RandomRange(ClampTo<uint16_t>(t->cache.num_houses / 10)) + 3;
 
			t->cache.num_houses += amount;
 
			UpdateTownRadius(t);
 

	
src/vehicle_gui.cpp
Show inline comments
 
@@ -1359,14 +1359,14 @@ static bool VehicleAgeSorter(const Vehic
 
/** Sort vehicles by this year profit */
 
static bool VehicleProfitThisYearSorter(const Vehicle * const &a, const Vehicle * const &b)
 
{
 
	int r = ClampToI32(a->GetDisplayProfitThisYear() - b->GetDisplayProfitThisYear());
 
	int r = ClampTo<int32_t>(a->GetDisplayProfitThisYear() - b->GetDisplayProfitThisYear());
 
	return (r != 0) ? r < 0 : VehicleNumberSorter(a, b);
 
}
 

	
 
/** Sort vehicles by last year profit */
 
static bool VehicleProfitLastYearSorter(const Vehicle * const &a, const Vehicle * const &b)
 
{
 
	int r = ClampToI32(a->GetDisplayProfitLastYear() - b->GetDisplayProfitLastYear());
 
	int r = ClampTo<int32_t>(a->GetDisplayProfitLastYear() - b->GetDisplayProfitLastYear());
 
	return (r != 0) ? r < 0 : VehicleNumberSorter(a, b);
 
}
 

	
 
@@ -1419,7 +1419,7 @@ static bool VehicleValueSorter(const Veh
 
	for (u = a; u != nullptr; u = u->Next()) diff += u->value;
 
	for (u = b; u != nullptr; u = u->Next()) diff -= u->value;
 

	
 
	int r = ClampToI32(diff);
 
	int r = ClampTo<int32_t>(diff);
 
	return (r != 0) ? r < 0 : VehicleNumberSorter(a, b);
 
}
 

	
 
@@ -1433,7 +1433,7 @@ static bool VehicleLengthSorter(const Ve
 
/** Sort vehicles by the time they can still live */
 
static bool VehicleTimeToLiveSorter(const Vehicle * const &a, const Vehicle * const &b)
 
{
 
	int r = ClampToI32((a->max_age - a->age) - (b->max_age - b->age));
 
	int r = ClampTo<int32_t>((a->max_age - a->age) - (b->max_age - b->age));
 
	return (r != 0) ? r < 0 : VehicleNumberSorter(a, b);
 
}
 

	
0 comments (0 inline, 0 general)