units
Use physical dimensions at compile-time or run-time.
encoding.hpp
Go to the documentation of this file.
1 /// @file vnix/rat/encoding.hpp
2 /// @brief Definition of vnix::rat::encoding.
3 /// @copyright 2019 Thomas E. Vaughan; all rights reserved.
4 /// @license BSD Three-Clause; see LICENSE.
5 
6 #ifndef VNIX_RAT_ENCODING_HPP
7 #define VNIX_RAT_ENCODING_HPP
8 
9 #include <vnix/bit-range.hpp>
10 #include <vnix/rat/normalized-pair.hpp>
11 
12 namespace vnix {
13 namespace rat {
14 
15 
16 /// Encoding of numerator and denominator into unsigned word.
17 /// @tparam NMR_BITS Number of bits for numerator.
18 /// @tparam DNM_BITS Number of bits for denominator.
19 template <unsigned NMR_BITS, unsigned DNM_BITS> class encoding {
20 public:
21  enum { /** Total number of bits. */ BITS = NMR_BITS + DNM_BITS };
22  using utype = typename int_types<BITS>::US; ///< Unsigned type for encoding.
23  using stype = typename int_types<BITS>::SS; ///< Signed type for encoding.
24  using uftype = typename int_types<BITS>::UF; ///< Unsigned fast type.
25  using sftype = typename int_types<BITS>::SF; ///< Signed fast type.
26 
27  /// Mask for each of numerator and denominator.
28  enum {
29  DNM_MASK = bit_range<utype>(0, DNM_BITS - 1), ///< Mask for denominator.
30  NMR_MASK = bit_range<utype>(DNM_BITS, BITS - 1) ///< Mask for numerator.
31  };
32 
33 private:
34  /// Calculated encoding from normalized numerator and denominator.
35  /// @param p Normalized numerator and denominator.
36  constexpr static utype encode(normalized_pair<NMR_BITS, DNM_BITS> p) {
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  }
41 
42 protected:
43  utype c_; ///< Unsigned word storing encoding.
44 
45  /// Allow descendant to construct from code-word.
46  /// @param c Code-word.
47  constexpr encoding(utype c) : c_(c) {}
48 
49 public:
50  /// Initialize from normalized numerator and denominator.
51  /// @param p Normalized numerator and denominator.
52  constexpr encoding(normalized_pair<NMR_BITS, DNM_BITS> p) : c_(encode(p)) {}
53 
54  /// Normalized numerator.
55  constexpr typename int_types<NMR_BITS>::SF n() const {
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  }
66 
67  /// Normalized denominator.
68  constexpr typename int_types<DNM_BITS>::UF d() const {
69  return (c_ & DNM_MASK) + 1;
70  }
71 };
72 
73 
74 } // namespace rat
75 } // namespace vnix
76 
77 #endif // ndef VNIX_RAT_ENCODING_HPP
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.
Definition: encoding.hpp:68
Total number of bits.
Definition: encoding.hpp:21
Mask for denominator.
Definition: encoding.hpp:29
Smallest integer types for holding certain number of bits.
Definition: int-types.hpp:21
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
constexpr encoding(normalized_pair< NMR_BITS, DNM_BITS > p)
Initialize from normalized numerator and denominator.
Definition: encoding.hpp:52
constexpr encoding(utype c)
Allow descendant to construct from code-word.
Definition: encoding.hpp:47
Mask for numerator.
Definition: encoding.hpp:30
Thomas E. Vaughan&#39;s public software.
Definition: rational.hpp:13
constexpr int_types< NMR_BITS >::SF n() const
Normalized numerator.
Definition: encoding.hpp:55
Classes and functions supporting a model of a fixed-precision rational number.
Definition: rational.hpp:17
Encoding of numerator and denominator into unsigned word.
Definition: encoding.hpp:19