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)
23#define INT64X64_CAIRO_H
24
25#include <cmath> // pow
26
28
35namespace ns3 {
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
61public:
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 }
94 inline int64x64_t (const double value)
95 {
96 const int64x64_t tmp ((long double)value);
97 _v = tmp._v;
98 }
99 inline int64x64_t (const long double value)
100 {
101 const bool negative = value < 0;
102 const long double v = negative ? -value : value;
103
104 long double fhi;
105 long double flo = std::modf (v, &fhi);
106 // Add 0.5 to round, which improves the last count
107 // This breaks these tests:
108 // TestSuite devices-mesh-dot11s-regression
109 // TestSuite devices-mesh-flame-regression
110 // TestSuite routing-aodv-regression
111 // TestSuite routing-olsr-regression
112 // Setting round = 0; breaks:
113 // TestSuite int64x64
114 const long double round = 0.5;
115 flo = flo * HP_MAX_64 + round;
117 const cairo_uint64_t lo = (cairo_uint64_t)flo;
118 if (flo >= HP_MAX_64)
119 {
120 // conversion to uint64 rolled over
121 ++hi;
122 }
123 _v.hi = hi;
124 _v.lo = lo;
125 _v = negative ? _cairo_int128_negate (_v) : _v;
126 }
138 inline int64x64_t (const int v)
139 {
140 _v.hi = v;
141 _v.lo = 0;
142 }
143 inline int64x64_t (const long int v)
144 {
145 _v.hi = v;
146 _v.lo = 0;
147 }
148 inline int64x64_t (const long long int v)
149 {
150 _v.hi = v;
151 _v.lo = 0;
152 }
153 inline int64x64_t (const unsigned int v)
154 {
155 _v.hi = v;
156 _v.lo = 0;
157 }
158 inline int64x64_t (const unsigned long int v)
159 {
160 _v.hi = v;
161 _v.lo = 0;
162 }
163 inline int64x64_t (const unsigned long long int v)
164 {
165 _v.hi = v;
166 _v.lo = 0;
167 }
175 explicit inline int64x64_t (const int64_t hi, const uint64_t lo)
176 {
177 _v.hi = hi;
178 _v.lo = lo;
179 }
180
186 inline int64x64_t (const int64x64_t & o)
187 : _v (o._v)
188 {}
196 {
197 _v = o._v;
198 return *this;
199 }
200
202 inline explicit operator bool () const
203 {
204 return (_v != 0);
205 }
206
212 inline double GetDouble (void) const
213 {
214 const bool negative = _cairo_int128_negative (_v);
215 const cairo_int128_t value = negative ? _cairo_int128_negate (_v) : _v;
216 const long double fhi = static_cast<long double> (value.hi);
217 const long double flo = value.lo / HP_MAX_64;
218 long double retval = fhi;
219 retval += flo;
220 retval = negative ? -retval : retval;
221 return static_cast<double> (retval);
222 }
228 inline int64_t GetHigh (void) const
229 {
230 return (int64_t)_v.hi;
231 }
237 inline uint64_t GetLow (void) const
238 {
239 return _v.lo;
240 }
241
247 int64_t GetInt (void) const
248 {
249 const bool negative = _cairo_int128_negative (_v);
250 const cairo_int128_t value = negative ? _cairo_int128_negate (_v) : _v;
251 int64_t retval = value.hi;
252 retval = negative ? - retval : retval;
253 return retval;
254 }
255
262 int64_t Round (void) const
263 {
264 const bool negative = _cairo_int128_negative (_v);
265 cairo_uint128_t value = negative ? _cairo_int128_negate (_v) : _v;
266 cairo_uint128_t half {1ULL << 63, 0}; // lo, hi
267 value = _cairo_uint128_add (value, half);
268 int64_t retval = value.hi;
269 retval = negative ? - retval : retval;
270 return retval;
271 }
272
281 void MulByInvert (const int64x64_t & o);
282
296 static int64x64_t Invert (const uint64_t v);
297
298private:
299
311 // *NS_CHECK_STYLE_OFF*
312 friend inline bool operator == (const int64x64_t & lhs, const int64x64_t & rhs) { return _cairo_int128_eq (lhs._v, rhs._v); };
313 friend inline bool operator < (const int64x64_t & lhs, const int64x64_t & rhs) { return _cairo_int128_lt (lhs._v, rhs._v); };
314 friend inline bool operator > (const int64x64_t & lhs, const int64x64_t & rhs) { return _cairo_int128_gt (lhs._v, rhs._v); };
315
316 friend inline int64x64_t & operator += (int64x64_t & lhs, const int64x64_t & rhs)
317 {
318 lhs._v = _cairo_int128_add ( lhs._v, rhs._v );
319 return lhs;
320 };
321 friend inline int64x64_t & operator -= (int64x64_t & lhs, const int64x64_t & rhs)
322 {
323 lhs._v = _cairo_int128_sub ( lhs._v, rhs._v );
324 return lhs;
325 };
326 friend inline int64x64_t & operator *= (int64x64_t & lhs, const int64x64_t & rhs)
327 {
328 lhs.Mul (rhs);
329 return lhs;
330 };
331 friend inline int64x64_t & operator /= (int64x64_t & lhs, const int64x64_t & rhs)
332 {
333 lhs.Div (rhs);
334 return lhs;
335 };
336 // *NS_CHECK_STYLE_ON*
349 friend inline int64x64_t operator + (const int64x64_t & lhs) { return lhs; };
350 friend inline int64x64_t operator - (const int64x64_t & lhs)
351 {
352 int64x64_t tmp = lhs;
353 tmp._v = _cairo_int128_negate (tmp._v);
354 return tmp;
355 };
356 friend inline int64x64_t operator ! (const int64x64_t & lhs)
357 {
358 return (lhs == int64x64_t ()) ? int64x64_t (1, 0) : int64x64_t ();
359 };
367 void Mul (const int64x64_t & o);
373 void Div (const int64x64_t & o);
398 static cairo_uint128_t Umul (const cairo_uint128_t a, const cairo_uint128_t b);
406 static cairo_uint128_t Udiv (const cairo_uint128_t a, const cairo_uint128_t b);
416 static cairo_uint128_t UmulByInvert (const cairo_uint128_t a, const cairo_uint128_t b);
417
419
420}; // class int64x64_t
421
422
423} // namespace ns3
424
425#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)
int64_t cairo_int64_t
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)
uint64_t cairo_uint64_t
#define _cairo_int128_negate(a)
High precision numerical type, implementing Q64.64 fixed precision.
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:73
int64_t GetInt(void) const
Truncate to an integer.
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(void) const
Round to the nearest int.
void Mul(const int64x64_t &o)
Implement *=.
Definition: int64x64-128.cc:64
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.
int64_t GetHigh(void) const
Get the integer portion.
int64x64_t(const int64_t hi, const uint64_t lo)
Construct from explicit high and low values.
void Div(const int64x64_t &o)
Implement /=.
uint64_t GetLow(void) const
Get the fractional portion of this value, unscaled.
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.
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.
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.
double GetDouble(void) const
Get this value as a double.
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.