Universal Numbers Library C++ API
sw::universal -- header-only custom arithmetic types (posit, cfloat, lns, fixpnt, dd/qd, and more)
Loading...
Searching...
No Matches
sw::universal::blockfraction< _nbits, bt > Class Template Reference

a block-based floating-point fraction More...

#include <blockfraction.hpp>

Public Types

typedef bt BlockType

Public Member Functions

constexpr blockfraction () noexcept
 NOTE: don't set a default blocktype as this makes the integration more brittle as blocktriple uses the blocksignificant as storage class and needs to interact with the client number system, which is also blocked.
template<size_t... I>
constexpr blockfraction (const uint64_t raw, int radixPoint, std::index_sequence< I... >) noexcept
constexpr blockfraction (const uint64_t raw, int radixPoint) noexcept
constexpr blockfraction (const blockfraction &) noexcept=default
constexpr blockfraction (blockfraction &&) noexcept=default
constexpr blockfraction & operator= (const blockfraction &) noexcept=default
constexpr blockfraction & operator= (blockfraction &&) noexcept=default
constexpr operator float () const noexcept
 explicit conversion operators
constexpr operator double () const noexcept
constexpr blockfraction operator~ () const noexcept
 prefix operators
constexpr bool anyAfter (unsigned bitIndex) const noexcept
 logic operators
constexpr void increment () noexcept
 arithmetic operators
void add (const blockfraction &lhs, const blockfraction &rhs) noexcept
 add two fractions of the form 00h.fffff, that is, radix point at nbits-3 In this encoding, all valid values are encapsulated
void sub (const blockfraction &lhs, const blockfraction &rhs) noexcept
void mul (const blockfraction &lhs, const blockfraction &rhs) noexcept
void div (const blockfraction &lhs, const blockfraction &rhs) noexcept
void scaleByBase (const blockfraction &fraction, const blockfraction &integerBase) noexcept
constexpr blockfraction & operator<<= (int bitsToShift) noexcept
constexpr blockfraction & operator>>= (int bitsToShift) noexcept
constexpr void clear () noexcept
constexpr void setzero () noexcept
constexpr void setradix (int radix) noexcept
constexpr void setbit (unsigned i, bool v=true) noexcept
constexpr void setblock (unsigned b, const bt &block) noexcept
constexpr void setbits (uint64_t value) noexcept
constexpr blockfraction & flip () noexcept
constexpr blockfraction & twosComplement () noexcept
constexpr bool iszero () const noexcept
constexpr int radix () const noexcept
constexpr bool isodd () const noexcept
constexpr bool iseven () const noexcept
constexpr bool sign () const noexcept
constexpr bool isneg () const noexcept
constexpr unsigned integer () const noexcept
constexpr bool test (unsigned bitIndex) const noexcept
constexpr bool at (unsigned bitIndex) const noexcept
constexpr bool checkCarry () const noexcept
constexpr uint8_t nibble (unsigned n) const noexcept
constexpr bt block (unsigned b) const noexcept
constexpr blockfraction fraction () const noexcept
constexpr int msb () const noexcept
constexpr float to_float () const noexcept
constexpr double to_double () const noexcept
constexpr bool roundingDirection (unsigned targetLsb) const noexcept
constexpr bool any (unsigned msb) const noexcept

Public Attributes

int radixPoint
bt _block [nrBlocks]

Static Public Attributes

