openMSX
Classes | Public Member Functions | Static Public Member Functions | Static Public Attributes | Friends | List of all members
openmsx::FixedPoint< FRACTION_BITS_ > Class Template Reference

A fixed point number, implemented by a 32-bit signed integer. More...

#include <FixedPoint.hh>

Inheritance diagram for openmsx::FixedPoint< FRACTION_BITS_ >:
Inheritance graph
[legend]

Public Member Functions

constexpr FixedPoint ()=default
 Creates a zero-initialized fixed point object.
 
constexpr FixedPoint (int i)
 
constexpr FixedPoint (unsigned i)
 
 FixedPoint (float f)
 
 FixedPoint (double d)
 
template<unsigned BITS2>
constexpr FixedPoint (FixedPoint< BITS2 > other)
 
constexpr int toInt () const
 Returns the integer part (rounded down) of this fixed point number.
 
constexpr float toFloat () const
 Returns the float value that corresponds to this fixed point number.
 
constexpr double toDouble () const
 Returns the double value that corresponds to this fixed point number.
 
constexpr float fractionAsFloat () const
 Returns the fractional part of this fixed point number as a float.
 
constexpr double fractionAsDouble () const
 Returns the fractional part of this fixed point number as a double.
 
constexpr int divAsInt (FixedPoint other) const
 Returns the result of a division between this fixed point number and another, rounded towards zero.
 
constexpr FixedPoint floor () const
 Returns this value rounded down.
 
constexpr FixedPoint fract () const
 Returns the fractional part of this value.
 
constexpr unsigned fractAsInt () const
 Returns the fractional part of this value as an integer.
 
constexpr auto operator<=> (const FixedPoint &) const =default
 
constexpr void operator+= (FixedPoint other)
 
constexpr void operator-= (FixedPoint other)
 
constexpr void addQuantum ()
 Increase this value with the smallest possible amount.
 
constexpr int getRawValue () const
 
template<typename Archive >
void serialize (Archive &ar, unsigned)
 

Static Public Member Functions

static constexpr FixedPoint create (int value)
 Create new fixed point object from given representation.
 
static constexpr FixedPoint roundRatioDown (unsigned n, unsigned d)
 
static constexpr int shiftHelper (int x, int s)
 

Static Public Attributes

static constexpr unsigned FRACTION_BITS = FRACTION_BITS_
 Number of fractional bits (export template parameter as a constant so that external code can use it more easily).
 

Friends

constexpr friend FixedPoint operator+ (FixedPoint x, FixedPoint y)
 
constexpr friend FixedPoint operator- (FixedPoint x, FixedPoint y)
 
constexpr friend FixedPoint operator* (FixedPoint x, FixedPoint y)
 
constexpr friend FixedPoint operator* (FixedPoint x, int y)
 
constexpr friend FixedPoint operator* (int x, FixedPoint y)
 
constexpr friend FixedPoint operator/ (FixedPoint x, FixedPoint y)
 Divides two fixed point numbers.
 
constexpr friend FixedPoint operator/ (FixedPoint x, int y)
 
constexpr friend FixedPoint operator<< (FixedPoint x, int y)
 
constexpr friend FixedPoint operator>> (FixedPoint x, int y)
 

Detailed Description

template<unsigned FRACTION_BITS_>
class openmsx::FixedPoint< FRACTION_BITS_ >

A fixed point number, implemented by a 32-bit signed integer.

The FRACTION_BITS template argument selects the position of the "binary point" (base 2 equivalent to decimal point).

Definition at line 16 of file FixedPoint.hh.

Constructor & Destructor Documentation

◆ FixedPoint() [1/6]

template<unsigned FRACTION_BITS_>
constexpr openmsx::FixedPoint< FRACTION_BITS_ >::FixedPoint ( )
constexprdefault

Creates a zero-initialized fixed point object.

Referenced by openmsx::FixedPoint< FRACTION_BITS_ >::create().

◆ FixedPoint() [2/6]

template<unsigned FRACTION_BITS_>
constexpr openmsx::FixedPoint< FRACTION_BITS_ >::FixedPoint ( int  i)
inlineexplicitconstexpr

Definition at line 55 of file FixedPoint.hh.

◆ FixedPoint() [3/6]

template<unsigned FRACTION_BITS_>
constexpr openmsx::FixedPoint< FRACTION_BITS_ >::FixedPoint ( unsigned  i)
inlineexplicitconstexpr

Definition at line 56 of file FixedPoint.hh.

◆ FixedPoint() [4/6]

template<unsigned FRACTION_BITS_>
openmsx::FixedPoint< FRACTION_BITS_ >::FixedPoint ( float  f)
inlineexplicit

Definition at line 57 of file FixedPoint.hh.

◆ FixedPoint() [5/6]

template<unsigned FRACTION_BITS_>
openmsx::FixedPoint< FRACTION_BITS_ >::FixedPoint ( double  d)
inlineexplicit

