intutil.h 22.9 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
// [AsmJit]
// Complete x86/x64 JIT and Remote Assembler for C++.
//
// [License]
// Zlib - See LICENSE.md file in the package.

// [Guard]
#ifndef _ASMJIT_BASE_INTUTIL_H
#define _ASMJIT_BASE_INTUTIL_H

// [Dependencies - AsmJit]
#include "../base/globals.h"

14
15
16
#if defined(_MSC_VER) && _MSC_VER >= 1400
# include <intrin.h>
# pragma intrinsic(_BitScanForward)
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#endif // ASMJIT_OS_WINDOWS

// [Api-Begin]
#include "../apibegin.h"

namespace asmjit {

//! \addtogroup asmjit_base_util
//! \{

// ============================================================================
// [asmjit::IntTraits]
// ============================================================================

//! \internal
template<typename T>
struct IntTraits {
  enum {
    kIsSigned = static_cast<T>(~static_cast<T>(0)) < static_cast<T>(0),
    kIsUnsigned = !kIsSigned,

    kIs8Bit = sizeof(T) == 1,
    kIs16Bit = sizeof(T) == 2,
    kIs32Bit = sizeof(T) == 4,
    kIs64Bit = sizeof(T) == 8,

    kIsIntPtr = sizeof(T) == sizeof(intptr_t)
  };
};

47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// \internal
template<size_t Size, int IsSigned>
struct AsInt_ { typedef int64_t Int; };

template<> struct AsInt_<1, 0> { typedef int Int; };
template<> struct AsInt_<1, 1> { typedef int Int; };
template<> struct AsInt_<2, 0> { typedef int Int; };
template<> struct AsInt_<2, 1> { typedef int Int; };
template<> struct AsInt_<4, 1> { typedef int Int; };

// \internal
//
// Map an integer `T` to an `int` or `int64_t`, depending on the type. Used
// internally by AsmJit to dispatch an argument of arbitrary integer type into
// a function that accepts either `int` or `int64_t`.
template<typename T>
struct AsInt {
  typedef typename AsInt_<sizeof(T), IntTraits<T>::kIsSigned>::Int Int;
};

template<typename T>
ASMJIT_INLINE typename AsInt<T>::Int asInt(T value) {
  return static_cast<typename AsInt<T>::Int>(value);
}

72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// ============================================================================
// [asmjit::IntUtil]
// ============================================================================

//! Integer utilities.
struct IntUtil {
  // --------------------------------------------------------------------------
  // [Float <-> Int]
  // --------------------------------------------------------------------------

  //! \internal
  union Float {
    int32_t i;
    float f;
  };

  //! \internal
  union Double {
    int64_t i;
    double d;
  };

  //! Bit-cast `float` to 32-bit integer.
  static ASMJIT_INLINE int32_t floatAsInt(float f) { Float m; m.f = f; return m.i; }
  //! Bit-cast 32-bit integer to `float`.
  static ASMJIT_INLINE float intAsFloat(int32_t i) { Float m; m.i = i; return m.f; }

  //! Bit-cast `double` to 64-bit integer.
  static ASMJIT_INLINE int64_t doubleAsInt(double d) { Double m; m.d = d; return m.i; }
  //! Bit-cast 64-bit integer to `double`.
  static ASMJIT_INLINE double intAsDouble(int64_t i) { Double m; m.i = i; return m.d; }

  // --------------------------------------------------------------------------
  // [AsmJit - Pack / Unpack]
  // --------------------------------------------------------------------------

  //! Pack two 8-bit integer and one 16-bit integer into a 32-bit integer as it
  //! is an array of `{u0,u1,w2}`.
  static ASMJIT_INLINE uint32_t pack32_2x8_1x16(uint32_t u0, uint32_t u1, uint32_t w2) {
111
#if defined(ASMJIT_ARCH_LE)
112
113
114
    return u0 + (u1 << 8) + (w2 << 16);
#else
    return (u0 << 24) + (u1 << 16) + (w2);
115
#endif
116
117
118
119
  }

