openMSX
uint128.cc
Go to the documentation of this file.
1 #if defined __x86_64 && !defined _MSC_VER
2 
3 // nothing
4 
5 #else // __x86_64 && !_MSC_VER
6 
7 #include "uint128.hh"
8 
10 {
11  uint128 a = *this;
12  uint128 t = b;
13 
14  lo = 0;
15  hi = 0;
16  while (t != 0) {
17  if (t.lo & 1) {
18  *this += a;
19  }
20  a <<= 1;
21  t >>= 1;
22  }
23  return *this;
24 }
25 
26 uint128 uint128::div(const uint128& ds, uint128& remainder) const
27 {
28  uint128 dd = *this;
29 
30  uint128 r = 0;
31  uint128 q = 0;
32 
33  unsigned b = 127;
34  while (r < ds) {
35  r <<= 1;
36  if (dd.bit(b--)) {
37  r.lo |= 1;
38  }
39  }
40  ++b;
41 
42  while (true) {
43  if (r < ds) {
44  if (!(b--)) break;
45  r <<= 1;
46  if (dd.bit(b)) {
47  r.lo |= 1;
48  }
49  } else {
50  r -= ds;
51  q.setBit(b);
52  }
53  }
54  remainder = r;
55  return q;
56 }
57 
58 bool uint128::bit(unsigned n) const
59 {
60  if (n < 64) {
61  return (lo & (1ull << n)) != 0;
62  } else {
63  return (hi & (1ull << (n - 64))) != 0;
64  }
65 }
66 
67 void uint128::setBit(unsigned n)
68 {
69  if (n < 64) {
70  lo |= (1ull << n);
71  } else {
72  hi |= (1ull << (n - 64));
73  }
74 }
75 
76 #endif // __x86_64 && !_MSC_VER
Unsigned 128-bit integer type.
Definition: uint128.hh:21
uint128 & operator*=(const uint128 &b)
Definition: uint128.cc:9
TclObject t