openMSX

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 1bit in binary representation. More...  
template<typename T >  
constexpr T  floodRight (T x) noexcept 
Returns the smallest number of the form 2^n1 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...  
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...  
unsigned  countLeadingZeros (unsigned x) 
Count the number of leading zerobits in the given word. More...  
unsigned  findFirstSet (unsigned x) 
Find the least significant bit that is set. More...  

noexcept 
Returns the smallest number that is both >=a and a power of two.
This will be part of c++20: https://en.cppreference.com/w/cpp/numeric/ceil2
Definition at line 84 of file Math.hh.
References floodRight(), and openmsx::x.
Referenced by openmsx::MSXMemoryMapper::peekIO(), openmsx::SDLSoundDriver::SDLSoundDriver(), TEST_CASE(), openmsx::VDPVRAM::updateSpritesEnabled(), and openmsx::MSXMemoryMapper::writeIO().

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 101 of file Math.hh.
References openmsx::x.
Referenced by openmsx::GLPostProcessor::update(), openmsx::Mouse::~Mouse(), and openmsx::Trackball::~Trackball().

inline 
Clip x to range [0,255].
Optimized for the case when no clipping is needed.
Definition at line 119 of file Math.hh.
References likely, and openmsx::x.
Referenced by openmsx::VDP::getMSX1Palette(), and TEST_CASE().

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 110 of file Math.hh.
References likely, and openmsx::x.
Referenced by openmsx::Y8950Adpcm::calcSample(), openmsx::WavWriter::flush(), openmsx::DCFilter::operator()(), openmsx::AviRecorder::stop(), TEST_CASE(), and openmsx::Y8950::writeReg().

inline 
Count the number of leading zerobits in the given word.
The result is undefined when the input is zero (all bits are zero).
Definition at line 235 of file Math.hh.
Referenced by openmsx::SpriteChecker::reset(), and TEST_CASE().

inline 

noexcept 
Returns the smallest number of the form 2^n1 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 1bit in the input all bits more to the right are also 1.
Definition at line 68 of file Math.hh.
References openmsx::x.
Referenced by ceil2(), openmsx::VRAMWindow::getReadAreaPlanar(), openmsx::VRAMWindow::isContinuous(), openmsx::TigerTree::notifyChange(), and TEST_CASE().

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 134 of file Math.hh.
Referenced by openmsx::FBPostProcessor< Pixel >::paint(), and openmsx::GLPostProcessor::~GLPostProcessor().

noexcept 
Is the given number an integral power of two? That is, does it have exactly one 1bit 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 56 of file Math.hh.
References openmsx::x.
Referenced by openmsx::AmdFlash::AmdFlash(), openmsx::PixelOperations< uint16_t >::blend(), openmsx::MemoryOps::mallocAligned(), openmsx::MSXMusicBase::MSXMusicBase(), openmsx::RomBlocks< BANK_SIZE_ >::RomBlocks(), and TEST_CASE().

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.
Definition at line 38 of file Math.hh.
References openmsx::x.
Referenced by openmsx::PixelOperations< uint16_t >::blend(), DivModByConstPrivate::getAlgorithm(), openmsx::DivModBySame::setDivisor(), and TEST_CASE().

inline 
Reverse the bits in a byte.
This is equivalent to (but faster than) reverseNBits(x, 8);
Definition at line 212 of file Math.hh.
Referenced by openmsx::ImagePrinter::printGraphicByte(), openmsx::LaserdiscPlayer::serialize(), and openmsx::SunriseIDE::writeMem().

inline 
Reverse the lower N bits of a given value.
The upper 32N bits from the input are ignored and will be returned as 0. For example reverseNBits('xxxabcde', 5) returns '000edcba' (binary notation).
Definition at line 162 of file Math.hh.
Referenced by openmsx::LaserdiscPlayer::serialize().