openMSX
Functions
openmsx::Math Namespace Reference

Functions

unsigned powerOfTwo (unsigned a)
 Returns the smallest number that is both >=a and a power of two. More...
 
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...
 
short clipIntToShort (int x)
 Clip x to range [-32768,32767]. More...
 
byte clipIntToByte (int x)
 Clip x to range [0,255]. More...
 
template<int LO, int HI>
int clip (float r, float factor)
 Clips r * factor to the range [LO,HI]. 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...
 
byte reverseByte (byte 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...
 

Function Documentation

template<int LO, int HI>
int openmsx::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 29 of file Math.hh.

template<int LO, int HI>
int openmsx::Math::clip ( float  r,
float  factor 
)
inline

Clips r * factor to the range [LO,HI].

Definition at line 55 of file Math.hh.

byte openmsx::Math::clipIntToByte ( int  x)
inline

Clip x to range [0,255].

Optimized for the case when no clipping is needed.

Definition at line 46 of file Math.hh.

References likely.

short openmsx::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 37 of file Math.hh.

References likely.

Referenced by openmsx::WavImage::getSampleAt(), and openmsx::Y8950::Impl::writeReg().

unsigned openmsx::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 188 of file Math.hh.

template<typename T >
T openmsx::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 174 of file Math.hh.

Referenced by openmsx::VRAMWindow::getReadArea(), openmsx::VRAMWindow::getReadAreaPlanar(), and powerOfTwo().

unsigned openmsx::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 70 of file Math.hh.

Referenced by openmsx::FBPostProcessor< Pixel >::paint().

bool openmsx::Math::isPowerOfTwo ( unsigned  a)
inline

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

Referenced by openmsx::MemoryOps::mallocAligned().

unsigned openmsx::Math::powerOfTwo ( unsigned  a)

Returns the smallest number that is both >=a and a power of two.

Definition at line 8 of file Math.cc.

References floodRight().

Referenced by openmsx::SDLGLOutputSurface::init(), and openmsx::SDLSoundDriver::SDLSoundDriver().

byte openmsx::Math::reverseByte ( byte  a)
inline

Reverse the bits in a byte.

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

Definition at line 148 of file Math.hh.

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

unsigned openmsx::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 98 of file Math.hh.

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