Unlock this feature

This feature isn’t part of your plan yet

Contact sales to get upgraded to the full DevStudio experience.

Unlock this feature

This feature isn't part of your plan yet.

Elementary Math Functions

Logarithmic Functions

log2function
File: /src/math.hppLine 151

Computes compile-time binary logarithm (logarithm to the base 2) of an integer.

constexpr std::int32_t log2(std::int32_t n)
NameTypeDescription
nstd::int32_tThe input value.

constexpr std::int32_t — result The binary logarithm output value.

logfunction
File: /src/math.hppLines 699–704

Computes the natural logarithm of input variable inp.

Convenience overload that deduces numFracBitsIn from the input type and computes a safe numFracBitsOut.

template <typename T,
          FracRepType numFracBitsIn = getNumFractionalBitsOrZero<typename RemoveCore<T>::type>(),
          FracRepType numFracBitsOut = calcLogFracBits<numFracBitsIn>()>
decltype(auto) log(const T& inp)
NameTypeDefaultDescription
TtypenameInput type: qVar_t<FixedPoint<U,F>> for CORE, FixedPoint<U,F> for IMD.
numFracBitsInFracRepTypegetNumFractionalBitsOrZero<typename RemoveCore<T>::type>()Number of fractional bits in the input, deduced from T.
numFracBitsOutFracRepTypecalcLogFracBits<numFracBitsIn>()Number of fractional bits in the output. Defaults to `calcLogFracBits<numFracBitsIn>()`.
NameTypeDescription
inpconst T&The input value.

decltype(auto) — qVar_t<FixedPoint<U,numFracBitsOut>> for CORE input, FixedPoint<U,numFracBitsOut> for IMD input.

Min, Max, Clip Functions

clipfunction
File: /src/math.hppLines 1667–1672

Clips input to the required range and converts to the required type.

template <typename T, typename InputType>
qVar_t<T> clip(qVar_t<InputType> value,
               InputType min = std::numeric_limits<T>::min(),
               InputType max = std::numeric_limits<T>::max())
NameTypeDefaultDescription
TtypenameType of output.
InputTypetypenameType of input.
NameTypeDescription
valueqVar_t<InputType>Value to be converted.
minInputTypeMin value of a required range. Default is type `T` min value.
maxInputTypeMax value of a required range. Default is type `T` min value.

qVar_t<T> — qVar_t<T> Converted value within [min, max] range

minfunction
File: /src/math.hppLines 111–114

Compute variadic min operation, can accept different int and FX types for the arguments as long as they are all signed or all unsigned

template <typename T, std::enable_if_t<same_signed<T, Args...>::value, int> = 0>
std::common_type_t<T, Args...> min(T value, Args... args)
NameTypeDefaultDescription
Ttypenamethe type of the first argument
NameTypeDescription
valueTthe value of the first argument
argsArgs...the values of the rest of the arguments

std::common_type_t<T, Args...> — The min value among the arguments

maxfunction
File: /src/math.hppLines 85–93

Computes the max value in an NDArray.

template <std::size_t numElements, typename NDArrayType>
decltype(auto) max(const NDArrayType& ndArray)
NameTypeDefaultDescription
numElementsstd::size_tThe number of values in the `NDArray`.
NDArrayTypetypenameEither an NDArray or NDArrayView containing our elements.
NameTypeDescription
ndArrayconst NDArrayType&The input array.

decltype(auto) — The max value.

minfunction
File: /src/math.hppLines 111–114

Compute variadic min operation, can accept different int and FX types for the arguments as long as they are all signed or all unsigned

template <typename T, std::enable_if_t<same_signed<T, Args...>::value, int> = 0>
std::common_type_t<T, Args...> min(T value, Args... args)
NameTypeDefaultDescription
Ttypenamethe type of the first argument
NameTypeDescription
valueTthe value of the first argument
argsArgs...the values of the rest of the arguments

std::common_type_t<T, Args...> — The min value among the arguments

minfunction
File: /src/math.hppLines 125–132

Computes the min value in an NDArray.

template <std::size_t numElements, typename T>
T min(const container::NDArray<T, numElements>& ndArray)
NameTypeDefaultDescription
numElementsstd::size_tThe number of values in the `NDArray`.
TtypenameThe type of the input.
NameTypeDescription
ndArrayconst container::NDArray<T, numElements>&The input array.

T — The min value.

Exponent & Power Functions

sqrtfunction
File: /src/math.hppLines 1583–1607

Computes the square root of input qInput.

template <FracRepType numFracBitsIn,
          FracRepType numFracBitsOut = calcSqrtFracBits<numFracBitsIn>(),
          typename T,
          IfIntegerTy = 0>
qVar_t<FixedPoint<T, numFracBitsOut>> sqrt(qVar_t<FixedPoint<T, numFracBitsIn>> qInput)
NameTypeDefaultDescription
numFracBitsInFracRepTypeNumber of bits used for fractional part on input.
numFracBitsOutFracRepTypecalcSqrtFracBits<numFracBitsIn>()Number of bits used for fractional part of output.
TtypenameThe underlying type for the FixedPoint values.
NameTypeDescription
qInputqVar_t<FixedPoint<T, numFracBitsIn>>The input to the square-root function.

qVar_t<FixedPoint<T, numFracBitsOut>> — qVar_t<FixedPoint<T, numFracBitsOut>> The output to the square-root function.

expfunction
File: /src/math.hppLines 857–882

Computes the natural exponent of input variable inp.

Primary overload requiring explicit numFracBitsIn. expMethod and numFracBitsOut have defaults and do not need to be specified unless overriding. Use the convenience overload below for automatic deduction of numFracBitsIn from FixedPoint input types.

