A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
int64x64-double.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_DOUBLE_H) && (defined(INT64X64_USE_DOUBLE) || defined(PYTHON_SCAN))
21/** Using the ns3::int64x64_t based on double values. */
22#define INT64X64_DOUBLE_H
23
24#include <cmath> // pow
25#include <stdint.h>
26#include <utility> // pair
27
28/**
29 * \file
30 * \ingroup highprec
31 * Declaration and implementation of the ns3::int64x64_t type
32 * using the double type.
33 */
34
35namespace ns3
36{
37
38/**
39 * \internal
40 * The implementation documented here uses native long double.
41 */
42class int64x64_t
43{
44 /// Mask for fraction part
45 static const uint64_t HP_MASK_LO = 0xffffffffffffffffULL;
46 /**
47 * Floating point value of HP_MASK_LO + 1
48 * We really want:
49 * \code
50 * static const long double HP_MAX_64 = std:pow (2.0L, 64);
51 * \endcode
52 * but we can't call functions in const definitions,
53 * We could make this a static and initialize in int64x64-double.cc or
54 * int64x64.cc, but this requires handling static initialization order
55 * when most of the implementation is inline. Instead, we resort to
56 * this define.
57 */
58#define HP_MAX_64 (std::pow(2.0L, 64))
59
60 public:
61 /**
62 * Type tag for the underlying implementation.
63 *
64 * A few testcases are are sensitive to implementation,
65 * specifically the double implementation. To handle this,
66 * we expose the underlying implementation type here.
67 */
69 {
70 int128_impl, //!< Native int128_t implementation.
71 cairo_impl, //!< cairo wideint implementation
72 ld_impl, //!< long double implementation
73 };
74
75 /// Type tag for this implementation.
76 static const enum impl_type implementation = ld_impl;
77
78 /// Default constructor
79 inline int64x64_t()
80 : _v(0)
81 {
82 }
83
84 /**
85 * \name Construct from a floating point value.
86 */
87 /**
88 * @{
89 * Constructor from a floating point.
90 *
91 * \param [in] value Floating value to represent.
92 */
93 inline int64x64_t(double value)
94 : _v(value)
95 {
96 }
97
98 inline int64x64_t(long double value)
99 : _v(value)
100 {
101 }
102
103 /**@}*/
104
105 /**
106 * \name Construct from an integral type.
107 */
108 /**@{*/
109 /**
110 * Construct from an integral type.
111 *
112 * \param [in] v Integer value to represent
113 */
114 inline int64x64_t(int v)
115 : _v(v)
116 {
117 }
118
119 inline int64x64_t(long int v)
120 : _v(v)
121 {
122 }
123
124 inline int64x64_t(long long int v)
125 : _v(static_cast<long double>(v))
126 {
127 }
128
129 inline int64x64_t(unsigned int v)
130 : _v(v)
131 {
132 }
133
134 inline int64x64_t(unsigned long int v)
135 : _v(v)
136 {
137 }
138
139 inline int64x64_t(unsigned long long int v)
140 : _v(static_cast<long double>(v))
141 {
142 }
143
144 /**@}*/
145 /**
146 * Construct from explicit high and low values.
147 *
148 * \param [in] hi Integer portion.
149 * \param [in] lo Fractional portion, already scaled to HP_MAX_64.
150 */
151 explicit inline int64x64_t(int64_t hi, uint64_t lo)
152 {
153 const bool negative = hi < 0;
154 const long double hild = static_cast<long double>(hi);
155 const long double fhi = negative ? -hild : hild;
156 const long double flo = lo / HP_MAX_64;
157 _v = negative ? -fhi : fhi;
158 _v += flo;
159 // _v = negative ? -_v : _v;
160 }
161
162 /**
163 * Copy constructor.
164 *
165 * \param [in] o Value to copy.
166 */
167 inline int64x64_t(const int64x64_t& o)
168 : _v(o._v)
169 {
170 }
171
172 /**
173 * Assignment.
174 *
175 * \param [in] o Value to assign to this int64x64_t.
176 * \returns a copy of \pname{o}
177 */
179 {
180 _v = o._v;
181 return *this;
182 }
183
184 /** Explicit bool conversion. */
185 inline explicit operator bool() const
186 {
187 return (_v != 0);
188 }
189
190 /**
191 * Get this value as a double.
192 *
193 * \return This value in floating form.
194 */
195 inline double GetDouble() const
196 {
197 return (double)_v;
198 }
199
200 private:
201 /**
202 * Get the high and low portions of this value.
203 *
204 * \return A pair of the high and low words
205 */
206 std::pair<int64_t, uint64_t> GetHighLow() const
207 {
208 const bool negative = _v < 0;
209 const long double v = negative ? -_v : _v;
210
211 long double fhi;
212 long double flo = std::modf(v, &fhi);
213 // Add 0.5 to round, which improves the last count
214 // This breaks these tests:
215 // TestSuite devices-mesh-dot11s-regression
216 // TestSuite devices-mesh-flame-regression
217 // TestSuite routing-aodv-regression
218 // TestSuite routing-olsr-regression
219 // Setting round = 0; breaks:
220 // TestSuite int64x64
221 const long double round = 0.5;
222 flo = flo * HP_MAX_64 + round;
223 int64_t hi = static_cast<int64_t>(fhi);
224 uint64_t lo = static_cast<uint64_t>(flo);
225 if (flo >= HP_MAX_64)
226 {
227 // conversion to uint64 rolled over
228 ++hi;
229 }
230 if (negative)
231 {
232 lo = ~lo;
233 hi = ~hi;
234 if (++lo == 0)
235 {
236 ++hi;
237 }
238 }
239 return std::make_pair(hi, lo);
240 }
241
242 public:
243 /**
244 * Get the integer portion.
245 *
246 * \return The integer portion of this value.
247 */
248 inline int64_t GetHigh() const
249 {
250 return GetHighLow().first;
251 }
252
253 /**
254 * Get the fractional portion of this value, unscaled.
255 *
256 * \return The fractional portion, unscaled, as an integer.
257 */
258 inline uint64_t GetLow() const
259 {
260 return GetHighLow().second;
261 }
262
263 /**
264 * Truncate to an integer.
265 * Truncation is always toward zero,
266 * \return The value truncated toward zero.
267 */
268 int64_t GetInt() const
269 {
270 int64_t retval = static_cast<int64_t>(_v);
271 return retval;
272 }
273
274 /**
275 * Round to the nearest int.
276 * Similar to std::round this rounds halfway cases away from zero,
277 * regardless of the current (floating) rounding mode.
278 * \return The value rounded to the nearest int.
279 */
280 int64_t Round() const
281 {
282 int64_t retval = std::round(_v);
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 * \note There is no difference between Q64.64 and Q0.128 in this implementation,
293 * so this function is a simple multiply.
294 */
295 inline void MulByInvert(const int64x64_t& o)
296 {
297 _v *= o._v;
298 }
299
300 /**
301 * Compute the inverse of an integer value.
302 *
303 * \param [in] v The value to compute the inverse of.
304 * \return The inverse.
305 */
306 static inline int64x64_t Invert(uint64_t v)
307 {
308 int64x64_t tmp((long double)1 / v);
309 return tmp;
310 }
311
312 private:
313 /**
314 * \name Arithmetic Operators
315 * Arithmetic operators for int64x64_t.
316 */
317 /**
318 * @{
319 * Arithmetic operator.
320 * \param [in] lhs Left hand argument
321 * \param [in] rhs Right hand argument
322 * \return The result of the operator.
323 */
324
325 friend inline bool operator==(const int64x64_t& lhs, const int64x64_t& rhs)
326 {
327 return lhs._v == rhs._v;
328 }
329
330 friend inline bool operator<(const int64x64_t& lhs, const int64x64_t& rhs)
331 {
332 return lhs._v < rhs._v;
333 }
334
335 friend inline bool operator>(const int64x64_t& lhs, const int64x64_t& rhs)
336 {
337 return lhs._v > rhs._v;
338 }
339
340 friend inline int64x64_t& operator+=(int64x64_t& lhs, const int64x64_t& rhs)
341 {
342 lhs._v += rhs._v;
343 return lhs;
344 }
345
346 friend inline int64x64_t& operator-=(int64x64_t& lhs, const int64x64_t& rhs)
347 {
348 lhs._v -= rhs._v;
349 return lhs;
350 }
351
352 friend inline int64x64_t& operator*=(int64x64_t& lhs, const int64x64_t& rhs)
353 {
354 lhs._v *= rhs._v;
355 return lhs;
356 }
357
358 friend inline int64x64_t& operator/=(int64x64_t& lhs, const int64x64_t& rhs)
359 {
360 lhs._v /= rhs._v;
361 return lhs;
362 }
363
364 /**@}*/
365
366 /**
367 * \name Unary Operators
368 * Unary operators for int64x64_t.
369 */
370 /**
371 * @{
372 * Unary operator.
373 * \param [in] lhs Left hand argument
374 * \return The result of the operator.
375 */
376 friend inline int64x64_t operator+(const int64x64_t& lhs)
377 {
378 return lhs;
379 }
380
381 friend inline int64x64_t operator-(const int64x64_t& lhs)
382 {
383 return int64x64_t(-lhs._v);
384 }
385
386 friend inline int64x64_t operator!(const int64x64_t& lhs)
387 {
388 return int64x64_t(!lhs._v);
389 }
390
391 /**@}*/
392
393 long double _v; //!< The Q64.64 value.
394
395}; // class int64x64_t
396
397} // namespace ns3
398
399#endif /* INT64X64_DOUBLE_H */
High precision numerical type, implementing Q64.64 fixed precision.
Definition: int64x64-128.h:56
int64_t GetHigh() const
Get the integer portion.
int64x64_t(unsigned long int v)
Construct from an integral type.
int64x64_t(int v)
Construct from an integral type.
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
static int64x64_t Invert(uint64_t v)
Compute the inverse of an integer value.
friend int64x64_t & operator-=(int64x64_t &lhs, const int64x64_t &rhs)
Arithmetic operator.
friend int64x64_t operator+(const int64x64_t &lhs)
Unary operator.
int64x64_t(long double value)
Constructor from a floating point.
int64x64_t(unsigned int v)
Construct from an integral type.
int64_t Round() const
Round to the nearest int.
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.
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
int64x64_t(unsigned long long int v)
Construct from an integral type.
friend int64x64_t & operator*=(int64x64_t &lhs, const int64x64_t &rhs)
Arithmetic operator.
int64x64_t(long long int v)
Construct from an integral type.
int64x64_t(int64_t hi, uint64_t lo)
Construct from explicit high and low values.
double GetDouble() const
Get this value as a double.
friend int64x64_t & operator+=(int64x64_t &lhs, const int64x64_t &rhs)
Arithmetic operator.
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.
long double _v
The Q64.64 value.
std::pair< int64_t, uint64_t > GetHighLow() const
Get the high and low portions of this value.
uint64_t GetLow() const
Get the fractional portion of this value, unscaled.
int64x64_t(double value)
Constructor from a floating point.
friend int64x64_t operator-(const int64x64_t &lhs)
Unary operator.
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.
int64x64_t(long int v)
Construct from an integral type.
#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.