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  {
69  int128_impl,
70  cairo_impl,
71  ld_impl,
72  };
73 
75  static const enum impl_type implementation = ld_impl;
76 
78  inline int64x64_t ()
79  : _v (0)
80  {}
87  inline int64x64_t (double v)
88  : _v (v)
89  {}
90  inline int64x64_t (long double v)
91  : _v (v)
92  {}
101  inline int64x64_t (int v)
102  : _v (v)
103  {}
104  inline int64x64_t (long int v)
105  : _v (v)
106  {}
107  inline int64x64_t (long long int v)
108  : _v (static_cast<double> (v))
109  {}
110  inline int64x64_t (unsigned int v)
111  : _v (v)
112  {}
113  inline int64x64_t (unsigned long int v)
114  : _v (v)
115  {}
116  inline int64x64_t (unsigned long long int v)
117  : _v (static_cast<double> (v))
118  {}
126  explicit inline int64x64_t (int64_t hi, uint64_t lo)
127  {
128  const bool negative = hi < 0;
129  const long double hild = static_cast<long double> (hi);
130  const long double fhi = negative ? -hild : hild;
131  const long double flo = lo / HP_MAX_64;
132  _v = negative ? -fhi : fhi;
133  _v += flo;
134  // _v = negative ? -_v : _v;
135  }
136 
142  inline int64x64_t (const int64x64_t & o)
143  : _v (o._v)
144  {}
151  inline int64x64_t & operator = (const int64x64_t & o)
152  {
153  _v = o._v;
154  return *this;
155  }
156 
162  inline double GetDouble (void) const
163  {
164  return (double)_v;
165  }
166 
167 private:
173  std::pair<int64_t, uint64_t> GetHighLow (void) const
174  {
175  const bool negative = _v < 0;
176  const long double v = negative ? -_v : _v;
177 
178  long double fhi;
179  long double flo = std::modf (v, &fhi);
180  // Add 0.5 to round, which improves the last count
181  // This breaks these tests:
182  // TestSuite devices-mesh-dot11s-regression
183  // TestSuite devices-mesh-flame-regression
184  // TestSuite routing-aodv-regression
185  // TestSuite routing-olsr-regression
186  // Setting round = 0; breaks:
187  // TestSuite int64x64
188  const long double round = 0.5;
189  flo = flo * HP_MAX_64 + round;
190  int64_t hi = static_cast<int64_t> (fhi);
191  uint64_t lo = static_cast<uint64_t> (flo);
192  if (flo >= HP_MAX_64)
193  {
194  // conversion to uint64 rolled over
195  ++hi;
196  }
197  if (negative)
198  {
199  lo = ~lo;
200  hi = ~hi;
201  if (++lo == 0)
202  {
203  ++hi;
204  }
205  }
206  return std::make_pair (hi, lo);
207  }
208 
209 public:
215  inline int64_t GetHigh (void) const
216  {
217  return GetHighLow ().first;
218  }
224  inline uint64_t GetLow (void) const
225  {
226  return GetHighLow ().second;
227  }
228 
234  int64_t GetInt (void) const
235  {
236  int64_t retval = static_cast<int64_t> (_v);
237  return retval;
238  }
239 
246  int64_t Round (void) const
247  {
248  int64_t retval = std::round (_v);
249  return retval;
250  }
251 
261  inline void MulByInvert (const int64x64_t & o)
262  {
263  _v *= o._v;
264  }
265 
272  static inline int64x64_t Invert (uint64_t v)
273  {
274  int64x64_t tmp ((long double)1 / v);
275  return tmp;
276  }
277 
278 private:
279 
291  friend bool operator == (const int64x64_t & lhs, const int64x64_t & rhs);
292 
293  friend bool operator < (const int64x64_t & lhs, const int64x64_t & rhs);
294  friend bool operator > (const int64x64_t & lhs, const int64x64_t & rhs);
295 
296  friend int64x64_t & operator += ( int64x64_t & lhs, const int64x64_t & rhs);
297  friend int64x64_t & operator -= ( int64x64_t & lhs, const int64x64_t & rhs);
298  friend int64x64_t & operator *= ( int64x64_t & lhs, const int64x64_t & rhs);
299  friend int64x64_t & operator /= ( int64x64_t & lhs, const int64x64_t & rhs);
312  friend int64x64_t operator - (const int64x64_t & lhs);
313  friend int64x64_t operator ! (const int64x64_t & lhs);
316  long double _v;
317 
318 }; // class int64x64_t
319 
320 
328 inline bool operator == (const int64x64_t & lhs, const int64x64_t & rhs)
329 {
330  return lhs._v == rhs._v;
331 }
339 inline bool operator < (const int64x64_t & lhs, const int64x64_t & rhs)
340 {
341  return lhs._v < rhs._v;
342 }
350 inline bool operator > (const int64x64_t & lhs, const int64x64_t & rhs)
351 {
352  return lhs._v > rhs._v;
353 }
354 
362 inline int64x64_t & operator += (int64x64_t & lhs, const int64x64_t & rhs)
363 {
364  lhs._v += rhs._v;
365  return lhs;
366 }
374 inline int64x64_t & operator -= (int64x64_t & lhs, const int64x64_t & rhs)
375 {
376  lhs._v -= rhs._v;
377  return lhs;
378 }
386 inline int64x64_t & operator *= (int64x64_t & lhs, const int64x64_t & rhs)
387 {
388  lhs._v *= rhs._v;
389  return lhs;
390 }
398 inline int64x64_t & operator /= (int64x64_t & lhs, const int64x64_t & rhs)
399 {
400  lhs._v /= rhs._v;
401  return lhs;
402 }
403 
410 inline int64x64_t operator + (const int64x64_t & lhs)
411 {
412  return lhs;
413 }
420 inline int64x64_t operator - (const int64x64_t & lhs)
421 {
422  return int64x64_t (-lhs._v);
423 }
430 inline int64x64_t operator ! (const int64x64_t & lhs)
431 {
432  return int64x64_t (!lhs._v);
433 }
434 
435 
436 } // namespace ns3
437 
438 #endif /* INT64X64_DOUBLE_H */
int64x64_t & operator+=(int64x64_t &lhs, const int64x64_t &rhs)
Compound addition operator.
Definition: int64x64-128.h:443
int64x64_t operator+(const int64x64_t &lhs)
Unary plus operator.
Definition: int64x64-128.h:491
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:467
int64x64_t operator-(const int64x64_t &lhs)
Unary negation operator (change sign operator).
Definition: int64x64-128.h:501
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:511
int64_t Round(void) const
Round to the nearest int.
friend int64x64_t & operator*=(int64x64_t &lhs, const int64x64_t &rhs)
Compound multiplication operator.
Definition: int64x64-128.h:467
int64_t GetInt(void) const
Truncate to an integer.
friend bool operator==(const int64x64_t &lhs, const int64x64_t &rhs)
Arithmetic operator.
Definition: int64x64-128.h:409
bool operator<(const EventId &a, const EventId &b)
Definition: event-id.h:160
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:479
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:397
friend int64x64_t & operator-=(int64x64_t &lhs, const int64x64_t &rhs)
Compound subtraction operator.
Definition: int64x64-128.h:455
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:195
int64x64_t(int64_t hi, uint64_t lo)
Construct from explicit high and low values.
Native int128_t implementation.
Definition: int64x64-128.h:78
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:420
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:431
bool operator==(const EventId &a, const EventId &b)
Definition: event-id.h:142
Cairo wideint implementation.
Definition: int64x64-128.h:79
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:455
friend int64x64_t & operator+=(int64x64_t &lhs, const int64x64_t &rhs)
Compound addition operator.
Definition: int64x64-128.h:443
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 operator.
Definition: int64x64-128.h:501
int64x64_t & operator/=(int64x64_t &lhs, const int64x64_t &rhs)
Compound division operator.
Definition: int64x64-128.h:479
friend bool operator>(const int64x64_t &lhs, const int64x64_t &rhs)
Greater operator.
Definition: int64x64-128.h:431
int64x64_t(const int64x64_t &o)
Copy constructor.
long double implementation.
Definition: int64x64-128.h:80
int64x64_t operator!(const int64x64_t &lhs)
Logical not operator.
Definition: int64x64-128.h:511
int64_t GetHigh(void) const
Get the integer portion.
static enum impl_type implementation
Type tag for this implementation.
Definition: int64x64-128.h:84