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 (static_cast<double> (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 (static_cast<double> (v)) {}
116  explicit inline int64x64_t (int64_t hi, uint64_t lo)
117  {
118  const bool negative = hi < 0;
119  const long double hild = static_cast<long double> (hi);
120  const long double fhi = negative ? -hild : hild;
121  const long double flo = lo / HP_MAX_64;
122  _v = negative ? - fhi : fhi;
123  _v += flo;
124  // _v = negative ? -_v : _v;
125  }
126 
132  inline int64x64_t (const int64x64_t & o)
133  : _v (o._v) {}
139  inline int64x64_t & operator = (const int64x64_t & o)
140  {
141  _v = o._v;
142  return *this;
143  }
144 
150  inline double GetDouble (void) const
151  {
152  return (double)_v;
153  }
154 private:
160  std::pair<int64_t, uint64_t> GetHighLow (void) const
161  {
162  const bool negative = _v < 0;
163  const long double v = negative ? -_v : _v;
164 
165  long double fhi;
166  long double flo = std::modf (v, &fhi);
167  // Add 0.5 to round, which improves the last count
168  // This breaks these tests:
169  // TestSuite devices-mesh-dot11s-regression
170  // TestSuite devices-mesh-flame-regression
171  // TestSuite routing-aodv-regression
172  // TestSuite routing-olsr-regression
173  // Setting round = 0; breaks:
174  // TestSuite int64x64
175  const long double round = 0.5;
176  flo = flo * HP_MAX_64 + round;
177  int64_t hi = static_cast<int64_t> (fhi);
178  uint64_t lo = static_cast<uint64_t> (flo);
179  if (flo >= HP_MAX_64)
180  {
181  // conversion to uint64 rolled over
182  ++hi;
183  }
184  if (negative)
185  {
186  lo = ~lo;
187  hi = ~hi;
188  if (++lo == 0)
189  {
190  ++hi;
191  }
192  }
193  return std::make_pair (hi, lo);
194  }
195 public:
201  inline int64_t GetHigh (void) const
202  {
203  return GetHighLow ().first;
204  }
210  inline uint64_t GetLow (void) const
211  {
212  return GetHighLow ().second;
213  }
214 
224  inline void MulByInvert (const int64x64_t & o)
225  {
226  _v *= o._v;
227  }
228 
235  static inline int64x64_t Invert (uint64_t v)
236  {
237  int64x64_t tmp ((long double)1 / v);
238  return tmp;
239  }
240 
241 private:
242  friend bool operator == (const int64x64_t & lhs, const int64x64_t & rhs);
243 
244  friend bool operator < (const int64x64_t & lhs, const int64x64_t & rhs);
245  friend bool operator > (const int64x64_t & lhs, const int64x64_t & rhs);
246 
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  friend int64x64_t & operator /= ( int64x64_t & lhs, const int64x64_t & rhs);
251 
252  friend int64x64_t operator - (const int64x64_t & lhs);
253  friend int64x64_t operator ! (const int64x64_t & lhs);
254 
255  long double _v;
256 
257 }; // class int64x64_t
258 
259 
264 inline bool operator == (const int64x64_t & lhs, const int64x64_t & rhs)
265 {
266  return lhs._v == rhs._v;
267 }
272 inline bool operator < (const int64x64_t & lhs, const int64x64_t & rhs)
273 {
274  return lhs._v < rhs._v;
275 }
280 inline bool operator > (const int64x64_t & lhs, const int64x64_t & rhs)
281 {
282  return lhs._v > rhs._v;
283 }
284 
289 inline int64x64_t & operator += (int64x64_t & lhs, const int64x64_t & rhs)
290 {
291  lhs._v += rhs._v;
292  return lhs;
293 }
298 inline int64x64_t & operator -= (int64x64_t & lhs, const int64x64_t & rhs)
299 {
300  lhs._v -= rhs._v;
301  return lhs;
302 }
307 inline int64x64_t & operator *= (int64x64_t & lhs, const int64x64_t & rhs)
308 {
309  lhs._v *= rhs._v;
310  return lhs;
311 }
316 inline int64x64_t & operator /= (int64x64_t & lhs, const int64x64_t & rhs)
317 {
318  lhs._v /= rhs._v;
319  return lhs;
320 }
321 
326 inline int64x64_t operator + (const int64x64_t & lhs)
327 {
328  return lhs;
329 }
334 inline int64x64_t operator - (const int64x64_t & lhs)
335 {
336  return int64x64_t (-lhs._v);
337 }
342 inline int64x64_t operator ! (const int64x64_t & lhs)
343 {
344  return int64x64_t (!lhs._v);
345 }
346 
347 
348 } // namespace ns3
349 
350 #endif /* INT64X64_DOUBLE_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
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...
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
uint64_t GetLow(void) const
Get the fractional portion of this value, unscaled.
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.
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
int64_t GetHigh(void) const
Get the integer portion.
static enum impl_type implementation
Type tag for this implementation.
Definition: int64x64-128.h:83