INET Framework for OMNeT++/OMNEST
inet::Int128 Class Reference

#include <int128.h>

Public Member Functions

void set (const char *sz)
 
 Int128 ()
 
 Int128 (const Int128 &a)
 
 Int128 (const uint32_t &a)
 
 Int128 (const int32_t &a)
 
 Int128 (const uint64_t &a)
 
 Int128 (const int64_t &a)
 
 Int128 (const float a)
 
 Int128 (const double &a)
 
 Int128 (const long double &a)
 
 Int128 (const char *sz)
 
Int128operator= (const Int128 &other)
 
Int128operator= (const int32_t &a)
 
Int128operator= (const uint32_t &a)
 
Int128operator= (const int64_t &a)
 
Int128operator= (const uint64_t &a)
 
Int128operator= (const char *sz)
 
Int128operator= (const float &a)
 
Int128operator= (const double &a)
 
Int128operator= (const long double &a)
 
bool operator! () const
 
Int128 operator- () const
 
Int128 operator~ () const
 
Int128operator++ ()
 
Int128operator-- ()
 
Int128 operator++ (int)
 
Int128 operator-- (int)
 
Int128operator+= (const Int128 &b)
 
Int128operator*= (const Int128 &b)
 
Int128operator>>= (unsigned int n)
 
Int128operator<<= (unsigned int n)
 
Int128operator|= (const Int128 &b)
 
Int128operator&= (const Int128 &b)
 
Int128operator^= (const Int128 &b)
 
const Int128operator+ () const
 
Int128operator-= (const Int128 &b)
 
Int128operator/= (const Int128 &b)
 
Int128operator%= (const Int128 &b)
 
int toInt () const
 
int64_t toInt64 () const
 
const char * toString (uint32_t radix=10) const
 
float toFloat () const
 
double toDouble () const
 
long double toLongDouble () const
 
Int128 div (const Int128 &, Int128 &) const
 
bool bit (unsigned int n) const
 
void bit (unsigned int n, bool val)
 
 operator double ()
 
 operator int ()
 

Static Public Attributes

static const Int128 INT128_MAX
 
static const Int128 INT128_MIN
 

Private Member Functions

 Int128 (const uint64_t &a, const int64_t &b)
 

Private Attributes

uint64_t lo
 
int64_t hi
 

Friends

bool operator< (const Int128 &, const Int128 &)
 
bool operator== (const Int128 &, const Int128 &)
 
bool operator|| (const Int128 &, const Int128 &)
 
bool operator&& (const Int128 &, const Int128 &)
 

Constructor & Destructor Documentation

inet::Int128::Int128 ( )
inline

Referenced by div(), operator-(), and set().

61 {}
inet::Int128::Int128 ( const Int128 a)
inline
62 : lo(a.lo), hi(a.hi) {}
int64_t hi
Definition: int128.h:43
uint64_t lo
Definition: int128.h:42
inet::Int128::Int128 ( const uint32_t &  a)
inline
64 : lo(a), hi(0ll) {}
int64_t hi
Definition: int128.h:43
uint64_t lo
Definition: int128.h:42
inet::Int128::Int128 ( const int32_t &  a)
inline
65  : lo(a), hi(0ll)
66  {
67  if (a < 0) hi = -1ll;
68  }
int64_t hi
Definition: int128.h:43
uint64_t lo
Definition: int128.h:42
inet::Int128::Int128 ( const uint64_t &  a)
inline
70 : lo(a), hi(0ll) {}
int64_t hi
Definition: int128.h:43
uint64_t lo
Definition: int128.h:42
inet::Int128::Int128 ( const int64_t &  a)
inline
71  : lo(a), hi(0ll)
72  {
73  if (a < 0) hi = -1ll;
74  }
int64_t hi
Definition: int128.h:43
uint64_t lo
Definition: int128.h:42
inet::Int128::Int128 ( const float  a)
112  : lo((uint64_t)fmodf(a, 18446744073709551616.0f)),
113  hi((int64_t)(a / 18446744073709551616.0f)) {}
int64_t hi
Definition: int128.h:43
uint64_t lo
Definition: int128.h:42
inet::Int128::Int128 ( const double &  a)
116  : lo((uint64_t)fmod(a, 18446744073709551616.0)),
117  hi((int64_t)(a / 18446744073709551616.0)) {}
int64_t hi
Definition: int128.h:43
uint64_t lo
Definition: int128.h:42
inet::Int128::Int128 ( const long double &  a)
120  : lo((uint64_t)fmodl(a, 18446744073709551616.0l)),
121  hi((int64_t)(a / 18446744073709551616.0l)) {}
int64_t hi
Definition: int128.h:43
uint64_t lo
Definition: int128.h:42
inet::Int128::Int128 ( const char *  sz)
inline
80 { set(sz); }
inet::Int128::Int128 ( const uint64_t &  a,
const int64_t &  b 
)
inlineprivate
100  : lo(a), hi(b) {}
int64_t hi
Definition: int128.h:43
uint64_t lo
Definition: int128.h:42
value< double, units::m > b
Definition: Units.h:1054

