A Discrete-Event Network Simulator
API
int64x64-double.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 #if !defined(INT64X64_DOUBLE_H) && (defined (INT64X64_USE_DOUBLE) || defined(PYTHON_SCAN))
22 #define INT64X64_DOUBLE_H
23 
24 #include <stdint.h>
25 #include <cmath> // pow
26 #include <utility> // pair
27 
35 namespace ns3 {
36 
41 class int64x64_t
42 {
44  static const uint64_t HP_MASK_LO = 0xffffffffffffffffULL;
57 #define HP_MAX_64 (std::pow (2.0L, 64))
58 
59 public:
67  enum impl_type {
68  int128_impl,
69  cairo_impl,
70  ld_impl,
71  };
72 
74  static const enum impl_type implementation = ld_impl;
75 
77  inline int64x64_t ()
78  : _v (0) {}
85  inline int64x64_t (double v)
86  : _v (v) {}
87  inline int64x64_t (long double v)
88  : _v (v) {}
97  inline int64x64_t (int v)
98  : _v (v) {}
99  inline int64x64_t (long int v)
100  : _v (v) {}
101  inline int64x64_t (long long int v)
102  : _v (v) {}
103  inline int64x64_t (unsigned int v)
104  : _v (v) {}
105  inline int64x64_t (unsigned long int v)
106  : _v (v) {}
107  inline int64x64_t (unsigned long long int v)
108  : _v (v) {}
116  explicit inline int64x64_t (int64_t hi, uint64_t lo)
117  {
118  const bool negative = hi < 0;
119  const long double fhi = negative ? -hi : hi;
120  const long double flo = lo / HP_MAX_64;
121  _v = negative ? - fhi : fhi;
122  _v += flo;
123  // _v = negative ? -_v : _v;
124  }
125 
131  inline int64x64_t (const int64x64_t & o)
132  : _v (o._v) {}
138  inline int64x64_t & operator = (const int64x64_t & o)
139  {
140  _v = o._v;
141  return *this;
142  }
143 
149  inline double GetDouble (void) const
150  {
151  return (double)_v;
152  }
153 private:
159  std::pair<int64_t, uint64_t> GetHighLow (void) const
160  {
161  const bool negative = _v < 0;
162  const long double v = negative ? -_v : _v;
163 
164  long double fhi;
165  long double flo = std::modf (v, &fhi);
166  // Add 0.5 to round, which improves the last count
167  // This breaks these tests:
168  // TestSuite devices-mesh-dot11s-regression
169  // TestSuite devices-mesh-flame-regression
170  // TestSuite routing-aodv-regression
171  // TestSuite routing-olsr-regression
172  // Setting round = 0; breaks:
173  // TestSuite int64x64
174  const long double round = 0.5;
175  flo = flo * HP_MAX_64 + round;
176  int64_t hi = fhi;
177  uint64_t lo = flo;
178  if (flo >= HP_MAX_64)
179  {
180  // conversion to uint64 rolled over
181  ++hi;
182  }
183  if (negative)
184  {
185  lo = ~lo;
186  hi = ~hi;
187  if (++lo == 0)
188  {
189  ++hi;
190  }
191  }
192  return std::make_pair (hi, lo);
193  }
194 public:
200  inline int64_t GetHigh (void) const
201  {
202  return GetHighLow ().first;
203  }
209  inline uint64_t GetLow (void) const
210  {
211  return GetHighLow ().second;
212  }
213 
223  inline void MulByInvert (const int64x64_t & o)
224  {
225  _v *= o._v;
226  }
227 
234  static inline int64x64_t Invert (uint64_t v)
235  {
236  int64x64_t tmp ((long double)1 / v);
237  return tmp;
238  }
239 
240 private:
241  friend bool operator == (const int64x64_t & lhs, const int64x64_t & rhs);
242 
243  friend bool operator < (const int64x64_t & lhs, const int64x64_t & rhs);
244  friend bool operator > (const int64x64_t & lhs, const int64x64_t & rhs);
245 
246  friend int64x64_t & operator += ( int64x64_t & lhs, const int64x64_t & rhs);
247  friend int64x64_t & operator -= ( int64x64_t & lhs, const int64x64_t & rhs);
248  friend int64x64_t & operator *= ( int64x64_t & lhs, const int64x64_t & rhs);
249  friend int64x64_t & operator /= ( int64x64_t & lhs, const int64x64_t & rhs);
250 
251  friend int64x64_t operator - (const int64x64_t & lhs);
252  friend int64x64_t operator ! (const int64x64_t & lhs);
253 
254  long double _v;
255 
256 }; // class int64x64_t
257 
258 
263 inline bool operator == (const int64x64_t & lhs, const int64x64_t & rhs)
264 {
265  return lhs._v == rhs._v;
266 }
271 inline bool operator < (const int64x64_t & lhs, const int64x64_t & rhs)
272 {
273  return lhs._v < rhs._v;
274 }
279 inline bool operator > (const int64x64_t & lhs, const int64x64_t & rhs)
280 {
281  return lhs._v > rhs._v;
282 }
283 
288 inline int64x64_t & operator += (int64x64_t & lhs, const int64x64_t & rhs)
289 {
290  lhs._v += rhs._v;
291  return lhs;
292 }
297 inline int64x64_t & operator -= (int64x64_t & lhs, const int64x64_t & rhs)
298 {
299  lhs._v -= rhs._v;
300  return lhs;
301 }
306 inline int64x64_t & operator *= (int64x64_t & lhs, const int64x64_t & rhs)
307 {
308  lhs._v *= rhs._v;
309  return lhs;
310 }
315 inline int64x64_t & operator /= (int64x64_t & lhs, const int64x64_t & rhs)
316 {
317  lhs._v /= rhs._v;
318  return lhs;
319 }
320 
325 inline int64x64_t operator + (const int64x64_t & lhs)
326 {
327  return lhs;
328 }
333 inline int64x64_t operator - (const int64x64_t & lhs)
334 {
335  return int64x64_t (-lhs._v);
336 }
341 inline int64x64_t operator ! (const int64x64_t & lhs)
342 {
343  return int64x64_t (!lhs._v);
344 }
345 
346 
347 } // namespace ns3
348 
349 #endif /* INT64X64_DOUBLE_H */
int64x64_t & operator+=(int64x64_t &lhs, const int64x64_t &rhs)
Compound addition operator.
Definition: int64x64-128.h:373
uint64_t GetLow(void) const
Get the fractional portion of this value, unscaled.
int64x64_t operator+(const int64x64_t &lhs)
Unary plus operator.
Definition: int64x64-128.h:410
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
int64x64_t operator-(const int64x64_t &lhs)
Unary negation operator (change sign operator).
Definition: int64x64-128.h:418
impl_type
Type tag for the underlying implementation.
Definition: int64x64-128.h:76
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.
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
std::pair< int64_t, uint64_t > GetHighLow(void) const
Get the high and low portions of this value.
int64x64_t(long int v)
Construct from an integral type.
friend int64x64_t & operator/=(int64x64_t &lhs, const int64x64_t &rhs)
Compound division operator.
Definition: int64x64-128.h:400
int64x64_t(long double v)
Construct from a floating point value.
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
friend int64x64_t & operator-=(int64x64_t &lhs, const int64x64_t &rhs)
Compound subtraction operator.
Definition: int64x64-128.h:382
int64x64_t(unsigned long long int v)
Construct from an integral type.
int64x64_t(unsigned long int v)
Construct from an integral type.
int64x64_t()
Default constructor.
#define HP_MAX_64
Floating point value of HP_MASK_LO + 1 We really want:
Every class exported by the ns3 library is enclosed in the ns3 namespace.
int64x64_t(int v)
Construct from an integral type.
int64x64_t(double v)
Construct from a floating point value.
long double _v
The Q64.64 value.
int64x64_t & operator=(const int64x64_t &o)
Assignment.
Definition: int64x64-128.h:192
int64x64_t(int64_t hi, uint64_t lo)
Construct from explicit high and low values.
Native int128_t implementation.
Definition: int64x64-128.h:77
static int64x64_t Invert(uint64_t v)
Compute the inverse of an integer value.
friend bool operator<(const int64x64_t &lhs, const int64x64_t &rhs)
Less than operator.
Definition: int64x64-128.h:356
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
int64_t GetHigh(void) const
Get the integer portion.
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
int64x64_t(long long int v)
Construct from an integral type.
int64x64_t(unsigned int v)
Construct from an integral type.
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
int64x64_t(const int64x64_t &o)
Copy constructor.
long double implementation.
Definition: int64x64-128.h:79
int64x64_t operator!(const int64x64_t &lhs)
Logical not operator.
Definition: int64x64-128.h:426
static enum impl_type implementation
Type tag for this implementation.
Definition: int64x64-128.h:83