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 ()
 Creates a zero-initialized fixed point object. More...
 
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. More...
 
constexpr float toFloat () const
 Returns the float value that corresponds to this fixed point number. More...
 
constexpr double toDouble () const
 Returns the double value that corresponds to this fixed point number. More...
 
constexpr float fractionAsFloat () const
 Returns the fractional part of this fixed point number as a float. More...
 
constexpr double fractionAsDouble () const
 Returns the fractional part of this fixed point number as a double. More...
 
constexpr int divAsInt (FixedPoint other) const
 Returns the result of a division between this fixed point number and another, rounded towards zero. More...
 
constexpr FixedPoint floor () const
 Returns this value rounded down. More...
 
constexpr FixedPoint fract () const
 Returns the fractional part of this value. More...
 
constexpr unsigned fractAsInt () const
 Returns the fractional part of this value as an integer. More...
 
void operator+= (FixedPoint other)
 
void operator-= (FixedPoint other)
 
void addQuantum ()
 Increase this value with the smallest possible amount. More...
 
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. More...
 
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). More...
 

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. More...
 
constexpr friend FixedPoint operator/ (FixedPoint x, int y)
 
constexpr friend FixedPoint operator<< (FixedPoint x, int y)
 
constexpr friend FixedPoint operator>> (FixedPoint x, int y)
 
constexpr friend bool operator== (FixedPoint x, FixedPoint y)
 
constexpr friend bool operator!= (FixedPoint x, FixedPoint y)
 
constexpr friend bool operator< (FixedPoint x, FixedPoint y)
 
constexpr friend bool operator<= (FixedPoint x, FixedPoint y)
 
constexpr friend bool operator> (FixedPoint x, FixedPoint y)
 
constexpr friend bool operator>= (FixedPoint x, FixedPoint 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 14 of file FixedPoint.hh.

Constructor & Destructor Documentation

◆ FixedPoint() [1/6]

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

Creates a zero-initialized fixed point object.

Definition at line 49 of file FixedPoint.hh.

Referenced by openmsx::FixedPoint< EG_DP_BITS - EG_BITS >::create(), and openmsx::FixedPoint< EG_DP_BITS - EG_BITS >::serialize().

◆ FixedPoint() [2/6]

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

Definition at line 53 of file FixedPoint.hh.

◆ FixedPoint() [3/6]

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

Definition at line 54 of file FixedPoint.hh.

◆ FixedPoint() [4/6]

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

Definition at line 55 of file FixedPoint.hh.

◆ FixedPoint() [5/6]

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

Definition at line 56 of file FixedPoint.hh.

◆ FixedPoint() [6/6]

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

Definition at line 66 of file FixedPoint.hh.

Member Function Documentation

◆ addQuantum()

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

Increase this value with the smallest possible amount.

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

Definition at line 216 of file FixedPoint.hh.

Referenced by openmsx::YMF262::peekStatus(), and openmsx::YM2413Burczynski::YM2413::YM2413().

◆ create()

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

Create new fixed point object from given representation.

Used by the overloaded operators.

Parameters
valuethe internal representation.

Definition at line 43 of file FixedPoint.hh.

Referenced by openmsx::FixedPoint< EG_DP_BITS - EG_BITS >::floor(), openmsx::FixedPoint< EG_DP_BITS - EG_BITS >::fract(), and openmsx::FixedPoint< EG_DP_BITS - EG_BITS >::roundRatioDown().

◆ divAsInt()

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

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

Definition at line 119 of file FixedPoint.hh.

◆ floor()

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

Returns this value rounded down.

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

Definition at line 127 of file FixedPoint.hh.

◆ fract()

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

Returns the fractional part of this value.

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

Definition at line 135 of file FixedPoint.hh.

◆ fractAsInt()

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

Returns the fractional part of this value as an integer.

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

Definition at line 143 of file FixedPoint.hh.

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

◆ fractionAsDouble()

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

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

◆ fractionAsFloat()

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

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 99 of file FixedPoint.hh.

◆ getRawValue()

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

Definition at line 222 of file FixedPoint.hh.

◆ operator+=()

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

Definition at line 206 of file FixedPoint.hh.

◆ operator-=()

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

Definition at line 209 of file FixedPoint.hh.

◆ roundRatioDown()

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

Definition at line 58 of file FixedPoint.hh.

◆ serialize()

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

Definition at line 227 of file FixedPoint.hh.

◆ shiftHelper()

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

Definition at line 62 of file FixedPoint.hh.

◆ toDouble()

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

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

Definition at line 89 of file FixedPoint.hh.

◆ toFloat()

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

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

Definition at line 82 of file FixedPoint.hh.

◆ toInt()

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

Returns the integer part (rounded down) of this fixed point number.

Note that for negative numbers, rounding occurs away from zero.

Definition at line 75 of file FixedPoint.hh.

Referenced by openmsx::BlipBuffer::addDelta(), openmsx::YM2413Burczynski::Slot::calc_phase(), openmsx::YMF262::peekStatus(), and openmsx::YM2413Burczynski::YM2413::YM2413().

Friends And Related Function Documentation

◆ operator!=

template<unsigned FRACTION_BITS_>
constexpr friend bool operator!= ( FixedPoint< FRACTION_BITS_ >  x,
FixedPoint< FRACTION_BITS_ >  y 
)
friend

Definition at line 188 of file FixedPoint.hh.

◆ operator* [1/3]

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

Definition at line 155 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 159 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 162 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 149 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 152 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 169 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 173 of file FixedPoint.hh.

◆ operator<

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

Definition at line 191 of file FixedPoint.hh.

◆ operator<<

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

Definition at line 176 of file FixedPoint.hh.

◆ operator<=

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

Definition at line 194 of file FixedPoint.hh.

◆ operator==

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

Definition at line 185 of file FixedPoint.hh.

◆ operator>

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

Definition at line 197 of file FixedPoint.hh.

◆ operator>=

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

Definition at line 200 of file FixedPoint.hh.

◆ operator>>

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

Definition at line 179 of file FixedPoint.hh.

Member Data Documentation

◆ FRACTION_BITS

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

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

Definition at line 18 of file FixedPoint.hh.

Referenced by openmsx::FixedPoint< EG_DP_BITS - EG_BITS >::toInt().


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