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)
21/** Using the ns3::int64x64_t based on Cairo 128-bit integers. */
22#define INT64X64_CAIRO_H
23
25
26#include <cmath> // pow
27
28/**
29 * \file
30 * \ingroup highprec
31 * Declaration of the ns3::int64x64_t type using the Cairo implementation.
32 */
33
34namespace ns3
35{
36
37/**
38 * \internal
39 * The implementation documented here uses cairo 128-bit integers.
40 */
41class int64x64_t
42{
43 /// High bit of fractional part
44 static const uint64_t HPCAIRO_MASK_HI_BIT = (((uint64_t)1) << 63);
45 /// Mask for fraction part
46 static const uint64_t HP_MASK_LO = 0xffffffffffffffffULL;
47 /**
48 * Floating point value of HP_MASK_LO + 1
49 * We really want:
50 * \code
51 * static const long double HP_MAX_64 = std:pow (2.0L, 64);
52 * \endcode
53 * but we can't call functions in const definitions,
54 * We could make this a static and initialize in int64x64-cairo.cc or
55 * int64x64.cc, but this requires handling static initialization order
56 * when most of the implementation is inline. Instead, we resort to
57 * this define.
58 */
59#define HP_MAX_64 (std::pow(2.0L, 64))
60
61 public:
62 /**
63 * Type tag for the underlying implementation.
64 *
65 * A few testcases are sensitive to implementation,
66 * specifically the double implementation. To handle this,
67 * we expose the underlying implementation type here.
68 */
70 {
71 int128_impl, //!< Native int128_t implementation.
72 cairo_impl, //!< cairo wideint implementation
73 ld_impl, //!< long double implementation
74 };
75
76 /// Type tag for this implementation.
77 static const enum impl_type implementation = cairo_impl;
78
79 /// Default constructor
80 inline int64x64_t()
81 {
82 _v.hi = 0;
83 _v.lo = 0;
84 }
85
86 /**
87 * \name Construct from a floating point value.
88 */
89 /**
90 * @{
91 * Constructor from a floating point.
92 *
93 * \param [in] value Floating value to represent.
94 */
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
130 /**@}*/
131
132 /**
133 * \name Construct from an integral type.
134 */
135 /**@{*/
136 /**
137 * Construct from an integral type.
138 *
139 * \param [in] v Integer value to represent
140 */
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
177 /**@}*/
178 /**
179 * Construct from explicit high and low values.
180 *
181 * \param [in] hi Integer portion.
182 * \param [in] lo Fractional portion, already scaled to HP_MAX_64.
183 */
184 explicit inline int64x64_t(const int64_t hi, const uint64_t lo)
185 {
186 _v.hi = hi;
187 _v.lo = lo;
188 }
189
190 /**
191 * Copy constructor.
192 *
193 * \param [in] o Value to copy.
194 */
195 inline int64x64_t(const int64x64_t& o)
196 : _v(o._v)
197 {
198 }
199
200 /**
201 * Assignment.
202 *
203 * \param [in] o Value to assign to this int64x64_t.
204 * \returns a copy of \pname{o}
205 */
207 {
208 _v = o._v;
209 return *this;
210 }
211
212 /** Explicit bool conversion. */
213 inline explicit operator bool() const
214 {
215 return (_v.hi != 0 || _v.lo != 0);
216 }
217
218 /**
219 * Get this value as a double.
220 *
221 * \return This value in floating form.
222 */
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
235 /**
236 * Get the integer portion.
237 *
238 * \return The integer portion of this value.
239 */
240 inline int64_t GetHigh() const
241 {
242 return (int64_t)_v.hi;
243 }
244
245 /**
246 * Get the fractional portion of this value, unscaled.
247 *
248 * \return The fractional portion, unscaled, as an integer.
249 */
250 inline uint64_t GetLow() const
251 {
252 return _v.lo;
253 }
254
255 /**
256 * Truncate to an integer.
257 * Truncation is always toward zero,
258 * \return The value truncated toward zero.
259 */
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
269 /**
270 * Round to the nearest int.
271 * Similar to std::round this rounds halfway cases away from zero,
272 * regardless of the current (floating) rounding mode.
273 * \return The value rounded to the nearest int.
274 */
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
286 /**
287 * Multiply this value by a Q0.128 value, presumably representing an inverse,
288 * completing a division operation.
289 *
290 * \param [in] o The inverse operand.
291 *
292 * \see Invert
293 */
294 void MulByInvert(const int64x64_t& o);
295
296 /**
297 * Compute the inverse of an integer value.
298 *
299 * Ordinary division by an integer would be limited to 64 bits of precision.
300 * Instead, we multiply by the 128-bit inverse of the divisor.
301 * This function computes the inverse to 128-bit precision.
302 * MulByInvert() then completes the division.
303 *
304 * (Really this should be a separate type representing Q0.128.)
305 *
306 * \param [in] v The value to compute the inverse of.
307 * \return A Q0.128 representation of the inverse.
308 */
309 static int64x64_t Invert(const uint64_t v);
310
311 private:
312 /**
313 * \name Arithmetic Operators
314 * Arithmetic operators for int64x64_t.
315 */
316 /**
317 * @{
318 * Arithmetic operator.
319 * \param [in] lhs Left hand argument
320 * \param [in] rhs Right hand argument
321 * \return The result of the operator.
322 */
323
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
363 /** @} */
364
365 /**
366 * \name Unary Operators
367 * Unary operators for int64x64_t.
368 */
369 /**
370 * @{
371 * Unary operator.
372 * \param [in] lhs Left hand argument
373 * \return The result of the operator.
374 */
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
392 /** @} */
393
394 /**
395 * Implement `*=`.
396 *
397 * \param [in] o The other factor.
398 */
399 void Mul(const int64x64_t& o);
400 /**
401 * Implement `/=`.
402 *
403 * \param [in] o The divisor.
404 */
405 void Div(const int64x64_t& o);
406 /**
407 * Unsigned multiplication of Q64.64 values.
408 *
409 * Mathematically this should produce a Q128.128 value;
410 * we keep the central 128 bits, representing the Q64.64 result.
411 * We assert on integer overflow beyond the 64-bit integer portion.
412 *
413 * \param [in] a First factor.
414 * \param [in] b Second factor.
415 * \return The Q64.64 product.
416 *
417 * \internal
418 *
419 * It might be tempting to just use \pname{a} `*` \pname{b}
420 * and be done with it, but it's not that simple. With \pname{a}
421 * and \pname{b} as 128-bit integers, \pname{a} `*` \pname{b}
422 * mathematically produces a 256-bit result, which the computer
423 * truncates to the lowest 128 bits. In our case, where \pname{a}
424 * and \pname{b} are interpreted as Q64.64 fixed point numbers,
425 * the multiplication mathematically produces a Q128.128 fixed point number.
426 * We want the middle 128 bits from the result, truncating both the
427 * high and low 64 bits. To achieve this, we carry out the multiplication
428 * explicitly with 64-bit operands and 128-bit intermediate results.
429 */
430 static cairo_uint128_t Umul(const cairo_uint128_t a, const cairo_uint128_t b);
431 /**
432 * Unsigned division of Q64.64 values.
433 *
434 * \param [in] a Numerator.
435 * \param [in] b Denominator.
436 * \return The Q64.64 representation of `a / b`
437 */
438 static cairo_uint128_t Udiv(const cairo_uint128_t a, const cairo_uint128_t b);
439 /**
440 * Unsigned multiplication of Q64.64 and Q0.128 values.
441 *
442 * \param [in] a The numerator, a Q64.64 value.
443 * \param [in] b The inverse of the denominator, a Q0.128 value
444 * \return The product `a * b`, representing the ration `a / b^-1`
445 *
446 * \see Invert
447 */
448 static cairo_uint128_t UmulByInvert(const cairo_uint128_t a, const cairo_uint128_t b);
449
450 cairo_int128_t _v; //!< The Q64.64 value.
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.
Definition: int64x64-128.h:56
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.
Definition: int64x64-128.h:60
impl_type
Type tag for the underlying implementation.
Definition: int64x64-128.h:87
@ int128_impl
Native int128_t implementation.
Definition: int64x64-128.h:88
@ ld_impl
long double implementation.
Definition: int64x64-128.h:90
@ cairo_impl
Cairo wideint implementation.
Definition: int64x64-128.h:89
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
static uint128_t Udiv(const uint128_t a, const uint128_t b)
Unsigned division of Q64.64 values.
void MulByInvert(const int64x64_t &o)
Multiply this value by a Q0.128 value, presumably representing an inverse, completing a division oper...
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 uint128_t UmulByInvert(const uint128_t a, const uint128_t b)
Unsigned multiplication of Q64.64 and Q0.128 values.
static enum impl_type implementation
Type tag for this implementation.
Definition: int64x64-128.h:94
friend int64x64_t operator!(const int64x64_t &lhs)
Unary operator.
int128_t _v
The Q64.64 value.
Definition: int64x64-128.h:469
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.
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.
static uint128_t Umul(const uint128_t a, const uint128_t b)
Unsigned multiplication of Q64.64 values.
Definition: int64x64-128.cc:71
static int64x64_t Invert(const uint64_t v)
Compute the inverse of an integer value.
#define HP_MAX_64
Floating point value of HP_MASK_LO + 1.
Definition: int64x64-128.h:76
Every class exported by the ns3 library is enclosed in the ns3 namespace.