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 
28 
29 namespace ns3 {
30 
35 class int64x64_t
36 {
38  static const uint64_t HP_MASK_LO = 0xffffffffffffffffULL;
51 #define HP_MAX_64 (std::pow (2.0L, 64))
52 
53 public:
61  enum impl_type {
62  int128_impl,
63  cairo_impl,
64  ld_impl,
65  };
66 
68  static const enum impl_type implementation = ld_impl;
69 
71  inline int64x64_t ()
72  : _v (0) {}
79  inline int64x64_t (double v)
80  : _v (v) {}
81  inline int64x64_t (long double v)
82  : _v (v) {}
91  inline int64x64_t (int v)
92  : _v (v) {}
93  inline int64x64_t (long int v)
94  : _v (v) {}
95  inline int64x64_t (long long int v)
96  : _v (v) {}
97  inline int64x64_t (unsigned int v)
98  : _v (v) {}
99  inline int64x64_t (unsigned long int v)
100  : _v (v) {}
101  inline int64x64_t (unsigned long long int v)
102  : _v (v) {}
110  explicit inline int64x64_t (int64_t hi, uint64_t lo)
111  {
112  const bool negative = hi < 0;
113  const long double fhi = negative ? -hi : hi;
114  const long double flo = lo / HP_MAX_64;
115  _v = negative ? - fhi : fhi;
116  _v += flo;
117  // _v = negative ? -_v : _v;
118  }
119 
125  inline int64x64_t (const int64x64_t & o)
126  : _v (o._v) {}
132  inline int64x64_t & operator = (const int64x64_t & o)
133  {
134  _v = o._v;
135  return *this;
136  }
137 
143  inline double GetDouble (void) const
144  {
145  return (double)_v;
146  }
147 private:
153  std::pair<int64_t, uint64_t> GetHighLow (void) const
154  {
155  const bool negative = _v < 0;
156  const long double v = negative ? -_v : _v;
157 
158  long double fhi;
159  long double flo = std::modf (v, &fhi);
160  // Add 0.5 to round, which improves the last count
161  // This breaks these tests:
162  // TestSuite devices-mesh-dot11s-regression
163  // TestSuite devices-mesh-flame-regression
164  // TestSuite routing-aodv-regression
165  // TestSuite routing-olsr-regression
166  // Setting round = 0; breaks:
167  // TestSuite int64x64
168  const long double round = 0.5;
169  flo = flo * HP_MAX_64 + round;
170  int64_t hi = fhi;
171  uint64_t lo = flo;
172  if (flo >= HP_MAX_64)
173  {
174  // conversion to uint64 rolled over
175  ++hi;
176  }
177  if (negative)
178  {
179  lo = ~lo;
180  hi = ~hi;
181  if (++lo == 0)
182  {
183  ++hi;
184  }
185  }
186  return std::make_pair (hi, lo);
187  }
188 public:
194  inline int64_t GetHigh (void) const
195  {
196  return GetHighLow ().first;
197  }
203  inline uint64_t GetLow (void) const
204  {
205  return GetHighLow ().second;
206  }
207 
217  inline void MulByInvert (const int64x64_t & o)
218  {
219  _v *= o._v;
220  }
221 
228  static inline int64x64_t Invert (uint64_t v)
229  {
230  int64x64_t tmp ((long double)1 / v);
231  return tmp;
232  }
233 
234 private:
235  friend bool operator == (const int64x64_t & lhs, const int64x64_t & rhs);
236 
237  friend bool operator < (const int64x64_t & lhs, const int64x64_t & rhs);
238  friend bool operator > (const int64x64_t & lhs, const int64x64_t & rhs);
239 
240  friend int64x64_t & operator += ( int64x64_t & lhs, const int64x64_t & rhs);
241  friend int64x64_t & operator -= ( int64x64_t & lhs, const int64x64_t & rhs);
242  friend int64x64_t & operator *= ( int64x64_t & lhs, const int64x64_t & rhs);
243  friend int64x64_t & operator /= ( int64x64_t & lhs, const int64x64_t & rhs);
244 
245  friend int64x64_t operator - (const int64x64_t & lhs);
246  friend int64x64_t operator ! (const int64x64_t & lhs);
247 
248  long double _v;
249 
250 }; // class int64x64_t
251 
252 
257 inline bool operator == (const int64x64_t & lhs, const int64x64_t & rhs)
258 {
259  return lhs._v == rhs._v;
260 }
265 inline bool operator < (const int64x64_t & lhs, const int64x64_t & rhs)
266 {
267  return lhs._v < rhs._v;
268 }
273 inline bool operator > (const int64x64_t & lhs, const int64x64_t & rhs)
274 {
275  return lhs._v > rhs._v;
276 }
277 
282 inline int64x64_t & operator += (int64x64_t & lhs, const int64x64_t & rhs)
283 {
284  lhs._v += rhs._v;
285  return lhs;
286 }
291 inline int64x64_t & operator -= (int64x64_t & lhs, const int64x64_t & rhs)
292 {
293  lhs._v -= rhs._v;
294  return lhs;
295 }
300 inline int64x64_t & operator *= (int64x64_t & lhs, const int64x64_t & rhs)
301 {
302  lhs._v *= rhs._v;
303  return lhs;
304 }
309 inline int64x64_t & operator /= (int64x64_t & lhs, const int64x64_t & rhs)
310 {
311  lhs._v /= rhs._v;
312  return lhs;
313 }
314 
319 inline int64x64_t operator + (const int64x64_t & lhs)
320 {
321  return lhs;
322 }
327 inline int64x64_t operator - (const int64x64_t & lhs)
328 {
329  return int64x64_t (-lhs._v);
330 }
335 inline int64x64_t operator ! (const int64x64_t & lhs)
336 {
337  return int64x64_t (!lhs._v);
338 }
339 
340 
341 } // namespace ns3
342 
343 #endif /* INT64X64_DOUBLE_H */
long double implementation.
Definition: int64x64-128.h:73
uint64_t GetLow(void) const
Get the fractional portion of this value, unscaled.
Definition: int64x64-128.h:223
int64x64_t operator+(const int64x64_t &lhs)
Unary plus operator.
Definition: int64x64-128.h:404
int64x64_t & operator/=(int64x64_t &lhs, const int64x64_t &rhs)
Compound division operator.
Definition: int64x64-128.h:394
static int64x64_t Invert(const uint64_t v)
Compute the inverse of an integer value.
int64x64_t operator-(const int64x64_t &lhs)
Unary negation operator (change sign operator).
Definition: int64x64-128.h:412
impl_type
Type tag for the underlying implementation.
Definition: int64x64-128.h:70
int64x64_t & operator+=(int64x64_t &lhs, const int64x64_t &rhs)
Compound addition operator.
Definition: int64x64-128.h:367
static enum impl_type implementation
Type tag for this implementation.
Definition: int64x64-128.h:77
void MulByInvert(const int64x64_t &o)
Multiply this value by a Q0.128 value, presumably representing an inverse, completing a division oper...
double GetDouble(void) const
Get this value as a double.
Definition: int64x64-128.h:197
friend int64x64_t operator!(const int64x64_t &lhs)
Logical not operator.
Definition: int64x64-128.h:420
friend int64x64_t & operator*=(int64x64_t &lhs, const int64x64_t &rhs)
Compound multiplication operator.
Definition: int64x64-128.h:385
friend bool operator==(const int64x64_t &lhs, const int64x64_t &rhs)
Equality operator.
Definition: int64x64-128.h:342
friend int64x64_t & operator/=(int64x64_t &lhs, const int64x64_t &rhs)
Compound division operator.
Definition: int64x64-128.h:394
int128_t _v
The Q64.64 value.
Definition: int64x64-128.h:333
friend int64x64_t & operator-=(int64x64_t &lhs, const int64x64_t &rhs)
Compound subtraction operator.
Definition: int64x64-128.h:376
int64x64_t()
Default constructor.
Definition: int64x64-128.h:80
bool operator<(const int64x64_t &lhs, const int64x64_t &rhs)
Less than operator.
Definition: int64x64-128.h:350
Every class exported by the ns3 library is enclosed in the ns3 namespace.
int64x64_t & operator*=(int64x64_t &lhs, const int64x64_t &rhs)
Compound multiplication operator.
Definition: int64x64-128.h:385
Native int128_t implementation.
Definition: int64x64-128.h:71
static const uint64_t HP_MASK_LO
Mask for fraction part.
Definition: int64x64-128.h:44
int64x64_t & operator=(const int64x64_t &o)
Assignment.
Definition: int64x64-128.h:186
#define HP_MAX_64
Floating point value of HP_MASK_LO + 1.
Definition: int64x64-128.h:60
friend bool operator<(const int64x64_t &lhs, const int64x64_t &rhs)
Less than operator.
Definition: int64x64-128.h:350
bool operator>(const int64x64_t &lhs, const int64x64_t &rhs)
Greater operator.
Definition: int64x64-128.h:358
bool operator==(const EventId &a, const EventId &b)
Definition: event-id.cc:95
int64_t GetHigh(void) const
Get the integer portion.
Definition: int64x64-128.h:213
Cairo wideint implementation.
Definition: int64x64-128.h:72
friend int64x64_t & operator+=(int64x64_t &lhs, const int64x64_t &rhs)
Compound addition operator.
Definition: int64x64-128.h:367
friend int64x64_t operator-(const int64x64_t &lhs)
Unary negation operator (change sign operator).
Definition: int64x64-128.h:412
friend bool operator>(const int64x64_t &lhs, const int64x64_t &rhs)
Greater operator.
Definition: int64x64-128.h:358
int64x64_t operator!(const int64x64_t &lhs)
Logical not operator.
Definition: int64x64-128.h:420
int64x64_t & operator-=(int64x64_t &lhs, const int64x64_t &rhs)
Compound subtraction operator.
Definition: int64x64-128.h:376