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))
23 #define INT64X64_DOUBLE_H
24 
25 #include <stdint.h>
26 #include <cmath> // pow
27 #include <utility> // pair
28 
36 namespace ns3 {
37 
42 class int64x64_t
43 {
45  static const uint64_t HP_MASK_LO = 0xffffffffffffffffULL;
58 #define HP_MAX_64 (std::pow (2.0L, 64))
59 
60 public:
68  enum impl_type
69  {
70  int128_impl,
71  cairo_impl,
72  ld_impl,
73  };
74 
76  static const enum impl_type implementation = ld_impl;
77 
79  inline int64x64_t ()
80  : _v (0)
81  {}
91  inline int64x64_t (double value)
92  : _v (value)
93  {}
94  inline int64x64_t (long double value)
95  : _v (value)
96  {}
108  inline int64x64_t (int v)
109  : _v (v)
110  {}
111  inline int64x64_t (long int v)
112  : _v (v)
113  {}
114  inline int64x64_t (long long int v)
115  : _v (static_cast<long double> (v))
116  {}
117  inline int64x64_t (unsigned int v)
118  : _v (v)
119  {}
120  inline int64x64_t (unsigned long int v)
121  : _v (v)
122  {}
123  inline int64x64_t (unsigned long long int v)
124  : _v (static_cast<long double> (v))
125  {}
133  explicit inline int64x64_t (int64_t hi, uint64_t lo)
134  {
135  const bool negative = hi < 0;
136  const long double hild = static_cast<long double> (hi);
137  const long double fhi = negative ? -hild : hild;
138  const long double flo = lo / HP_MAX_64;
139  _v = negative ? -fhi : fhi;
140  _v += flo;
141  // _v = negative ? -_v : _v;
142  }
143 
149  inline int64x64_t (const int64x64_t & o)
150  : _v (o._v)
151  {}
158  inline int64x64_t & operator = (const int64x64_t & o)
159  {
160  _v = o._v;
161  return *this;
162  }
163 
165  inline explicit operator bool () const
166  {
167  return (_v != 0);
168  }
169 
175  inline double GetDouble (void) const
176  {
177  return (double)_v;
178  }
179 
180 private:
186  std::pair<int64_t, uint64_t> GetHighLow (void) const
187  {
188  const bool negative = _v < 0;
189  const long double v = negative ? -_v : _v;
190 
191  long double fhi;
192  long double flo = std::modf (v, &fhi);
193  // Add 0.5 to round, which improves the last count
194  // This breaks these tests:
195  // TestSuite devices-mesh-dot11s-regression
196  // TestSuite devices-mesh-flame-regression
197  // TestSuite routing-aodv-regression
198  // TestSuite routing-olsr-regression
199  // Setting round = 0; breaks:
200  // TestSuite int64x64
201  const long double round = 0.5;
202  flo = flo * HP_MAX_64 + round;
203  int64_t hi = static_cast<int64_t> (fhi);
204  uint64_t lo = static_cast<uint64_t> (flo);
205  if (flo >= HP_MAX_64)
206  {
207  // conversion to uint64 rolled over
208  ++hi;
209  }
210  if (negative)
211  {
212  lo = ~lo;
213  hi = ~hi;
214  if (++lo == 0)
215  {
216  ++hi;
217  }
218  }
219  return std::make_pair (hi, lo);
220  }
221 
222 public:
228  inline int64_t GetHigh (void) const
229  {
230  return GetHighLow ().first;
231  }
237  inline uint64_t GetLow (void) const
238  {
239  return GetHighLow ().second;
240  }
241 
247  int64_t GetInt (void) const
248  {
249  int64_t retval = static_cast<int64_t> (_v);
250  return retval;
251  }
252 
259  int64_t Round (void) const
260  {
261  int64_t retval = std::round (_v);
262  return retval;
263  }
264 
274  inline void MulByInvert (const int64x64_t & o)
275  {
276  _v *= o._v;
277  }
278 
285  static inline int64x64_t Invert (uint64_t v)
286  {
287  int64x64_t tmp ((long double)1 / v);
288  return tmp;
289  }
290 
291 private:
292 
297  /*
298  * @{
299  * Arithmetic operator.
300  * \param [in] lhs Left hand argument
301  * \param [in] rhs Right hand argument
302  * \return The result of the operator.
303  */
304  // *NS_CHECK_STYLE_OFF*
305  friend inline bool operator == (const int64x64_t & lhs, const int64x64_t & rhs) { return lhs._v == rhs._v; };
306  friend inline bool operator < (const int64x64_t & lhs, const int64x64_t & rhs) { return lhs._v < rhs._v; };
307  friend inline bool operator > (const int64x64_t & lhs, const int64x64_t & rhs) { return lhs._v > rhs._v; };
308 
309  friend inline int64x64_t & operator += ( int64x64_t & lhs, const int64x64_t & rhs)
310  {
311  lhs._v += rhs._v;
312  return lhs;
313  };
314  friend inline int64x64_t & operator -= ( int64x64_t & lhs, const int64x64_t & rhs)
315  {
316  lhs._v -= rhs._v;
317  return lhs;
318  };
319  friend inline int64x64_t & operator *= ( int64x64_t & lhs, const int64x64_t & rhs)
320  {
321  lhs._v *= rhs._v;
322  return lhs;
323  };
324  friend inline int64x64_t & operator /= ( int64x64_t & lhs, const int64x64_t & rhs)
325  {
326  lhs._v /= rhs._v;
327  return lhs;
328  };
329  // *NS_CHECK_STYLE_ON*
336  /*
337  * @{
338  * Unary operator.
339  * \param [in] lhs Left hand argument
340  * \return The result of the operator.
341  */
342  friend inline int64x64_t operator + (const int64x64_t & lhs) { return lhs; };
343  friend inline int64x64_t operator - (const int64x64_t & lhs) { return int64x64_t (-lhs._v); };
344  friend inline int64x64_t operator ! (const int64x64_t & lhs) { return int64x64_t (!lhs._v); };
347  long double _v;
348 
349 }; // class int64x64_t
350 
351 
352 
353 } // namespace ns3
354 
355 #endif /* INT64X64_DOUBLE_H */
High precision numerical type, implementing Q64.64 fixed precision.
Definition: int64x64-128.h:56
int64x64_t(unsigned long int v)
Construct from an integral type.
int64x64_t(int v)
Construct from an integral type.
friend bool operator==(const int64x64_t &lhs, const int64x64_t &rhs)
Arithmetic operator.
Definition: int64x64-128.h:333
static const uint64_t HP_MASK_LO
Mask for fraction part.
Definition: int64x64-128.h:60
friend int64x64_t & operator*=(int64x64_t &lhs, const int64x64_t &rhs)
Arithmetic operator.
Definition: int64x64-128.h:347
int64_t GetInt(void) const
Truncate to an integer.
int64x64_t & operator=(const int64x64_t &o)
Assignment.
Definition: int64x64-128.h:214
impl_type
Type tag for the underlying implementation.
Definition: int64x64-128.h:87
@ int128_impl
Native int128_t implementation.
Definition: int64x64-128.h:88
@ ld_impl
long double implementation.
Definition: int64x64-128.h:90
@ cairo_impl
Cairo wideint implementation.
Definition: int64x64-128.h:89
static int64x64_t Invert(uint64_t v)
Compute the inverse of an integer value.
friend int64x64_t operator+(const int64x64_t &lhs)
Unary operator.
Definition: int64x64-128.h:370
int64_t Round(void) const
Round to the nearest int.
int64x64_t(long double value)
Constructor from a floating point.
int64x64_t(unsigned int v)
Construct from an integral type.
void MulByInvert(const int64x64_t &o)
Multiply this value by a Q0.128 value, presumably representing an inverse, completing a division oper...
friend bool operator<(const int64x64_t &lhs, const int64x64_t &rhs)
Arithmetic operator.
Definition: int64x64-128.h:334
friend int64x64_t & operator+=(int64x64_t &lhs, const int64x64_t &rhs)
Arithmetic operator.
Definition: int64x64-128.h:337
static enum impl_type implementation
Type tag for this implementation.
Definition: int64x64-128.h:94
friend int64x64_t operator!(const int64x64_t &lhs)
Unary operator.
Definition: int64x64-128.h:372
int128_t _v
The Q64.64 value.
Definition: int64x64-128.h:431
int64x64_t(unsigned long long int v)
Construct from an integral type.
int64_t GetHigh(void) const
Get the integer portion.
std::pair< int64_t, uint64_t > GetHighLow(void) const
Get the high and low portions of this value.
int64x64_t(long long int v)
Construct from an integral type.
uint64_t GetLow(void) const
Get the fractional portion of this value, unscaled.
friend int64x64_t & operator-=(int64x64_t &lhs, const int64x64_t &rhs)
Arithmetic operator.
Definition: int64x64-128.h:342
int64x64_t(int64_t hi, uint64_t lo)
Construct from explicit high and low values.
friend bool operator>(const int64x64_t &lhs, const int64x64_t &rhs)
Arithmetic operator.
Definition: int64x64-128.h:335
long double _v
The Q64.64 value.
double GetDouble(void) const
Get this value as a double.
int64x64_t(double value)
Constructor from a floating point.
friend int64x64_t operator-(const int64x64_t &lhs)
Unary operator.
Definition: int64x64-128.h:371
friend int64x64_t & operator/=(int64x64_t &lhs, const int64x64_t &rhs)
Arithmetic operator.
Definition: int64x64-128.h:352
int64x64_t(const int64x64_t &o)
Copy constructor.
int64x64_t()
Default constructor.
int64x64_t(long int v)
Construct from an integral type.
#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.