WPILibC++ 2024.3.2
MathExtras.h File Reference
#include "wpi/bit.h"
#include "wpi/Compiler.h"
#include <bit>
#include <cassert>
#include <climits>
#include <cstdint>
#include <cstring>
#include <limits>
#include <type_traits>

Go to the source code of this file.

Namespaces

namespace  wpi
 

Macros

#define R2(n)   n, n + 2 * 64, n + 1 * 64, n + 3 * 64
 
#define R4(n)   R2(n), R2(n + 2 * 16), R2(n + 1 * 16), R2(n + 3 * 16)
 
#define R6(n)   R4(n), R4(n + 2 * 4), R4(n + 1 * 4), R4(n + 3 * 4)
 

Functions

template<typename T >
wpi::maskTrailingOnes (unsigned N)
 Create a bitmask with the N right-most bits set to 1, and all other bits set to 0. More...
 
template<typename T >
wpi::maskLeadingOnes (unsigned N)
 Create a bitmask with the N left-most bits set to 1, and all other bits set to 0. More...
 
template<typename T >
wpi::maskTrailingZeros (unsigned N)
 Create a bitmask with the N right-most bits set to 0, and all other bits set to 1. More...
 
template<typename T >
wpi::maskLeadingZeros (unsigned N)
 Create a bitmask with the N left-most bits set to 0, and all other bits set to 1. More...
 
template<typename T >
wpi::reverseBits (T Val)
 Reverse the bits in Val. More...
 
constexpr uint32_t wpi::Hi_32 (uint64_t Value)
 Return the high 32 bits of a 64 bit value. More...
 
constexpr uint32_t wpi::Lo_32 (uint64_t Value)
 Return the low 32 bits of a 64 bit value. More...
 
constexpr uint64_t wpi::Make_64 (uint32_t High, uint32_t Low)
 Make a 64-bit integer from a high / low pair of 32-bit integers. More...
 
template<unsigned N>
constexpr bool wpi::isInt (int64_t x)
 Checks if an integer fits into the given bit width. More...
 
template<unsigned N, unsigned S>
constexpr bool wpi::isShiftedInt (int64_t x)
 Checks if a signed integer is an N bit number shifted left by S. More...
 
template<unsigned N>
constexpr bool wpi::isUInt (uint64_t x)
 Checks if an unsigned integer fits into the given bit width. More...
 
template<unsigned N, unsigned S>
constexpr bool wpi::isShiftedUInt (uint64_t x)
 Checks if a unsigned integer is an N bit number shifted left by S. More...
 
uint64_t wpi::maxUIntN (uint64_t N)
 Gets the maximum value for a N-bit unsigned integer. More...
 
int64_t wpi::minIntN (int64_t N)
 Gets the minimum value for a N-bit signed integer. More...
 
int64_t wpi::maxIntN (int64_t N)
 Gets the maximum value for a N-bit signed integer. More...
 
bool wpi::isUIntN (unsigned N, uint64_t x)
 Checks if an unsigned integer fits into the given (dynamic) bit width. More...
 
bool wpi::isIntN (unsigned N, int64_t x)
 Checks if an signed integer fits into the given (dynamic) bit width. More...
 
constexpr bool wpi::isMask_32 (uint32_t Value)
 Return true if the argument is a non-empty sequence of ones starting at the least significant bit with the remainder zero (32 bit version). More...
 
constexpr bool wpi::isMask_64 (uint64_t Value)
 Return true if the argument is a non-empty sequence of ones starting at the least significant bit with the remainder zero (64 bit version). More...
 
constexpr bool wpi::isShiftedMask_32 (uint32_t Value)
 Return true if the argument contains a non-empty sequence of ones with the remainder zero (32 bit version.) Ex. More...
 
constexpr bool wpi::isShiftedMask_64 (uint64_t Value)
 Return true if the argument contains a non-empty sequence of ones with the remainder zero (64 bit version.) More...
 
constexpr bool wpi::isPowerOf2_32 (uint32_t Value)
 Return true if the argument is a power of two > 0. More...
 
constexpr bool wpi::isPowerOf2_64 (uint64_t Value)
 Return true if the argument is a power of two > 0 (64 bit edition.) More...
 
bool wpi::isShiftedMask_32 (uint32_t Value, unsigned &MaskIdx, unsigned &MaskLen)
 Return true if the argument contains a non-empty sequence of ones with the remainder zero (32 bit version.) Ex. More...
 
bool wpi::isShiftedMask_64 (uint64_t Value, unsigned &MaskIdx, unsigned &MaskLen)
 Return true if the argument contains a non-empty sequence of ones with the remainder zero (64 bit version.) If true, MaskIdx will specify the index of the lowest set bit and MaskLen is updated to specify the length of the mask, else neither are updated. More...
 
template<size_t kValue>
constexpr size_t wpi::CTLog2 ()
 Compile time Log2. More...
 
template<>
constexpr size_t wpi::CTLog2< 1 > ()
 
unsigned wpi::Log2_32 (uint32_t Value)
 Return the floor log base 2 of the specified value, -1 if the value is zero. More...
 
unsigned wpi::Log2_64 (uint64_t Value)
 Return the floor log base 2 of the specified value, -1 if the value is zero. More...
 
