units
Use physical dimensions at compile-time or run-time.
Public Types | Public Member Functions | Protected Member Functions | Protected Attributes | Static Private Member Functions | List of all members
vnix::rat::encoding< NMR_BITS, DNM_BITS > Class Template Reference

Encoding of numerator and denominator into unsigned word. More...

#include <encoding.hpp>

Inheritance diagram for vnix::rat::encoding< NMR_BITS, DNM_BITS >:
[legend]
Collaboration diagram for vnix::rat::encoding< NMR_BITS, DNM_BITS >:
[legend]

Public Types

enum  { BITS = NMR_BITS + DNM_BITS }
 
enum  { DNM_MASK = bit_range<utype>(0, DNM_BITS - 1), NMR_MASK = bit_range<utype>(DNM_BITS, BITS - 1) }
 Mask for each of numerator and denominator. More...
 
using utype = typename int_types< BITS >::US
 Unsigned type for encoding. More...
 
using stype = typename int_types< BITS >::SS
 Signed type for encoding. More...
 
using uftype = typename int_types< BITS >::UF
 Unsigned fast type. More...
 
using sftype = typename int_types< BITS >::SF
 Signed fast type. More...
 

Public Member Functions

constexpr encoding (normalized_pair< NMR_BITS, DNM_BITS > p)
 Initialize from normalized numerator and denominator. More...
 
constexpr int_types< NMR_BITS >::SF n () const
 Normalized numerator. More...
 
constexpr int_types< DNM_BITS >::UF d () const
 Normalized denominator. More...
 

Protected Member Functions

constexpr encoding (utype c)
 Allow descendant to construct from code-word. More...
 

Protected Attributes

utype c_
 Unsigned word storing encoding. More...
 

Static Private Member Functions

static constexpr utype encode (normalized_pair< NMR_BITS, DNM_BITS > p)
 Calculated encoding from normalized numerator and denominator. More...
 

Detailed Description

template<unsigned NMR_BITS, unsigned DNM_BITS>
class vnix::rat::encoding< NMR_BITS, DNM_BITS >

Encoding of numerator and denominator into unsigned word.

Template Parameters
NMR_BITSNumber of bits for numerator.
DNM_BITSNumber of bits for denominator.

Definition at line 19 of file encoding.hpp.

Member Typedef Documentation

template<unsigned NMR_BITS, unsigned DNM_BITS>
using vnix::rat::encoding< NMR_BITS, DNM_BITS >::sftype = typename int_types<BITS>::SF

Signed fast type.

Definition at line 25 of file encoding.hpp.

template<unsigned NMR_BITS, unsigned DNM_BITS>
using vnix::rat::encoding< NMR_BITS, DNM_BITS >::stype = typename int_types<BITS>::SS

Signed type for encoding.

Definition at line 23 of file encoding.hpp.

template<unsigned NMR_BITS, unsigned DNM_BITS>
using vnix::rat::encoding< NMR_BITS, DNM_BITS >::uftype = typename int_types<BITS>::UF

Unsigned fast type.

Definition at line 24 of file encoding.hpp.

template<unsigned NMR_BITS, unsigned DNM_BITS>
using vnix::rat::encoding< NMR_BITS, DNM_BITS >::utype = typename int_types<BITS>::US

Unsigned type for encoding.

Definition at line 22 of file encoding.hpp.

Member Enumeration Documentation

template<unsigned NMR_BITS, unsigned DNM_BITS>
anonymous enum
Enumerator
BITS 

Total number of bits.

Definition at line 21 of file encoding.hpp.

21 { BITS = NMR_BITS + DNM_BITS };
Total number of bits.
Definition: encoding.hpp:21
template<unsigned NMR_BITS, unsigned DNM_BITS>
anonymous enum

Mask for each of numerator and denominator.

Enumerator
DNM_MASK 

Mask for denominator.

NMR_MASK 

Mask for numerator.

Definition at line 28 of file encoding.hpp.

28  {
29  DNM_MASK = bit_range<utype>(0, DNM_BITS - 1),
30  NMR_MASK = bit_range<utype>(DNM_BITS, BITS - 1)
31  };
Total number of bits.
Definition: encoding.hpp:21
Mask for denominator.
Definition: encoding.hpp:29
Mask for numerator.
Definition: encoding.hpp:30

