A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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  int128_impl,
69  cairo_impl,
70  ld_impl,
71  };
72 
74  static const enum impl_type implementation = ld_impl;
75 
77  inline int64x64_t ()
78  : _v (0) {}
85  inline int64x64_t (double v)
86  : _v (v) {}
87  inline int64x64_t (long double v)
88  : _v (v) {}
97  inline int64x64_t (int v)
98  : _v (v) {}
99  inline int64x64_t (long int v)
100  : _v (v) {}
101  inline int64x64_t (long long int v)
102  : _v (v) {}
103  inline int64x64_t (unsigned int v)
104  : _v (v) {}
105  inline int64x64_t (unsigned long int v)
106  : _v (v) {}
107  inline int64x64_t (unsigned long long int v)
108  : _v (v) {}
116  explicit inline int64x64_t (int64_t hi, uint64_t lo)
117  {
118  const bool negative = hi < 0;
119  const long double fhi = negative ? -hi : hi;
120  const long double flo = lo / HP_MAX_64;
121  _v = negative ? - fhi : fhi;
122  _v += flo;
123  // _v = negative ? -_v : _v;
124  }
125 
131  inline int64x64_t (const int64x64_t & o)
132  : _v (o._v) {}
138  inline int64x64_t & operator = (const int64x64_t & o)
139  {
140  _v = o._v;
141  return *this;
142  }
143 
149  inline double GetDouble (void) const
150  {
151  return (double)_v;
152  }
153 private:
159  std::pair<int64_t, uint64_t> GetHighLow (void) const
160  {
161  const bool negative = _v < 0;
162  const long double v = negative ? -_v : _v;
163 
164  long double fhi;
165  long double flo = std::modf (v, &fhi);
166  // Add 0.5 to round, which improves the last count
167  // This breaks these tests:
168  // TestSuite devices-mesh-dot11s-regression
169  // TestSuite devices-mesh-flame-regression
170  // TestSuite routing-aodv-regression
171  // TestSuite routing-olsr-regression
172  // Setting round = 0; breaks:
173  // TestSuite int64x64
174  const long double round = 0.5;
175  flo = flo * HP_MAX_64 + round;
176  int64_t hi = fhi;
177  uint64_t lo = flo;
178  if (flo >= HP_MAX_64)
179  {
180  // conversion to uint64 rolled over
181  ++hi;
182  }
183  if (negative)
184  {
185  lo = ~lo;
186  hi = ~hi;
187  if (++lo == 0)
188  {
189  ++hi;
190  }
191  }
192  return std::make_pair (hi, lo);
193  }
194 public:
200  inline int64_t GetHigh (void) const
201  {
202  return GetHighLow ().first;
203  }
209  inline uint64_t GetLow (void) const
210  {
211  return GetHighLow ().second;
212  }
213 
223  inline void MulByInvert (const int64x64_t & o)
224  {
225  _v *= o._v;
226  }
227 
234  static inline int64x64_t Invert (uint64_t v)
235  {
236  int64x64_t tmp ((long double)1 / v);
237  return tmp;
238  }
239 
240 private:
241  friend bool operator == (const int64x64_t & lhs, const int64x64_t & rhs);
242 
243  friend bool operator < (const int64x64_t & lhs, const int64x64_t & rhs);
244  friend bool operator > (const int64x64_t & lhs, const int64x64_t & rhs);
245 
246  friend int64x64_t & operator += ( int64x64_t & lhs, const int64x64_t & rhs);
247  friend int64x64_t & operator -= ( int64x64_t & lhs, const int64x64_t & rhs);
248  friend int64x64_t & operator *= ( int64x64_t & lhs, const int64x64_t & rhs);
249  friend int64x64_t & operator /= ( int64x64_t & lhs, const int64x64_t & rhs);
250 
251  friend int64x64_t operator - (const int64x64_t & lhs);
252  friend int64x64_t operator ! (const int64x64_t & lhs);
253 
254  long double _v;
255 
256 }; // class int64x64_t
257 
258 
263 inline bool operator == (const int64x64_t & lhs, const int64x64_t & rhs)
264 {
265  return lhs._v == rhs._v;
266 }
271 inline bool operator < (const int64x64_t & lhs, const int64x64_t & rhs)
272 {
273  return lhs._v < rhs._v;
274 }
279 inline bool operator > (const int64x64_t & lhs, const int64x64_t & rhs)
280 {
281  return lhs._v > rhs._v;
282 }
283 
288 inline int64x64_t & operator += (int64x64_t & lhs, const int64x64_t & rhs)
289 {
290  lhs._v += rhs._v;
291  return lhs;
292 }
297 inline int64x64_t & operator -= (int64x64_t & lhs, const int64x64_t & rhs)
298 {
299  lhs._v -= rhs._v;
300  return lhs;
301 }
306 inline int64x64_t & operator *= (int64x64_t & lhs, const int64x64_t & rhs)
307 {
308  lhs._v *= rhs._v;
309  return lhs;
310 }
315 inline int64x64_t & operator /= (int64x64_t & lhs, const int64x64_t & rhs)
316 {
317  lhs._v /= rhs._v;
318  return lhs;
319 }
320 
325 inline int64x64_t operator + (const int64x64_t & lhs)
326 {
327  return lhs;
328 }
333 inline int64x64_t operator - (const int64x64_t & lhs)
334 {
335  return int64x64_t (-lhs._v);
336 }
341 inline int64x64_t operator ! (const int64x64_t & lhs)
342 {
343  return int64x64_t (!lhs._v);
344 }
345 
346 
347 } // namespace ns3
348 
349 #endif /* INT64X64_DOUBLE_H */
High precision numerical type, implementing Q64.64 fixed precision.
bool operator>(const Time &lhs, const Time &rhs)
Definition: nstime.h:752
Time & operator+=(Time &lhs, const Time &rhs)
Definition: nstime.h:791
TracedValue< T > & operator/=(TracedValue< T > &lhs, const U &rhs)
Operator assignment for TracedValue.
Definition: traced-value.h:673
int64x64_t operator+(const int64x64_t &lhs, const int64x64_t &rhs)
Addition operator.
Definition: int64x64.h:86
TracedValue< T > operator!(const TracedValue< T > &lhs)
Unary arithmetic operator for TracedValue.
Definition: traced-value.h:763
int64x64_t operator-(const int64x64_t &lhs, const int64x64_t &rhs)
Subtraction operator.
Definition: int64x64.h:97
Time & operator-=(Time &lhs, const Time &rhs)
Definition: nstime.h:796
bool operator==(const BleAccessAddress &a, const BleAccessAddress &b)
TracedValue< T > & operator*=(TracedValue< T > &lhs, const U &rhs)
Operator assignment for TracedValue.
Definition: traced-value.h:664
bool operator<(const BleAccessAddress &a, const BleAccessAddress &b)