A Discrete-Event Network Simulator
API
int64x64-128.h
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2010 INRIA
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  */
19 
20 #include "ns3/core-config.h"
21 
22 #if !defined(INT64X64_128_H) && defined (INT64X64_USE_128) && !defined(PYTHON_SCAN)
23 #define INT64X64_128_H
24 
25 #include <stdint.h>
26 #include <cmath> // pow
27 
28 #if defined(HAVE___UINT128_T) && !defined(HAVE_UINT128_T)
29 typedef __uint128_t uint128_t;
30 typedef __int128_t int128_t;
31 #endif
32 
39 namespace ns3 {
40 
46 {
48  static const uint128_t HP128_MASK_HI_BIT = (((int128_t)1)<<127);
50  static const uint64_t HP_MASK_LO = 0xffffffffffffffffULL;
52  static const uint64_t HP_MASK_HI = ~HP_MASK_LO;
66 #define HP_MAX_64 (std::pow (2.0L, 64))
67 
68 public:
76  enum impl_type {
80  };
81 
83  static const enum impl_type implementation = int128_impl;
84 
86  inline int64x64_t ()
87  : _v (0) {}
94  inline int64x64_t (const double value)
95  {
96  const int64x64_t tmp ((long double)value);
97  _v = tmp._v;
98  }
99  inline int64x64_t (const long double value)
100  {
101  const bool negative = value < 0;
102  const long double v = negative ? -value : value;
103 
104  long double fhi;
105  long double flo = std::modf (v, &fhi);
106  // Add 0.5 to round, which improves the last count
107  // This breaks these tests:
108  // TestSuite devices-mesh-dot11s-regression
109  // TestSuite devices-mesh-flame-regression
110  // TestSuite routing-aodv-regression
111  // TestSuite routing-olsr-regression
112  // Setting round = 0; breaks:
113  // TestSuite int64x64
114  const long double round = 0.5;
115  flo = flo * HP_MAX_64 + round;
116  int128_t hi = fhi;
117  const uint64_t lo = flo;
118  if (flo >= HP_MAX_64)
119  {
120  // conversion to uint64 rolled over
121  ++hi;
122  }
123  _v = hi << 64;
124  _v |= lo;
125  _v = negative ? -_v : _v;
126  }
135  inline int64x64_t (const int v)
136  : _v (v)
137  {
138  _v <<= 64;
139  }
140  inline int64x64_t (const long int v)
141  : _v (v)
142  {
143  _v <<= 64;
144  }
145  inline int64x64_t (const long long int v)
146  : _v (v)
147  {
148  _v <<= 64;
149  }
150  inline int64x64_t (const unsigned int v)
151  : _v (v)
152  {
153  _v <<= 64;
154  }
155  inline int64x64_t (const unsigned long int v)
156  : _v (v)
157  {
158  _v <<= 64;
159  }
160  inline int64x64_t (const unsigned long long int v)
161  : _v (v)
162  {
163  _v <<= 64;
164  }
173  explicit inline int64x64_t (const int64_t hi, const uint64_t lo)
174  {
175  _v = (int128_t)hi << 64;
176  _v |= lo;
177  }
178 
184  inline int64x64_t (const int64x64_t & o)
185  : _v (o._v) {}
192  inline int64x64_t & operator = (const int64x64_t & o)
193  {
194  _v = o._v;
195  return *this;
196  }
197 
203  inline double GetDouble (void) const
204  {
205  const bool negative = _v < 0;
206  const uint128_t value = negative ? -_v : _v;
207  const long double fhi = value >> 64;
208  const long double flo = (value & HP_MASK_LO) / HP_MAX_64;
209  long double retval = fhi;
210  retval += flo;
211  retval = negative ? -retval : retval;
212  return retval;
213  }
219  inline int64_t GetHigh (void) const
220  {
221  const int128_t retval = _v >> 64;
222  return retval;
223  }
229  inline uint64_t GetLow (void) const
230  {
231  const uint128_t retval = _v & HP_MASK_LO;
232  return retval;
233  }
234 
243  void MulByInvert (const int64x64_t & o);
244 
258  static int64x64_t Invert (const uint64_t v);
259 
260 private:
261 
262  friend bool operator == (const int64x64_t & lhs, const int64x64_t & rhs);
263 
264  friend bool operator < (const int64x64_t & lhs, const int64x64_t & rhs);
265  friend bool operator > (const int64x64_t & lhs, const int64x64_t & rhs);
266 
267  friend int64x64_t & operator += ( int64x64_t & lhs, const int64x64_t & rhs);
268  friend int64x64_t & operator -= ( int64x64_t & lhs, const int64x64_t & rhs);
269  friend int64x64_t & operator *= ( int64x64_t & lhs, const int64x64_t & rhs);
270  friend int64x64_t & operator /= ( int64x64_t & lhs, const int64x64_t & rhs);
271 
272  friend int64x64_t operator - (const int64x64_t & lhs);
273  friend int64x64_t operator ! (const int64x64_t & lhs);
274 
280  void Mul (const int64x64_t & o);
286  void Div (const int64x64_t & o);
311  static uint128_t Umul (const uint128_t a, const uint128_t b);
319  static uint128_t Udiv (const uint128_t a, const uint128_t b);
329  static uint128_t UmulByInvert (const uint128_t a, const uint128_t b);
330 
336  inline int64x64_t (const int128_t v)
337  : _v (v) {}
338 
340 
341 }; // class int64x64_t
342 
343 
348 inline bool operator == (const int64x64_t & lhs, const int64x64_t & rhs)
349 {
350  return lhs._v == rhs._v;
351 }
356 inline bool operator < (const int64x64_t & lhs, const int64x64_t & rhs)
357 {
358  return lhs._v < rhs._v;
359 }
364 inline bool operator > (const int64x64_t & lhs, const int64x64_t & rhs)
365 {
366  return lhs._v > rhs._v;
367 }
368 
373 inline int64x64_t & operator += (int64x64_t & lhs, const int64x64_t & rhs)
374 {
375  lhs._v += rhs._v;
376  return lhs;
377 }
382 inline int64x64_t & operator -= (int64x64_t & lhs, const int64x64_t & rhs)
383 {
384  lhs._v -= rhs._v;
385  return lhs;
386 }
391 inline int64x64_t & operator *= (int64x64_t & lhs, const int64x64_t & rhs)
392 {
393  lhs.Mul (rhs);
394  return lhs;
395 }
400 inline int64x64_t & operator /= (int64x64_t & lhs, const int64x64_t & rhs)
401 {
402  lhs.Div (rhs);
403  return lhs;
404 }
405 
410 inline int64x64_t operator + (const int64x64_t & lhs)
411 {
412  return lhs;
413 }
418 inline int64x64_t operator - (const int64x64_t & lhs)
419 {
420  return int64x64_t (-lhs._v);
421 }
426 inline int64x64_t operator ! (const int64x64_t & lhs)
427 {
428  return int64x64_t (!lhs._v);
429 }
430 
431 
432 } // namespace ns3
433 
434 #endif /* INT64X64_128_H */
int64x64_t & operator+=(int64x64_t &lhs, const int64x64_t &rhs)
Compound addition operator.
Definition: int64x64-128.h:373
int64x64_t operator+(const int64x64_t &lhs)
Unary plus operator.
Definition: int64x64-128.h:410
int64x64_t(const long int v)
Definition: int64x64-128.h:140
int64x64_t(const int128_t v)
Construct from an integral type.
Definition: int64x64-128.h:336
int64x64_t(const long double value)
Definition: int64x64-128.h:99
High precision numerical type, implementing Q64.64 fixed precision.
Definition: int64x64-128.h:45
int64x64_t & operator*=(int64x64_t &lhs, const int64x64_t &rhs)
Compound multiplication operator.
Definition: int64x64-128.h:391
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:418
static uint128_t Udiv(const uint128_t a, const uint128_t b)
Unsigned division of Q64.64 values.
impl_type
Type tag for the underlying implementation.
Definition: int64x64-128.h:76
__int128_t int128_t
Definition: int64x64-128.h:30
void MulByInvert(const int64x64_t &o)
Multiply this value by a Q0.128 value, presumably representing an inverse, completing a division oper...
friend int64x64_t operator!(const int64x64_t &lhs)
Logical not operator.
Definition: int64x64-128.h:426
friend int64x64_t & operator*=(int64x64_t &lhs, const int64x64_t &rhs)
Compound multiplication operator.
Definition: int64x64-128.h:391
friend bool operator==(const int64x64_t &lhs, const int64x64_t &rhs)
Equality operator.
Definition: int64x64-128.h:348
bool operator<(const EventId &a, const EventId &b)
Definition: event-id.h:153
__uint128_t uint128_t
Definition: int64x64-128.h:29
friend int64x64_t & operator/=(int64x64_t &lhs, const int64x64_t &rhs)
Compound division operator.
Definition: int64x64-128.h:400
int64x64_t(const unsigned long int v)
Definition: int64x64-128.h:155
static const uint64_t HP_MASK_LO
Mask for fraction part.
Definition: int64x64-128.h:50
int128_t _v
The Q64.64 value.
Definition: int64x64-128.h:339
int64x64_t(const int64_t hi, const uint64_t lo)
Construct from explicit high and low values.
Definition: int64x64-128.h:173
friend int64x64_t & operator-=(int64x64_t &lhs, const int64x64_t &rhs)
Compound subtraction operator.
Definition: int64x64-128.h:382
int64x64_t(const long long int v)
Definition: int64x64-128.h:145
void Mul(const int64x64_t &o)
Implement *=.
Definition: int64x64-128.cc:64
int64x64_t()
Default constructor.
Definition: int64x64-128.h:86
static uint128_t UmulByInvert(const uint128_t a, const uint128_t b)
Unsigned multiplication of Q64.64 and Q0.128 values.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
int64x64_t(const int v)
Definition: int64x64-128.h:135
int64x64_t(const unsigned long long int v)
Definition: int64x64-128.h:160
static const uint128_t HP128_MASK_HI_BIT
uint128_t high bit (sign bit).
Definition: int64x64-128.h:48
int64x64_t & operator=(const int64x64_t &o)
Assignment.
Definition: int64x64-128.h:192
#define HP_MAX_64
Floating point value of HP_MASK_LO + 1.
Definition: int64x64-128.h:66
static uint128_t Umul(const uint128_t a, const uint128_t b)
Unsigned multiplication of Q64.64 values.
Definition: int64x64-128.cc:73
void Div(const int64x64_t &o)
Implement /=.
Native int128_t implementation.
Definition: int64x64-128.h:77
int64x64_t(const unsigned int v)
Definition: int64x64-128.h:150
friend bool operator<(const int64x64_t &lhs, const int64x64_t &rhs)
Less than operator.
Definition: int64x64-128.h:356
uint64_t GetLow(void) const
Get the fractional portion of this value, unscaled.
Definition: int64x64-128.h:229
bool operator>(const int64x64_t &lhs, const int64x64_t &rhs)
Greater operator.
Definition: int64x64-128.h:364
bool operator==(const EventId &a, const EventId &b)
Definition: event-id.h:135
Cairo wideint implementation.
Definition: int64x64-128.h:78
double GetDouble(void) const
Get this value as a double.
Definition: int64x64-128.h:203
int64x64_t & operator-=(int64x64_t &lhs, const int64x64_t &rhs)
Compound subtraction operator.
Definition: int64x64-128.h:382
friend int64x64_t & operator+=(int64x64_t &lhs, const int64x64_t &rhs)
Compound addition operator.
Definition: int64x64-128.h:373
friend int64x64_t operator-(const int64x64_t &lhs)
Unary negation operator (change sign operator).
Definition: int64x64-128.h:418
int64x64_t & operator/=(int64x64_t &lhs, const int64x64_t &rhs)
Compound division operator.
Definition: int64x64-128.h:400
friend bool operator>(const int64x64_t &lhs, const int64x64_t &rhs)
Greater operator.
Definition: int64x64-128.h:364
static const uint64_t HP_MASK_HI
Mask for sign + integer part.
Definition: int64x64-128.h:52
int64x64_t(const int64x64_t &o)
Copy constructor.
Definition: int64x64-128.h:184
long double implementation.
Definition: int64x64-128.h:79
int64x64_t operator!(const int64x64_t &lhs)
Logical not operator.
Definition: int64x64-128.h:426
int64x64_t(const double value)
Definition: int64x64-128.h:94
int64_t GetHigh(void) const
Get the integer portion.
Definition: int64x64-128.h:219
static enum impl_type implementation
Type tag for this implementation.
Definition: int64x64-128.h:83