  //! Pack four 8-bit integer into a 32-bit integer as it is an array of `{u0,u1,u2,u3}`.
  static ASMJIT_INLINE uint32_t pack32_4x8(uint32_t u0, uint32_t u1, uint32_t u2, uint32_t u3) {
120
#if defined(ASMJIT_ARCH_LE)
121
122
123
    return u0 + (u1 << 8) + (u2 << 16) + (u3 << 24);
#else
    return (u0 << 24) + (u1 << 16) + (u2 << 8) + u3;
124
#endif
125
126
127
128
  }

  //! Pack two 32-bit integer into a 64-bit integer as it is an array of `{u0,u1}`.
  static ASMJIT_INLINE uint64_t pack64_2x32(uint32_t u0, uint32_t u1) {
129
#if defined(ASMJIT_ARCH_LE)
130
131
132
    return (static_cast<uint64_t>(u1) << 32) + u0;
#else
    return (static_cast<uint64_t>(u0) << 32) + u1;
133
#endif
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
  }

  // --------------------------------------------------------------------------
  // [AsmJit - Min/Max]
  // --------------------------------------------------------------------------

  // NOTE: Because some environments declare min() and max() as macros, it has
  // been decided to use different name so we never collide with them.

  //! Get minimum value of `a` and `b`.
  template<typename T>
  static ASMJIT_INLINE T iMin(const T& a, const T& b) { return a < b ? a : b; }

  //! Get maximum value of `a` and `b`.
  template<typename T>
  static ASMJIT_INLINE T iMax(const T& a, const T& b) { return a > b ? a : b; }

  // --------------------------------------------------------------------------
  // [AsmJit - MaxUInt]
  // --------------------------------------------------------------------------

  //! Get maximum unsigned value of `T`.
  template<typename T>
  static ASMJIT_INLINE T maxUInt() { return ~T(0); }

  // --------------------------------------------------------------------------
  // [AsmJit - InInterval]
  // --------------------------------------------------------------------------

  //! Get whether `x` is greater or equal than `start` and less or equal than `end`.
  template<typename T>
  static ASMJIT_INLINE bool inInterval(const T& x, const T& start, const T& end) {
    return x >= start && x <= end;
  }

  // --------------------------------------------------------------------------
  // [AsmJit - IsInt/IsUInt]
  // --------------------------------------------------------------------------

  //! Get whether the given integer `x` can be casted to 8-bit signed integer.
  template<typename T>
  static ASMJIT_INLINE bool isInt8(T x) {
    if (IntTraits<T>::kIsSigned)
      return sizeof(T) <= sizeof(int8_t) ? true : x >= T(-128) && x <= T(127);
    else
      return x <= T(127);
  }

  //! Get whether the given integer `x` can be casted to 8-bit unsigned integer.
  template<typename T>
  static ASMJIT_INLINE bool isUInt8(T x) {
    if (IntTraits<T>::kIsSigned)
      return x >= T(0) && (sizeof(T) <= sizeof(uint8_t) ? true : x <= T(255));
    else
      return sizeof(T) <= sizeof(uint8_t) ? true : x <= T(255);
  }

  //! Get whether the given integer `x` can be casted to 16-bit signed integer.
  template<typename T>
  static ASMJIT_INLINE bool isInt16(T x) {
    if (IntTraits<T>::kIsSigned)
      return sizeof(T) <= sizeof(int16_t) ? true : x >= T(-32768) && x <= T(32767);
    else
      return x >= T(0) && (sizeof(T) <= sizeof(int16_t) ? true : x <= T(32767));
  }

