A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
int64x64-128.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
21#if !defined(INT64X64_128_H) && defined(INT64X64_USE_128) && !defined(PYTHON_SCAN)
22/**
23 * \ingroup highprec
24 * Use uint128_t for int64x64_t implementation
25 */
26#define INT64X64_128_H
27
28#include <cmath> // pow
29#include <stdint.h>
30
31#if defined(HAVE___UINT128_T) && !defined(HAVE_UINT128_T)
32/**
33 * \ingroup highprec
34 * Some compilers do not have this defined, so we define it.
35 * @{
36 */
37typedef __uint128_t uint128_t;
38typedef __int128_t int128_t;
39/** @} */
40#endif
41
42/**
43 * \file
44 * \ingroup highprec
45 * Declaration of the ns3::int64x64_t type using a native int128_t type.
46 */
47
48namespace ns3
49{
50
51/**
52 * \internal
53 * The implementation documented here is based on native 128-bit integers.
54 */
56{
57 /// uint128_t high bit (sign bit).
58 static const uint128_t HP128_MASK_HI_BIT = (((int128_t)1) << 127);
59 /// Mask for fraction part.
60 static const uint64_t HP_MASK_LO = 0xffffffffffffffffULL;
61 /**
62 * Floating point value of HP_MASK_LO + 1.
63 * We really want:
64 * \code
65 * static const long double HP_MAX_64 = std:pow (2.0L, 64);
66 * \endcode
67 * but we can't call functions in const definitions.
68 *
69 * We could make this a static and initialize in int64x64-128.cc or
70 * int64x64.cc, but this requires handling static initialization order
71 * when most of the implementation is inline. Instead, we resort to
72 * this define.
73 */
74#define HP_MAX_64 (std::pow(2.0L, 64))
75
76 public:
77 /**
78 * Type tag for the underlying implementation.
79 *
80 * A few testcases are are sensitive to implementation,
81 * specifically the double implementation. To handle this,
82 * we expose the underlying implementation type here.
83 */
85 {
86 int128_impl, //!< Native \c int128_t implementation.
87 cairo_impl, //!< Cairo wideint implementation.
88 ld_impl, //!< `long double` implementation.
89 };
90
91 /// Type tag for this implementation.
93
94 /// Default constructor.
95 inline int64x64_t()
96 : _v(0)
97 {
98 }
99
100 /**
101 * \name Construct from a floating point value.
102 */
103 /**
104 * @{
105 * Constructor from a floating point.
106 *
107 * \param [in] value Floating value to represent.
108 */
109 inline int64x64_t(const double value)
110 {
111 const int64x64_t tmp((long double)value);
112 _v = tmp._v;
113 }
114
115 inline int64x64_t(const long double value)
116 {
117 const bool negative = value < 0;
118 const long double v = negative ? -value : value;
119
120 long double fhi;
121 long double flo = std::modf(v, &fhi);
122 // Add 0.5 to round, which improves the last count
123 // This breaks these tests:
124 // TestSuite devices-mesh-dot11s-regression
125 // TestSuite devices-mesh-flame-regression
126 // TestSuite routing-aodv-regression
127 // TestSuite routing-olsr-regression
128 // Setting round = 0; breaks:
129 // TestSuite int64x64
130 const long double round = 0.5;
131 flo = flo * HP_MAX_64 + round;
132 int128_t hi = fhi;
133 const uint64_t lo = flo;
134 if (flo >= HP_MAX_64)
135 {
136 // conversion to uint64 rolled over
137 ++hi;
138 }
139 _v = hi << 64;
140 _v |= lo;
141 _v = negative ? -_v : _v;
142 }
143
144 /**@}*/
145
146 /**
147 * \name Construct from an integral type.
148 */
149 /**@{*/
150 /**
151 * Construct from an integral type.
152 *
153 * \param [in] v Integer value to represent.
154 */
155 inline int64x64_t(const int v)
156 : _v(v)
157 {
158 _v <<= 64;
159 }
160
161 inline int64x64_t(const long int v)
162 : _v(v)
163 {
164 _v <<= 64;
165 }
166
167 inline int64x64_t(const long long int v)
168 : _v(v)
169 {
170 _v <<= 64;
171 }
172
173 inline int64x64_t(const unsigned int v)
174 : _v(v)
175 {
176 _v <<= 64;
177 }
178
179 inline int64x64_t(const unsigned long int v)
180 : _v(v)
181 {
182 _v <<= 64;
183 }
184
185 inline int64x64_t(const unsigned long long int v)
186 : _v(v)
187 {
188 _v <<= 64;
189 }
190
191 /**@}*/
192
193 /**
194 * Construct from explicit high and low values.
195 *
196 * \param [in] hi Integer portion.
197 * \param [in] lo Fractional portion, already scaled to HP_MAX_64.
198 */
199 explicit inline int64x64_t(const int64_t hi, const uint64_t lo)
200 {
201 _v = (int128_t)hi << 64;
202 _v |= lo;
203 }
204
205 /**
206 * Copy constructor.
207 *
208 * \param [in] o Value to copy.
209 */
210 inline int64x64_t(const int64x64_t& o)
211 : _v(o._v)
212 {
213 }
214
215 /**
216 * Assignment.
217 *
218 * \param [in] o Value to assign to this int64x64_t.
219 * \returns This int64x64_t.
220 */
222 {
223 _v = o._v;
224 return *this;
225 }
226
227 /** Explicit bool conversion. */
228 inline explicit operator bool() const
229 {
230 return (_v != 0);
231 }
232
233 /**
234 * Get this value as a double.
235 *
236 * \return This value in floating form.
237 */
238 inline double GetDouble() const
239 {
240 const bool negative = _v < 0;
241 const uint128_t value = negative ? -_v : _v;
242 const long double fhi = value >> 64;
243 const long double flo = (value & HP_MASK_LO) / HP_MAX_64;
244 long double retval = fhi;
245 retval += flo;
246 retval = negative ? -retval : retval;
247 return retval;
248 }
249
250 /**
251 * Get the integer portion.
252 *
253 * \return The integer portion of this value.
254 */
255 inline int64_t GetHigh() const
256 {
257 const int128_t retval = _v >> 64;
258 return retval;
259 }
260
261 /**
262 * Get the fractional portion of this value, unscaled.
263 *
264 * \return The fractional portion, unscaled, as an integer.
265 */
266 inline uint64_t GetLow() const
267 {
268 const uint128_t retval = _v & HP_MASK_LO;
269 return retval;
270 }
271
272 /**
273 * Truncate to an integer.
274 * Truncation is always toward zero,
275 * \return The value truncated toward zero.
276 */
277 int64_t GetInt() const
278 {
279 const bool negative = _v < 0;
280 const uint128_t value = negative ? -_v : _v;
281 int64_t retval = value >> 64;
282 retval = negative ? -retval : retval;
283 return retval;
284 }
285
286 /**
287 * Round to the nearest int.
288 * Similar to std::round this rounds halfway cases away from zero,
289 * regardless of the current (floating) rounding mode.
290 * \return The value rounded to the nearest int.
291 */
292 int64_t Round() const
293 {
294 const bool negative = _v < 0;
295 int64x64_t value = (negative ? -(*this) : *this);
296 const int64x64_t half(0, 1LL << 63);
297 value += half;
298 int64_t retval = value.GetHigh();
299 retval = negative ? -retval : retval;
300 return retval;
301 }
302
303 /**
304 * Multiply this value by a Q0.128 value, presumably representing an inverse,
305 * completing a division operation.
306 *
307 * \param [in] o The inverse operand.
308 *
309 * \see Invert()
310 */
311 void MulByInvert(const int64x64_t& o);
312
313 /**
314 * Compute the inverse of an integer value.
315 *
316 * Ordinary division by an integer would be limited to 64 bits of precision.
317 * Instead, we multiply by the 128-bit inverse of the divisor.
318 * This function computes the inverse to 128-bit precision.
319 * MulByInvert() then completes the division.
320 *
321 * (Really this should be a separate type representing Q0.128.)
322 *
323 * \param [in] v The value to compute the inverse of.
324 * \return A Q0.128 representation of the inverse.
325 */
326 static int64x64_t Invert(const uint64_t v);
327
328 private:
329 /**
330 * \name Arithmetic Operators
331 * Arithmetic operators for int64x64_t.
332 */
333 /**
334 * @{
335 * Arithmetic operator.
336 * \param [in] lhs Left hand argument
337 * \param [in] rhs Right hand argument
338 * \return The result of the operator.
339 */
340
341 friend inline bool operator==(const int64x64_t& lhs, const int64x64_t& rhs)
342 {
343 return lhs._v == rhs._v;
344 }
345
346 friend inline bool operator<(const int64x64_t& lhs, const int64x64_t& rhs)
347 {
348 return lhs._v < rhs._v;
349 }
350
351 friend inline bool operator>(const int64x64_t& lhs, const int64x64_t& rhs)
352 {
353 return lhs._v > rhs._v;
354 }
355
356 friend inline int64x64_t& operator+=(int64x64_t& lhs, const int64x64_t& rhs)
357 {
358 lhs._v += rhs._v;
359 return lhs;
360 }
361
362 friend inline int64x64_t& operator-=(int64x64_t& lhs, const int64x64_t& rhs)
363 {
364 lhs._v -= rhs._v;
365 return lhs;
366 }
367
368 friend inline int64x64_t& operator*=(int64x64_t& lhs, const int64x64_t& rhs)
369 {
370 lhs.Mul(rhs);
371 return lhs;
372 }
373
374 friend inline int64x64_t& operator/=(int64x64_t& lhs, const int64x64_t& rhs)
375 {
376 lhs.Div(rhs);
377 return lhs;
378 }
379
380 /**@}*/
381
382 /**
383 * \name Unary Operators
384 * Unary operators for int64x64_t.
385 */
386 /**
387 * @{
388 * Unary operator.
389 * \param [in] lhs Left hand argument
390 * \return The result of the operator.
391 */
392 friend inline int64x64_t operator+(const int64x64_t& lhs)
393 {
394 return lhs;
395 }
396
397 friend inline int64x64_t operator-(const int64x64_t& lhs)
398 {
399 int64x64_t res;
400 res._v = -lhs._v;
401 return res;
402 }
403
404 friend inline int64x64_t operator!(const int64x64_t& lhs)
405 {
406 return int64x64_t(!lhs._v);
407 }
408
409 /**@}*/
410
411 /**
412 * Implement `*=`.
413 * We assert if the product cannot be encoded in int64x64_t.
414 *
415 * \param [in] o The other factor.
416 */
417 void Mul(const int64x64_t& o);
418 /**
419 * Implement `/=`.
420 *
421 * \param [in] o The divisor.
422 */
423 void Div(const int64x64_t& o);
424 /**
425 * Unsigned multiplication of Q64.64 values.
426 *
427 * Mathematically this should produce a Q128.128 value;
428 * we keep the central 128 bits, representing the Q64.64 result.
429 * We might assert if the result, in uint128_t format, exceeds 2^127.
430 *
431 * \param [in] a First factor.
432 * \param [in] b Second factor.
433 * \return The Q64.64 product.
434 *
435 * \internal
436 *
437 * It might be tempting to just use \pname{a} `*` \pname{b}
438 * and be done with it, but it's not that simple. With \pname{a}
439 * and \pname{b} as 128-bit integers, \pname{a} `*` \pname{b}
440 * mathematically produces a 256-bit result, which the computer
441 * truncates to the lowest 128 bits. In our case, where \pname{a}
442 * and \pname{b} are interpreted as Q64.64 fixed point numbers,
443 * the multiplication mathematically produces a Q128.128 fixed point number.
444 * We want the middle 128 bits from the result, truncating both the
445 * high and low 64 bits. To achieve this, we carry out the multiplication
446 * explicitly with 64-bit operands and 128-bit intermediate results.
447 */
448 static uint128_t Umul(const uint128_t a, const uint128_t b);
449 /**
450 * Unsigned division of Q64.64 values.
451 *
452 * \param [in] a Numerator.
453 * \param [in] b Denominator.
454 * \return The Q64.64 representation of `a / b`.
455 */
456 static uint128_t Udiv(const uint128_t a, const uint128_t b);
457 /**
458 * Unsigned multiplication of Q64.64 and Q0.128 values.
459 *
460 * \param [in] a The numerator, a Q64.64 value.
461 * \param [in] b The inverse of the denominator, a Q0.128 value
462 * \return The product `a * b`, representing the ration `a / b^-1`.
463 *
464 * \see Invert()
465 */
466 static uint128_t UmulByInvert(const uint128_t a, const uint128_t b);
467
468 int128_t _v; //!< The Q64.64 value.
469
470}; // class int64x64_t
471
472} // namespace ns3
473
474#endif /* INT64X64_128_H */
High precision numerical type, implementing Q64.64 fixed precision.
Definition: int64x64-128.h:56
int64_t GetHigh() const
Get the integer portion.
Definition: int64x64-128.h:255
friend bool operator==(const int64x64_t &lhs, const int64x64_t &rhs)
Arithmetic operator.
Definition: int64x64-128.h:341
static const uint64_t HP_MASK_LO
Mask for fraction part.
Definition: int64x64-128.h:60
static const uint128_t HP128_MASK_HI_BIT
uint128_t high bit (sign bit).
Definition: int64x64-128.h:58
impl_type
Type tag for the underlying implementation.
Definition: int64x64-128.h:85
@ int128_impl
Native int128_t implementation.
Definition: int64x64-128.h:86
@ ld_impl
long double implementation.
Definition: int64x64-128.h:88
@ cairo_impl
Cairo wideint implementation.
Definition: int64x64-128.h:87
friend int64x64_t & operator-=(int64x64_t &lhs, const int64x64_t &rhs)
Arithmetic operator.
Definition: int64x64-128.h:362
friend int64x64_t operator+(const int64x64_t &lhs)
Unary operator.
Definition: int64x64-128.h:392
int64_t Round() const
Round to the nearest int.
Definition: int64x64-128.h:292
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.
Definition: int64x64-128.h:346
int64x64_t(const long double value)
Constructor from a floating point.
Definition: int64x64-128.h:115
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:92
friend int64x64_t operator!(const int64x64_t &lhs)
Unary operator.
Definition: int64x64-128.h:404
int128_t _v
The Q64.64 value.
Definition: int64x64-128.h:468
friend int64x64_t & operator*=(int64x64_t &lhs, const int64x64_t &rhs)
Arithmetic operator.
Definition: int64x64-128.h:368
int64x64_t(const int64_t hi, const uint64_t lo)
Construct from explicit high and low values.
Definition: int64x64-128.h:199
void Div(const int64x64_t &o)
Implement /=.
int64x64_t(const unsigned long long int v)
Construct from an integral type.
Definition: int64x64-128.h:185
int64x64_t(const unsigned int v)
Construct from an integral type.
Definition: int64x64-128.h:173
int64x64_t(const long int v)
Construct from an integral type.
Definition: int64x64-128.h:161
int64x64_t(const long long int v)
Construct from an integral type.
Definition: int64x64-128.h:167
double GetDouble() const
Get this value as a double.
Definition: int64x64-128.h:238
friend int64x64_t & operator+=(int64x64_t &lhs, const int64x64_t &rhs)
Arithmetic operator.
Definition: int64x64-128.h:356
int64x64_t(const unsigned long int v)
Construct from an integral type.
Definition: int64x64-128.h:179
int64_t GetInt() const
Truncate to an integer.
Definition: int64x64-128.h:277
int64x64_t & operator=(const int64x64_t &o)
Assignment.
Definition: int64x64-128.h:221
friend bool operator>(const int64x64_t &lhs, const int64x64_t &rhs)
Arithmetic operator.
Definition: int64x64-128.h:351
int64x64_t(const int v)
Construct from an integral type.
Definition: int64x64-128.h:155
uint64_t GetLow() const
Get the fractional portion of this value, unscaled.
Definition: int64x64-128.h:266
friend int64x64_t operator-(const int64x64_t &lhs)
Unary operator.
Definition: int64x64-128.h:397
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.
Definition: int64x64-128.h:109
int64x64_t(const int64x64_t &o)
Copy constructor.
Definition: int64x64-128.h:210
friend int64x64_t & operator/=(int64x64_t &lhs, const int64x64_t &rhs)
Arithmetic operator.
Definition: int64x64-128.h:374
int64x64_t()
Default constructor.
Definition: int64x64-128.h:95
static uint128_t Umul(const uint128_t a, const uint128_t b)
Unsigned multiplication of Q64.64 values.
Definition: int64x64-128.cc:80
__uint128_t uint128_t
Some compilers do not have this defined, so we define it.
Definition: int64x64-128.h:37
__int128_t int128_t
Some compilers do not have this defined, so we define it.
Definition: int64x64-128.h:38
#define HP_MAX_64
Floating point value of HP_MASK_LO + 1.
Definition: int64x64-128.h:74
Every class exported by the ns3 library is enclosed in the ns3 namespace.