Gray C++ Libraries  0.0.2
A set of C++ libraries for MSVC, GNU on Windows, WinCE, Linux
cUInt64.h
Go to the documentation of this file.
1 //
4 //
5 
6 #ifndef _INC_cUInt64_H
7 #define _INC_cUInt64_H
8 #ifndef NO_PRAGMA_ONCE
9 #pragma once
10 #endif
11 
12 #include "StrArg.h"
13 #include "cBits.h"
14 #include "cTypes.h"
15 #include "cString.h"
16 
17 namespace Gray
18 {
19  class cThreadState;
20 
21 #pragma pack(push,1)
23  {
31 
32  public:
33 #ifdef USE_INT64 // native support.
34  typedef UINT64 UNIT_t;
35  static const unsigned k_UNIT_BITS = 64;
36 #else
37  typedef UINT32 UNIT_t; // break into 2 parts.
38  static const unsigned k_UNIT_BITS = 32;
39 #endif
40 
41  private:
42  // don't use cUnion64 so we can use CUINT64(h,l) for init.
43 #ifdef USE_INT64 // native support for UINT 64 bit.
44  UNIT_t m_u;
45 #elif defined(USE_LITTLE_ENDIAN)
46  UNIT_t m_uLo;
47  UNIT_t m_uHi;
48 #else
49  UNIT_t m_uHi;
50  UNIT_t m_uLo;
51 #endif
52 
53  public:
55 #ifdef USE_INT64
56  : m_u(0)
57 #else
58  : m_uLo(0)
59  , m_uHi(0)
60 #endif
61  {
62  }
64 #ifdef USE_INT64
65  : m_u(n)
66 #else
67  : m_uLo(n)
68  , m_uHi(0)
69 #endif
70  {
71  }
72  cUInt64(const char* pszVal, RADIX_t n = 10)
73  {
74  SetStr(pszVal, n);
75  }
76 
77  // Test Operators
78  bool isZero() const
79  {
80 #ifdef USE_INT64
81  return m_u == 0;
82 #else
83  return m_uLo == 0 && m_uHi == 0;
84 #endif
85  }
86  bool isOdd() const
87  {
88 #ifdef USE_INT64
89  return m_u & 1;
90 #else
91  return m_uLo & 1;
92 #endif
93  }
94  bool IsSet(BIT_ENUM_t nBit) const
95  {
99 #ifdef USE_INT64
100  return cBits::IsSet(m_u, nBit);
101 #else
102  if (nBit < k_UNIT_BITS)
103  return cBits::IsSet(m_uLo, nBit);
104  else
105  return cBits::IsSet(m_uHi, nBit - k_UNIT_BITS);
106 #endif
107  }
108  bool operator == (const cUInt64& n) const
109  {
110 #ifdef USE_INT64
111  return m_u == n.m_u;
112 #else
113  return m_uLo == n.m_uLo && m_uHi == n.m_uHi;
114 #endif
115  }
116  bool operator != (const cUInt64& n) const
117  {
118  return !(*this == n);
119  }
120  bool operator == (UNIT_t n) const
121  {
122 #ifdef USE_INT64
123  return m_u == n;
124 #else
125  return m_uLo == n;
126 #endif
127  }
128  bool operator>(const cUInt64& n) const
129  {
130 #ifdef USE_INT64
131  return m_u > n.m_u;
132 #else
133  if (m_uHi == n.m_uHi)
134  return m_uLo > n.m_uLo;
135  else
136  return m_uHi > n.m_uHi;
137 #endif
138  }
139 
140  bool operator<(const cUInt64& n) const
141  {
142 #ifdef USE_INT64
143  return m_u < n.m_u;
144 #else
145  if (m_uHi == n.m_uHi)
146  return m_uLo < n.m_uLo;
147  else
148  return m_uHi < n.m_uHi;
149 #endif
150  }
151 
152  bool operator <=(const cUInt64& n) const
153  {
154 #ifdef USE_INT64
155  return m_u <= n.m_u;
156 #else
157  if (m_uHi == n.m_uHi)
158  return m_uLo <= n.m_uLo;
159  else
160  return m_uHi < n.m_uHi;
161 #endif
162  }
163 
164  template <typename TYPE>
165  TYPE get_Val() const
166  {
168 #ifdef USE_INT64
169  return (TYPE)m_u;
170 #else
171  ASSERT(0);
172  return 0;
173 #endif
174  }
175 
176  // Math Action Operators
177  void operator++()
178  {
179 #ifdef USE_INT64
180  m_u++;
181 #else
182  UNIT_t n = m_uLo;
183  m_uLo++;
184  if (m_uLo < n) // carry bit.
185  m_uHi++;
186 #endif
187  }
188  void operator ++(int)
189  {
191  ++* this;
192  }
193 
194  void operator--()
195  {
196 #ifdef USE_INT64
197  m_u--;
198 #else
199  UNIT_t n = m_uLo;
200  m_uLo--;
201  if (m_uLo > n) // carry bit.
202  m_uHi--;
203 #endif
204  }
205  void operator --(int)
206  {
208  --* this;
209  }
210 
212  {
213 #ifdef USE_INT64
214  m_u += n.m_u;
215 #else
216  m_uLo += n.m_uLo;
217  if (m_uLo < n.m_uLo) // carry bit.
218  m_uHi++;
219  m_uHi += n.m_uHi;
220 #endif
221  return *this;
222  }
224  {
225 #ifdef USE_INT64
226  m_u -= n.m_u;
227 #else
228  ASSERT(0);
229 #endif
230  return *this;
231  }
232  cUInt64& operator *=(const cUInt64& x)
233  {
234  cUInt64 ans;
235 #ifdef USE_INT64
236  m_u *= x.m_u;
237 #else
238  ASSERT(0);
239 #endif
240  return *this;
241  }
242  cUInt64 operator *(const cUInt64& x) const
243  {
244  cUInt64 ans;
245 #ifdef USE_INT64
246  ans.m_u = m_u * x.m_u;
247 #else
248  ASSERT(0);
249 #endif
250  return ans;
251  }
252  void operator %=(const cUInt64& x)
253  {
255 #ifdef USE_INT64
256  m_u %= x.m_u;
257 #else
258  ASSERT(0);
259 #endif
260  }
261 
262  // Bit Action Operators
263  void SetBit(BIT_ENUM_t uiBit)
264  {
265 #ifdef USE_INT64
266  m_u |= ((UNIT_t)1) << uiBit;
267 #else
268  if (uiBit < k_UNIT_BITS)
269  {
270  m_uLo |= ((UNIT_t)1) << uiBit;
271  }
272  else
273  {
274  m_uHi |= ((UNIT_t)1) << (uiBit - k_UNIT_BITS);
275  }
276 #endif
277  }
278 
280  {
281 #ifdef USE_INT64
282  m_u |= n.m_u;
283 #else
284  m_uLo |= n.m_uLo;
285  m_uHi |= n.m_uHi;
286 #endif
287  return *this;
288  }
289 
291  {
292 #ifdef USE_INT64
293  m_u &= n.m_u;
294 #else
295  m_uLo &= n.m_uLo;
296  m_uHi &= n.m_uHi;
297 #endif
298  return *this;
299  }
300 
302  {
303 #ifdef USE_INT64
304  m_u ^= n.m_u;
305 #else
306  m_uLo ^= n.m_uLo;
307  m_uHi ^= n.m_uHi;
308 #endif
309  return *this;
310  }
311 
313  {
314 #ifdef USE_INT64
315  m_u <<= uiBits;
316 #else
317  if (uiBits < k_UNIT_BITS)
318  {
319  (m_uHi <<= uiBits) |= (m_uLo >> (k_UNIT_BITS - uiBits));
320  m_uLo <<= uiBits;
321  }
322  else
323  {
324  m_uHi = m_uLo << (uiBits - k_UNIT_BITS);
325  m_uLo = 0;
326  }
327 #endif
328  return *this;
329  }
330 
332  {
333 #ifdef USE_INT64
334  m_u >>= uiBits;
335 #else
336  if (uiBits < k_UNIT_BITS)
337  {
338  (m_uLo >>= uiBits) |= (m_uHi << (k_UNIT_BITS - uiBits));
339  m_uHi >>= uiBits;
340  }
341  else
342  {
343  m_uLo = m_uHi >> (uiBits - k_UNIT_BITS);
344  m_uHi = 0;
345  }
346 #endif
347  return *this;
348  }
349 
350  StrLen_t GetStr(char* pszOut, StrLen_t iOutMax, RADIX_t nBaseRadix = 10) const;
351  cString GetStr(RADIX_t nBaseRadix = 10) const;
352  bool SetStr(const char* pszVal, RADIX_t nBaseRadix = 10, const char** ppszEnd = (const char**) nullptr);
353 
354  BIT_ENUM_t get_Highest1Bit() const;
355  HRESULT SetRandomBits(BIT_ENUM_t nBits);
356  void SetPowerMod(const cUInt64& base, const cUInt64& exponent, const cUInt64& modulus);
357 
358  bool isPrime() const;
359  int SetRandomPrime(BIT_ENUM_t nBits, cThreadState* pCancel = nullptr);
360  void OpBitShiftLeft1(UNIT_t nBitMask);
361 
362  static void GRAYCALL Divide(const cUInt64& dividend, const cUInt64& divisor, OUT cUInt64& quotient, OUT cUInt64& remainder);
363  static void GRAYCALL EuclideanAlgorithm(const cUInt64& x, const cUInt64& y, OUT cUInt64& a, OUT cUInt64& b, OUT cUInt64& g);
364 
366  };
367 
368 #pragma pack(pop)
369 
370  inline cUInt64 operator+(const cUInt64& roUI64_1, const cUInt64& roUI64_2)
371  {
372  cUInt64 temp = roUI64_1;
373  temp += roUI64_2;
374  return temp;
375  }
376 
377  inline cUInt64 operator|(const cUInt64& roUI64_1, const cUInt64& roUI64_2)
378  {
379  cUInt64 temp = roUI64_1;
380  temp |= roUI64_2;
381  return temp;
382  }
383 
384  inline cUInt64 operator&(const cUInt64& roUI64_1, const cUInt64& roUI64_2)
385  {
386  cUInt64 temp = roUI64_1;
387  temp &= roUI64_2;
388  return temp;
389  }
390 
391  inline cUInt64 operator^(const cUInt64& roUI64_1, const cUInt64& roUI64_2)
392  {
393  cUInt64 temp = roUI64_1;
394  temp ^= roUI64_2;
395  return temp;
396  }
397 
398  inline cUInt64 operator<<(const cUInt64& n, BIT_ENUM_t uiBits)
399  {
400  cUInt64 temp = n;
401  temp <<= uiBits;
402  return temp;
403  }
404 
405  inline cUInt64 operator>>(const cUInt64& n, BIT_ENUM_t uiBits)
406  {
407  cUInt64 temp = n;
408  temp >>= uiBits;
409  return temp;
410  }
411 };
412 
413 #endif
#define GRAYCORE_LINK
Definition: GrayCore.h:47
#define GRAYCALL
declare calling convention for static functions so everyone knows the arg passing scheme....
Definition: GrayCore.h:36
#define CATTR_PACKED
Definition: GrayCore.h:87
Using X files without the sources and the makefile How to use you just create a debug directory e g
Definition: Readme.txt:21
#define TYPE
Definition: StrT.cpp:38
INT32 HRESULT
_WIN32 style error codes. INT32
Definition: SysTypes.h:465
#define ASSERT(exp)
Definition: cDebugAssert.h:87
#define UNITTEST_FRIEND(n)
Define this in the class body to be unit tested. Allow the unit test to access private/protected stuf...
Definition: cUnitTestDecl.h:17
Definition: cThreadLock.h:137
Definition: cUInt64.h:23
bool isOdd() const
Definition: cUInt64.h:86
void operator--()
Definition: cUInt64.h:194
bool operator<(const cUInt64 &n) const
Definition: cUInt64.h:140
bool operator>(const cUInt64 &n) const
Definition: cUInt64.h:128
cUInt64(UNIT_t n)
Definition: cUInt64.h:63
bool IsSet(BIT_ENUM_t nBit) const
Definition: cUInt64.h:94
cUInt64 & operator>>=(BIT_ENUM_t uiBits)
Definition: cUInt64.h:331
cUInt64 & operator<<=(BIT_ENUM_t uiBits)
Definition: cUInt64.h:312
cUInt64()
Definition: cUInt64.h:54
void operator++()
Definition: cUInt64.h:177
cUInt64 & operator&=(const cUInt64 &n)
Definition: cUInt64.h:290
cUInt64 & operator+=(const cUInt64 &n)
Definition: cUInt64.h:211
cUInt64(const char *pszVal, RADIX_t n=10)
Definition: cUInt64.h:72
cUInt64 & operator^=(const cUInt64 &n)
Definition: cUInt64.h:301
TYPE get_Val() const
Definition: cUInt64.h:165
cUInt64 & operator-=(const cUInt64 &n)
Definition: cUInt64.h:223
cUInt64 & operator|=(const cUInt64 &n)
Definition: cUInt64.h:279
bool isZero() const
Definition: cUInt64.h:78
void SetBit(BIT_ENUM_t uiBit)
Definition: cUInt64.h:263
UINT64 UNIT_t
Definition: cUInt64.h:34
cVecT2< TYPE > operator*(const TYPE nVal, const cVecT2< TYPE > &v2)
Definition: cVecT.h:522
< The main namespace for all Core functions.
Definition: GrayCore.cpp:14
cUInt64 operator|(const cUInt64 &roUI64_1, const cUInt64 &roUI64_2)
Definition: cUInt64.h:377
int StrLen_t
the length of a string in chars (bytes for UTF8, wchar_t for UNICODE). or offset in characters....
Definition: StrConst.h:32
void operator>>(cArchive &ar, cStringT< _TYPE_CH > &pOb)
Definition: cString.h:678
cUInt64 operator^(const cUInt64 &roUI64_1, const cUInt64 &roUI64_2)
Definition: cUInt64.h:391
WORD RADIX_t
Base for convert of numbers to strings. e.g. 10 base vs 16 base hex numbers.
Definition: StrChar.h:27
unsigned int BIT_ENUM_t
Enumerate number of bits or address a single bit in some array of bits.
Definition: cBits.h:20
cStringA operator+(const char *pStr1, const cStringA &s2)
Definition: cString.h:642
void operator<<(cArchive &ar, const cStringT< _TYPE_CH > &pOb)
Definition: cString.h:680
bool operator!=(const cTimeDouble &dt1, const cTimeDouble &dt2)
Definition: cTimeDouble.h:254
bool operator==(const cTimeDouble &dt1, const cTimeDouble &dt2)
Definition: cTimeDouble.h:250
bool operator<=(const cTimeDouble &dt1, const cTimeDouble &dt2)
Definition: cTimeDouble.h:238
cUInt64 operator&(const cUInt64 &roUI64_1, const cUInt64 &roUI64_2)
Definition: cUInt64.h:384
static constexpr bool IsSet(TYPE nVal, BIT_ENUM_t nBit) noexcept
Definition: cBits.h:77