NSVD Reader  0.0.1
bit_range.hpp
Go to the documentation of this file.
1 
5 #ifndef __NODAMUSHI_SVD_NORMALIZED_BIT_RANGE_HPP__
6 #define __NODAMUSHI_SVD_NORMALIZED_BIT_RANGE_HPP__
7 
8 # include <cstdint>
9 # include <ostream>
10 
11 namespace nodamushi{
12 namespace svd{
13 namespace normalized{
14 
18 struct bit_range
19 {
20  constexpr bool operator ==(const bit_range& r)const noexcept{return _lsb == r._lsb && _msb == r._msb;}
21  constexpr bool operator !=(const bit_range& r)const noexcept{return _lsb != r._lsb || _msb != r._msb;}
22  constexpr bool operator <(const bit_range& r)const noexcept{return _lsb < r._lsb;}
23  constexpr bool operator <=(const bit_range& r)const noexcept{return _lsb <= r._lsb;}
24  constexpr bool operator >(const bit_range& r)const noexcept{return _lsb > r._lsb;}
25  constexpr bool operator >=(const bit_range& r)const noexcept{return _lsb >= r._lsb;}
27  constexpr unsigned int width()const noexcept{return _msb - _lsb + 1;}
29  constexpr unsigned int size()const noexcept{return _msb - _lsb + 1;}
31  constexpr unsigned int left()const noexcept{return _msb;}
33  constexpr unsigned int right()const noexcept{return _lsb;}
34  constexpr unsigned int msb()const noexcept{return _msb;}
35  constexpr unsigned int lsb()const noexcept{return _lsb;}
37  void msb(unsigned int x)noexcept{if(_lsb <= x)_msb = x;else _lsb = x;}
39  void lsb(unsigned int x)noexcept{if(x <= _msb)_lsb = x;else _msb = x;}
45  uint64_t get(uint64_t v){
46  auto w = size();
47  if(w == 64) return v;
48  v >>=_lsb;
49  const uint64_t mask = (((uint64_t)1) << w) -1;
50  return v & mask;
51  }
59  uint64_t bit(uint64_t v){
60  auto w = size();
61  if(w == 64) return v;
62  const uint64_t mask = ((((uint64_t)1) << w) -1) << _lsb;
63  return v & mask;
64  }
65 
66  //constructors
67  bit_range(unsigned int x,unsigned int y)
68  :_lsb(x<y? x:y),_msb(x <y? y:x){}
69 
70  bit_range(const bit_range&)=default;
71  bit_range(bit_range&&)=default;
72 
73  template<typename T>
74  bit_range(const T& n)://don't change name
75  _lsb(0),_msb(0)
76  {
77  unsigned int l=0,w=1;
80  if(lsb && msb){
81  l = *lsb;
82  w = *msb +1 -l;
83  }else{
85  if(bitRange){
86  const auto& r = *bitRange;
87  l = r.lsb;
88  w = r.msb +1 -r.lsb;
89  }else{
92  if(bitOffset &&bitWidth){
93  l = *bitOffset;
94  w = *bitWidth;
95  }else{
96  //TODO ? error
97  }
98  }
99  }
100  if(n.dim && n.dim.index!=0){
101  __NORMALIZED_DERIVED_FROM_HELPER(dimIncrement);
102  size_t inc = 1;
103  if(dimIncrement)
104  inc = *dimIncrement;
105  l += inc * n.dim.index;
106  }
107  unsigned int m = l + w -1;
108  _lsb = l;
109  _msb = m;
110  }
111  private:
112  using uint = uint16_t;
113  uint _lsb;
114  uint _msb;
115 
116 };
117 
118 }}}// end namesapce
119 
120 namespace std{
121 inline ostream& operator <<(ostream& o ,const ::nodamushi::svd::normalized::bit_range& r)
122 {
123  return o << '[' << r.left() << ':' << r.right() << ']';
124 }
125 }
126 
127 #endif // __NODAMUSHI_SVD_NORMALIZED_BITRANGE_HPP__
128 
constexpr bool operator<(const bit_range &r) const noexcept
Definition: bit_range.hpp:22
constexpr bool operator<=(const bit_range &r) const noexcept
Definition: bit_range.hpp:23
ostream & operator<<(std::ostream &os, const ::nodamushi::svd::Access &value)
Definition: Access.hpp:144
<field>.<bitRange> element
Definition: bitRange.hpp:23
uint64_t get(uint64_t v)
get the bit number of bit-range.
Definition: bit_range.hpp:45
constexpr bool operator==(const bit_range &r) const noexcept
Definition: bit_range.hpp:20
Definition: Access.hpp:143
constexpr bool operator >=(const bit_range &r) const noexcept
Definition: bit_range.hpp:25
constexpr unsigned int lsb() const noexcept
Definition: bit_range.hpp:35
void lsb(unsigned int x) noexcept
set lsb
Definition: bit_range.hpp:39
uint32_t lsb
lsb: the bit position of the least significant bit within the register.
Definition: bitRange.hpp:26
constexpr unsigned int right() const noexcept
lsb
Definition: bit_range.hpp:33
constexpr unsigned int msb() const noexcept
Definition: bit_range.hpp:34
constexpr unsigned int size() const noexcept
bit size
Definition: bit_range.hpp:29
bit_range(unsigned int x, unsigned int y)
Definition: bit_range.hpp:67
constexpr bool operator >(const bit_range &r) const noexcept
Definition: bit_range.hpp:24
constexpr bool operator !=(const bit_range &r) const noexcept
Definition: bit_range.hpp:21
uint64_t bit(uint64_t v)
get the bit of bit-range.
Definition: bit_range.hpp:59
void msb(unsigned int x) noexcept
set msb
Definition: bit_range.hpp:37
constexpr unsigned int left() const noexcept
msb
Definition: bit_range.hpp:31
#define __NORMALIZED_DERIVED_FROM_HELPER(name)
constexpr unsigned int width() const noexcept
bit size
Definition: bit_range.hpp:27