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 
238  inline void MulByInvert (const int64x64_t & o)
239  {
240  _v *= o._v;
241  }
242 
249  static inline int64x64_t Invert (uint64_t v)
250  {
251  int64x64_t tmp ((long double)1 / v);
252  return tmp;
253  }
254 
255 private:
256 
268  friend bool operator == (const int64x64_t & lhs, const int64x64_t & rhs);
269 
270  friend bool operator < (const int64x64_t & lhs, const int64x64_t & rhs);
271  friend bool operator > (const int64x64_t & lhs, const int64x64_t & rhs);
272 
273  friend int64x64_t & operator += ( int64x64_t & lhs, const int64x64_t & rhs);
274  friend int64x64_t & operator -= ( int64x64_t & lhs, const int64x64_t & rhs);
275  friend int64x64_t & operator *= ( int64x64_t & lhs, const int64x64_t & rhs);
276  friend int64x64_t & operator /= ( int64x64_t & lhs, const int64x64_t & rhs);
289  friend int64x64_t operator - (const int64x64_t & lhs);
290  friend int64x64_t operator ! (const int64x64_t & lhs);
293  long double _v;
294 
295 }; // class int64x64_t
296 
297 
305 inline bool operator == (const int64x64_t & lhs, const int64x64_t & rhs)
306 {
307  return lhs._v == rhs._v;
308 }
316 inline bool operator < (const int64x64_t & lhs, const int64x64_t & rhs)
317 {
318  return lhs._v < rhs._v;
319 }
327 inline bool operator > (const int64x64_t & lhs, const int64x64_t & rhs)
328 {
329  return lhs._v > rhs._v;
330 }
331 
339 inline int64x64_t & operator += (int64x64_t & lhs, const int64x64_t & rhs)
340 {
341  lhs._v += rhs._v;
342  return lhs;
343 }
351 inline int64x64_t & operator -= (int64x64_t & lhs, const int64x64_t & rhs)
352 {
353  lhs._v -= rhs._v;
354  return lhs;
355 }
363 inline int64x64_t & operator *= (int64x64_t & lhs, const int64x64_t & rhs)
364 {
365  lhs._v *= rhs._v;
366  return lhs;
367 }
375 inline int64x64_t & operator /= (int64x64_t & lhs, const int64x64_t & rhs)
376 {
377  lhs._v /= rhs._v;
378  return lhs;
379 }
380 
387 inline int64x64_t operator + (const int64x64_t & lhs)
388 {
389  return lhs;
390 }
397 inline int64x64_t operator - (const int64x64_t & lhs)
398 {
399  return int64x64_t (-lhs._v);
400 }
407 inline int64x64_t operator ! (const int64x64_t & lhs)
408 {
409  return int64x64_t (!lhs._v);
410 }
411 
412 
413 } // namespace ns3
414 
415 #endif /* INT64X64_DOUBLE_H */
int64x64_t & operator+=(int64x64_t &lhs, const int64x64_t &rhs)
Compound addition operator.
Definition: int64x64-128.h:412
int64x64_t operator+(const int64x64_t &lhs)
Unary plus operator.
Definition: int64x64-128.h:460
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:436
int64x64_t operator-(const int64x64_t &lhs)
Unary negation operator (change sign operator).
Definition: int64x64-128.h:470
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:480
friend int64x64_t & operator*=(int64x64_t &lhs, const int64x64_t &rhs)
Compound multiplication operator.
Definition: int64x64-128.h:436
friend bool operator==(const int64x64_t &lhs, const int64x64_t &rhs)
Arithmetic operator.
Definition: int64x64-128.h:378
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:448
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:366
friend int64x64_t & operator-=(int64x64_t &lhs, const int64x64_t &rhs)
Compound subtraction operator.
Definition: int64x64-128.h:424
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:389
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:400
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:424
friend int64x64_t & operator+=(int64x64_t &lhs, const int64x64_t &rhs)
Compound addition operator.
Definition: int64x64-128.h:412
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:470
int64x64_t & operator/=(int64x64_t &lhs, const int64x64_t &rhs)
Compound division operator.
Definition: int64x64-128.h:448
friend bool operator>(const int64x64_t &lhs, const int64x64_t &rhs)
Greater operator.
Definition: int64x64-128.h:400
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:480
int64_t GetHigh(void) const
Get the integer portion.
static enum impl_type implementation
Type tag for this implementation.
Definition: int64x64-128.h:84