OpenTTD Source  20240915-master-g3784a3d3d6
math_func.hpp File Reference

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...
 

Detailed Description

Integer math functions

Definition in file math_func.hpp.

Function Documentation

◆ abs()

◆ Align()

template<typename T >
constexpr T Align ( const T  x,
uint  n 
)
constexpr

Return the smallest multiple of n equal or greater than x.

Note
n must be a power of 2
Parameters
xThe min value
nThe base of the number we are searching
Returns
The smallest multiple of n equal or greater than x

Definition at line 37 of file math_func.hpp.

Referenced by CacheSpriteAllocator::AllocatePtr(), MakeBMPImage(), OpenGLSprite::Update(), and ViewportSign::UpdatePosition().

◆ AlignPtr()

template<typename T >
constexpr T* AlignPtr ( T *  x,
uint  n 
)
constexpr

Return the smallest multiple of n equal or greater than x Applies to pointers only.

Note
n must be a power of 2
Parameters
xThe min value
nThe base of the number we are searching
Returns
The smallest multiple of n equal or greater than x
See also
Align()

Definition at line 55 of file math_func.hpp.

◆ Ceil()

constexpr uint Ceil ( uint  a,
uint  b 
)
constexpr

Computes ceil(a / b) * b for non-negative a and b.

Parameters
aNumerator
bDenominator
Returns
a rounded up to the nearest multiple of b.

Definition at line 331 of file math_func.hpp.

References CeilDiv().

Referenced by SurveyMemoryToText().

◆ CeilDiv()

◆ Clamp() [1/2]

constexpr int Clamp ( const int  a,
const int  min,
const int  max 
)
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'.

Note
The min value must be less or equal of max or you get some unexpected results.
Parameters
aThe value to clamp/truncate.
minThe minimum of the interval.
maxthe maximum of the interval.
Returns
A value between min and max which is closest to a.
See also
ClampU(uint, uint, uint)

Definition at line 129 of file math_func.hpp.

◆ Clamp() [2/2]

template<typename T >
constexpr T Clamp ( const T  a,
const T  min,
const T  max 
)
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'.

Note
The min value must be less or equal of max or you get some unexpected results.
Parameters
aThe value to clamp/truncate.
minThe minimum of the interval.
maxthe maximum of the interval.
Returns
A value between min and max which is closest to a.
See also
ClampU(uint, uint, uint)
Clamp(int, int, int)

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().

◆ ClampTo()

template<typename To , typename From , std::enable_if_t< std::is_integral< From >::value, int > = 0>
constexpr To ClampTo ( From  value)
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.

Parameters
aThe 64-bit value to clamp.
Returns
The 64-bit value reduced to a value within the given allowed range for the return type.
See also
Clamp(int, int, int)

Definition at line 167 of file math_func.hpp.

◆ ClampU()

constexpr uint ClampU ( const uint  a,
const uint  min,
const uint  max 
)
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'.

Note
The min value must be less or equal of max or you get some unexpected results.
Parameters
aThe value to clamp/truncate.
minThe minimum of the interval.
maxthe maximum of the interval.
Returns
A value between min and max which is closest to a.
See also
Clamp(int, int, int)

Definition at line 150 of file math_func.hpp.

Referenced by ConvertFromOldCompanyManagerFace(), and VideoDriver::UpdateAutoResolution().

◆ Delta()

template<typename T >
constexpr T Delta ( const T  a,
const T  b 
)
constexpr

Returns the (absolute) difference between two (scalar) variables.

Parameters
aThe first scalar
bThe second scalar
Returns
The absolute difference between the given scalars

Definition at line 234 of file math_func.hpp.

Referenced by DistanceManhattan(), DistanceMax(), and DistanceMaxPlusManhattan().

◆ DivideApprox()

int DivideApprox ( int  a,
int  b 
)

Deterministic approximate division.

Cancels out division errors stemming from the integer nature of the division over multiple runs.

Parameters
aDividend.
bDivisor.
Returns
a/b or (a/b)+1.

Definition at line 22 of file math_func.cpp.

References abs().

◆ IntSqrt()

uint32_t IntSqrt ( uint32_t  num)

Compute the integer square root.

Parameters
numRadicand.
Returns
Rounded integer square root.
Note
Algorithm taken from http://en.wikipedia.org/wiki/Methods_of_computing_square_roots

Definition at line 42 of file math_func.cpp.

◆ IsInsideBS()

template<typename T >
constexpr bool IsInsideBS ( const T  x,
const size_t  base,
const size_t  size 
)
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.

Parameters
xThe value to check
baseThe base value of the interval
sizeThe size of the interval
Returns
True if the value is in the interval, false else.

Definition at line 252 of file math_func.hpp.

Referenced by AirportGetNearestTown(), CmdPlantTree(), OrthogonalTileArea::Contains(), NewGRFInspectWindow::HasVariableParameter(), Scrollbar::IsVisible(), and MoveBuoysToWaypoints().

◆ IsInsideMM()

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 
)
constexprnoexcept

Checks if a value is in an interval.

Returns true if a value is in the interval of [min, max).

Parameters
xThe value to check
minThe minimum of the interval
maxThe maximum of the interval
See also
IsInsideBS()

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(), SkipGarbage(), and TTDPStringIDToOTTDStringIDMapping().

◆ PowerOfTen()

constexpr uint64_t PowerOfTen ( int  power)
constexpr

Computes ten to the given power.

Parameters
powerThe power of ten to get.
Returns
The power of ten.

Definition at line 358 of file math_func.hpp.

◆ RoundDivSU()

constexpr int RoundDivSU ( int  a,
uint  b 
)
constexpr

Computes round(a / b) for signed a and unsigned b.

Parameters
aNumerator
bDenominator
Returns
Quotient, rounded to nearest

Definition at line 342 of file math_func.hpp.

◆ SoftClamp()

template<typename T >
constexpr T SoftClamp ( const T  a,
const T  min,
const T  max 
)
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'.

Note
If the min value is greater than the max, return value is the average of the min and max.
Parameters
aThe value to clamp/truncate.
minThe minimum of the interval.
maxthe maximum of the interval.
Returns
A value between min and max which is closest to a.

Definition at line 102 of file math_func.hpp.

◆ Swap()

template<typename T >
constexpr void Swap ( T &  a,
T &  b 
)
constexpr

Type safe swap operation.

Parameters
avariable to swap with b
bvariable to swap with a

Definition at line 283 of file math_func.hpp.

Referenced by CalcHeightdiff(), IsInRangeInclusive(), AirportSpec::IsWithinMapBounds(), OrthogonalTileArea::OrthogonalTileArea(), and FlowStat::SwapShares().

◆ ToPercent16()

constexpr uint ToPercent16 ( uint  i)
constexpr

Converts a "fract" value 0..65535 to "percent" value 0..100.

Parameters
ivalue to convert, range 0..65535
Returns
value in range 0..100

Definition at line 306 of file math_func.hpp.

◆ ToPercent8()

constexpr uint ToPercent8 ( uint  i)
constexpr

Converts a "fract" value 0..255 to "percent" value 0..100.

Parameters
ivalue to convert, range 0..255
Returns
value in range 0..100

Definition at line 295 of file math_func.hpp.

Referenced by StationViewWindow::DrawCargoRatings(), and IndustryDirectoryWindow::GetCargoTransportedPercentsIfValid().