  //! Get whether the given integer `x` can be casted to 16-bit unsigned integer.
  template<typename T>
  static ASMJIT_INLINE bool isUInt16(T x) {
    if (IntTraits<T>::kIsSigned)
      return x >= T(0) && (sizeof(T) <= sizeof(uint16_t) ? true : x <= T(65535));
    else
      return sizeof(T) <= sizeof(uint16_t) ? true : x <= T(65535);
  }

  //! Get whether the given integer `x` can be casted to 32-bit signed integer.
  template<typename T>
  static ASMJIT_INLINE bool isInt32(T x) {
    if (IntTraits<T>::kIsSigned)
      return sizeof(T) <= sizeof(int32_t) ? true : x >= T(-2147483647) - 1 && x <= T(2147483647);
    else
      return x >= T(0) && (sizeof(T) <= sizeof(int32_t) ? true : x <= T(2147483647));
  }

  //! Get whether the given integer `x` can be casted to 32-bit unsigned integer.
  template<typename T>
  static ASMJIT_INLINE bool isUInt32(T x) {
    if (IntTraits<T>::kIsSigned)
      return x >= T(0) && (sizeof(T) <= sizeof(uint32_t) ? true : x <= T(4294967295U));
    else
      return sizeof(T) <= sizeof(uint32_t) ? true : x <= T(4294967295U);
  }

  // --------------------------------------------------------------------------
  // [AsmJit - IsPowerOf2]
  // --------------------------------------------------------------------------

  //! Get whether the `n` value is a power of two (only one bit is set).
  template<typename T>
  static ASMJIT_INLINE bool isPowerOf2(T n) {
    return n != 0 && (n & (n - 1)) == 0;
  }

  // --------------------------------------------------------------------------
  // [AsmJit - Mask]
  // --------------------------------------------------------------------------

  //! Generate a bit-mask that has `x` bit set.
  static ASMJIT_INLINE uint32_t mask(uint32_t x) {
    ASMJIT_ASSERT(x < 32);
    return (1U << x);
  }

  //! Generate a bit-mask that has `x0` and `x1` bits set.
  static ASMJIT_INLINE uint32_t mask(uint32_t x0, uint32_t x1) {
    return mask(x0) | mask(x1);
  }

  //! Generate a bit-mask that has `x0`, `x1` and `x2` bits set.
  static ASMJIT_INLINE uint32_t mask(uint32_t x0, uint32_t x1, uint32_t x2) {
    return mask(x0) | mask(x1) | mask(x2);
  }

  //! Generate a bit-mask that has `x0`, `x1`, `x2` and `x3` bits set.
  static ASMJIT_INLINE uint32_t mask(uint32_t x0, uint32_t x1, uint32_t x2, uint32_t x3) {
    return mask(x0) | mask(x1) | mask(x2) | mask(x3);
  }

  //! Generate a bit-mask that has `x0`, `x1`, `x2`, `x3` and `x4` bits set.
  static ASMJIT_INLINE uint32_t mask(uint32_t x0, uint32_t x1, uint32_t x2, uint32_t x3, uint32_t x4) {
    return mask(x0) | mask(x1) | mask(x2) | mask(x3) |
           mask(x4) ;
  }

  //! Generate a bit-mask that has `x0`, `x1`, `x2`, `x3`, `x4` and `x5` bits set.
  static ASMJIT_INLINE uint32_t mask(uint32_t x0, uint32_t x1, uint32_t x2, uint32_t x3, uint32_t x4, uint32_t x5) {
    return mask(x0) | mask(x1) | mask(x2) | mask(x3) |
           mask(x4) | mask(x5) ;
  }

  //! Generate a bit-mask that has `x0`, `x1`, `x2`, `x3`, `x4`, `x5` and `x6` bits set.
  static ASMJIT_INLINE uint32_t mask(uint32_t x0, uint32_t x1, uint32_t x2, uint32_t x3, uint32_t x4, uint32_t x5, uint32_t x6) {
    return mask(x0) | mask(x1) | mask(x2) | mask(x3) |
           mask(x4) | mask(x5) | mask(x6) ;
  }

