KiCad PCB EDA Suite
Loading...
Searching...
No Matches
util.h File Reference
#include <config.h>
#include <cassert>
#include <cmath>
#include <cstdint>
#include <limits>
#include <typeinfo>
#include <type_traits>
#include <utility>
#include <algorithm>

Go to the source code of this file.

Functions

void kimathLogDebug (const char *aFormatString,...)
 Helper to avoid directly including wx/log.h for the templated functions in kimath.
 
void kimathLogOverflow (double v, const char *aTypeName)
 Workaround to avoid the empty-string conversion issue in wxWidgets.
 
template<typename in_type = long long int, typename ret_type = int>
constexpr ret_type KiCheckedCast (in_type v)
 Perform a cast between numerical types.
 
template<typename fp_type, typename ret_type = int>
constexpr ret_type KiROUND (fp_type v, bool aQuiet=false)
 Round a numeric value to an integer using "round halfway cases away from zero" and clamp the result to the limits of the return type.
 
template<typename T>
T rescale (T aNumerator, T aValue, T aDenominator)
 Scale a number (value) by rational (numerator/denominator).
 
template<typename T>
constexpr int sign (T val)
 
template<>
int rescale (int aNumerator, int aValue, int aDenominator)
 
template<>
int64_t rescale (int64_t aNumerator, int64_t aValue, int64_t aDenominator)
 
template<typename T>
constexpr T ct_sqrt_helper (T aX, T aLo, T aHi)
 
template<typename T>
constexpr T ct_sqrt (T aX)
 Floor of the square root of an integer, evaluated at compile time.
 
template<typename T>
T isqrt (T aX)
 Exact floor of the square root of an integer.
 
constexpr uint64_t UnsignedAbs (int64_t aX)
 Magnitude of a signed integer, well defined for INT64_MIN.
 
uint64_t RoundedHypotWide (uint64_t aX, uint64_t aY)
 Nearest integer to sqrt( aX^2 + aY^2 ) for magnitudes of at most 2^63, using 128-bit arithmetic.
 
uint64_t RoundedHypot (int64_t aX, int64_t aY)
 Nearest integer to sqrt( aX^2 + aY^2 ), computed exactly.
 
template<class T>
std::enable_if< std::is_floating_point< T >::value, bool >::type equals (T aFirst, T aSecond, T aEpsilon=std::numeric_limits< T >::epsilon())
 Template to compare two floating point values for equality within a required epsilon.
 

Function Documentation

◆ ct_sqrt()

template<typename T>
T ct_sqrt ( T aX)
constexpr

Floor of the square root of an integer, evaluated at compile time.

Definition at line 197 of file util.h.

References ct_sqrt_helper(), and T.

Referenced by isqrt(), RoundedHypot(), and VECTOR2< T >::SquaredEuclideanNorm().

◆ ct_sqrt_helper()

template<typename T>
T ct_sqrt_helper ( T aX,
T aLo,
T aHi )
constexpr

Definition at line 180 of file util.h.

References ct_sqrt_helper(), and T.

Referenced by ct_sqrt(), and ct_sqrt_helper().

◆ equals()

template<class T>
std::enable_if< std::is_floating_point< T >::value, bool >::type equals ( T aFirst,
T aSecond,
T aEpsilon = std::numeric_limits<T>::epsilon() )

Template to compare two floating point values for equality within a required epsilon.

Parameters
aFirstvalue to compare.
aSecondvalue to compare.
aEpsilonallowed error.
Returns
true if the values considered equal within the specified epsilon, otherwise false.

Definition at line 276 of file util.h.

References std::abs(), and T.

◆ isqrt()

template<typename T>
T isqrt ( T aX)

Exact floor of the square root of an integer.

Negative input returns the largest root representable in T.

Definition at line 206 of file util.h.

References ct_sqrt(), and T.

Referenced by SEG::Collide(), SEG::Distance(), SEG::Distance(), SEG::LineDistance(), and RoundedHypot().

◆ KiCheckedCast()

template<typename in_type = long long int, typename ret_type = int>
ret_type KiCheckedCast ( in_type v)
inlineconstexpr

Perform a cast between numerical types.

Will clamp the return value to numerical type limits.

In Debug build an assert fires if will not fit into the return type.

Definition at line 66 of file util.h.

References kimathLogOverflow(), and name.

Referenced by BOX2< VECTOR2I >::BOX2(), BOX2< VECTOR2I >::Centre(), BOX2< VECTOR2I >::GetBottom(), BOX2< VECTOR2I >::GetRight(), BOX2< VECTOR2I >::inflateAxis(), BOX2< VECTOR2I >::Intersects(), and BOX2< VECTOR2I >::Normalize().

◆ kimathLogDebug()

void kimathLogDebug ( const char * aFormatString,
... )

Helper to avoid directly including wx/log.h for the templated functions in kimath.

Definition at line 41 of file util.cpp.

Referenced by rescale().

◆ kimathLogOverflow()

void kimathLogOverflow ( double v,
const char * aTypeName )

Workaround to avoid the empty-string conversion issue in wxWidgets.

Definition at line 55 of file util.cpp.

Referenced by KiCheckedCast(), and KiROUND().

◆ KiROUND()

template<typename fp_type, typename ret_type = int>
ret_type KiROUND ( fp_type v,
bool aQuiet = false )
constexpr

Round a numeric value to an integer using "round halfway cases away from zero" and clamp the result to the limits of the return type.

In Debug build an assert fires if will not fit into the return type.

Definition at line 99 of file util.h.

References kimathLogOverflow(), and name.

◆ rescale() [1/3]

template<>
int rescale ( int aNumerator,
int aValue,
int aDenominator )

Definition at line 82 of file util.cpp.

◆ rescale() [2/3]

template<>
int64_t rescale ( int64_t aNumerator,
int64_t aValue,
int64_t aDenominator )

Definition at line 96 of file util.cpp.

References std::abs(), kimathLogDebug(), result, and sign().

◆ rescale() [3/3]

◆ RoundedHypot()

uint64_t RoundedHypot ( int64_t aX,
int64_t aY )
inline

Nearest integer to sqrt( aX^2 + aY^2 ), computed exactly.

Ties cannot occur for integer input.

Definition at line 247 of file util.h.

References ct_sqrt(), isqrt(), RoundedHypotWide(), and UnsignedAbs().

Referenced by VECTOR2< T >::EuclideanNorm().

◆ RoundedHypotWide()

uint64_t RoundedHypotWide ( uint64_t aX,
uint64_t aY )

Nearest integer to sqrt( aX^2 + aY^2 ) for magnitudes of at most 2^63, using 128-bit arithmetic.

Definition at line 62 of file util.cpp.

Referenced by RoundedHypot().

◆ sign()

◆ UnsignedAbs()

uint64_t UnsignedAbs ( int64_t aX)
constexpr

Magnitude of a signed integer, well defined for INT64_MIN.

Definition at line 234 of file util.h.

Referenced by RoundedHypot(), and VECTOR2< T >::SquaredEuclideanNorm().