6 #ifndef VNIX_RAT_ENCODING_HPP 7 #define VNIX_RAT_ENCODING_HPP 9 #include <vnix/bit-range.hpp> 10 #include <vnix/rat/normalized-pair.hpp> 19 template <
unsigned NMR_BITS,
unsigned DNM_BITS>
class encoding {
21 enum {
BITS = NMR_BITS + DNM_BITS };
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;
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));
64 return sftype(shifted);
Numerator and denominator of a rational number as separate numbers, not encoded into the same word...
constexpr int_types< DNM_BITS >::UF d() const
Normalized denominator.
Smallest integer types for holding certain number of bits.
static constexpr utype encode(normalized_pair< NMR_BITS, DNM_BITS > p)
Calculated encoding from normalized numerator and denominator.
utype c_
Unsigned word storing encoding.
constexpr encoding(normalized_pair< NMR_BITS, DNM_BITS > p)
Initialize from normalized numerator and denominator.
constexpr encoding(utype c)
Allow descendant to construct from code-word.
Thomas E. Vaughan's public software.
constexpr int_types< NMR_BITS >::SF n() const
Normalized numerator.
Classes and functions supporting a model of a fixed-precision rational number.
Encoding of numerator and denominator into unsigned word.