  //! Generate a bit-mask that has `x0`, `x1`, `x2`, `x3`, `x4`, `x5`, `x6` and `x7` bits set.
  static ASMJIT_INLINE uint32_t mask(uint32_t x0, uint32_t x1, uint32_t x2, uint32_t x3, uint32_t x4, uint32_t x5, uint32_t x6, uint32_t x7) {
    return mask(x0) | mask(x1) | mask(x2) | mask(x3) |
           mask(x4) | mask(x5) | mask(x6) | mask(x7) ;
  }

  //! Generate a bit-mask that has `x0`, `x1`, `x2`, `x3`, `x4`, `x5`, `x6`, `x7` and `x8` bits set.
  static ASMJIT_INLINE uint32_t mask(uint32_t x0, uint32_t x1, uint32_t x2, uint32_t x3, uint32_t x4, uint32_t x5, uint32_t x6, uint32_t x7, uint32_t x8) {
    return mask(x0) | mask(x1) | mask(x2) | mask(x3) |
           mask(x4) | mask(x5) | mask(x6) | mask(x7) |
           mask(x8) ;
  }

  //! Generate a bit-mask that has `x0`, `x1`, `x2`, `x3`, `x4`, `x5`, `x6`, `x7`, `x8` and `x9` bits set.
  static ASMJIT_INLINE uint32_t mask(uint32_t x0, uint32_t x1, uint32_t x2, uint32_t x3, uint32_t x4, uint32_t x5, uint32_t x6, uint32_t x7, uint32_t x8, uint32_t x9) {
    return mask(x0) | mask(x1) | mask(x2) | mask(x3) |
           mask(x4) | mask(x5) | mask(x6) | mask(x7) |
           mask(x8) | mask(x9) ;
  }

  // --------------------------------------------------------------------------
  // [AsmJit - Bits]
  // --------------------------------------------------------------------------

  //! Generate a bit-mask that has `x` most significant bits set.
  static ASMJIT_INLINE uint32_t bits(uint32_t x) {
    // Shifting more bits that the type has has undefined behavior. Everything
    // we need is that application shouldn't crash because of that, but the
    // content of register after shift is not defined. So in case that the
    // requested shift is too large for the type we correct this undefined
    // behavior by setting all bits to ones (this is why we generate an overflow
    // mask).
    uint32_t overflow = static_cast<uint32_t>(
      -static_cast<int32_t>(x >= sizeof(uint32_t) * 8));

    return ((static_cast<uint32_t>(1) << x) - 1U) | overflow;
  }

  // --------------------------------------------------------------------------
  // [AsmJit - HasBit]
  // --------------------------------------------------------------------------

  //! Get whether `x` has bit `n` set.
  static ASMJIT_INLINE bool hasBit(uint32_t x, uint32_t n) {
    return static_cast<bool>((x >> n) & 0x1);
  }

  // --------------------------------------------------------------------------
  // [AsmJit - BitCount]
  // --------------------------------------------------------------------------

  //! Get count of bits in `x`.
  //!
  //! Taken from http://graphics.stanford.edu/~seander/bithacks.html .
  static ASMJIT_INLINE uint32_t bitCount(uint32_t x) {
    x = x - ((x >> 1) & 0x55555555U);
    x = (x & 0x33333333U) + ((x >> 2) & 0x33333333U);
    return (((x + (x >> 4)) & 0x0F0F0F0FU) * 0x01010101U) >> 24;
  }

  // --------------------------------------------------------------------------
  // [AsmJit - FindFirstBit]
  // --------------------------------------------------------------------------

  //! \internal
  static ASMJIT_INLINE uint32_t findFirstBitSlow(uint32_t mask) {
    // This is a reference (slow) implementation of findFirstBit(), used when
    // we don't have compiler support for this task. The implementation speed
    // has been improved to check for 2 bits per iteration.
    uint32_t i = 1;

    while (mask != 0) {
      uint32_t two = mask & 0x3;
      if (two != 0x0)
        return i - (two & 0x1);

      i += 2;
      mask >>= 2;
    }

    return 0xFFFFFFFFU;
  }

