openMSX
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...

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...

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...

## ◆ ceil2()

template<typename T >
 constexpr T Math::ceil2 ( T x )
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.

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

References openmsx::x.

## ◆ 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 119 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 110 of file Math.hh.

References likely, and openmsx::x.

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

## ◆ floodRight()

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

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

References openmsx::x.

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

## ◆ ispow2()

template<typename T >
 constexpr bool Math::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.

(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.

## ◆ log2p1()

template<typename T >
 constexpr T Math::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.

Definition at line 38 of file Math.hh.

References openmsx::x.

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

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

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