Definition at line 58 of file FixedPoint.hh.

◆ FixedPoint() [6/6]

template<unsigned FRACTION_BITS_>
template<unsigned BITS2>
constexpr openmsx::FixedPoint< FRACTION_BITS_ >::FixedPoint ( FixedPoint< BITS2 >  other)
inlineexplicitconstexpr

Definition at line 68 of file FixedPoint.hh.

Member Function Documentation

◆ addQuantum()

template<unsigned FRACTION_BITS_>
constexpr void openmsx::FixedPoint< FRACTION_BITS_ >::addQuantum ( )
inlineconstexpr

Increase this value with the smallest possible amount.

Typically used to implement counters at the resolution of this datatype.

Definition at line 200 of file FixedPoint.hh.

Referenced by openmsx::YM2413Burczynski::YM2413::generateChannels(), and TEST_CASE().

◆ create()

template<unsigned FRACTION_BITS_>
static constexpr FixedPoint openmsx::FixedPoint< FRACTION_BITS_ >::create ( int  value)
inlinestaticconstexpr

Create new fixed point object from given representation.

Used by the overloaded operators.

Parameters
valuethe internal representation.

Definition at line 45 of file FixedPoint.hh.

References openmsx::FixedPoint< FRACTION_BITS_ >::FixedPoint().

Referenced by openmsx::FixedPoint< FRACTION_BITS_ >::floor(), openmsx::FixedPoint< FRACTION_BITS_ >::fract(), and openmsx::FixedPoint< FRACTION_BITS_ >::roundRatioDown().

◆ divAsInt()

template<unsigned FRACTION_BITS_>
constexpr int openmsx::FixedPoint< FRACTION_BITS_ >::divAsInt ( FixedPoint< FRACTION_BITS_ >  other) const
inlineconstexpr

Returns the result of a division between this fixed point number and another, rounded towards zero.

Definition at line 121 of file FixedPoint.hh.

Referenced by TEST_CASE().

◆ floor()

template<unsigned FRACTION_BITS_>
constexpr FixedPoint openmsx::FixedPoint< FRACTION_BITS_ >::floor ( ) const
inlineconstexpr

Returns this value rounded down.

The result is equal to FixedPoint(fp.toInt()).

Definition at line 129 of file FixedPoint.hh.

References openmsx::FixedPoint< FRACTION_BITS_ >::create().

◆ fract()

template<unsigned FRACTION_BITS_>
constexpr FixedPoint openmsx::FixedPoint< FRACTION_BITS_ >::fract ( ) const
inlineconstexpr

Returns the fractional part of this value.

The result is equal to fp - floor(fp).

Definition at line 137 of file FixedPoint.hh.

References openmsx::FixedPoint< FRACTION_BITS_ >::create().

◆ fractAsInt()

template<unsigned FRACTION_BITS_>
constexpr unsigned openmsx::FixedPoint< FRACTION_BITS_ >::fractAsInt ( ) const
inlineconstexpr

Returns the fractional part of this value as an integer.

The result is equal to (fract() * (1 << FRACTION_BITS)).toInt()

Definition at line 145 of file FixedPoint.hh.

Referenced by openmsx::BlipBuffer::addDelta().

◆ fractionAsDouble()

template<unsigned FRACTION_BITS_>
constexpr double openmsx::FixedPoint< FRACTION_BITS_ >::fractionAsDouble ( ) const
inlineconstexpr

Returns the fractional part of this fixed point number as a double.

The fractional part is never negative, even for negative fixed point numbers. x.toInt() + x.fractionAsDouble() is approximately equal to x.toDouble()

Definition at line 111 of file FixedPoint.hh.

◆ fractionAsFloat()

template<unsigned FRACTION_BITS_>
constexpr float openmsx::FixedPoint< FRACTION_BITS_ >::fractionAsFloat ( ) const
inlineconstexpr

Returns the fractional part of this fixed point number as a float.

The fractional part is never negative, even for negative fixed point numbers. x.toInt() + x.fractionAsFloat() is approximately equal to x.toFloat()

Definition at line 101 of file FixedPoint.hh.

◆ getRawValue()

template<unsigned FRACTION_BITS_>
constexpr int openmsx::FixedPoint< FRACTION_BITS_ >::getRawValue ( ) const
inlineconstexpr

Definition at line 206 of file FixedPoint.hh.

◆ operator+=()

template<unsigned FRACTION_BITS_>
constexpr void openmsx::FixedPoint< FRACTION_BITS_ >::operator+= ( FixedPoint< FRACTION_BITS_ >  other)
inlineconstexpr

Definition at line 190 of file FixedPoint.hh.

◆ operator-=()

template<unsigned FRACTION_BITS_>
constexpr void openmsx::FixedPoint< FRACTION_BITS_ >::operator-= ( FixedPoint< FRACTION_BITS_ >  other)
inlineconstexpr

Definition at line 193 of file FixedPoint.hh.

◆ operator<=>()