  //! Find a first bit in `mask`.
  static ASMJIT_INLINE uint32_t findFirstBit(uint32_t mask) {
365
#if defined(_MSC_VER) && _MSC_VER >= 1400
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
    DWORD i;
    if (_BitScanForward(&i, mask)) {
      ASMJIT_ASSERT(findFirstBitSlow(mask) == i);
      return static_cast<uint32_t>(i);
    }
    return 0xFFFFFFFFU;
#else
    return findFirstBitSlow(mask);
#endif
  }

  // --------------------------------------------------------------------------
  // [AsmJit - Misc]
  // --------------------------------------------------------------------------

  static ASMJIT_INLINE uint32_t keepNOnesFromRight(uint32_t mask, uint32_t nBits) {
    uint32_t m = 0x1;

    do {
      nBits -= (mask & m) == 0;
      m <<= 1;
      if (nBits == 0) {
        m -= 1;
        mask &= m;
        break;
      }
    } while (m);

    return mask;
  }

  static ASMJIT_INLINE uint32_t indexNOnesFromRight(uint8_t* dst, uint32_t mask, uint32_t nBits) {
    uint32_t totalBits = nBits;
    uint8_t i = 0;
    uint32_t m = 0x1;

    do {
      if (mask & m) {
        *dst++ = i;
        if (--nBits == 0)
          break;
      }

      m <<= 1;
      i++;
    } while (m);

    return totalBits - nBits;
  }

  // --------------------------------------------------------------------------
  // [AsmJit - Alignment]
  // --------------------------------------------------------------------------

  template<typename T>
  static ASMJIT_INLINE bool isAligned(T base, T alignment) {
    return (base % alignment) == 0;
  }

  //! Align `base` to `alignment`.
  template<typename T>
  static ASMJIT_INLINE T alignTo(T base, T alignment) {
    return (base + (alignment - 1)) & ~(alignment - 1);
  }

  template<typename T>
  static ASMJIT_INLINE T alignToPowerOf2(T base) {
    // Implementation is from "Hacker's Delight" by Henry S. Warren, Jr.
    base -= 1;

#if defined(_MSC_VER)
# pragma warning(push)
# pragma warning(disable: 4293)
#endif // _MSC_VER

    base = base | (base >> 1);
    base = base | (base >> 2);
    base = base | (base >> 4);

    // 8/16/32 constants are multiplied by the condition to prevent a compiler
    // complaining about the 'shift count >= type width' (GCC).
    if (sizeof(T) >= 2) base = base | (base >> ( 8 * (sizeof(T) >= 2))); // Base >>  8.
    if (sizeof(T) >= 4) base = base | (base >> (16 * (sizeof(T) >= 4))); // Base >> 16.
    if (sizeof(T) >= 8) base = base | (base >> (32 * (sizeof(T) >= 8))); // Base >> 32.

#if defined(_MSC_VER)
# pragma warning(pop)
#endif // _MSC_VER

    return base + 1;
  }

  //! Get delta required to align `base` to `alignment`.
  template<typename T>
  static ASMJIT_INLINE T deltaTo(T base, T alignment) {
    return alignTo(base, alignment) - base;
  }
};

// ============================================================================
// [asmjit::UInt64]
// ============================================================================

union UInt64 {
  // --------------------------------------------------------------------------
  // [Construction / Destruction]
  // --------------------------------------------------------------------------

  ASMJIT_INLINE UInt64 fromUInt64(uint64_t val) {
    UInt64 data;
    data.setUInt64(val);
    return data;
  }

  ASMJIT_INLINE UInt64 fromUInt64(const UInt64& val) {
    UInt64 data;
    data.setUInt64(val);
    return data;
  }