static constexpr unsigned nbits = _nbits
static constexpr unsigned bitsInByte = 8
static constexpr unsigned bitsInBlock = sizeof(bt) * bitsInByte
static constexpr unsigned nrBlocks = 1ull + ((nbits - 1ull) / bitsInBlock)
static constexpr uint64_t storageMask = (0xFFFF'FFFF'FFFF'FFFFull >> (64 - bitsInBlock))
static constexpr uint64_t maxRightShift = ((64 - nbits + 3) > 62) ? 63 : (64 - nbits + 3)
static constexpr uint64_t fmask = (64 - nbits + 3) > 63 ? 0ull : (0xFFFF'FFFF'FFFF'FFFFull >> maxRightShift)
static constexpr unsigned MSU = nrBlocks - 1
static constexpr bt ALL_ONES = bt(~0)
static constexpr bt MSU_MASK = (ALL_ONES >> (nrBlocks * bitsInBlock - nbits))
static constexpr bt OVERFLOW_BIT = ~(MSU_MASK >> 1) & MSU_MASK

Friends

constexpr bool operator== (const blockfraction &lhs, const blockfraction &rhs) noexcept
constexpr bool operator!= (const blockfraction &lhs, const blockfraction &rhs) noexcept
constexpr bool operator< (const blockfraction &lhs, const blockfraction &rhs) noexcept
constexpr bool operator<= (const blockfraction &lhs, const blockfraction &rhs) noexcept
constexpr bool operator> (const blockfraction &lhs, const blockfraction &rhs) noexcept
constexpr bool operator>= (const blockfraction &lhs, const blockfraction &rhs) noexcept
constexpr blockfraction operator<< (const blockfraction &a, const long b) noexcept
constexpr blockfraction operator>> (const blockfraction &a, const long b) noexcept

Detailed Description

template<unsigned _nbits, typename bt>
class sw::universal::blockfraction< _nbits, bt >

a block-based floating-point fraction

A blockfraction is by definition an unsigned entity. As arithmetic operators introduce additional bits, the radixpoint is controllable.

Template Parameters
bt

Member Typedef Documentation

◆ BlockType

template<unsigned _nbits, typename bt>
typedef bt sw::universal::blockfraction< _nbits, bt >::BlockType

Constructor & Destructor Documentation

◆ blockfraction() [1/5]

template<unsigned _nbits, typename bt>
sw::universal::blockfraction< _nbits, bt >::blockfraction ( )
inlineconstexprnoexcept

NOTE: don't set a default blocktype as this makes the integration more brittle as blocktriple uses the blocksignificant as storage class and needs to interact with the client number system, which is also blocked.

Using the same blocktype simplifies the copying of fraction bits from and to the client.

◆ blockfraction() [2/5]

template<unsigned _nbits, typename bt>
template<size_t... I>
sw::universal::blockfraction< _nbits, bt >::blockfraction ( const uint64_t raw,
int radixPoint,
std::index_sequence< I... >  )
inlineconstexprnoexcept

◆ blockfraction() [3/5]

template<unsigned _nbits, typename bt>
sw::universal::blockfraction< _nbits, bt >::blockfraction ( const uint64_t raw,
int radixPoint )
inlineconstexprnoexcept

◆ blockfraction() [4/5]

template<unsigned _nbits, typename bt>
sw::universal::blockfraction< _nbits, bt >::blockfraction ( const blockfraction< _nbits, bt > & )
constexprdefaultnoexcept

◆ blockfraction() [5/5]

template<unsigned _nbits, typename bt>
sw::universal::blockfraction< _nbits, bt >::blockfraction ( blockfraction< _nbits, bt > && )
constexprdefaultnoexcept

Member Function Documentation

◆ add()

template<unsigned _nbits, typename bt>
void sw::universal::blockfraction< _nbits, bt >::add ( const blockfraction< _nbits, bt > & lhs,
const blockfraction< _nbits, bt > & rhs )
inlinenoexcept

add two fractions of the form 00h.fffff, that is, radix point at nbits-3 In this encoding, all valid values are encapsulated

Parameters
lhsnbits of fraction in the form 00h.ffff
rhsnbits of fraction in the form 00h.ffff

◆ any()

template<unsigned _nbits, typename bt>
bool sw::universal::blockfraction< _nbits, bt >::any ( unsigned msb) const
inlineconstexprnoexcept

◆ anyAfter()

template<unsigned _nbits, typename bt>
bool sw::universal::blockfraction< _nbits, bt >::anyAfter ( unsigned bitIndex) const
inlineconstexprnoexcept

logic operators

◆ at()

template<unsigned _nbits, typename bt>
bool sw::universal::blockfraction< _nbits, bt >::at ( unsigned bitIndex) const
inlineconstexprnoexcept

◆ block()

template<unsigned _nbits, typename bt>
bt sw::universal::blockfraction< _nbits, bt >::block ( unsigned b) const
inlineconstexprnoexcept

◆ checkCarry()

template<unsigned _nbits, typename bt>
bool sw::universal::blockfraction< _nbits, bt >::checkCarry ( ) const
inlineconstexprnoexcept

◆ clear()

template<unsigned _nbits, typename bt>
void sw::universal::blockfraction< _nbits, bt >::clear ( )
inlineconstexprnoexcept

◆ div()

template<unsigned _nbits, typename bt>
void sw::universal::blockfraction< _nbits, bt >::div ( const blockfraction< _nbits, bt > & lhs,
const blockfraction< _nbits, bt > & rhs )
inlinenoexcept

◆ flip()

template<unsigned _nbits, typename bt>
blockfraction & sw::universal::blockfraction< _nbits, bt >::flip ( )
inlineconstexprnoexcept

◆ fraction()

template<unsigned _nbits, typename bt>
blockfraction sw::universal::blockfraction< _nbits, bt >::fraction ( ) const
inlineconstexprnoexcept

◆ increment()

template<unsigned _nbits, typename bt>
void sw::universal::blockfraction< _nbits, bt >::increment ( )
inlineconstexprnoexcept

arithmetic operators

increment the value by one

◆ integer()

template<unsigned _nbits, typename bt>
unsigned sw::universal::blockfraction< _nbits, bt >::integer ( ) const
inlineconstexprnoexcept

◆ iseven()

template<unsigned _nbits, typename bt>
bool sw::universal::blockfraction< _nbits, bt >::iseven ( ) const
inlineconstexprnoexcept

◆ isneg()

template<unsigned _nbits, typename bt>
bool sw::universal::blockfraction< _nbits, bt >::isneg ( ) const
inlineconstexprnoexcept

◆ isodd()

template<unsigned _nbits, typename bt>
bool sw::universal::blockfraction< _nbits, bt >::isodd ( ) const
inlineconstexprnoexcept

◆ iszero()

template<unsigned _nbits, typename bt>
bool sw::universal::blockfraction< _nbits, bt >::iszero ( ) const
inlineconstexprnoexcept

◆ msb()

template<unsigned _nbits, typename bt>
int sw::universal::blockfraction< _nbits, bt >::msb ( ) const
inlineconstexprnoexcept

◆ mul()

template<unsigned _nbits, typename bt>
void sw::universal::blockfraction< _nbits, bt >::mul ( const blockfraction< _nbits, bt > & lhs,
const blockfraction< _nbits, bt > & rhs )
inlinenoexcept

◆ nibble()

template<unsigned _nbits, typename bt>
uint8_t sw::universal::blockfraction< _nbits, bt >::nibble ( unsigned n) const
inlineconstexprnoexcept

◆ operator double()

template<unsigned _nbits, typename bt>
sw::universal::blockfraction< _nbits, bt >::operator double ( ) const
inlineexplicitconstexprnoexcept

◆ operator float()

template<unsigned _nbits, typename bt>
sw::universal::blockfraction< _nbits, bt >::operator float ( ) const
inlineexplicitconstexprnoexcept

explicit conversion operators

◆ operator<<=()

template<unsigned _nbits, typename bt>
blockfraction & sw::universal::blockfraction< _nbits, bt >::operator<<= ( int bitsToShift)
inlineconstexprnoexcept

◆ operator=() [1/2]

template<unsigned _nbits, typename bt>
blockfraction & sw::universal::blockfraction< _nbits, bt >::operator= ( blockfraction< _nbits, bt > && )
constexprdefaultnoexcept

◆ operator=() [2/2]

template<unsigned _nbits, typename bt>
blockfraction & sw::universal::blockfraction< _nbits, bt >::operator= ( const blockfraction< _nbits, bt > & )
constexprdefaultnoexcept

◆ operator>>=()

template<unsigned _nbits, typename bt>
blockfraction & sw::universal::blockfraction< _nbits, bt >::operator>>= ( int bitsToShift)
inlineconstexprnoexcept

◆ operator~()

template<unsigned _nbits, typename bt>
blockfraction sw::universal::blockfraction< _nbits, bt >::operator~ ( ) const
inlineconstexprnoexcept

prefix operators

◆ radix()

template<unsigned _nbits, typename bt>
int sw::universal::blockfraction< _nbits, bt >::radix ( ) const
inlineconstexprnoexcept

◆ roundingDirection()

template<unsigned _nbits, typename bt>
bool sw::universal::blockfraction< _nbits, bt >::roundingDirection ( unsigned targetLsb) const
inlineconstexprnoexcept

◆ scaleByBase()

template<unsigned _nbits, typename bt>
void sw::universal::blockfraction< _nbits, bt >::scaleByBase ( const blockfraction< _nbits, bt > & fraction,
const blockfraction< _nbits, bt > & integerBase )
inlinenoexcept

◆ setbit()

template<unsigned _nbits, typename bt>
void sw::universal::blockfraction< _nbits, bt >::setbit ( unsigned i,
bool v = true )
inlineconstexprnoexcept

◆ setbits()

template<unsigned _nbits, typename bt>
void sw::universal::blockfraction< _nbits, bt >::setbits ( uint64_t value)
inlineconstexprnoexcept

◆ setblock()

template<unsigned _nbits, typename bt>
void sw::universal::blockfraction< _nbits, bt >::setblock ( unsigned b,
const bt & block )
inlineconstexprnoexcept

◆ setradix()

template<unsigned _nbits, typename bt>
void sw::universal::blockfraction< _nbits, bt >::setradix ( int radix)
inlineconstexprnoexcept

◆ setzero()

template<unsigned _nbits, typename bt>
void sw::universal::blockfraction< _nbits, bt >::setzero ( )
inlineconstexprnoexcept

◆ sign()

template<unsigned _nbits, typename bt>
bool sw::universal::blockfraction< _nbits, bt >::sign ( ) const
inlineconstexprnoexcept

◆ sub()

template<unsigned _nbits, typename bt>
void sw::universal::blockfraction< _nbits, bt >::sub ( const blockfraction< _nbits, bt > & lhs,
const blockfraction< _nbits, bt > & rhs )
inlinenoexcept

◆ test()

template<unsigned _nbits, typename bt>
bool sw::universal::blockfraction< _nbits, bt >::test ( unsigned bitIndex) const
inlineconstexprnoexcept

◆ to_double()

template<unsigned _nbits, typename bt>
double sw::universal::blockfraction< _nbits, bt >::to_double ( ) const
inlineconstexprnoexcept

◆ to_float()

template<unsigned _nbits, typename bt>
float sw::universal::blockfraction< _nbits, bt >::to_float ( ) const
inlineconstexprnoexcept

◆ twosComplement()

template<unsigned _nbits, typename bt>
blockfraction & sw::universal::blockfraction< _nbits, bt >::twosComplement ( )
inlineconstexprnoexcept

◆ operator!=

template<unsigned _nbits, typename bt>
bool operator!= ( const blockfraction< _nbits, bt > & lhs,
const blockfraction< _nbits, bt > & rhs )
friend

◆ operator<

template<unsigned _nbits, typename bt>
bool operator< ( const blockfraction< _nbits, bt > & lhs,
const blockfraction< _nbits, bt > & rhs )
friend

◆ operator<<

template<unsigned _nbits, typename bt>
blockfraction operator<< ( const blockfraction< _nbits, bt > & a,
const long b )
friend

◆ operator<=

template<unsigned _nbits, typename bt>
bool operator<= ( const blockfraction< _nbits, bt > & lhs,
const blockfraction< _nbits, bt > & rhs )
friend

◆ operator==

template<unsigned _nbits, typename bt>
bool operator== ( const blockfraction< _nbits, bt > & lhs,
const blockfraction< _nbits, bt > & rhs )
friend

◆ operator>

template<unsigned _nbits, typename bt>
bool operator> ( const blockfraction< _nbits, bt > & lhs,
const blockfraction< _nbits, bt > & rhs )
friend

◆ operator>=

template<unsigned _nbits, typename bt>
bool operator>= ( const blockfraction< _nbits, bt > & lhs,
const blockfraction< _nbits, bt > & rhs )
friend

◆ operator>>

template<unsigned _nbits, typename bt>
blockfraction operator>> ( const blockfraction< _nbits, bt > & a,
const long b )
friend

Member Data Documentation

◆ _block

template<unsigned _nbits, typename bt>
bt sw::universal::blockfraction< _nbits, bt >::_block[nrBlocks]

◆ ALL_ONES

template<unsigned _nbits, typename bt>
bt sw::universal::blockfraction< _nbits, bt >::ALL_ONES = bt(~0)
staticconstexpr

◆ bitsInBlock

template<unsigned _nbits, typename bt>
unsigned sw::universal::blockfraction< _nbits, bt >::bitsInBlock = sizeof(bt) * bitsInByte
staticconstexpr

◆ bitsInByte

template<unsigned _nbits, typename bt>
unsigned sw::universal::blockfraction< _nbits, bt >::bitsInByte = 8
staticconstexpr

◆ fmask

template<unsigned _nbits, typename bt>
uint64_t sw::universal::blockfraction< _nbits, bt >::fmask = (64 - nbits + 3) > 63 ? 0ull : (0xFFFF'FFFF'FFFF'FFFFull >> maxRightShift)
staticconstexpr

◆ maxRightShift

template<unsigned _nbits, typename bt>
uint64_t sw::universal::blockfraction< _nbits, bt >::maxRightShift = ((64 - nbits + 3) > 62) ? 63 : (64 - nbits + 3)
staticconstexpr

◆ MSU

template<unsigned _nbits, typename bt>
unsigned sw::universal::blockfraction< _nbits, bt >::MSU = nrBlocks - 1
staticconstexpr

◆ MSU_MASK

template<unsigned _nbits, typename bt>
bt sw::universal::blockfraction< _nbits, bt >::MSU_MASK = (ALL_ONES >> (nrBlocks * bitsInBlock - nbits))
staticconstexpr

◆ nbits

template<unsigned _nbits, typename bt>
unsigned sw::universal::blockfraction< _nbits, bt >::nbits = _nbits
staticconstexpr

◆ nrBlocks

template<unsigned _nbits, typename bt>
unsigned sw::universal::blockfraction< _nbits, bt >::nrBlocks = 1ull + ((nbits - 1ull) / bitsInBlock)
staticconstexpr

◆ OVERFLOW_BIT

template<unsigned _nbits, typename bt>
bt sw::universal::blockfraction< _nbits, bt >::OVERFLOW_BIT = ~(MSU_MASK >> 1) & MSU_MASK
staticconstexpr

◆ radixPoint

template<unsigned _nbits, typename bt>
int sw::universal::blockfraction< _nbits, bt >::radixPoint

◆ storageMask

template<unsigned _nbits, typename bt>
uint64_t sw::universal::blockfraction< _nbits, bt >::storageMask = (0xFFFF'FFFF'FFFF'FFFFull >> (64 - bitsInBlock))
staticconstexpr

The documentation for this class was generated from the following file:
  • /home/runner/work/universal/universal/include/sw/universal/internal/blockfraction/blockfraction.hpp