Elementary Functions

argmax

template<bool minIndex = false, typename T>
INLINE qVar_t<std::int32_t> qmath::argmax(qVar_t<T> qIn[], const std::int32_t tiles)

Get the max index for an array of data.

Return

the index of the max element

Template Parameters
  • minIndex: by default, the function returns an arbitrary element in case of a tie, set minIndex to true if a min index is required, requiring a min index will incur some overhead

  • T: type for the input

Parameters
  • qIn: input, will be overwriteen with max value

  • tiles: the number of tiles

template<bool minIndex = false, typename T>
INLINE qVar_t<std::int32_t> qmath::argmax(qVar_t<T> &qIn)

Get the max index for a tile of data.

Return

the index of the max element

Template Parameters
  • minIndex: by default, the function returns an arbitrary element in case of a tie, set minIndex to true if a min index is required, requiring a min index will incur some overhead

  • T: type for the input

Parameters
  • qIn: input, will be overwriteen with max value

cos

template<FracRepType numFracBits, bool transformRange = false, typename T, IfIntegerTy<T> = 0>
INLINE qVar_t<T> qmath::cos(const qVar_t<T> &inp)

Computes cosine of input qVar.

Template Parameters
  • T: Underlying type of qVar (deductible)

  • numIntBits: Number of bits used for integer part

  • numFracBits: Number of bits used for fractional part

  • transformRange: allows input with arbitrary value, produces additional overhead