  // --------------------------------------------------------------------------
  // [Reset]
  // --------------------------------------------------------------------------

  ASMJIT_INLINE void reset() {
    if (kArchHost64Bit) {
      u64 = 0;
    }
    else {
      u32[0] = 0;
      u32[1] = 0;
    }
  }

  // --------------------------------------------------------------------------
  // [Accessors]
  // --------------------------------------------------------------------------

  ASMJIT_INLINE uint64_t getUInt64() const {
    return u64;
  }

  ASMJIT_INLINE UInt64& setUInt64(uint64_t val) {
    u64 = val;
    return *this;
  }

  ASMJIT_INLINE UInt64& setUInt64(const UInt64& val) {
    if (kArchHost64Bit) {
      u64 = val.u64;
    }
    else {
      u32[0] = val.u32[0];
      u32[1] = val.u32[1];
    }
    return *this;
  }

  ASMJIT_INLINE UInt64& setPacked_2x32(uint32_t u0, uint32_t u1) {
    if (kArchHost64Bit) {
      u64 = IntUtil::pack64_2x32(u0, u1);
    }
    else {
      u32[0] = u0;
      u32[1] = u1;
    }
    return *this;
  }

  // --------------------------------------------------------------------------
  // [Add]
  // --------------------------------------------------------------------------

  ASMJIT_INLINE UInt64& add(uint64_t val) {
    u64 += val;
    return *this;
  }

  ASMJIT_INLINE UInt64& add(const UInt64& val) {
    if (kArchHost64Bit) {
      u64 += val.u64;
    }
    else {
      u32[0] += val.u32[0];
      u32[1] += val.u32[1];
    }
    return *this;
  }

  // --------------------------------------------------------------------------
  // [Sub]
  // --------------------------------------------------------------------------

  ASMJIT_INLINE UInt64& sub(uint64_t val) {
    u64 -= val;
    return *this;
  }

  ASMJIT_INLINE UInt64& sub(const UInt64& val) {
    if (kArchHost64Bit) {
      u64 -= val.u64;
    }
    else {
      u32[0] -= val.u32[0];
      u32[1] -= val.u32[1];
    }
    return *this;
  }

  // --------------------------------------------------------------------------
  // [And]
  // --------------------------------------------------------------------------

  ASMJIT_INLINE UInt64& and_(uint64_t val) {
    u64 &= val;
    return *this;
  }

  ASMJIT_INLINE UInt64& and_(const UInt64& val) {
    if (kArchHost64Bit) {
      u64 &= val.u64;
    }
    else {
      u32[0] &= val.u32[0];
      u32[1] &= val.u32[1];
    }
    return *this;
  }

595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
  // --------------------------------------------------------------------------
  // [AndNot]
  // --------------------------------------------------------------------------

  ASMJIT_INLINE UInt64& andNot(uint64_t val) {
    u64 &= ~val;
    return *this;
  }

  ASMJIT_INLINE UInt64& andNot(const UInt64& val) {
    if (kArchHost64Bit) {
      u64 &= ~val.u64;
    }
    else {
      u32[0] &= ~val.u32[0];
      u32[1] &= ~val.u32[1];
    }
    return *this;
  }

615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
  // --------------------------------------------------------------------------
  // [Or]
  // --------------------------------------------------------------------------

  ASMJIT_INLINE UInt64& or_(uint64_t val) {
    u64 |= val;
    return *this;
  }

  ASMJIT_INLINE UInt64& or_(const UInt64& val) {
    if (kArchHost64Bit) {
      u64 |= val.u64;
    }
    else {
      u32[0] |= val.u32[0];
      u32[1] |= val.u32[1];
    }
    return *this;
  }

  // --------------------------------------------------------------------------
  // [Xor]
  // --------------------------------------------------------------------------

  ASMJIT_INLINE UInt64& xor_(uint64_t val) {
    u64 ^= val;
    return *this;
  }