template <FracRepType numFracBitsIn,
          ExpMethod expMethod = ExpMethod::FAST,
          FracRepType numFracBitsOut = numFracBitsIn,
          typename T>
decltype(auto) exp(const T& inp)
NameTypeDefaultDescription
numFracBitsInFracRepTypeNumber of fractional bits in the input.
expMethodExpMethodExpMethod::FASTAlgorithm: ExpMethod::FAST (default) or ExpMethod::REMEZ (more accurate).
numFracBitsOutFracRepTypenumFracBitsInNumber of fractional bits in the output.
TtypenameInput type: qVar_t<FixedPoint<U,F>>, FixedPoint<U,F>, qVar_t<U>, or integer U.
NameTypeDescription
inpconst T&The input value.

decltype(auto) — qVar_t<FixedPoint<U,F>>, FixedPoint<U,F>, qVar_t<U>, or integer U.

ExpMethod::REMEZ

powfunction
File: /src/math.hppLines 919–1000

Computes runtime Pow of input variable.

template <typename InpType,
          typename ExpType,
          FracRepType numFracBitsOut = getNumFractionalBitsOrZero<InpType>(),
          IfNotIntegerTy = 0>
INLINE decltype(auto) pow(const InpType& inp, const ExpType& exponent)
NameTypeDefaultDescription
InpTypetypenameThe input type of the base.
ExpTypetypenameThe input type of the exponent.
numFracBitsOutFracRepTypegetNumFractionalBitsOrZero<InpType>()
NameTypeDescription
inpconst InpType&The input value.
exponentconst ExpType&The exponent value.

INLINE decltype(auto) — FixedPoint32<numFracBitsOut> or qVar<FixedPoint32<numFracBitsOut>> The power of input 'inp' with exponent 'exponent'.

Trigonometric Functions

cosfunction
File: /src/math.hppLines 1266–1277

Computes the cosine of input FixedPoint angle.

template <bool transformRange = false,
          FracRepType outNumFracBitsVal = 32,
          typename OutT = std::int32_t,
          typename AngleType>
decltype(auto) cos(const AngleType& inp)
NameTypeDefaultDescription
transformRangeboolfalseWhether to transform the input angle to [-pi/2, pi/2] range. Allows input with arbitrary value, produces additional overhead.
outNumFracBitsValFracRepType32Number of bits to be used for fractional part in the output `FixedPoint`
OutTtypenamestd::int32_tUnderlying type of the output `FixedPoint`, defaults to `std::int32_t`
AngleTypetypenameType of the input angle, expected to be a `FixedPoint`
NameTypeDescription
inpconst AngleType&The input angle in radians, as a `FixedPoint`

decltype(auto) — The cosine of input `FixedPoint`.

sinfunction
File: /src/math.hppLines 1237–1248

Computes the sine of input FixedPoint angle.

template <bool transformRange = false,
          FracRepType outNumFracBitsVal = 32,
          typename OutT = std::int32_t,
          typename AngleType>
decltype(auto) sin(const AngleType& inp)
NameTypeDefaultDescription
transformRangeboolfalseWhether to transform the input angle to [-pi/2, pi/2] range. Allows input with arbitrary value, produces additional overhead.
outNumFracBitsValFracRepType32Number of bits to be used for fractional part in the output `FixedPoint`
OutTtypenamestd::int32_tUnderlying type of the output `FixedPoint`, defaults to `std::int32_t`
AngleTypetypenameType of the input angle, expected to be a `FixedPoint`
NameTypeDescription
inpconst AngleType&The input angle in radians, as a `FixedPoint`

decltype(auto) — The sine of input `FixedPoint`.

Round & Truncate

roundfunction
File: /src/math.hppLines 1472–1485

Rounds the input fixed-point value to the nearest integer in the same fixed point representation.

Ties in case of half-integer fixed-points are resolved by the specified rounding method.

template <RoundMethod t,
          FracRepType numFracBits,
          typename T,
          std::enable_if_t<t != RoundMethod::awayFromZero, bool> = 0>
qVar_t<FixedPoint<T, numFracBits>> round(qVar_t<FixedPoint<T, numFracBits>> inp)
NameTypeDefaultDescription
tRoundMethod`math::RoundMethod` to be used for tie resolution (default: `RoundMethod::towardsPositiveInfinity`).
numFracBitsFracRepTypeNumber of fractional bits used in the fixed point representation.
TtypenameUnderlying type of the `FixedPoint` input.
NameTypeDescription
inpqVar_t<FixedPoint<T, numFracBits>>Input value to be rounded to the nearest integer in same fixed-point representation.

qVar_t<FixedPoint<T, numFracBits>> — Rounded fixed-point value.

truncfunction
File: /src/math.hppLines 1301–1309

Truncates the input to the nearest integer towards negative infinity.

Note: Unlike <code><a href="https://en.cppreference.com/w/cpp/numeric/math/trunc">std::trunc</a></code> which truncates towards zero, this function truncates towards negative infinity. For example,

std::trunc(-2.7) == -2.0
chimera::math::trunc(-2.7) == -3.0     // The double values are representative of the fixed point values.
template <FracRepType numFracBits,
          typename T,
          std::enable_if_t<IsFixedPoint<typename RemoveCore<T>::type>::value, bool> = 0>
T trunc(const T& inp)
NameTypeDefaultDescription
numFracBitsFracRepTypeNumber of fractional bits in the input `FixedPoint`.
TtypenameType of input value.
NameTypeDescription
inpconst T&Input fixed point number.

T — Nearest integer towards negative infinity. / // clang-format on

Sign in to your account

Don't have an account? 
By signing in, you are agreeing to our Terms of Use and Privacy Policy.
Quadric // One architecture. Every algorithm.

Develop.

Simulate.

Profile.

Collaborate.