A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
int64x64-128.h
Go to the documentation of this file.
1 #include "ns3/core-config.h"
2 #if !defined(INT64X64_128_H) && defined (INT64X64_USE_128) && !defined(PYTHON_SCAN)
3 #define INT64X64_128_H
4 
5 #include <stdint.h>
6 #include <cmath> // pow
7 
8 #if defined(HAVE___UINT128_T) && !defined(HAVE_UINT128_T)
9 typedef __uint128_t uint128_t;
10 typedef __int128_t int128_t;
11 #endif
12 
13 
14 namespace ns3 {
15 
21 {
23  static const uint128_t HP128_MASK_HI_BIT = (((int128_t)1)<<127);
25  static const uint64_t HP_MASK_LO = 0xffffffffffffffffULL;
27  static const uint64_t HP_MASK_HI = ~HP_MASK_LO;
40 #define HP_MAX_64 (std::pow (2.0L, 64))
41 
42 public:
50  enum impl_type {
54  };
55 
57  static const enum impl_type implementation = int128_impl;
58 
60  inline int64x64_t ()
61  : _v (0) {}
68  inline int64x64_t (const double value)
69  {
70  const int64x64_t tmp ((long double)value);
71  _v = tmp._v;
72  }
73  inline int64x64_t (const long double value)
74  {
75  const bool negative = value < 0;
76  const long double v = negative ? -value : value;
77 
78  long double fhi;
79  long double flo = std::modf (v, &fhi);
80  // Add 0.5 to round, which improves the last count
81  // This breaks these tests:
82  // TestSuite devices-mesh-dot11s-regression
83  // TestSuite devices-mesh-flame-regression
84  // TestSuite routing-aodv-regression
85  // TestSuite routing-olsr-regression
86  // Setting round = 0; breaks:
87  // TestSuite int64x64
88  const long double round = 0.5;
89  flo = flo * HP_MAX_64 + round;
90  int128_t hi = fhi;
91  const uint64_t lo = flo;
92  if (flo >= HP_MAX_64)
93  {
94  // conversion to uint64 rolled over
95  ++hi;
96  }
97  _v = hi << 64;
98  _v |= lo;
99  _v = negative ? -_v : _v;
100  }
109  inline int64x64_t (const int v)
110  : _v (v)
111  {
112  _v <<= 64;
113  }
114  inline int64x64_t (const long int v)
115  : _v (v)
116  {
117  _v <<= 64;
118  }
119  inline int64x64_t (const long long int v)
120  : _v (v)
121  {
122  _v <<= 64;
123  }
124  inline int64x64_t (const unsigned int v)
125  : _v (v)
126  {
127  _v <<= 64;
128  }
129  inline int64x64_t (const unsigned long int v)
130  : _v (v)
131  {
132  _v <<= 64;
133  }
134  inline int64x64_t (const unsigned long long int v)
135  : _v (v)
136  {
137  _v <<= 64;
138  }
146  explicit inline int64x64_t (const int64_t hi, const uint64_t lo)
147  {
148  _v = (int128_t)hi << 64;
149  _v |= lo;
150  }
151 
157  inline int64x64_t (const int64x64_t & o)
158  : _v (o._v) {}
164  inline int64x64_t & operator = (const int64x64_t & o)
165  {
166  _v = o._v;
167  return *this;
168  }
169 
175  inline double GetDouble (void) const
176  {
177  const bool negative = _v < 0;
178  const uint128_t value = negative ? -_v : _v;
179  const long double fhi = value >> 64;
180  const long double flo = (value & HP_MASK_LO) / HP_MAX_64;
181  long double retval = fhi;
182  retval += flo;
183  retval = negative ? -retval : retval;
184  return retval;
185  }
191  inline int64_t GetHigh (void) const
192  {
193  const int128_t retval = _v >> 64;
194  return retval;
195  }
201  inline uint64_t GetLow (void) const
202  {
203  const uint128_t retval = _v & HP_MASK_LO;
204  return retval;
205  }
206 
215  void MulByInvert (const int64x64_t & o);
216 
230  static int64x64_t Invert (const uint64_t v);
231 
232 private:
233  friend bool operator == (const int64x64_t & lhs, const int64x64_t & rhs);
234 
235  friend bool operator < (const int64x64_t & lhs, const int64x64_t & rhs);
236  friend bool operator > (const int64x64_t & lhs, const int64x64_t & rhs);
237 
238  friend int64x64_t & operator += ( int64x64_t & lhs, const int64x64_t & rhs);
239  friend int64x64_t & operator -= ( int64x64_t & lhs, const int64x64_t & rhs);
240  friend int64x64_t & operator *= ( int64x64_t & lhs, const int64x64_t & rhs);
241  friend int64x64_t & operator /= ( int64x64_t & lhs, const int64x64_t & rhs);
242 
243  friend int64x64_t operator - (const int64x64_t & lhs);
244  friend int64x64_t operator ! (const int64x64_t & lhs);
245 
251  void Mul (const int64x64_t & o);
257  void Div (const int64x64_t & o);
282  static uint128_t Umul (const uint128_t a, const uint128_t b);
290  static uint128_t Udiv (const uint128_t a, const uint128_t b);
300  static uint128_t UmulByInvert (const uint128_t a, const uint128_t b);
301 
307  inline int64x64_t (const int128_t v)
308  : _v (v) {}
309 
311 
312 }; // class int64x64_t
313 
314 
319 inline bool operator == (const int64x64_t & lhs, const int64x64_t & rhs)
320 {
321  return lhs._v == rhs._v;
322 }
327 inline bool operator < (const int64x64_t & lhs, const int64x64_t & rhs)
328 {
329  return lhs._v < rhs._v;
330 }
335 inline bool operator > (const int64x64_t & lhs, const int64x64_t & rhs)
336 {
337  return lhs._v > rhs._v;
338 }
339 
344 inline int64x64_t & operator += (int64x64_t & lhs, const int64x64_t & rhs)
345 {
346  lhs._v += rhs._v;
347  return lhs;
348 }
353 inline int64x64_t & operator -= (int64x64_t & lhs, const int64x64_t & rhs)
354 {
355  lhs._v -= rhs._v;
356  return lhs;
357 }
362 inline int64x64_t & operator *= (int64x64_t & lhs, const int64x64_t & rhs)
363 {
364  lhs.Mul (rhs);
365  return lhs;
366 }
371 inline int64x64_t & operator /= (int64x64_t & lhs, const int64x64_t & rhs)
372 {
373  lhs.Div (rhs);
374  return lhs;
375 }
376 
381 inline int64x64_t operator + (const int64x64_t & lhs)
382 {
383  return lhs;
384 }
389 inline int64x64_t operator - (const int64x64_t & lhs)
390 {
391  return int64x64_t (-lhs._v);
392 }
397 inline int64x64_t operator ! (const int64x64_t & lhs)
398 {
399  return int64x64_t (!lhs._v);
400 }
401 
402 
403 } // namespace ns3
404 
405 #endif /* INT64X64_128_H */
long double implementation
Definition: int64x64-128.h:53
uint64_t GetLow(void) const
Get the fractional portion of this value, unscaled.
Definition: int64x64-128.h:201
int64x64_t operator+(const int64x64_t &lhs)
Unary plus operator.
Definition: int64x64-128.h:381
int64x64_t(const long int v)
Construct from an integral type.
Definition: int64x64-128.h:114
int64x64_t(const int128_t v)
Construct from an integral type.
Definition: int64x64-128.h:307
int64x64_t & operator/=(int64x64_t &lhs, const int64x64_t &rhs)
Compound division operator.
Definition: int64x64-128.h:371
int64x64_t(const long double value)
Construct from a floating point value.
Definition: int64x64-128.h:73
High precision numerical type, implementing Q64.64 fixed precision.
Definition: int64x64-128.h:20
static uint128_t Udiv(const uint128_t a, const uint128_t b)
Unsigned division of Q64.64 values.
Definition: int64x64-128.cc:87
static int64x64_t Invert(const uint64_t v)
Compute the inverse of an integer value.
int64x64_t operator-(const int64x64_t &lhs)
Unary negation operator (change sign operator)
Definition: int64x64-128.h:389
impl_type
Type tag for the underlying implementation.
Definition: int64x64-128.h:50
__int128_t int128_t
Definition: int64x64-128.h:10
int64x64_t & operator+=(int64x64_t &lhs, const int64x64_t &rhs)
Compound addition operator.
Definition: int64x64-128.h:344
static enum impl_type implementation
Type tag for this implementation.
Definition: int64x64-128.h:57
void MulByInvert(const int64x64_t &o)
Multiply this value by a Q0.128 value, presumably representing an inverse, completing a division oper...
static uint128_t UmulByInvert(const uint128_t a, const uint128_t b)
Unsigned multiplication of Q64.64 and Q0.128 values.
double GetDouble(void) const
Get this value as a double.
Definition: int64x64-128.h:175
friend int64x64_t operator!(const int64x64_t &lhs)
Logical not operator.
Definition: int64x64-128.h:397
friend int64x64_t & operator*=(int64x64_t &lhs, const int64x64_t &rhs)
Compound multiplication operator.
Definition: int64x64-128.h:362
friend bool operator==(const int64x64_t &lhs, const int64x64_t &rhs)
Equality operator.
Definition: int64x64-128.h:319
__uint128_t uint128_t
Definition: int64x64-128.h:9
friend int64x64_t & operator/=(int64x64_t &lhs, const int64x64_t &rhs)
Compound division operator.
Definition: int64x64-128.h:371
int64x64_t(const unsigned long int v)
Construct from an integral type.
Definition: int64x64-128.h:129
static uint128_t Umul(const uint128_t a, const uint128_t b)
Unsigned multiplication of Q64.64 values.
Definition: int64x64-128.cc:38
int128_t _v
The Q64.64 value.
Definition: int64x64-128.h:310
int64x64_t(const int64_t hi, const uint64_t lo)
Construct from explicit high and low values.
Definition: int64x64-128.h:146
friend int64x64_t & operator-=(int64x64_t &lhs, const int64x64_t &rhs)
Compound subtraction operator.
Definition: int64x64-128.h:353
int64x64_t(const long long int v)
Construct from an integral type.
Definition: int64x64-128.h:119
void Mul(const int64x64_t &o)
Implement *=.
Definition: int64x64-128.cc:29
int64x64_t()
Default constructor.
Definition: int64x64-128.h:60
bool operator<(const int64x64_t &lhs, const int64x64_t &rhs)
Less than operator.
Definition: int64x64-128.h:327
int64x64_t & operator*=(int64x64_t &lhs, const int64x64_t &rhs)
Compound multiplication operator.
Definition: int64x64-128.h:362
int64x64_t(const int v)
Construct from an integral type.
Definition: int64x64-128.h:109
int64x64_t(const unsigned long long int v)
Construct from an integral type.
Definition: int64x64-128.h:134
static const uint128_t HP128_MASK_HI_BIT
uint128_t high bit (sign bit)
Definition: int64x64-128.h:23
Native int128_t implementation.
Definition: int64x64-128.h:51
static const uint64_t HP_MASK_LO
Mask for fraction part.
Definition: int64x64-128.h:25
int64x64_t & operator=(const int64x64_t &o)
Assignment.
Definition: int64x64-128.h:164
#define HP_MAX_64
Floating point value of HP_MASK_LO + 1 We really want:
Definition: int64x64-128.h:40
void Div(const int64x64_t &o)
Implement /=.
Definition: int64x64-128.cc:78
int64x64_t(const unsigned int v)
Construct from an integral type.
Definition: int64x64-128.h:124
friend bool operator<(const int64x64_t &lhs, const int64x64_t &rhs)
Less than operator.
Definition: int64x64-128.h:327
bool operator>(const int64x64_t &lhs, const int64x64_t &rhs)
Greater operator.
Definition: int64x64-128.h:335
bool operator==(const EventId &a, const EventId &b)
Definition: event-id.cc:89
int64_t GetHigh(void) const
Get the integer portion.
Definition: int64x64-128.h:191
cairo wideint implementation
Definition: int64x64-128.h:52
friend int64x64_t & operator+=(int64x64_t &lhs, const int64x64_t &rhs)
Compound addition operator.
Definition: int64x64-128.h:344
friend int64x64_t operator-(const int64x64_t &lhs)
Unary negation operator (change sign operator)
Definition: int64x64-128.h:389
friend bool operator>(const int64x64_t &lhs, const int64x64_t &rhs)
Greater operator.
Definition: int64x64-128.h:335
static const uint64_t HP_MASK_HI
Mask for sign + integer part.
Definition: int64x64-128.h:27
int64x64_t(const int64x64_t &o)
Copy constructor.
Definition: int64x64-128.h:157
int64x64_t operator!(const int64x64_t &lhs)
Logical not operator.
Definition: int64x64-128.h:397
int64x64_t(const double value)
Construct from a floating point value.
Definition: int64x64-128.h:68
int64x64_t & operator-=(int64x64_t &lhs, const int64x64_t &rhs)
Compound subtraction operator.
Definition: int64x64-128.h:353