  ASMJIT_INLINE UInt64& xor_(const UInt64& val) {
    if (kArchHost64Bit) {
      u64 ^= val.u64;
    }
    else {
      u32[0] ^= val.u32[0];
      u32[1] ^= val.u32[1];
    }
    return *this;
  }

  // --------------------------------------------------------------------------
  // [Eq]
  // --------------------------------------------------------------------------

  ASMJIT_INLINE bool isZero() const {
    return kArchHost64Bit ? u64 == 0 : (u32[0] | u32[1]) == 0;
  }

  ASMJIT_INLINE bool isNonZero() const {
    return kArchHost64Bit ? u64 != 0 : (u32[0] | u32[1]) != 0;
  }

  ASMJIT_INLINE bool eq(uint64_t val) const {
    return u64 == val;
  }

  ASMJIT_INLINE bool eq(const UInt64& val) const {
    return kArchHost64Bit ? u64 == val.u64 : (u32[0] == val.u32[0]) & (u32[1] == val.u32[1]);
  }

  // --------------------------------------------------------------------------
  // [Operator Overload]
  // --------------------------------------------------------------------------

  ASMJIT_INLINE UInt64& operator+=(uint64_t val) { return add(val); }
  ASMJIT_INLINE UInt64& operator+=(const UInt64& val) { return add(val); }

  ASMJIT_INLINE UInt64& operator-=(uint64_t val) { return sub(val); }
  ASMJIT_INLINE UInt64& operator-=(const UInt64& val) { return sub(val); }

  ASMJIT_INLINE UInt64& operator&=(uint64_t val) { return and_(val); }
  ASMJIT_INLINE UInt64& operator&=(const UInt64& val) { return and_(val); }

  ASMJIT_INLINE UInt64& operator|=(uint64_t val) { return or_(val); }
  ASMJIT_INLINE UInt64& operator|=(const UInt64& val) { return or_(val); }

  ASMJIT_INLINE UInt64& operator^=(uint64_t val) { return xor_(val); }
  ASMJIT_INLINE UInt64& operator^=(const UInt64& val) { return xor_(val); }

  ASMJIT_INLINE bool operator==(uint64_t val) const { return eq(val); }
  ASMJIT_INLINE bool operator==(const UInt64& val) const { return eq(val); }

  ASMJIT_INLINE bool operator!=(uint64_t val) const { return !eq(val); }
  ASMJIT_INLINE bool operator!=(const UInt64& val) const { return !eq(val); }

  ASMJIT_INLINE bool operator<(uint64_t val) const { return u64 < val; }
  ASMJIT_INLINE bool operator<(const UInt64& val) const { return u64 < val.u64; }

  ASMJIT_INLINE bool operator<=(uint64_t val) const { return u64 <= val; }
  ASMJIT_INLINE bool operator<=(const UInt64& val) const { return u64 <= val.u64; }

  ASMJIT_INLINE bool operator>(uint64_t val) const { return u64 > val; }
  ASMJIT_INLINE bool operator>(const UInt64& val) const { return u64 > val.u64; }

  ASMJIT_INLINE bool operator>=(uint64_t val) const { return u64 >= val; }
  ASMJIT_INLINE bool operator>=(const UInt64& val) const { return u64 >= val.u64; }

  // --------------------------------------------------------------------------
  // [Members]
  // --------------------------------------------------------------------------

  uint64_t u64;

  uint32_t u32[2];
  uint16_t u16[4];
  uint8_t u8[8];

  struct {
723
#if defined(ASMJIT_ARCH_LE)
724
725
726
    uint32_t lo, hi;
#else
    uint32_t hi, lo;
727
#endif // ASMJIT_ARCH_LE
728
729
730
731
732
733
734
735
736
737
738
739
  };
};

//! \}

} // asmjit namespace

// [Api-End]
#include "../apiend.h"

// [Guard]
#endif // _ASMJIT_BASE_INTUTIL_H