A Discrete-Event Network Simulator
API
int64x64-double.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010 INRIA
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation;
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  *
17  */
18 
19 #include "ns3/core-config.h"
20 #if !defined(INT64X64_DOUBLE_H) && (defined(INT64X64_USE_DOUBLE) || defined(PYTHON_SCAN))
22 #define INT64X64_DOUBLE_H
23 
24 #include <cmath> // pow
25 #include <stdint.h>
26 #include <utility> // pair
27 
35 namespace ns3
36 {
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  {
82  }
83 
93  inline int64x64_t(double value)
94  : _v(value)
95  {
96  }
97 
98  inline int64x64_t(long double value)
99  : _v(value)
100  {
101  }
102 
114  inline int64x64_t(int v)
115  : _v(v)
116  {
117  }
118 
119  inline int64x64_t(long int v)
120  : _v(v)
121  {
122  }
123 
124  inline int64x64_t(long long int v)
125  : _v(static_cast<long double>(v))
126  {
127  }
128 
129  inline int64x64_t(unsigned int v)
130  : _v(v)
131  {
132  }
133 
134  inline int64x64_t(unsigned long int v)
135  : _v(v)
136  {
137  }
138 
139  inline int64x64_t(unsigned long long int v)
140  : _v(static_cast<long double>(v))
141  {
142  }
143 
151  explicit inline int64x64_t(int64_t hi, uint64_t lo)
152  {
153  const bool negative = hi < 0;
154  const long double hild = static_cast<long double>(hi);
155  const long double fhi = negative ? -hild : hild;
156  const long double flo = lo / HP_MAX_64;
157  _v = negative ? -fhi : fhi;
158  _v += flo;
159  // _v = negative ? -_v : _v;
160  }
161 
167  inline int64x64_t(const int64x64_t& o)
168  : _v(o._v)
169  {
170  }
171 
178  inline int64x64_t& operator=(const int64x64_t& o)
179  {
180  _v = o._v;
181  return *this;
182  }
183 
185  inline explicit operator bool() const
186  {
187  return (_v != 0);
188  }
189 
195  inline double GetDouble() const
196  {
197  return (double)_v;
198  }
199 
200  private:
206  std::pair<int64_t, uint64_t> GetHighLow() const
207  {
208  const bool negative = _v < 0;
209  const long double v = negative ? -_v : _v;
210 
211  long double fhi;
212  long double flo = std::modf(v, &fhi);
213  // Add 0.5 to round, which improves the last count
214  // This breaks these tests:
215  // TestSuite devices-mesh-dot11s-regression
216  // TestSuite devices-mesh-flame-regression
217  // TestSuite routing-aodv-regression
218  // TestSuite routing-olsr-regression
219  // Setting round = 0; breaks:
220  // TestSuite int64x64
221  const long double round = 0.5;
222  flo = flo * HP_MAX_64 + round;
223  int64_t hi = static_cast<int64_t>(fhi);
224  uint64_t lo = static_cast<uint64_t>(flo);
225  if (flo >= HP_MAX_64)
226  {
227  // conversion to uint64 rolled over
228  ++hi;
229  }
230  if (negative)
231  {
232  lo = ~lo;
233  hi = ~hi;
234  if (++lo == 0)
235  {
236  ++hi;
237  }
238  }
239  return std::make_pair(hi, lo);
240  }
241 
242  public:
248  inline int64_t GetHigh() const
249  {
250  return GetHighLow().first;
251  }
252 
258  inline uint64_t GetLow() const
259  {
260  return GetHighLow().second;
261  }
262 
268  int64_t GetInt() const
269  {
270  int64_t retval = static_cast<int64_t>(_v);
271  return retval;
272  }
273 
280  int64_t Round() const
281  {
282  int64_t retval = std::round(_v);
283  return retval;
284  }
285 
295  inline void MulByInvert(const int64x64_t& o)
296  {
297  _v *= o._v;
298  }
299 
306  static inline int64x64_t Invert(uint64_t v)
307  {
308  int64x64_t tmp((long double)1 / v);
309  return tmp;
310  }
311 
312  private:
325  friend inline bool operator==(const int64x64_t& lhs, const int64x64_t& rhs)
326  {
327  return lhs._v == rhs._v;
328  }
329 
330  friend inline bool operator<(const int64x64_t& lhs, const int64x64_t& rhs)
331  {
332  return lhs._v < rhs._v;
333  }
334 
335  friend inline bool operator>(const int64x64_t& lhs, const int64x64_t& rhs)
336  {
337  return lhs._v > rhs._v;
338  }
339 
340  friend inline int64x64_t& operator+=(int64x64_t& lhs, const int64x64_t& rhs)
341  {
342  lhs._v += rhs._v;
343  return lhs;
344  }
345 
346  friend inline int64x64_t& operator-=(int64x64_t& lhs, const int64x64_t& rhs)
347  {
348  lhs._v -= rhs._v;
349  return lhs;
350  }
351 
352  friend inline int64x64_t& operator*=(int64x64_t& lhs, const int64x64_t& rhs)
353  {
354  lhs._v *= rhs._v;
355  return lhs;
356  }
357 
358  friend inline int64x64_t& operator/=(int64x64_t& lhs, const int64x64_t& rhs)
359  {
360  lhs._v /= rhs._v;
361  return lhs;
362  }
363 
376  friend inline int64x64_t operator+(const int64x64_t& lhs)
377  {
378  return lhs;
379  }
380 
381  friend inline int64x64_t operator-(const int64x64_t& lhs)
382  {
383  return int64x64_t(-lhs._v);
384  }
385 
386  friend inline int64x64_t operator!(const int64x64_t& lhs)
387  {
388  return int64x64_t(!lhs._v);
389  }
390 
393  long double _v;
394 
395 }; // class int64x64_t
396 
397 } // namespace ns3
398 
399 #endif /* INT64X64_DOUBLE_H */
High precision numerical type, implementing Q64.64 fixed precision.
int64_t GetHigh() const
Get the integer portion.
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.
static const uint64_t HP_MASK_LO
Mask for fraction part.
friend int64x64_t & operator*=(int64x64_t &lhs, const int64x64_t &rhs)
Arithmetic operator.
int64x64_t & operator=(const int64x64_t &o)
Assignment.
impl_type
Type tag for the underlying implementation.
@ int128_impl
Native int128_t implementation.
@ ld_impl
long double implementation
@ cairo_impl
cairo wideint implementation
static int64x64_t Invert(uint64_t v)
Compute the inverse of an integer value.
friend int64x64_t operator+(const int64x64_t &lhs)
Unary operator.
int64x64_t(long double value)
Constructor from a floating point.
int64x64_t(unsigned int v)
Construct from an integral type.
int64_t Round() const
Round to the nearest int.
std::pair< int64_t, uint64_t > GetHighLow() const
Get the high and low portions of this value.
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.
friend int64x64_t & operator+=(int64x64_t &lhs, const int64x64_t &rhs)
Arithmetic operator.
static enum impl_type implementation
Type tag for this implementation.
friend int64x64_t operator!(const int64x64_t &lhs)
Unary operator.
int64x64_t(unsigned long long int v)
Construct from an integral type.
cairo_int128_t _v
The Q64.64 value.
int64x64_t(long long int v)
Construct from an integral type.
friend int64x64_t & operator-=(int64x64_t &lhs, const int64x64_t &rhs)
Arithmetic operator.
int64x64_t(int64_t hi, uint64_t lo)
Construct from explicit high and low values.
double GetDouble() const
Get this value as a double.
int64_t GetInt() const
Truncate to an integer.
friend bool operator>(const int64x64_t &lhs, const int64x64_t &rhs)
Arithmetic operator.
long double _v
The Q64.64 value.
uint64_t GetLow() const
Get the fractional portion of this value, unscaled.
int64x64_t(double value)
Constructor from a floating point.
friend int64x64_t operator-(const int64x64_t &lhs)
Unary operator.
friend int64x64_t & operator/=(int64x64_t &lhs, const int64x64_t &rhs)
Arithmetic operator.
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.