OpenTTD Source
20241108-master-g80f628063a
|
Integer math functions. More...
#include "strong_typedef_type.hpp"
Go to the source code of this file.
Functions | |
template<typename T > | |
constexpr T | abs (const T a) |
Returns the absolute value of (scalar) variable. More... | |
template<typename T > | |
constexpr T | Align (const T x, uint n) |
Return the smallest multiple of n equal or greater than x. More... | |
template<typename T > | |
constexpr T * | AlignPtr (T *x, uint n) |
Return the smallest multiple of n equal or greater than x Applies to pointers only. More... | |
template<typename T > | |
constexpr T | Clamp (const T a, const T min, const T max) |
Clamp a value between an interval. More... | |
template<typename T > | |
constexpr T | SoftClamp (const T a, const T min, const T max) |
Clamp a value between an interval. More... | |
constexpr int | Clamp (const int a, const int min, const int max) |
Clamp an integer between an interval. More... | |
constexpr uint | ClampU (const uint a, const uint min, const uint max) |
Clamp an unsigned integer between an interval. More... | |
template<typename To , typename From , std::enable_if_t< std::is_integral< From >::value, int > = 0> | |
constexpr To | ClampTo (From value) |
Clamp the given value down to lie within the requested type. More... | |
template<typename T > | |
constexpr T | Delta (const T a, const T b) |
Returns the (absolute) difference between two (scalar) variables. More... | |
template<typename T > | |
constexpr bool | IsInsideBS (const T x, const size_t base, const size_t size) |
Checks if a value is between a window started at some base point. More... | |
template<typename T , std::enable_if_t< std::disjunction_v< std::is_convertible< T, size_t >, std::is_base_of< StrongTypedefBase, T >>, int > = 0> | |
constexpr bool | IsInsideMM (const T x, const size_t min, const size_t max) noexcept |
Checks if a value is in an interval. More... | |
template<typename T > | |
constexpr void | Swap (T &a, T &b) |
Type safe swap operation. More... | |
constexpr uint | ToPercent8 (uint i) |
Converts a "fract" value 0..255 to "percent" value 0..100. More... | |
constexpr uint | ToPercent16 (uint i) |
Converts a "fract" value 0..65535 to "percent" value 0..100. More... | |
int | DivideApprox (int a, int b) |
Deterministic approximate division. More... | |
constexpr uint | CeilDiv (uint a, uint b) |
Computes ceil(a / b) for non-negative a and b. More... | |
constexpr uint | Ceil (uint a, uint b) |
Computes ceil(a / b) * b for non-negative a and b. More... | |
constexpr int | RoundDivSU (int a, uint b) |
Computes round(a / b) for signed a and unsigned b. More... | |
constexpr uint64_t | PowerOfTen (int power) |
Computes ten to the given power. More... | |
uint32_t | IntSqrt (uint32_t num) |
Compute the integer square root. More... | |
Integer math functions.
Definition in file math_func.hpp.
|
constexpr |
Returns the absolute value of (scalar) variable.
a | The value we want to unsign |
Definition at line 23 of file math_func.hpp.
Referenced by AircraftGetEntryPoint(), CalculateCoverageLine(), CheckTrainsLengths(), DeterminePluralForm(), DivideApprox(), LinkGraphOverlay::DrawContent(), EnumCheckRoadVehCrashTrain(), Kdtree< T, TxyFunc, CoordT, DistT >::FindNearestRecursive(), FindTrainCollideEnum(), CargoPacket::GetDistance(), GetTunnelBridgeLength(), DiagonalTileIterator::operator++(), CYapfDestinationTileT< Types >::PfCalcEstimate(), CYapfDestinationTileOrStationRailT< Types >::PfCalcEstimate(), CYapfDestinationTileRoadT< Types >::PfCalcEstimate(), CYapfDestinationTileWaterT< Types >::PfCalcEstimate(), and SettingsValueAbsolute().
|
constexpr |
Return the smallest multiple of n equal or greater than x.
x | The min value |
n | The base of the number we are searching |
Definition at line 37 of file math_func.hpp.
Referenced by CacheSpriteAllocator::AllocatePtr(), DEFINE_POOL_METHOD(), MakeBMPImage(), OpenGLSprite::Update(), and ViewportSign::UpdatePosition().
|
constexpr |
Return the smallest multiple of n equal or greater than x Applies to pointers only.
x | The min value |
n | The base of the number we are searching |
Definition at line 55 of file math_func.hpp.
|
constexpr |
Computes ceil(a / b) * b for non-negative a and b.
a | Numerator |
b | Denominator |
Definition at line 331 of file math_func.hpp.
References CeilDiv().
Referenced by SurveyMemoryToText().
|
constexpr |
Computes ceil(a / b) for non-negative a and b.
a | Numerator |
b | Denominator |
Definition at line 320 of file math_func.hpp.
Referenced by BuildLandLegend(), Ceil(), CompanyCheckBankrupt(), StationViewWindow::DrawAcceptedCargo(), StationViewWindow::DrawCargoRatings(), NWidgetMainToolbarContainer::GetButtonArrangement(), GetLoadAmount(), SmallMapWindow::GetNumberRowsLegend(), Industry::RecomputeProductionMultipliers(), Map::ScaleBySize(), Map::ScaleBySize1D(), ServerNetworkAdminSocketHandler::SendCompanyInfo(), ServerNetworkAdminSocketHandler::SendCompanyUpdate(), NWidgetMatrix::SetCount(), and SurveyMemoryToText().
|
constexpr |
Clamp an integer between an interval.
This function returns a value which is between the given interval of min and max. If the given value is in this interval the value itself is returned otherwise the border of the interval is returned, according which side of the interval was 'left'.
a | The value to clamp/truncate. |
min | The minimum of the interval. |
max | the maximum of the interval. |
Definition at line 129 of file math_func.hpp.
|
constexpr |
Clamp a value between an interval.
This function returns a value which is between the given interval of min and max. If the given value is in this interval the value itself is returned otherwise the border of the interval is returned, according which side of the interval was 'left'.
a | The value to clamp/truncate. |
min | The minimum of the interval. |
max | the maximum of the interval. |
Definition at line 79 of file math_func.hpp.
Referenced by ClickChangeDateCheat(), ClickChangeMaxHlCheat(), CmdTownRating(), EnsureVisibleCaption(), GetAmplitude(), Path::GetCapacityRatio(), GetGlobalVariable(), GetSavegameFormat(), VideoDriver::GetSuggestedUIScale(), GetTileHeightBelowAircraft(), VehicleScopeResolver::GetVariable(), HandleCrashedAircraft(), SpriteAlignerWindow::OnInvalidateData(), MultiCommodityFlow::PushFlow(), SetDateWindow::SetDateWindow(), Scrollbar::SetPosition(), ScriptConfig::SetSetting(), SetStartingYear(), SmallMapWindow::SetZoomLevel(), Train::UpdateAcceleration(), and UpdateGUIZoom().
|
constexpr |
Clamp the given value down to lie within the requested type.
Specialization of ClampTo for StrongType::Typedef.
For example ClampTo<uint8_t> will return a value clamped to the range of 0 to 255. Anything smaller will become 0, anything larger will become 255.
a | The 64-bit value to clamp. |
Definition at line 167 of file math_func.hpp.
|
constexpr |
Clamp an unsigned integer between an interval.
This function returns a value which is between the given interval of min and max. If the given value is in this interval the value itself is returned otherwise the border of the interval is returned, according which side of the interval was 'left'.
a | The value to clamp/truncate. |
min | The minimum of the interval. |
max | the maximum of the interval. |
Definition at line 150 of file math_func.hpp.
Referenced by ConvertFromOldCompanyManagerFace(), and VideoDriver::UpdateAutoResolution().
|
constexpr |
Returns the (absolute) difference between two (scalar) variables.
a | The first scalar |
b | The second scalar |
Definition at line 234 of file math_func.hpp.
Referenced by DistanceManhattan(), DistanceMax(), and DistanceMaxPlusManhattan().
int DivideApprox | ( | int | a, |
int | b | ||
) |
Deterministic approximate division.
Cancels out division errors stemming from the integer nature of the division over multiple runs.
a | Dividend. |
b | Divisor. |
Definition at line 22 of file math_func.cpp.
References abs().
uint32_t IntSqrt | ( | uint32_t | num | ) |
Compute the integer square root.
num | Radicand. |
Definition at line 42 of file math_func.cpp.
|
constexpr |
Checks if a value is between a window started at some base point.
This function checks if the value x is between the value of base and base+size. If x equals base this returns true. If x equals base+size this returns false.
x | The value to check |
base | The base value of the interval |
size | The size of the interval |
Definition at line 252 of file math_func.hpp.
Referenced by AirportGetNearestTown(), CmdPlantTree(), OrthogonalTileArea::Contains(), NewGRFInspectWindow::HasVariableParameter(), Scrollbar::IsVisible(), and MoveBuoysToWaypoints().
|
constexprnoexcept |
Checks if a value is in an interval.
Returns true if a value is in the interval of [min, max).
x | The value to check |
min | The minimum of the interval |
max | The maximum of the interval |
Definition at line 268 of file math_func.hpp.
Referenced by AddChildSpriteToFoundation(), AddDateIntroducedRailTypes(), AddDateIntroducedRoadTypes(), Map::Allocate(), RoadVehicle::Crash(), DeleteLastRoadVeh(), GoalListWindow::DrawListColumn(), FileList::FindItem(), GenerateCompanyName(), GetHalftileFoundationCorner(), IsNonContinuousFoundation(), IsSpecialRailFoundation(), IsValidCorner(), MapGRFStringID(), and TTDPStringIDToOTTDStringIDMapping().
|
constexpr |
Computes ten to the given power.
power | The power of ten to get. |
Definition at line 358 of file math_func.hpp.
|
constexpr |
Computes round(a / b) for signed a and unsigned b.
a | Numerator |
b | Denominator |
Definition at line 342 of file math_func.hpp.
|
constexpr |
Clamp a value between an interval.
This function returns a value which is between the given interval of min and max. If the given value is in this interval the value itself is returned otherwise the border of the interval is returned, according which side of the interval was 'left'.
a | The value to clamp/truncate. |
min | The minimum of the interval. |
max | the maximum of the interval. |
Definition at line 102 of file math_func.hpp.
|
constexpr |
Type safe swap operation.
a | variable to swap with b |
b | variable to swap with a |
Definition at line 283 of file math_func.hpp.
Referenced by CalcHeightdiff(), IsInRangeInclusive(), AirportSpec::IsWithinMapBounds(), OrthogonalTileArea::OrthogonalTileArea(), and FlowStat::SwapShares().
|
constexpr |
Converts a "fract" value 0..65535 to "percent" value 0..100.
i | value to convert, range 0..65535 |
Definition at line 306 of file math_func.hpp.
|
constexpr |
Converts a "fract" value 0..255 to "percent" value 0..100.
i | value to convert, range 0..255 |
Definition at line 295 of file math_func.hpp.
Referenced by StationViewWindow::DrawCargoRatings(), and IndustryDirectoryWindow::GetCargoTransportedPercentsIfValid().