openMSX
Functions
Math Namespace Reference

Functions

template<typename T >
constexpr T log2p1 (T x) noexcept
 Returns the number of bits needed to store the value 'x', that is: for x==0 : 0 for x!=0 : 1 + floor(log2(x)) This will be part of c++20: https://en.cppreference.com/w/cpp/numeric/log2p1. More...
 
template<typename T >
constexpr bool ispow2 (T x) noexcept
 Is the given number an integral power of two? That is, does it have exactly one 1-bit in binary representation. More...
 
template<typename T >
constexpr T floodRight (T x) noexcept
 Returns the smallest number of the form 2^n-1 that is greater or equal to the given number. More...
 
template<typename T >
constexpr T ceil2 (T x) noexcept
 Returns the smallest number that is both >=a and a power of two. More...
 
int16_t clipIntToShort (int x)
 Clip x to range [-32768,32767]. More...
 
uint8_t clipIntToByte (int x)
 Clip x to range [0,255]. More...
 
unsigned reverseNBits (unsigned x, unsigned bits)
 Reverse the lower N bits of a given value. More...
 
uint8_t reverseByte (uint8_t a)
 Reverse the bits in a byte. More...
 
unsigned countLeadingZeros (unsigned x)
 Count the number of leading zero-bits in the given word. More...
 
unsigned findFirstSet (unsigned x)
 Find the least significant bit that is set. More...
 
float cubicHermite (const float *y, float x)
 

Function Documentation

◆ ceil2()

template<typename T >
constexpr T Math::ceil2 ( x)
constexprnoexcept

◆ clipIntToByte()

uint8_t Math::clipIntToByte ( int  x)
inline

Clip x to range [0,255].

Optimized for the case when no clipping is needed.

Definition at line 109 of file Math.hh.

References likely, and openmsx::x.

Referenced by openmsx::VDP::getMSX1Palette(), and TEST_CASE().

◆ clipIntToShort()

int16_t Math::clipIntToShort ( int  x)
inline

Clip x to range [-32768,32767].

Special case of the version above. Optimized for the case when no clipping is needed.

Definition at line 100 of file Math.hh.

References likely, and openmsx::x.

Referenced by openmsx::WavImage::getSampleAt(), openmsx::DCFilter::operator()(), TEST_CASE(), and openmsx::Y8950::writeReg().

◆ countLeadingZeros()

unsigned Math::countLeadingZeros ( unsigned  x)
inline

Count the number of leading zero-bits in the given word.

The result is undefined when the input is zero (all bits are zero).

Definition at line 192 of file Math.hh.

References openmsx::x.

Referenced by TEST_CASE().

◆ cubicHermite()

float Math::cubicHermite ( const float *  y,
float  x 
)
inline

Definition at line 244 of file Math.hh.

References openmsx::x.

Referenced by openmsx::WavImage::getSampleAt().

◆ findFirstSet()

unsigned Math::findFirstSet ( unsigned  x)
inline

Find the least significant bit that is set.

Returns
0 if the input is zero (no bits are set), otherwise the index of the first set bit + 1.

Definition at line 212 of file Math.hh.

References openmsx::x.

◆ floodRight()

template<typename T >
constexpr T Math::floodRight ( x)
constexprnoexcept

Returns the smallest number of the form 2^n-1 that is greater or equal to the given number.

The resulting number has the same number of leading zeros as the input, but starting from the first 1-bit in the input all bits more to the right are also 1.

Definition at line 69 of file Math.hh.

References openmsx::x.

Referenced by ceil2(), openmsx::VRAMWindow::getReadAreaPlanar(), openmsx::VRAMWindow::isContinuous(), and TEST_CASE().

◆ ispow2()

template<typename T >
constexpr bool Math::ispow2 ( x)
constexprnoexcept

Is the given number an integral power of two? That is, does it have exactly one 1-bit in binary representation.

(So zero is not a power of two).

This will be part of c++20: https://en.cppreference.com/w/cpp/numeric/ispow2

Definition at line 57 of file Math.hh.

References openmsx::x.

Referenced by openmsx::PixelOperations< Pixel >::blend(), openmsx::MemoryOps::mallocAligned(), openmsx::MSXMusicBase::MSXMusicBase(), openmsx::RomBlocks< BANK_SIZE_ >::RomBlocks(), and TEST_CASE().

◆ log2p1()

template<typename T >
constexpr T Math::log2p1 ( x)
constexprnoexcept

Returns the number of bits needed to store the value 'x', that is: for x==0 : 0 for x!=0 : 1 + floor(log2(x)) This will be part of c++20: https://en.cppreference.com/w/cpp/numeric/log2p1.

Definition at line 39 of file Math.hh.

References openmsx::x.

Referenced by openmsx::PixelOperations< Pixel >::blend(), DivModByConstPrivate::getAlgorithm(), openmsx::DivModBySame::setDivisor(), and TEST_CASE().

◆ reverseByte()

uint8_t Math::reverseByte ( uint8_t  a)
inline

Reverse the bits in a byte.

This is equivalent to (but faster than) reverseNBits(x, 8);

Definition at line 169 of file Math.hh.

Referenced by openmsx::ImagePrinter::printGraphicByte(), and openmsx::LaserdiscPlayer::serialize().

◆ reverseNBits()

unsigned Math::reverseNBits ( unsigned  x,
unsigned  bits 
)
inline

Reverse the lower N bits of a given value.

The upper 32-N bits from the input are ignored and will be returned as 0. For example reverseNBits('xxxabcde', 5) returns '000edcba' (binary notation).

Definition at line 119 of file Math.hh.

References openmsx::x.

Referenced by openmsx::LaserdiscPlayer::serialize().