MFMv2.0.10
Movable Feast Machine Simulator 2.0.10
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Util.h
Go to the documentation of this file.
1 /* -*- mode:C++ -*-
2  Util.h Globally acessible extension methods
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 
28 #ifndef UTIL_H
29 #define UTIL_H
30 
31 #include "itype.h"
32 #include "math.h"
33 
34 namespace MFM {
35 
36 #define MARK_USED(X) ((void)(&(X)))
37 
38  template <const bool mustBeTrue>
39  inline void COMPILATION_REQUIREMENT()
40  {
41  typedef char errorIfFalse[mustBeTrue == 0 ? -1 : 1];
42  errorIfFalse t;
43  MARK_USED(t);
44  }
45 
52  inline u64 HexU64(const u32 hi, const u32 lo) {
53  return ((((u64) hi)<<32)|lo);
54  }
55 
63  inline u64 DecU64(const u32 billions, const u32 millions) {
64  return ((((u64) billions)*1000000000) + millions);
65  }
66 
67  inline s32 _SignExtend32(u32 val, u32 bitwidth) {
68  return ((s32)(val<<(32-bitwidth)))>>(32-bitwidth);
69  }
70 
71  inline s64 _SignExtend64(u64 val, u32 bitwidth) {
72  return ((s64)(val<<(64-bitwidth)))>>(64-bitwidth);
73  }
74 
75  inline u32 _GetNOnes32(u32 bitwidth) {
76  return (bitwidth >= 32) ? (u32) -1 : (((u32)1)<<bitwidth)-1;
77  }
78 
79  inline u64 _GetNOnes64(u32 bitwidth) {
80  return (bitwidth >= 64) ? HexU64((u32)-1,(u32)-1) : (((u64)1)<<bitwidth)-1;
81  }
82 
83  inline u32 _ShiftToBitNumber32(u32 value, u32 bitpos) {
84  return value<<bitpos;
85  }
86 
87  inline u64 _ShiftToBitNumber64(u32 value, u32 bitpos) {
88  return ((u64) value)<<bitpos;
89  }
90 
91  inline u32 _ShiftFromBitNumber32(u32 value, u32 bitpos) {
92  return value>>bitpos;
93  }
94 
95  inline u64 _ShiftFromBitNumber64(u64 value, u32 bitpos) {
96  return value>>bitpos;
97  }
98 
99  inline u32 _GetMask32(u32 bitpos, u32 bitwidth) {
100  return _ShiftToBitNumber32(_GetNOnes32(bitwidth),bitpos);
101  }
102 
103  inline u64 _GetMask64(u32 bitpos, u32 bitwidth) {
104  return _ShiftToBitNumber64(_GetNOnes64(bitwidth),bitpos);
105  }
106 
107  inline u32 _ExtractField32(u32 val, u32 bitpos,u32 bitwidth) {
108  return _ShiftFromBitNumber32(val,bitpos)&_GetNOnes32(bitwidth);
109  }
110 
111  inline u32 _ExtractUint32(u32 val, u32 bitpos,u32 bitwidth) {
112  return _ExtractField32(val,bitpos,bitwidth);
113  }
114 
115  inline s32 _ExtractSint32(u32 val, u32 bitpos,u32 bitwidth) {
116  return _SignExtend32(_ExtractField32(val,bitpos,bitwidth),bitwidth);
117  }
118 
119  inline u32 _getParity32(u32 v) {
120  v ^= v >> 16;
121  v ^= v >> 8;
122  v ^= v >> 4;
123  v &= 0xf;
124  return (0x6996 >> v) & 1;
125  }
126 
127  // v must be <= 0x7fffffff
128  inline u32 _getNextPowerOf2(u32 v) {
129  v |= v >> 16;
130  v |= v >> 8;
131  v |= v >> 4;
132  v |= v >> 2;
133  v |= v >> 1;
134  return v+1;
135  }
136 
141  inline u32 MakeMaskClip(const u32 length) {
142  if (length<32) return (1u << length) - 1;
143  return -1;
144  }
145 
146  inline u32 PopCount(const u32 bits) {
147  return __builtin_popcount(bits); // GCC
148  }
149 
150  template <class T>
151  inline T MAX(T x, T y) {
152  return (x > y) ? x : y;
153  }
154 
155  template <class T>
156  inline T MIN(T x, T y) {
157  return (x < y) ? x : y;
158  }
159 
160  template <class T>
161  inline T CLAMP(T min, T max, T val)
162  {
163  return val < min ? min : (val > max ? max : val);
164  }
165 
166  template <class T>
167  inline T ABS(T val)
168  {
169  return val > 0 ? val : (-val);
170  }
171 
172  template <class T>
173  inline double DISTANCE(T x1, T y1, T x2, T y2)
174  {
175  return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
176  }
177 
178  template <u32 BITS>
179  inline u32 PARITY_BITS(u32 value)
180  {
181  return PARITY_BITS<BITS/2>(value ^ (value>>(BITS/2)));
182  }
183 
184  template <>
185  inline u32 PARITY_BITS<1>(u32 value) {
186  return value&1;
187  }
188 
189  template <>
190  inline u32 PARITY_BITS<2>(u32 value) {
191  return (value^(value>>1))&1;
192  }
193 
194  template <>
195  inline u32 PARITY_BITS<3>(u32 value) {
196  return (0x96 >> (value&0x3)) & 1;
197  }
198 
199  template <>
200  inline u32 PARITY_BITS<4>(u32 value)
201  {
202  // 0000 0
203  // 0001 1
204  // 0010 1
205  // 0011 0
206  // 0100 1
207  // 0101 0
208  // 0110 0
209  // 0111 1
210  // 1xxx Same as above, just flipped:
211  // 01101001 10010110
212  // 0110 1001 1001 0110
213  // 0x6996
214  return (0x6996>>(value&0xf))&1;
215  }
216 
217  template <class T>
218  inline u32 PARITY(T value) {
219  return PARITY_BITS<8*sizeof(T)>((u32) value);
220  }
221 
222  template<u32 MIN_BITS>
223  struct _ITypeSizes
224  {
225  enum { value =
226  MIN_BITS <= 8 ? 8 :
227  MIN_BITS <= 16 ? 16 :
228  MIN_BITS <= 32 ? 32 :
229  64
230  };
231  };
232 
233  template<u64 NUMBER>
235  {
236  enum { value =
237  NUMBER <= 0xff ? 8 :
238  NUMBER <= 0xffff ? 16 :
239  NUMBER <= 0xffffffff ? 32 :
240  64
241  };
242  };
243 
245  template<u32 BITS> struct PickIType_;
246 
248  template<> struct PickIType_<8> { typedef u8 type; };
249 
251  template<> struct PickIType_<16> { typedef u16 type; };
252 
254  template<> struct PickIType_<32> { typedef u32 type; };
255 
257  template<> struct PickIType_<64> { typedef u64 type; };
258 
259  template<u32 BITS>
260  struct UForBits : PickIType_<_ITypeSizes<BITS>::value> {};
261 
262  template<u64 NUMBER>
263  struct UForNumber : PickIType_<_ITypeSizesForNumber<NUMBER>::value> {};
264 
275  template <class T>
276  u32 ITEM_COUNT(T* array, T item, u32 arrSize)
277  {
278  u32 count = 0;
279  u32 i;
280  for(i = 0; i < arrSize; i++)
281  {
282  if(array[i] == item)
283  {
284  count++;
285  }
286  }
287  return count;
288  }
289 
299  extern u32 DigitCount(u32 num, u32 base);
300 
308  extern void IntLexEncode(u32 num, char* output);
309 
323  extern void Sleep(u32 seconds, u64 nanos) ;
324 
325 }
326 
327 #endif /* UTIL_H */
Definition: Util.h:223
Definition: Util.h:260
Definition: Util.h:234
Definition: Util.h:245
Definition: Util.h:263