unsigned wpi::Log2_32_Ceil (uint32_t Value)
 Return the ceil log base 2 of the specified value, 32 if the value is zero. More...
 
unsigned wpi::Log2_64_Ceil (uint64_t Value)
 Return the ceil log base 2 of the specified value, 64 if the value is zero. More...
 
constexpr uint64_t wpi::MinAlign (uint64_t A, uint64_t B)
 A and B are either alignments or offsets. More...
 
constexpr uint64_t wpi::NextPowerOf2 (uint64_t A)
 Returns the next power of two (in 64-bits) that is strictly greater than A. More...
 
uint64_t wpi::PowerOf2Ceil (uint64_t A)
 Returns the power of two which is greater than or equal to the given value. More...
 
uint64_t wpi::alignTo (uint64_t Value, uint64_t Align)
 Returns the next integer (mod 2**64) that is greater than or equal to Value and is a multiple of Align. More...
 
uint64_t wpi::alignToPowerOf2 (uint64_t Value, uint64_t Align)
 
uint64_t wpi::alignTo (uint64_t Value, uint64_t Align, uint64_t Skew)
 If non-zero Skew is specified, the return value will be a minimal integer that is greater than or equal to Size and equal to A * N + Skew for some integer N. More...
 
template<uint64_t Align>
constexpr uint64_t wpi::alignTo (uint64_t Value)
 Returns the next integer (mod 2**64) that is greater than or equal to Value and is a multiple of Align. More...
 
uint64_t wpi::divideCeil (uint64_t Numerator, uint64_t Denominator)
 Returns the integer ceil(Numerator / Denominator). More...
 
uint64_t wpi::divideNearest (uint64_t Numerator, uint64_t Denominator)
 Returns the integer nearest(Numerator / Denominator). More...
 
uint64_t wpi::alignDown (uint64_t Value, uint64_t Align, uint64_t Skew=0)
 Returns the largest uint64_t less than or equal to Value and is Skew mod Align. More...
 
template<unsigned B>
constexpr int32_t wpi::SignExtend32 (uint32_t X)
 Sign-extend the number in the bottom B bits of X to a 32-bit integer. More...
 
int32_t wpi::SignExtend32 (uint32_t X, unsigned B)
 Sign-extend the number in the bottom B bits of X to a 32-bit integer. More...
 
template<unsigned B>
constexpr int64_t wpi::SignExtend64 (uint64_t x)
 Sign-extend the number in the bottom B bits of X to a 64-bit integer. More...
 
int64_t wpi::SignExtend64 (uint64_t X, unsigned B)
 Sign-extend the number in the bottom B bits of X to a 64-bit integer. More...
 
template<typename T >
std::enable_if_t< std::is_unsigned_v< T >, T > wpi::AbsoluteDifference (T X, T Y)
 Subtract two unsigned integers, X and Y, of type T and return the absolute value of the result. More...
 
template<typename T >
std::enable_if_t< std::is_unsigned_v< T >, T > wpi::SaturatingAdd (T X, T Y, bool *ResultOverflowed=nullptr)
 Add two unsigned integers, X and Y, of type T. More...
 
template<class T , class... Ts>
std::enable_if_t< std::is_unsigned_v< T >, T > wpi::SaturatingAdd (T X, T Y, T Z, Ts... Args)
 Add multiple unsigned integers of type T. More...
 
template<typename T >
std::enable_if_t< std::is_unsigned_v< T >, T > wpi::SaturatingMultiply (T X, T Y, bool *ResultOverflowed=nullptr)
 Multiply two unsigned integers, X and Y, of type T. More...
 
template<typename T >
std::enable_if_t< std::is_unsigned_v< T >, T > wpi::SaturatingMultiplyAdd (T X, T Y, T A, bool *ResultOverflowed=nullptr)
 Multiply two unsigned integers, X and Y, and add the unsigned integer, A to the product. More...
 
template<typename T >
std::enable_if_t< std::is_signed_v< T >, T > wpi::AddOverflow (T X, T Y, T &Result)
 Add two signed integers, computing the two's complement truncated result, returning true if overflow occurred. More...
 
template<typename T >
std::enable_if_t< std::is_signed_v< T >, T > wpi::SubOverflow (T X, T Y, T &Result)
 Subtract two signed integers, computing the two's complement truncated result, returning true if an overflow ocurred. More...
 
template<typename T >
std::enable_if_t< std::is_signed_v< T >, T > wpi::MulOverflow (T X, T Y, T &Result)
 Multiply two signed integers, computing the two's complement truncated result, returning true if an overflow ocurred. More...
 
template<typename T >
constexpr int wpi::sgn (T val)
 
template<typename T >
constexpr T wpi::Lerp (const T &startValue, const T &endValue, double t)
 Linearly interpolates between two values. More...
 

Macro Definition Documentation

◆ R2

#define R2 (   n)    n, n + 2 * 64, n + 1 * 64, n + 3 * 64

◆ R4

#define R4 (   n)    R2(n), R2(n + 2 * 16), R2(n + 1 * 16), R2(n + 3 * 16)

◆ R6

#define R6 (   n)    R4(n), R4(n + 2 * 4), R4(n + 1 * 4), R4(n + 3 * 4)