Parameters
  • [in] inp: The input theta in radians (between -pi/2 and pi/2) qVar. Input type is 32-bit fixed point. `*. Error Table:

     *   Number of Fractional Bits | Percent of Fixed Point computes w/ rel. error greater than 1e-4 | Maximum Absolute
    Error
     *             2               |                     84.6                                        |         2.47e-1
     *             3               |                     92.0                                        |         1.25e-1
     *             4               |                     96.1                                        |         6.25e-2
     *             5               |                     98.0                                        |         3.12e-2
     *             6               |                     99.0                                        |         1.56e-2
     *             7               |                     98.5                                        |         7.81e-3
     *             8               |                     97.0                                        |         3.91e-3
     *             9               |                     95.5                                        |         1.95e-3
     *            10               |                     93.3                                        |         9.77e-4
     *            11               |                     87.9                                        |         4.88e-4
     *            12               |                     74.3                                        |         2.44e-4
     *            13               |                     48.0                                        |         1.22e-4
     *            14               |                     20.3                                        |         6.11e-5
     *            15               |                     10.0                                        |         3.05e-5
     *            16               |                     5.00                                        |         1.53e-5
     *            17               |                     2.40                                        |         7.66e-6
     *            18               |                     1.42                                        |         3.84e-6
     *            19               |                     0.678                                       |         1.94e-6
     *            20               |                     0.197                                       |         1.01e-6
     *            21               |                     0.197                                       |         5.36e-7
     *            22               |                     0.0480                                      |         2.98e-7
     *            23               |                     0.0480                                      |         1.79e-7
     *            24               |                     0.0256                                      |         1.79e-7
     *            25               |                     0.0220                                      |         1.49e-7
     *            26               |                     0.0203                                      |         1.27e-7
     *            27               |                     0.0195                                      |         1.19e-7
     *            28               |                     0.0193                                      |         1.19e-7
     *            29               |                     0.0193                                      |         1.19e-7
     *
    

exp

template<FracRepType numFracBits, typename T, IfIntegerTy<T> = 0>
INLINE qVar_t<T> qmath::exp(const qVar_t<T> &inp)

Computes Natural Exponent of input variable.

Template Parameters
  • numFracBits: Number of bits used for fractional part

  • T: Underlying type of qVar (deductible)

Parameters
  • [in] inp: The input value. Input type is 32-bit fixed point.

incrementMagnitudeByHalf

template<FracRepType numFracBits, typename T>
INLINE const qVar_t<T> qmath::incrementMagnitudeByHalf(const qVar_t<T> &op)

performs fixed point increment which involves adding (1 << (numFracBits - 1)) in magnitude

Return

qVar_t<T> Fixed point result

Template Parameters
  • numFracBits: Number of Fraction bits used to represent fixed point input

Parameters
  • op: represented in fixed point

inverseSqrt

template<FracRepType numFracBits, typename T>
INLINE qVar_t<FixedPoint<T, numFracBits>> qmath::inverseSqrt(const qVar_t<FixedPoint<T, numFracBits>> &qX)

Computes inverse sqrt of input qVar using Newton’s Method.

see

https://www.geometrictools.com/Documentation/ApproxInvSqrt.pdf
Template Parameters
  • T: Underlying type of qVar (deductible)

  • numFracBits: Number of bits used for fractional part

Parameters
  • [in] x: The input of the square-root function

log

template<FracRepType numFracBits, typename T>
INLINE qVar_t<FixedPoint<T, numFracBits>> qmath::log(const qVar_t<FixedPoint<T, numFracBits>> &inp)

Computes natural logarithm of input qVar. The expected input type is signed 32-bit fixed-point with 16 bits for integer part and 16 bits for fractional part. The output will be generated in the same fashion.

https://www.quinapalus.com/efunc.html

Template Parameters
  • T: Underlying type of qVar (deductible)

  • numIntBits: Number of bits used for integer part.

  • numFracBits: Number of bits used for fractional part.

Parameters
  • [in] inp: The input qVar. Input type is 32-bit fixed point.

    *  Error Table:
    *   Number of Fractional Bits | Percent of Fixed Point computes w/ rel. error greater than 1e-4 | Maximum Absolute Error
    *             5               |                     95.8                                        |         1.40e-1
    *             6               |                     97.4                                        |         9.18e-2
    *             7               |                     95.4                                        |         5.25e-3
    *             8               |                     88.3                                        |         2.49e-2
    *             9               |                     63.4                                        |         1.04e-2
    *            10               |                     35.1                                        |         4.78e-3
    *            11               |                     2.84                                        |         2.10e-3
    *            12               |                     0.018                                       |         9.13e-4
    *            13               |                     0.00200                                     |         4.02e-4
    *            14               |                     0.000754                                    |         1.79e-4
    *            15               |                     0.000920                                    |         8.92e-5
    *            16               |                     0.00204                                     |         7.77e-5
    *            17               |                     0.00214                                     |         5.02e-5
    *            18               |                     0.00261                                     |         4.08e-5
    *            19               |                     0.00596                                     |         3.67e-5
    *            20               |                     0.0101                                      |         3.29e-5
    *            21               |                     0.0202                                      |         3.19e-5
    *            22               |                     0.0389                                      |         3.09e-5
    *            23               |                     0.0763                                      |         3.09e-5
    *            24               |                     0.152                                       |         3.05e-5
    *            25               |                     0.302                                       |         3.05e-5
    *            26               |                     0.604                                       |         3.05e-5
    *            27               |                     1.21                                        |         3.05e-5
                              |         3.05e-5
    *
    

log2

constexpr std::int32_t qmath::log2(std::int32_t n)

Computes compile time log2 function of integer.

Parameters
  • [in] n: The input value.

sin

template<FracRepType numFracBits, bool transformRange = false, typename T, IfIntegerTy<T> = 0>
INLINE qVar_t<T> qmath::sin(const qVar_t<T> &inp)

Computes sine of input qVar.

Template Parameters
  • T: Underlying type of qVar (deductible)

  • numIntBits: Number of bits used for integer part

  • numFracBits: Number of bits used for fractional part

  • transformRange: allows input with arbitrary value, produces additional overhead

Parameters
  • [in] inp: The input theta in radians (between -pi/2 and pi/2) qVar. Input type is 32-bit fixed point. . Error Table:

    *   Number of Fractional Bits | Percent of Fixed Point computes w/ rel. error greater than 1e-4 | Maximum Absolute
    Error
    *             2               |                     84.6                                        |         2.47e-1
    *             3               |                     92.0                                        |         1.25e-1
    *             4               |                     96.1                                        |         6.25e-2
    *             5               |                     98.0                                        |         3.12e-2
    *             6               |                     99.0                                        |         1.56e-2
    *             7               |                     98.5                                        |         7.81e-3
    *             8               |                     97.0                                        |         3.91e-3
    *             9               |                     95.5                                        |         1.95e-3
    *            10               |                     93.3                                        |         9.77e-4
    *            11               |                     87.9                                        |         4.88e-4
    *            12               |                     74.3                                        |         2.44e-4
    *            13               |                     48.0                                        |         1.22e-4
    *            14               |                     20.3                                        |         6.11e-5
    *            15               |                     10.0                                        |         3.05e-5
    *            16               |                     5.00                                        |         1.53e-5
    *            17               |                     2.40                                        |         7.66e-6
    *            18               |                     1.42                                        |         3.84e-6
    *            19               |                     0.678                                       |         1.94e-6
    *            20               |                     0.197                                       |         1.01e-6
    *            21               |                     0.197                                       |         5.36e-7
    *            22               |                     0.0480                                      |         2.98e-7
    *            23               |                     0.0480                                      |         1.79e-7
    *            24               |                     0.0256                                      |         1.79e-7
    *            25               |                     0.0220                                      |         1.49e-7
    *            26               |                     0.0203                                      |         1.27e-7
    *            27               |                     0.0195                                      |         1.19e-7
    *            28               |                     0.0193                                      |         1.19e-7
    *            29               |                     0.0193                                      |         1.19e-7
    *
    

sqrt

template<FracRepType numFracBits, typename T>
INLINE qVar_t<FixedPoint<T, numFracBits>> qmath::sqrt(qVar_t<FixedPoint<T, numFracBits>> qX)

Computes sqrt of input qVar using Newton’s Method.

Template Parameters
  • T: Underlying type of qVar (deductible)

  • numFracBits: Number of bits used for fractional part

Parameters
  • [in] x: The input of the square-root function

stdRound

template<FracRepType numFracBits, typename T>
INLINE const qVar_t<T> qmath::stdRound(const qVar_t<T> &op)

performs fixed point round which involves adding (1 << (numFracBits - 1)) then two shifts (and negation if necessary). Rounding at 0.5 is away from 0

Return

qVar_t<T> Fixed point result

Template Parameters
  • numFracBits: Number of Fraction bits used to represent fixed point input

Parameters
  • op: represented in fixed point

vectorMag

template<FracRepType numFracBits, typename T, IfIntegerTy<T> = 0>
INLINE qVar_t<T> qmath::vectorMag(qVar_t<T> &x, qVar_t<T> &y)

Computes magnitude of input qVar. Ranges will vary based on inputs but it recommended to use one more more integer bit to represent the largest input.

Template Parameters
  • T: Underlying type of qVar (deductible)

  • numIntBits: Number of bits used for integer part

  • numFracBits: Number of bits used for fractional part

Parameters
  • [in] x: The input in the x dimension (gets flipped if negative). Input type is 32-bit fixed point.

  • [in] y: The input in the y dimension. Input type is 32-bit fixed point.