MFMv2.0.10
Movable Feast Machine Simulator 2.0.10
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
BitField.h
Go to the documentation of this file.
1 /* -*- mode:C++ -*-
2  BitField.h Section of a BitVector
3  Copyright (C) 2014 The Regents of the University of New Mexico. All rights reserved.
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Lesser General Public
7  License as published by the Free Software Foundation; either
8  version 2.1 of the License, or (at your option) any later version.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this library; if not, write to the Free Software
17  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
18  USA
19 */
20 
27 #ifndef BITFIELD_H
28 #define BITFIELD_H
29 
30 #include "BitVector.h"
31 #include "VD.h"
32 #include "Atom.h"
33 
34 namespace MFM {
35 
40  template <class BV, VD::Type VT, u32 LEN, u32 IDX>
41  class BitField
42  {
43  public:
44  typedef typename VTypeToType<VT>::TYPE VTYPE;
45 
46  enum
47  {
48  BITS = BV::BITS,
49  START = IDX,
50  LENGTH = LEN,
51  END = START + LENGTH
52  };
53 
54  enum
55  {
56  BITFIELD_ERROR_OVERFLOWED_BITS = (END+1)/((BITS+1)/(END+1))
57  };
58 
59  enum
60  {
61  BITFIELD_ERROR_FIELD_TOO_LONG = (LENGTH+1)/((64+1)/(LENGTH+1))
62  };
63 
73  static u32 Read(const BV & bv)
74  {
75  return bv.Read(START,LENGTH);
76  }
77 
84  static void Write(BV & bv, u32 val)
85  {
86  bv.Write(START,LENGTH,val);
87  }
88 
98  static u64 ReadLong(const BV & bv)
99  {
100  return bv.ReadLong(START,LENGTH);
101  }
102 
112  static void SetBit(BV & bv, u32 bitnum)
113  {
114  return bv.SetBit(START+bitnum);
115  }
116 
117  template <class CC>
118  static void SetBit(Atom<CC> & atom, u32 bitnum)
119  {
120  return atom.m_bits.SetBit(START+bitnum);
121  }
122 
132  static void ClearBit(BV & bv, u32 bitnum)
133  {
134  return bv.ClearBit(START+bitnum);
135  }
136 
137  template <class CC>
138  static void ClearBit(Atom<CC> & atom, u32 bitnum)
139  {
140  return atom.m_bits.ClearBit(START+bitnum);
141  }
142 
152  static void ToggleBit(BV & bv, u32 bitnum)
153  {
154  return bv.ToggleBit(START+bitnum);
155  }
156 
157  template <class CC>
158  static void ToggleBit(Atom<CC> & atom, u32 bitnum)
159  {
160  return atom.m_bits.ToggleBit(START+bitnum);
161  }
162 
163 
175  static bool ReadBit(const BV & bv, u32 bitnum)
176  {
177  return bv.ReadBit(START+bitnum);
178  }
179 
180  template <class CC>
181  static bool ReadBit(const Atom<CC> & atom, u32 bitnum)
182  {
183  return atom.m_bits.ReadBit(START+bitnum);
184  }
185 
192  static void WriteLong(BV & bv, u64 val)
193  {
194  bv.WriteLong(START,LENGTH,val);
195  }
196 
197  template <class CC>
198  static u32 Read(const Atom<CC> & atom)
199  {
200  return Read(atom.m_bits);
201  }
202 
203  template <class CC>
204  static void Write(Atom<CC> & atom, u32 val)
205  {
206  Write(atom.m_bits, val);
207  }
208 
210  // Type specific: u32
211  template <class CC>
212  static void Load(const Atom<CC> & atom, u32 & val)
213  {
214  val = Read(atom);
215  }
216 
217  template <class CC>
218  static void Store(Atom<CC> & atom, const u32 & val)
219  {
220  Write(atom,val);
221  }
222 
224  // Type specific: s32
225  template <class CC>
226  static void Load(const Atom<CC> & atom, s32 & val)
227  {
228  u32 v = Read(atom);
229  val = _SignExtend32(v, LENGTH);
230  }
231 
232  template <class CC>
233  static void Store(Atom<CC> & atom, const s32 & val)
234  {
235  Write(atom,(u32) val);
236  }
237 
239  // Type specific: bool
240  template <class CC>
241  static void Load(const Atom<CC> & atom, bool & val)
242  {
243  u32 v = Read(atom);
244  val = PopCount(v) > LENGTH/2;
245  }
246 
247  template <class CC>
248  static void Store(Atom<CC> & atom, const bool & val)
249  {
250  Write(atom,val? _GetNOnes32(LENGTH) : 0u);
251  }
252 
254  // Type specific: BITS
255  template <class CC>
256  static void Load(const Atom<CC> & atom, u64 & val)
257  {
258  val = ReadLong(atom.m_bits);
259  }
260 
261  template <class CC>
262  static void Store(Atom<CC> & atom, const u64 & val)
263  {
264  WriteLong(atom.m_bits,val);
265  }
266 
267 
268  template <class CC>
269  static VTYPE GetValue(const Atom<CC> & a)
270  {
271  VTYPE temp;
272  Load(a, temp);
273  return temp;
274  }
275 
276  template <class CC>
277  static void SetValue(Atom<CC> & a, VTYPE val)
278  {
279  Store(a, val);
280  }
281 
282  };
283 } /* namespace MFM */
284 
285 #endif /*BITFIELD_H*/
static void Write(BV &bv, u32 val)
Definition: BitField.h:84
static u32 Read(const BV &bv)
Definition: BitField.h:73
static void ToggleBit(BV &bv, u32 bitnum)
Definition: BitField.h:152
Definition: Atom.h:71
static void SetBit(BV &bv, u32 bitnum)
Definition: BitField.h:112
static u64 ReadLong(const BV &bv)
Definition: BitField.h:98
void ClearBit(const u32 idx)
Definition: BitVector.h:217
bool ToggleBit(const u32 idx)
Definition: BitVector.tcc:54
static void WriteLong(BV &bv, u64 val)
Definition: BitField.h:192
void SetBit(const u32 idx)
Definition: BitVector.h:207
static void ClearBit(BV &bv, u32 bitnum)
Definition: BitField.h:132
bool ReadBit(u32 idx)
Definition: BitVector.tcc:65
BitVector< BPA > m_bits
Definition: Atom.h:99
static bool ReadBit(const BV &bv, u32 bitnum)
Definition: BitField.h:175