A Discrete-Event Network Simulator
API
int64x64-cairo.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_CAIRO_H) && defined (INT64X64_USE_CAIRO) && !defined(PYTHON_SCAN)
22 #define INT64X64_CAIRO_H
23 
24 #include <cmath> // pow
25 
26 #include "cairo-wideint-private.h"
27 
34 namespace ns3 {
35 
40 class int64x64_t
41 {
43  static const uint64_t HPCAIRO_MASK_HI_BIT = (((uint64_t)1)<<63);
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  int128_impl,
70  cairo_impl,
71  ld_impl,
72  };
73 
75  static const enum impl_type implementation = cairo_impl;
76 
78  inline int64x64_t ()
79  {
80  _v.hi = 0;
81  _v.lo = 0;
82  }
89  inline int64x64_t (const double value)
90  {
91  const int64x64_t tmp ((long double)value);
92  _v = tmp._v;
93  }
94  inline int64x64_t (const long double value)
95  {
96  const bool negative = value < 0;
97  const long double v = negative ? -value : value;
98 
99  long double fhi;
100  long double flo = std::modf (v, &fhi);
101  // Add 0.5 to round, which improves the last count
102  // This breaks these tests:
103  // TestSuite devices-mesh-dot11s-regression
104  // TestSuite devices-mesh-flame-regression
105  // TestSuite routing-aodv-regression
106  // TestSuite routing-olsr-regression
107  // Setting round = 0; breaks:
108  // TestSuite int64x64
109  const long double round = 0.5;
110  flo = flo * HP_MAX_64 + round;
111  cairo_int64_t hi = (cairo_int64_t)fhi;
112  const cairo_uint64_t lo = (cairo_uint64_t)flo;
113  if (flo >= HP_MAX_64)
114  {
115  // conversion to uint64 rolled over
116  ++hi;
117  }
118  _v.hi = hi;
119  _v.lo = lo;
120  _v = negative ? _cairo_int128_negate (_v) : _v;
121  }
130  inline int64x64_t (const int v)
131  {
132  _v.hi = v;
133  _v.lo = 0;
134  }
135  inline int64x64_t (const long int v)
136  {
137  _v.hi = v;
138  _v.lo = 0;
139  }
140  inline int64x64_t (const long long int v)
141  {
142  _v.hi = v;
143  _v.lo = 0;
144  }
145  inline int64x64_t (const unsigned int v)
146  {
147  _v.hi = v;
148  _v.lo = 0;
149  }
150  inline int64x64_t (const unsigned long int v)
151  {
152  _v.hi = v;
153  _v.lo = 0;
154  }
155  inline int64x64_t (const unsigned long long int v)
156  {
157  _v.hi = v;
158  _v.lo = 0;
159  }
167  explicit inline int64x64_t (const int64_t hi, const uint64_t lo)
168  {
169  _v.hi = hi;
170  _v.lo = lo;
171  }
172 
178  inline int64x64_t (const int64x64_t & o)
179  : _v (o._v) {}
185  inline int64x64_t & operator = (const int64x64_t & o)
186  {
187  _v = o._v;
188  return *this;
189  }
190 
196  inline double GetDouble (void) const
197  {
198  const bool negative = _cairo_int128_negative (_v);
199  const cairo_int128_t value = negative ? _cairo_int128_negate (_v) : _v;
200  const long double fhi = static_cast<long double> (value.hi);
201  const long double flo = value.lo / HP_MAX_64;
202  long double retval = fhi;
203  retval += flo;
204  retval = negative ? -retval : retval;
205  return static_cast<double> (retval);
206  }
212  inline int64_t GetHigh (void) const
213  {
214  return (int64_t)_v.hi;
215  }
221  inline uint64_t GetLow (void) const
222  {
223  return _v.lo;
224  }
225 
234  void MulByInvert (const int64x64_t & o);
235 
249  static int64x64_t Invert (const uint64_t v);
250 
251 private:
252  friend bool operator == (const int64x64_t & lhs, const int64x64_t & rhs);
253 
254  friend bool operator < (const int64x64_t & lhs, const int64x64_t & rhs);
255  friend bool operator > (const int64x64_t & lhs, const int64x64_t & rhs);
256 
257  friend int64x64_t & operator += ( int64x64_t & lhs, const int64x64_t & rhs);
258  friend int64x64_t & operator -= ( int64x64_t & lhs, const int64x64_t & rhs);
259  friend int64x64_t & operator *= ( int64x64_t & lhs, const int64x64_t & rhs);
260  friend int64x64_t & operator /= ( int64x64_t & lhs, const int64x64_t & rhs);
261 
262  friend int64x64_t operator - (const int64x64_t & lhs);
263  friend int64x64_t operator ! (const int64x64_t & lhs);
264 
270  void Mul (const int64x64_t & o);
276  void Div (const int64x64_t & o);
301  static cairo_uint128_t Umul (const cairo_uint128_t a, const cairo_uint128_t b);
309  static cairo_uint128_t Udiv (const cairo_uint128_t a, const cairo_uint128_t b);
319  static cairo_uint128_t UmulByInvert (const cairo_uint128_t a, const cairo_uint128_t b);
320 
322 
323 }; // class int64x64_t
324 
325 
330 inline bool operator == (const int64x64_t & lhs, const int64x64_t & rhs)
331 {
332  return _cairo_int128_eq (lhs._v, rhs._v);
333 }
338 inline bool operator < (const int64x64_t & lhs, const int64x64_t & rhs)
339 {
340  return _cairo_int128_lt (lhs._v, rhs._v);
341 }
346 inline bool operator > (const int64x64_t & lhs, const int64x64_t & rhs)
347 {
348  return _cairo_int128_gt (lhs._v, rhs._v);
349 }
350 
355 inline int64x64_t & operator += (int64x64_t & lhs, const int64x64_t & rhs)
356 {
357  lhs._v = _cairo_int128_add( lhs._v, rhs._v );
358  return lhs;
359 }
364 inline int64x64_t & operator -= (int64x64_t & lhs, const int64x64_t & rhs)
365 {
366  lhs._v = _cairo_int128_sub( lhs._v, rhs._v );
367  return lhs;
368 }
373 inline int64x64_t & operator *= (int64x64_t & lhs, const int64x64_t & rhs)
374 {
375  lhs.Mul (rhs);
376  return lhs;
377 }
382 inline int64x64_t & operator /= (int64x64_t & lhs, const int64x64_t & rhs)
383 {
384  lhs.Div (rhs);
385  return lhs;
386 }
387 
392 inline int64x64_t operator + (const int64x64_t & lhs)
393 {
394  return lhs;
395 }
400 inline int64x64_t operator - (const int64x64_t & lhs)
401 {
402  int64x64_t tmp = lhs;
403  tmp._v = _cairo_int128_negate (tmp._v);
404  return tmp;
405 }
410 inline int64x64_t operator ! (const int64x64_t & lhs)
411 {
412  return (lhs == int64x64_t ()) ? int64x64_t (1, 0) : int64x64_t ();
413 }
414 
415 
416 } // namespace ns3
417 
418 #endif /* INT64X64_CAIRO_H */
int64x64_t & operator+=(int64x64_t &lhs, const int64x64_t &rhs)
Compound addition operator.
Definition: int64x64-128.h:373
cairo_int128_t _v
The Q64.64 value.
int64x64_t operator+(const int64x64_t &lhs)
Unary plus operator.
Definition: int64x64-128.h:410
int64x64_t(const long int v)
Construct from an integral type.
int64x64_t(const long double value)
Construct from a floating point value.
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:391
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:418
static uint128_t Udiv(const uint128_t a, const uint128_t b)
Unsigned division of Q64.64 values.
impl_type
Type tag for the underlying implementation.
Definition: int64x64-128.h:76
int64_t cairo_int64_t
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:426
friend int64x64_t & operator*=(int64x64_t &lhs, const int64x64_t &rhs)
Compound multiplication operator.
Definition: int64x64-128.h:391
friend bool operator==(const int64x64_t &lhs, const int64x64_t &rhs)
Equality operator.
Definition: int64x64-128.h:348
int I _cairo_int128_lt(cairo_int128_t a, cairo_int128_t b)
bool operator<(const EventId &a, const EventId &b)
Definition: event-id.h:153
friend int64x64_t & operator/=(int64x64_t &lhs, const int64x64_t &rhs)
Compound division operator.
Definition: int64x64-128.h:400
int64x64_t(const unsigned long int v)
Construct from an integral type.
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:339
int64x64_t(const int64_t hi, const uint64_t lo)
Construct from explicit high and low values.
friend int64x64_t & operator-=(int64x64_t &lhs, const int64x64_t &rhs)
Compound subtraction operator.
Definition: int64x64-128.h:382
int64x64_t(const long long int v)
Construct from an integral type.
#define _cairo_int128_eq(a, b)
void Mul(const int64x64_t &o)
Implement *=.
Definition: int64x64-128.cc:64
int64x64_t()
Default constructor.
static uint128_t UmulByInvert(const uint128_t a, const uint128_t b)
Unsigned multiplication of Q64.64 and Q0.128 values.
#define _cairo_int128_sub(a, b)
Every class exported by the ns3 library is enclosed in the ns3 namespace.
#define _cairo_int128_negative(a)
int64x64_t(const int v)
Construct from an integral type.
int64x64_t(const unsigned long long int v)
Construct from an integral type.
uint64_t cairo_uint64_t
#define _cairo_int128_gt(a, b)
int64x64_t & operator=(const int64x64_t &o)
Assignment.
Definition: int64x64-128.h:192
cairo_x function declarations, which provide the fallback high precision arithmetic implementation...
static uint128_t Umul(const uint128_t a, const uint128_t b)
Unsigned multiplication of Q64.64 values.
Definition: int64x64-128.cc:73
void Div(const int64x64_t &o)
Implement /=.
Native int128_t implementation.
Definition: int64x64-128.h:77
int64x64_t(const unsigned int v)
Construct from an integral type.
friend bool operator<(const int64x64_t &lhs, const int64x64_t &rhs)
Less than operator.
Definition: int64x64-128.h:356
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:364
bool operator==(const EventId &a, const EventId &b)
Definition: event-id.h:135
#define _cairo_int128_negate(a)
Cairo wideint implementation.
Definition: int64x64-128.h:78
double GetDouble(void) const
Get this value as a double.
#define HP_MAX_64
Floating point value of HP_MASK_LO + 1 We really want:
static const uint64_t HPCAIRO_MASK_HI_BIT
High bit of fractional part.
int64x64_t & operator-=(int64x64_t &lhs, const int64x64_t &rhs)
Compound subtraction operator.
Definition: int64x64-128.h:382
friend int64x64_t & operator+=(int64x64_t &lhs, const int64x64_t &rhs)
Compound addition operator.
Definition: int64x64-128.h:373
#define _cairo_int128_add(a, b)
friend int64x64_t operator-(const int64x64_t &lhs)
Unary negation operator (change sign operator).
Definition: int64x64-128.h:418
int64x64_t & operator/=(int64x64_t &lhs, const int64x64_t &rhs)
Compound division operator.
Definition: int64x64-128.h:400
friend bool operator>(const int64x64_t &lhs, const int64x64_t &rhs)
Greater operator.
Definition: int64x64-128.h:364
int64x64_t(const int64x64_t &o)
Copy constructor.
long double implementation.
Definition: int64x64-128.h:79
int64x64_t operator!(const int64x64_t &lhs)
Logical not operator.
Definition: int64x64-128.h:426
int64x64_t(const double value)
Construct from a floating point value.
int64_t GetHigh(void) const
Get the integer portion.
static enum impl_type implementation
Type tag for this implementation.
Definition: int64x64-128.h:83