Member Function Documentation

bool inet::Int128::bit ( unsigned int  n) const

Referenced by div().

292 {
293  if (n >= 128)
294  return hi < 0;
295 
296  if (n < 64)
297  return lo & (1ull << n);
298  else
299  return hi & (1ull << (n - 64));
300 }
int64_t hi
Definition: int128.h:43
uint64_t lo
Definition: int128.h:42
void inet::Int128::bit ( unsigned int  n,
bool  val 
)
303 {
304  if (n >= 128)
305  return;
306 
307  if (val) {
308  if (n < 64)
309  lo |= (1ull << n);
310  else
311  hi |= (1ull << (n - 64));
312  }
313  else {
314  if (n < 64)
315  lo &= ~(1ull << n);
316  else
317  hi &= ~(1ull << (n - 64));
318  }
319 }
int64_t hi
Definition: int128.h:43
uint64_t lo
Definition: int128.h:42
Int128 inet::Int128::div ( const Int128 divisor,
Int128 remainder 
) const

Referenced by toString().

240 {
241  if (!divisor)
242  return 1u / (unsigned int)divisor.lo;
243  // or RaiseException (EXCEPTION_INT_DIVIDE_BY_ZERO,
244  // EXCEPTION_NONCONTINUABLE, 0, nullptr);
245 
246  Int128 ds = (divisor < 0) ? -divisor : divisor;
247  Int128 dd = (*this < 0) ? -*this : *this;
248 
249  // only remainder
250  if (ds > dd) {
251  remainder = *this;
252  return (Int128)0;
253  }
254 
255  Int128 r = (Int128)0;
256  Int128 q = (Int128)0;
257 // while (dd >= ds) { dd -= ds; q += 1; } // extreme slow version
258 
259  unsigned int b = 127;
260  while (r < ds) {
261  r <<= 1;
262  if (dd.bit(b--))
263  r.lo |= 1;
264  }
265  ++b;
266 
267  while (true)
268  if (r < ds) {
269  if (!(b--))
270  break;
271 
272  r <<= 1;
273  if (dd.bit(b))
274  r.lo |= 1;
275  }
276  else {
277  r -= ds;
278  q.bit(b, true);
279  }
280 
281  // correct
282  if ((divisor < 0) ^ (*this < 0))
283  q = -q;
284  if (*this < 0)
285  r = -r;
286 
287  remainder = r;
288  return q;
289 }
uint32_t ds
Definition: TCP_NSC.cc:76
Int128()
Definition: int128.h:61
value< double, units::m > b
Definition: Units.h:1054
inet::Int128::operator double ( )
inline
162 { return toDouble(); }
double toDouble() const
Definition: int128.cc:129
inet::Int128::operator int ( )
inline
163 { return toInt(); }
int toInt() const
Definition: int128.h:147
bool inet::Int128::operator! ( ) const
inline
104 { return !(hi || lo); }
int64_t hi
Definition: int128.h:43
uint64_t lo
Definition: int128.h:42
Int128& inet::Int128::operator%= ( const Int128 b)
inline
141  {
142  this->div(b, *this);
143  return *this;
144  }
Int128 div(const Int128 &, Int128 &) const
Definition: int128.cc:239
value< double, units::m > b
Definition: Units.h:1054
Int128& inet::Int128::operator&= ( const Int128 b)
inline
121 { hi &= b.hi; lo &= b.lo; return *this; }
int64_t hi
Definition: int128.h:43
uint64_t lo
Definition: int128.h:42
value< double, units::m > b
Definition: Units.h:1054
Int128 & inet::Int128::operator*= ( const Int128 b)
217 {
218  if (!b)
219  return *this = 0u;
220  if (b == 1u)
221  return *this;
222 
223  Int128 a(*this);
224  Int128 t(b);
225 
226  lo = 0ull;
227  hi = 0ll;
228 
229  for (unsigned int i = 0; i < 128; ++i) {
230  if (t.lo & 1)
231  *this += a << i;
232 
233  t >>= 1;
234  }
235 
236  return *this;
237 }
Int128()
Definition: int128.h:61
int64_t hi
Definition: int128.h:43
uint64_t lo
Definition: int128.h:42
value< double, units::m > b
Definition: Units.h:1054
const Int128& inet::Int128::operator+ ( ) const
inline
125 { return *this; }
Int128 & inet::Int128::operator++ ( )
171 {
172  ++lo;
173  if (!lo)
174  ++hi;
175 
176  return *this;
177 }
int64_t hi
Definition: int128.h:43
uint64_t lo
Definition: int128.h:42
Int128 inet::Int128::operator++ ( int  )
189 {
190  Int128 b(*this);
191  ++*this;
192 
193  return b;
194 }
Int128()
Definition: int128.h:61
value< double, units::m > b
Definition: Units.h:1054
Int128 & inet::Int128::operator+= ( const Int128 b)
205 {
206  uint64_t old_lo = lo;
207 
208  lo += b.lo;
209  hi += b.hi;
210  if (lo < old_lo)
211  ++hi;
212 
213  return *this;
214 }
int64_t hi
Definition: int128.h:43
uint64_t lo
Definition: int128.h:42
value< double, units::m > b
Definition: Units.h:1054
Int128 inet::Int128::operator- ( ) const
163 {
164  if (lo == 0)
165  return Int128(0ull, -hi);
166  else
167  return Int128(-lo, ~hi);
168 }
Int128()
Definition: int128.h:61
int64_t hi
Definition: int128.h:43
uint64_t lo
Definition: int128.h:42
Int128 & inet::Int128::operator-- ( )
180 {
181  if (!lo)
182  --hi;
183  --lo;
184 
185  return *this;
186 }
int64_t hi
Definition: int128.h:43
uint64_t lo
Definition: int128.h:42
Int128 inet::Int128::operator-- ( int  )
197 {
198  Int128 b(*this);
199  --*this;
200 
201  return b;
202 }
Int128()
Definition: int128.h:61
value< double, units::m > b
Definition: Units.h:1054
Int128& inet::Int128::operator-= ( const Int128 b)
inline
129  {
130  return *this += (-b);
131  }
value< double, units::m > b
Definition: Units.h:1054
Int128& inet::Int128::operator/= ( const Int128 b)
inline
134  {
135  Int128 dummy;
136  *this = this->div(b, dummy);
137  return *this;
138  }
Int128 div(const Int128 &, Int128 &) const
Definition: int128.cc:239
Int128()
Definition: int128.h:61
value< double, units::m > b
Definition: Units.h:1054
Int128 & inet::Int128::operator<<= ( unsigned int  n)
353 {
354  if (n >= 128) {
355  lo = hi = 0;
356  return *this;
357  }
358 
359  if (n >= 64) {
360  hi = lo << (n - 64);
361  lo = 0ull;
362  return *this;
363  }
364 
365  if (n) {
366  // shift high qword
367  hi <<= n;
368 
369  // get higher N bits of low qword
370  uint64_t mask = ~((1ull << (64 - n)) - 1);
371 
372  // and add them to high qword
373  hi |= (lo & mask) >> (64 - n);
374 
375  // and finally shift also low qword
376  lo <<= n;
377  }
378 
379  return *this;
380 }
int64_t hi
Definition: int128.h:43
uint64_t lo
Definition: int128.h:42
Int128& inet::Int128::operator= ( const Int128 other)
inline
85 { lo = other.lo; hi = other.hi; return *this; }
int64_t hi
Definition: int128.h:43
uint64_t lo
Definition: int128.h:42
Int128& inet::Int128::operator= ( const int32_t &  a)
inline
86 { lo = a; hi = 0; return *this; }
int64_t hi
Definition: int128.h:43
uint64_t lo
Definition: int128.h:42
Int128& inet::Int128::operator= ( const uint32_t &  a)
inline
87 { lo = a; hi = 0; return *this; }
int64_t hi
Definition: int128.h:43
uint64_t lo
Definition: int128.h:42
Int128& inet::Int128::operator= ( const int64_t &  a)
inline
89 { lo = a; hi = 0; return *this; }
int64_t hi
Definition: int128.h:43
uint64_t lo
Definition: int128.h:42
Int128& inet::Int128::operator= ( const uint64_t &  a)
inline
90 { lo = a; hi = 0; return *this; }
int64_t hi
Definition: int128.h:43
uint64_t lo
Definition: int128.h:42
Int128& inet::Int128::operator= ( const char *  sz)
inline
92 { set(sz); return *this; }
Int128 & inet::Int128::operator= ( const float &  a)
136 {
137  lo = ((uint64_t)fmodf(a, 18446744073709551616.0f));
138  hi = ((int64_t)(a / 18446744073709551616.0f));
139  return *this;
140 }
int64_t hi
Definition: int128.h:43
uint64_t lo
Definition: int128.h:42
Int128 & inet::Int128::operator= ( const double &  a)
143 {
144  lo = ((uint64_t)fmod(a, 18446744073709551616.0));
145  hi = ((int64_t)(a / 18446744073709551616.0));
146  return *this;
147 }
int64_t hi
Definition: int128.h:43
uint64_t lo
Definition: int128.h:42
Int128 & inet::Int128::operator= ( const long double &  a)
150 {
151  lo = ((uint64_t)fmodl(a, 18446744073709551616.0l));
152  hi = ((int64_t)(a / 18446744073709551616.0l));
153  return *this;
154 }
int64_t hi
Definition: int128.h:43
uint64_t lo
Definition: int128.h:42
Int128 & inet::Int128::operator>>= ( unsigned int  n)
322 {
323  if (n >= 128) {
324  hi = (hi < 0) ? -1ll : 0ll;
325  lo = hi;
326  return *this;
327  }
328 
329  if (n >= 64) {
330  lo = hi >> (n - 64);
331  hi = (hi < 0) ? -1ll : 0ll;
332  return *this;
333  }
334 
335  if (n) {
336  // shift low qword
337  lo >>= n;
338 
339  // get lower N bits of high qword
340  uint64_t mask = (1ull << n) - 1;
341 
342  // and add them to low qword
343  lo |= (hi & mask) << (64 - n);
344 
345  // and finally shift also high qword
346  hi >>= n;
347  }
348 
349  return *this;
350 }
int64_t hi
Definition: int128.h:43
uint64_t lo
Definition: int128.h:42
Int128& inet::Int128::operator^= ( const Int128 b)
inline
122 { hi ^= b.hi; lo ^= b.lo; return *this; }
int64_t hi
Definition: int128.h:43
uint64_t lo
Definition: int128.h:42
value< double, units::m > b
Definition: Units.h:1054
Int128& inet::Int128::operator|= ( const Int128 b)
inline
120 { hi |= b.hi; lo |= b.lo; return *this; }
int64_t hi
Definition: int128.h:43
uint64_t lo
Definition: int128.h:42
value< double, units::m > b
Definition: Units.h:1054
Int128 inet::Int128::operator~ ( ) const
inline
107 { return Int128(~lo, ~hi); }
Int128()
Definition: int128.h:61
int64_t hi
Definition: int128.h:43
uint64_t lo
Definition: int128.h:42
void inet::Int128::set ( const char *  sz)
65 {
66  lo = 0u;
67  hi = 0;
68 
69  if (!sz)
70  return;
71  if (!sz[0])
72  return;
73 
74  uint32_t radix = 10;
75  uint32_t i = 0;
76  bool minus = false;
77 
78  if (sz[i] == '-') {
79  ++i;
80  minus = true;
81  }
82 
83  if (sz[i] == '0') {
84  radix = 8;
85  ++i;
86  if (sz[i] == 'x') {
87  radix = 16;
88  ++i;
89  }
90  }
91 
92  for ( ; i < strlen(sz); ++i) {
93  uint32_t n = 0;
94  if (sz[i] >= '0' && sz[i] <= '9' && sz[i] < '0' + (int)radix)
95  n = sz[i] - '0';
96  else if (sz[i] >= 'a' && sz[i] <= 'a' + (int)radix - 10)
97  n = sz[i] - 'a' + 10;
98  else if (sz[i] >= 'A' && sz[i] <= 'A' + (int)radix - 10)
99  n = sz[i] - 'A' + 10;
100  else
101  break;
102 
103  (*this) *= radix;
104  (*this) += n;
105  }
106 
107  if (minus)
108  *this = Int128(0) - *this;
109 }
Int128()
Definition: int128.h:61
int64_t hi
Definition: int128.h:43
uint64_t lo
Definition: int128.h:42
double inet::Int128::toDouble ( ) const
130 {
131  return (double)hi * 18446744073709551616.0
132  + (double)lo;
133 }
int64_t hi
Definition: int128.h:43
uint64_t lo
Definition: int128.h:42
float inet::Int128::toFloat ( ) const
124 {
125  return (float)hi * 18446744073709551616.0f
126  + (float)lo;
127 }
int64_t hi
Definition: int128.h:43
uint64_t lo
Definition: int128.h:42
int inet::Int128::toInt ( ) const
inline

