openMSX
Functions
Math Namespace Reference

Functions

unsigned powerOfTwo (unsigned a)
 Returns the smallest number that is both >=a and a power of two. More...
 
constexpr bool isPowerOfTwo (unsigned a)
 Is the given number an integer power of 2? Not correct for zero (according to this test 0 is a power of 2). More...
 
template<int LO, int HI>
int clip (int x)
 Clips x to the range [LO,HI]. 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 gcd (unsigned a, unsigned b)
 Calculate greatest common divider of two strictly positive integers. 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...
 
template<typename T >
floodRight (T x)
 Returns the smallest number of the form 2^n-1 that is greater or equal to the given number. 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...
 

Function Documentation

◆ clip()

template<int LO, int HI>
int Math::clip ( int  x)
inline

Clips x to the range [LO,HI].

Slightly faster than std::min(HI, std::max(LO, x)) especially when no clipping is required.

Definition at line 48 of file Math.hh.

Referenced by openmsx::GLPostProcessor::update(), openmsx::Mouse::~Mouse(), and openmsx::Trackball::~Trackball().

◆ 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 66 of file Math.hh.

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 57 of file Math.hh.

Referenced by openmsx::Y8950Adpcm::calcSample(), openmsx::WavWriter::flush(), openmsx::DCFilter::operator()(), openmsx::AviRecorder::stop(), 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 199 of file Math.hh.

Referenced by openmsx::SpriteChecker::reset(), and TEST_CASE().

◆ 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 219 of file Math.hh.

◆ floodRight()

template<typename T >
T Math::floodRight ( x)
inline

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 185 of file Math.hh.

Referenced by openmsx::VRAMWindow::getReadAreaPlanar(), openmsx::VRAMWindow::isContinuous(), openmsx::TigerTree::notifyChange(), powerOfTwo(), and TEST_CASE().

◆ gcd()

unsigned Math::gcd ( unsigned  a,
unsigned  b 
)
inline

Calculate greatest common divider of two strictly positive integers.

Classical implementation is like this: while (unsigned t = b % a) { b = a; a = t; } return a; The following implementation avoids the costly modulo operation. It is about 40% faster on my machine.

require: a != 0 && b != 0

Definition at line 81 of file Math.hh.

Referenced by openmsx::FBPostProcessor< Pixel >::paint(), and openmsx::GLPostProcessor::~GLPostProcessor().

◆ isPowerOfTwo()

constexpr bool Math::isPowerOfTwo ( unsigned  a)

Is the given number an integer power of 2? Not correct for zero (according to this test 0 is a power of 2).

Definition at line 34 of file Math.hh.

References powerOfTwo().

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

◆ powerOfTwo()

unsigned Math::powerOfTwo ( unsigned  a)

◆ 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 159 of file Math.hh.

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

◆ 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 109 of file Math.hh.

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