Constructor & Destructor Documentation

template<unsigned NMR_BITS, unsigned DNM_BITS>
constexpr vnix::rat::encoding< NMR_BITS, DNM_BITS >::encoding ( utype  c)
inlineprotected

Allow descendant to construct from code-word.

Parameters
cCode-word.

Definition at line 47 of file encoding.hpp.

47 : c_(c) {}
utype c_
Unsigned word storing encoding.
Definition: encoding.hpp:43
template<unsigned NMR_BITS, unsigned DNM_BITS>
constexpr vnix::rat::encoding< NMR_BITS, DNM_BITS >::encoding ( normalized_pair< NMR_BITS, DNM_BITS >  p)
inline

Initialize from normalized numerator and denominator.

Parameters
pNormalized numerator and denominator.

Definition at line 52 of file encoding.hpp.

52 : c_(encode(p)) {}
static constexpr utype encode(normalized_pair< NMR_BITS, DNM_BITS > p)
Calculated encoding from normalized numerator and denominator.
Definition: encoding.hpp:36
utype c_
Unsigned word storing encoding.
Definition: encoding.hpp:43

Here is the caller graph for this function:

Member Function Documentation

template<unsigned NMR_BITS, unsigned DNM_BITS>
constexpr int_types<DNM_BITS>::UF vnix::rat::encoding< NMR_BITS, DNM_BITS >::d ( ) const
inline

Normalized denominator.

Definition at line 68 of file encoding.hpp.

68  {
69  return (c_ & DNM_MASK) + 1;
70  }
Mask for denominator.
Definition: encoding.hpp:29
utype c_
Unsigned word storing encoding.
Definition: encoding.hpp:43

Here is the caller graph for this function:

template<unsigned NMR_BITS, unsigned DNM_BITS>
static constexpr utype vnix::rat::encoding< NMR_BITS, DNM_BITS >::encode ( normalized_pair< NMR_BITS, DNM_BITS >  p)
inlinestaticprivate

Calculated encoding from normalized numerator and denominator.

Parameters
pNormalized numerator and denominator.

Definition at line 36 of file encoding.hpp.

36  {
37  utype const num_enc = utype(p.n()) << DNM_BITS;
38  utype const den_enc = (p.d() - 1) & DNM_MASK;
39  return num_enc | den_enc;
40  }
constexpr S n() const
Normalized numerator.
Mask for denominator.
Definition: encoding.hpp:29
typename int_types< BITS >::US utype
Unsigned type for encoding.
Definition: encoding.hpp:22
constexpr U d() const
Normalized denominator.

Here is the caller graph for this function:

template<unsigned NMR_BITS, unsigned DNM_BITS>
constexpr int_types<NMR_BITS>::SF vnix::rat::encoding< NMR_BITS, DNM_BITS >::n ( ) const
inline

Normalized numerator.

Definition at line 55 of file encoding.hpp.

55  {
56  // Perform arithmetic right-shift on signed number. With C++20, this could
57  // be done simply as 'return stype(c_) >> DNM_BITS'.
58  uftype const sign_bit = c_ & bit<uftype>(BITS - 1);
59  uftype const shifted = c_ >> DNM_BITS;
60  enum { UFTYPE_BITS = 8 * sizeof(uftype) };
61  if (sign_bit && NMR_BITS < UFTYPE_BITS) {
62  return sftype(shifted | bit_range<uftype>(NMR_BITS, UFTYPE_BITS - 1));
63  }
64  return sftype(shifted);
65  }
typename int_types< BITS >::UF uftype
Unsigned fast type.
Definition: encoding.hpp:24
Total number of bits.
Definition: encoding.hpp:21
utype c_
Unsigned word storing encoding.
Definition: encoding.hpp:43
typename int_types< BITS >::SF sftype
Signed fast type.
Definition: encoding.hpp:25

Here is the caller graph for this function:

Member Data Documentation

template<unsigned NMR_BITS, unsigned DNM_BITS>
utype vnix::rat::encoding< NMR_BITS, DNM_BITS >::c_
protected

Unsigned word storing encoding.

Definition at line 43 of file encoding.hpp.


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