Referenced by toString().

147 { return (int)lo; }
uint64_t lo
Definition: int128.h:42
int64_t inet::Int128::toInt64 ( ) const
inline
148 { return (int64_t)lo; }
uint64_t lo
Definition: int128.h:42
long double inet::Int128::toLongDouble ( ) const
157 {
158  return (long double)hi * 18446744073709551616.0l
159  + (long double)lo;
160 }
int64_t hi
Definition: int128.h:43
uint64_t lo
Definition: int128.h:42
const char * inet::Int128::toString ( uint32_t  radix = 10) const
38 {
39  if (!*this)
40  return "0";
41  if (radix < 2 || radix > 37)
42  return "(invalid radix)";
43 
44  static char sz[256];
45  memset(sz, 0, 256);
46 
47  Int128 r;
48  Int128 ii = (*this < 0) ? -*this : *this;
49  int i = 255;
50  Int128 aux = radix;
51 
52  while (!!ii && i) {
53  ii = ii.div(aux, r);
54  unsigned int c = r.toInt();
55  sz[--i] = c + ((c > 9) ? 'A' - 10 : '0');
56  }
57 
58  if (*this < 0)
59  sz[--i] = '-';
60 
61  return &sz[i];
62 }
const value< double, compose< units::m, pow< units::s,-1 > > > c(299792458)
Int128()
Definition: int128.h:61

Friends And Related Function Documentation

bool operator&& ( const Int128 a,
const Int128 b 
)
friend
182 {
183  return (a.hi || a.lo) && (b.hi || b.lo);
184 }
value< double, units::m > b
Definition: Units.h:1054
bool operator< ( const Int128 ,
const Int128  
)
friend
383 {
384  if (a.hi == b.hi) {
385  if (a.hi < 0)
386  return (int64_t)a.lo < (int64_t)b.lo;
387  else
388  return a.lo < b.lo;
389  }
390  else
391  return a.hi < b.hi;
392 }
value< double, units::m > b
Definition: Units.h:1054
bool operator== ( const Int128 a,
const Int128 b 
)
friend
177 {
178  return a.hi == b.hi && a.lo == b.lo;
179 }
value< double, units::m > b
Definition: Units.h:1054
bool operator|| ( const Int128 a,
const Int128 b 
)
friend
187 {
188  return (a.hi || a.lo) || (b.hi || b.lo);
189 }
value< double, units::m > b
Definition: Units.h:1054

Member Data Documentation

const Int128 inet::Int128::INT128_MAX
static
const Int128 inet::Int128::INT128_MIN
static

The documentation for this class was generated from the following files: