A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
int64x64-cairo.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_CAIRO_H) && defined(INT64X64_USE_CAIRO) && !defined(PYTHON_SCAN)
22#define INT64X64_CAIRO_H
23
25
26#include <cmath> // pow
27
34namespace ns3
35{
36
42{
44 static const uint64_t HPCAIRO_MASK_HI_BIT = (((uint64_t)1) << 63);
46 static const uint64_t HP_MASK_LO = 0xffffffffffffffffULL;
59#define HP_MAX_64 (std::pow(2.0L, 64))
60
61 public:
70 {
74 };
75
77 static const enum impl_type implementation = cairo_impl;
78
80 inline int64x64_t()
81 {
82 _v.hi = 0;
83 _v.lo = 0;
84 }
85
95 inline int64x64_t(const double value)
96 {
97 const int64x64_t tmp((long double)value);
98 _v = tmp._v;
99 }
100
101 inline int64x64_t(const long double value)
102 {
103 const bool negative = value < 0;
104 const long double v = negative ? -value : value;
105
106 long double fhi;
107 long double flo = std::modf(v, &fhi);
108 // Add 0.5 to round, which improves the last count
109 // This breaks these tests:
110 // TestSuite devices-mesh-dot11s-regression
111 // TestSuite devices-mesh-flame-regression
112 // TestSuite routing-aodv-regression
113 // TestSuite routing-olsr-regression
114 // Setting round = 0; breaks:
115 // TestSuite int64x64
116 const long double round = 0.5;
117 flo = flo * HP_MAX_64 + round;
119 const cairo_uint64_t lo = (cairo_uint64_t)flo;
120 if (flo >= HP_MAX_64)
121 {
122 // conversion to uint64 rolled over
123 ++hi;
124 }
125 _v.hi = hi;
126 _v.lo = lo;
127 _v = negative ? _cairo_int128_negate(_v) : _v;
128 }
129
141 inline int64x64_t(const int v)
142 {
143 _v.hi = v;
144 _v.lo = 0;
145 }
146
147 inline int64x64_t(const long int v)
148 {
149 _v.hi = v;
150 _v.lo = 0;
151 }
152
153 inline int64x64_t(const long long int v)
154 {
155 _v.hi = v;
156 _v.lo = 0;
157 }
158
159 inline int64x64_t(const unsigned int v)
160 {
161 _v.hi = v;
162 _v.lo = 0;
163 }
164
165 inline int64x64_t(const unsigned long int v)
166 {
167 _v.hi = v;
168 _v.lo = 0;
169 }
170
171 inline int64x64_t(const unsigned long long int v)
172 {
173 _v.hi = v;
174 _v.lo = 0;
175 }
176
184 explicit inline int64x64_t(const int64_t hi, const uint64_t lo)
185 {
186 _v.hi = hi;
187 _v.lo = lo;
188 }
189
195 inline int64x64_t(const int64x64_t& o)
196 : _v(o._v)
197 {
198 }
199
207 {
208 _v = o._v;
209 return *this;
210 }
211
213 inline explicit operator bool() const
214 {
215 return (_v.hi != 0 || _v.lo != 0);
216 }
217
223 inline double GetDouble() const
224 {
225 const bool negative = _cairo_int128_negative(_v);
226 const cairo_int128_t value = negative ? _cairo_int128_negate(_v) : _v;
227 const long double fhi = static_cast<long double>(value.hi);
228 const long double flo = value.lo / HP_MAX_64;
229 long double retval = fhi;
230 retval += flo;
231 retval = negative ? -retval : retval;
232 return static_cast<double>(retval);
233 }
234
240 inline int64_t GetHigh() const
241 {
242 return (int64_t)_v.hi;
243 }
244
250 inline uint64_t GetLow() const
251 {
252 return _v.lo;
253 }
254
260 int64_t GetInt() const
261 {
262 const bool negative = _cairo_int128_negative(_v);
263 const cairo_int128_t value = negative ? _cairo_int128_negate(_v) : _v;
264 int64_t retval = value.hi;
265 retval = negative ? -retval : retval;
266 return retval;
267 }
268
275 int64_t Round() const
276 {
277 const bool negative = _cairo_int128_negative(_v);
278 cairo_uint128_t value = negative ? _cairo_int128_negate(_v) : _v;
279 cairo_uint128_t half{1ULL << 63, 0}; // lo, hi
280 value = _cairo_uint128_add(value, half);
281 int64_t retval = value.hi;
282 retval = negative ? -retval : retval;
283 return retval;
284 }
285
294 void MulByInvert(const int64x64_t& o);
295
309 static int64x64_t Invert(const uint64_t v);
310
311 private:
324 friend inline bool operator==(const int64x64_t& lhs, const int64x64_t& rhs)
325 {
326 return _cairo_int128_eq(lhs._v, rhs._v);
327 }
328
329 friend inline bool operator<(const int64x64_t& lhs, const int64x64_t& rhs)
330 {
331 return _cairo_int128_lt(lhs._v, rhs._v);
332 }
333
334 friend inline bool operator>(const int64x64_t& lhs, const int64x64_t& rhs)
335 {
336 return _cairo_int128_gt(lhs._v, rhs._v);
337 }
338
339 friend inline int64x64_t& operator+=(int64x64_t& lhs, const int64x64_t& rhs)
340 {
341 lhs._v = _cairo_int128_add(lhs._v, rhs._v);
342 return lhs;
343 }
344
345 friend inline int64x64_t& operator-=(int64x64_t& lhs, const int64x64_t& rhs)
346 {
347 lhs._v = _cairo_int128_sub(lhs._v, rhs._v);
348 return lhs;
349 }
350
351 friend inline int64x64_t& operator*=(int64x64_t& lhs, const int64x64_t& rhs)
352 {
353 lhs.Mul(rhs);
354 return lhs;
355 }
356
357 friend inline int64x64_t& operator/=(int64x64_t& lhs, const int64x64_t& rhs)
358 {
359 lhs.Div(rhs);
360 return lhs;
361 }
362
375 friend inline int64x64_t operator+(const int64x64_t& lhs)
376 {
377 return lhs;
378 }
379
380 friend inline int64x64_t operator-(const int64x64_t& lhs)
381 {
382 int64x64_t tmp = lhs;
383 tmp._v = _cairo_int128_negate(tmp._v);
384 return tmp;
385 }
386
387 friend inline int64x64_t operator!(const int64x64_t& lhs)
388 {
389 return (lhs == int64x64_t()) ? int64x64_t(1, 0) : int64x64_t();
390 }
391
399 void Mul(const int64x64_t& o);
405 void Div(const int64x64_t& o);
430 static cairo_uint128_t Umul(const cairo_uint128_t a, const cairo_uint128_t b);
438 static cairo_uint128_t Udiv(const cairo_uint128_t a, const cairo_uint128_t b);
448 static cairo_uint128_t UmulByInvert(const cairo_uint128_t a, const cairo_uint128_t b);
449
451
452}; // class int64x64_t
453
454} // namespace ns3
455
456#endif /* INT64X64_CAIRO_H */
cairo_x function declarations, which provide the fallback high precision arithmetic implementation.
int cairo_I _cairo_int128_lt(cairo_int128_t a, cairo_int128_t b)
cairo_uint128_t cairo_I _cairo_uint128_add(cairo_uint128_t a, cairo_uint128_t b)
#define _cairo_int128_add(a, b)
#define _cairo_int128_eq(a, b)
#define _cairo_int128_negative(a)
#define _cairo_int128_gt(a, b)
#define _cairo_int128_sub(a, b)
#define _cairo_int128_negate(a)
High precision numerical type, implementing Q64.64 fixed precision.
int64_t GetHigh() const
Get the integer portion.
friend bool operator==(const int64x64_t &lhs, const int64x64_t &rhs)
Arithmetic operator.
static const uint64_t HP_MASK_LO
Mask for fraction part.
static cairo_uint128_t Umul(const cairo_uint128_t a, const cairo_uint128_t b)
Unsigned multiplication of Q64.64 values.
Definition: int64x64-128.cc:71
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 cairo_uint128_t UmulByInvert(const cairo_uint128_t a, const cairo_uint128_t b)
Unsigned multiplication of Q64.64 and Q0.128 values.
friend int64x64_t & operator-=(int64x64_t &lhs, const int64x64_t &rhs)
Arithmetic operator.
friend int64x64_t operator+(const int64x64_t &lhs)
Unary operator.
int64_t Round() const
Round to the nearest int.
void Mul(const int64x64_t &o)
Implement *=.
Definition: int64x64-128.cc:61
void MulByInvert(const int64x64_t &o)
Multiply this value by a Q0.128 value, presumably representing an inverse, completing a division oper...
static cairo_uint128_t Udiv(const cairo_uint128_t a, const cairo_uint128_t b)
Unsigned division of Q64.64 values.
friend bool operator<(const int64x64_t &lhs, const int64x64_t &rhs)
Arithmetic operator.
int64x64_t(const long double value)
Constructor from a floating point.
static enum impl_type implementation
Type tag for this implementation.
friend int64x64_t operator!(const int64x64_t &lhs)
Unary operator.
cairo_int128_t _v
The Q64.64 value.
friend int64x64_t & operator*=(int64x64_t &lhs, const int64x64_t &rhs)
Arithmetic operator.
int64x64_t(const int64_t hi, const uint64_t lo)
Construct from explicit high and low values.
void Div(const int64x64_t &o)
Implement /=.
static const uint64_t HPCAIRO_MASK_HI_BIT
High bit of fractional part.
int64x64_t(const unsigned long long int v)
Construct from an integral type.
int64x64_t(const unsigned int v)
Construct from an integral type.
int64x64_t(const long int v)
Construct from an integral type.
int64x64_t(const long long int v)
Construct from an integral type.
double GetDouble() const
Get this value as a double.
friend int64x64_t & operator+=(int64x64_t &lhs, const int64x64_t &rhs)
Arithmetic operator.
int64x64_t(const unsigned long int v)
Construct from an integral type.
int64_t GetInt() const
Truncate to an integer.
int64x64_t & operator=(const int64x64_t &o)
Assignment.
friend bool operator>(const int64x64_t &lhs, const int64x64_t &rhs)
Arithmetic operator.
int64x64_t(const int v)
Construct from an integral type.
uint64_t GetLow() const
Get the fractional portion of this value, unscaled.
friend int64x64_t operator-(const int64x64_t &lhs)
Unary operator.
static int64x64_t Invert(const uint64_t v)
Compute the inverse of an integer value.
int64x64_t(const double value)
Constructor from a floating point.
int64x64_t(const int64x64_t &o)
Copy constructor.
friend int64x64_t & operator/=(int64x64_t &lhs, const int64x64_t &rhs)
Arithmetic operator.
int64x64_t()
Default constructor.
#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.