A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
int64x64-double.h
Go to the documentation of this file.
1 #include "ns3/core-config.h"
2 #if !defined(INT64X64_DOUBLE_H) && (defined (INT64X64_USE_DOUBLE) || defined(PYTHON_SCAN))
3 #define INT64X64_DOUBLE_H
4 
5 #include <stdint.h>
6 #include <cmath> // pow
7 #include <utility> // pair
8 
9 
10 namespace ns3 {
11 
16 class int64x64_t
17 {
19  static const uint64_t HP_MASK_LO = 0xffffffffffffffffULL;
32 #define HP_MAX_64 (std::pow (2.0L, 64))
33 
34 public:
42  enum impl_type {
43  int128_impl,
44  cairo_impl,
45  ld_impl,
46  };
47 
49  static const enum impl_type implementation = ld_impl;
50 
52  inline int64x64_t ()
53  : _v (0) {}
60  inline int64x64_t (double v)
61  : _v (v) {}
62  inline int64x64_t (long double v)
63  : _v (v) {}
72  inline int64x64_t (int v)
73  : _v (v) {}
74  inline int64x64_t (long int v)
75  : _v (v) {}
76  inline int64x64_t (long long int v)
77  : _v (v) {}
78  inline int64x64_t (unsigned int v)
79  : _v (v) {}
80  inline int64x64_t (unsigned long int v)
81  : _v (v) {}
82  inline int64x64_t (unsigned long long int v)
83  : _v (v) {}
91  explicit inline int64x64_t (int64_t hi, uint64_t lo)
92  {
93  const bool negative = hi < 0;
94  const long double fhi = negative ? -hi : hi;
95  const long double flo = lo / HP_MAX_64;
96  _v = negative ? - fhi : fhi;
97  _v += flo;
98  // _v = negative ? -_v : _v;
99  }
100 
106  inline int64x64_t (const int64x64_t & o)
107  : _v (o._v) {}
113  inline int64x64_t & operator = (const int64x64_t & o)
114  {
115  _v = o._v;
116  return *this;
117  }
118 
124  inline double GetDouble (void) const
125  {
126  return (double)_v;
127  }
128 private:
134  std::pair<int64_t, uint64_t> GetHighLow (void) const
135  {
136  const bool negative = _v < 0;
137  const long double v = negative ? -_v : _v;
138 
139  long double fhi;
140  long double flo = std::modf (v, &fhi);
141  // Add 0.5 to round, which improves the last count
142  // This breaks these tests:
143  // TestSuite devices-mesh-dot11s-regression
144  // TestSuite devices-mesh-flame-regression
145  // TestSuite routing-aodv-regression
146  // TestSuite routing-olsr-regression
147  // Setting round = 0; breaks:
148  // TestSuite int64x64
149  const long double round = 0.5;
150  flo = flo * HP_MAX_64 + round;
151  int64_t hi = fhi;
152  uint64_t lo = flo;
153  if (flo >= HP_MAX_64)
154  {
155  // conversion to uint64 rolled over
156  ++hi;
157  }
158  if (negative)
159  {
160  lo = ~lo;
161  hi = ~hi;
162  if (++lo == 0)
163  {
164  ++hi;
165  }
166  }
167  return std::make_pair (hi, lo);
168  }
169 public:
175  inline int64_t GetHigh (void) const
176  {
177  return GetHighLow ().first;
178  }
184  inline uint64_t GetLow (void) const
185  {
186  return GetHighLow ().second;
187  }
188 
198  inline void MulByInvert (const int64x64_t & o)
199  {
200  _v *= o._v;
201  }
202 
209  static inline int64x64_t Invert (uint64_t v)
210  {
211  int64x64_t tmp ((long double)1 / v);
212  return tmp;
213  }
214 
215 private:
216  friend bool operator == (const int64x64_t & lhs, const int64x64_t & rhs);
217 
218  friend bool operator < (const int64x64_t & lhs, const int64x64_t & rhs);
219  friend bool operator > (const int64x64_t & lhs, const int64x64_t & rhs);
220 
221  friend int64x64_t & operator += ( int64x64_t & lhs, const int64x64_t & rhs);
222  friend int64x64_t & operator -= ( int64x64_t & lhs, const int64x64_t & rhs);
223  friend int64x64_t & operator *= ( int64x64_t & lhs, const int64x64_t & rhs);
224  friend int64x64_t & operator /= ( int64x64_t & lhs, const int64x64_t & rhs);
225 
226  friend int64x64_t operator - (const int64x64_t & lhs);
227  friend int64x64_t operator ! (const int64x64_t & lhs);
228 
229  long double _v;
230 
231 }; // class int64x64_t
232 
233 
238 inline bool operator == (const int64x64_t & lhs, const int64x64_t & rhs)
239 {
240  return lhs._v == rhs._v;
241 }
246 inline bool operator < (const int64x64_t & lhs, const int64x64_t & rhs)
247 {
248  return lhs._v < rhs._v;
249 }
254 inline bool operator > (const int64x64_t & lhs, const int64x64_t & rhs)
255 {
256  return lhs._v > rhs._v;
257 }
258 
263 inline int64x64_t & operator += (int64x64_t & lhs, const int64x64_t & rhs)
264 {
265  lhs._v += rhs._v;
266  return lhs;
267 }
272 inline int64x64_t & operator -= (int64x64_t & lhs, const int64x64_t & rhs)
273 {
274  lhs._v -= rhs._v;
275  return lhs;
276 }
281 inline int64x64_t & operator *= (int64x64_t & lhs, const int64x64_t & rhs)
282 {
283  lhs._v *= rhs._v;
284  return lhs;
285 }
290 inline int64x64_t & operator /= (int64x64_t & lhs, const int64x64_t & rhs)
291 {
292  lhs._v /= rhs._v;
293  return lhs;
294 }
295 
300 inline int64x64_t operator + (const int64x64_t & lhs)
301 {
302  return lhs;
303 }
308 inline int64x64_t operator - (const int64x64_t & lhs)
309 {
310  return int64x64_t (-lhs._v);
311 }
316 inline int64x64_t operator ! (const int64x64_t & lhs)
317 {
318  return int64x64_t (!lhs._v);
319 }
320 
321 
322 } // namespace ns3
323 
324 #endif /* INT64X64_DOUBLE_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 & operator/=(int64x64_t &lhs, const int64x64_t &rhs)
Compound division operator.
Definition: int64x64-128.h:371
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
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...
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
friend int64x64_t & operator/=(int64x64_t &lhs, const int64x64_t &rhs)
Compound division operator.
Definition: int64x64-128.h:371
int128_t _v
The Q64.64 value.
Definition: int64x64-128.h:310
friend int64x64_t & operator-=(int64x64_t &lhs, const int64x64_t &rhs)
Compound subtraction operator.
Definition: int64x64-128.h:353
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
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
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
int64x64_t operator!(const int64x64_t &lhs)
Logical not operator.
Definition: int64x64-128.h:397
int64x64_t & operator-=(int64x64_t &lhs, const int64x64_t &rhs)
Compound subtraction operator.
Definition: int64x64-128.h:353