template<unsigned FRACTION_BITS_>
constexpr auto openmsx::FixedPoint< FRACTION_BITS_ >::operator<=> ( const FixedPoint< FRACTION_BITS_ > &  ) const
constexprdefault

◆ roundRatioDown()

template<unsigned FRACTION_BITS_>
static constexpr FixedPoint openmsx::FixedPoint< FRACTION_BITS_ >::roundRatioDown ( unsigned  n,
unsigned  d 
)
inlinestaticconstexpr

◆ serialize()

template<unsigned FRACTION_BITS_>
template<typename Archive >
void openmsx::FixedPoint< FRACTION_BITS_ >::serialize ( Archive &  ar,
unsigned   
)
inline

Definition at line 211 of file FixedPoint.hh.

◆ shiftHelper()

template<unsigned FRACTION_BITS_>
static constexpr int openmsx::FixedPoint< FRACTION_BITS_ >::shiftHelper ( int  x,
int  s 
)
inlinestaticconstexpr

Definition at line 64 of file FixedPoint.hh.

◆ toDouble()

template<unsigned FRACTION_BITS_>
constexpr double openmsx::FixedPoint< FRACTION_BITS_ >::toDouble ( ) const
inlineconstexpr

Returns the double value that corresponds to this fixed point number.

Definition at line 91 of file FixedPoint.hh.

◆ toFloat()

template<unsigned FRACTION_BITS_>
constexpr float openmsx::FixedPoint< FRACTION_BITS_ >::toFloat ( ) const
inlineconstexpr

Returns the float value that corresponds to this fixed point number.

Definition at line 84 of file FixedPoint.hh.

◆ toInt()

template<unsigned FRACTION_BITS_>
constexpr int openmsx::FixedPoint< FRACTION_BITS_ >::toInt ( ) const
inlineconstexpr

Friends And Related Symbol Documentation

◆ operator* [1/3]

template<unsigned FRACTION_BITS_>
constexpr friend FixedPoint operator* ( FixedPoint< FRACTION_BITS_ >  x,
FixedPoint< FRACTION_BITS_ >  y 
)
friend

Definition at line 157 of file FixedPoint.hh.

◆ operator* [2/3]

template<unsigned FRACTION_BITS_>
constexpr friend FixedPoint operator* ( FixedPoint< FRACTION_BITS_ >  x,
int  y 
)
friend

Definition at line 161 of file FixedPoint.hh.

◆ operator* [3/3]

template<unsigned FRACTION_BITS_>
constexpr friend FixedPoint operator* ( int  x,
FixedPoint< FRACTION_BITS_ >  y 
)
friend

Definition at line 164 of file FixedPoint.hh.

◆ operator+

template<unsigned FRACTION_BITS_>
constexpr friend FixedPoint operator+ ( FixedPoint< FRACTION_BITS_ >  x,
FixedPoint< FRACTION_BITS_ >  y 
)
friend

Definition at line 151 of file FixedPoint.hh.

◆ operator-

template<unsigned FRACTION_BITS_>
constexpr friend FixedPoint operator- ( FixedPoint< FRACTION_BITS_ >  x,
FixedPoint< FRACTION_BITS_ >  y 
)
friend

Definition at line 154 of file FixedPoint.hh.

◆ operator/ [1/2]

template<unsigned FRACTION_BITS_>
constexpr friend FixedPoint operator/ ( FixedPoint< FRACTION_BITS_ >  x,
FixedPoint< FRACTION_BITS_ >  y 
)
friend

Divides two fixed point numbers.

The fractional part is rounded down.

Definition at line 171 of file FixedPoint.hh.

◆ operator/ [2/2]

template<unsigned FRACTION_BITS_>
constexpr friend FixedPoint operator/ ( FixedPoint< FRACTION_BITS_ >  x,
int  y 
)
friend

Definition at line 175 of file FixedPoint.hh.

◆ operator<<

template<unsigned FRACTION_BITS_>
constexpr friend FixedPoint operator<< ( FixedPoint< FRACTION_BITS_ >  x,
int  y 
)
friend

Definition at line 178 of file FixedPoint.hh.

◆ operator>>

template<unsigned FRACTION_BITS_>
constexpr friend FixedPoint operator>> ( FixedPoint< FRACTION_BITS_ >  x,
int  y 
)
friend

Definition at line 181 of file FixedPoint.hh.

Member Data Documentation

◆ FRACTION_BITS

template<unsigned FRACTION_BITS_>
constexpr unsigned openmsx::FixedPoint< FRACTION_BITS_ >::FRACTION_BITS = FRACTION_BITS_
staticconstexpr

Number of fractional bits (export template parameter as a constant so that external code can use it more easily).

Definition at line 20 of file FixedPoint.hh.

Referenced by openmsx::FixedPoint< FRACTION_BITS_ >::roundRatioDown(), and openmsx::FixedPoint< FRACTION_BITS_ >::toInt().


The documentation for this class was generated from the following file: