A Discrete-Event Network Simulator
API
int64x64.cc
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 "int64x64.h"
21 #include <stdint.h>
22 #include <iostream>
23 #include <iomanip> // showpos
24 #include <sstream>
25 #include "assert.h"
26 #include "log.h"
27 
35 namespace ns3 {
36 
37 // Note: Logging in this file is largely avoided due to the
38 // number of calls that are made to these functions and the possibility
39 // of causing recursions leading to stack overflow
41 
49 #define HEXHILOW(hi, lo) \
50  std::hex << std::setfill ('0') << std::right << " (0x" \
51  << std::setw (16) << hi << " " \
52  << std::setw (16) << lo \
53  << std::dec << std::setfill (' ') << std::left << ")"
54 
55 
69 std::ostream &operator << (std::ostream &os, const int64x64_t &value)
70 {
71  const bool negative = (value < 0);
72  const int64x64_t absVal = (negative ? -value : value);
73 
74  int64_t hi = absVal.GetHigh ();
75 
76  // Save stream format flags
77  std::size_t precision = static_cast<std::size_t> (os.precision ());
78  std::ios_base::fmtflags ff = os.flags ();
79  const bool floatfield = os.flags () & std::ios_base::floatfield;
80  os << std::setw (1) << std::noshowpos;
81 
82  os << std::right << (negative ? "-" : "+");
83 
84  std::ostringstream oss;
85  oss << hi << "."; // collect the digits here so we can round properly
86 
87 
88  int64x64_t low(0, absVal.GetLow ());
89  std::size_t places = 0; // Number of decimal places printed so far
90  bool more = true; // Should we print more digits?
91 
92  NS_LOG_LOGIC (std::endl
93  << (floatfield ? " f" : " ")
94  << "[" << precision << "] " << hi << ". "
95  << HEXHILOW (hi, low.GetLow ())
96  );
97 
98  int64_t digit;
99  do
100  {
101  low *= 10;
102  digit = low.GetHigh ();
103  NS_ASSERT_MSG ( (0 <= digit) && (digit <= 9),
104  "digit " << digit << " out of range [0,9] "
105  << " streaming out "
106  << HEXHILOW (value.GetHigh (), value.GetLow ()) );
107  low -= digit;
108 
109  oss << std::setw (1) << digit;
110 
111  ++places;
112  if (floatfield)
113  {
114  more = places < precision;
115  }
116  else // default
117  {
118  // Full resolution is 20 decimal digits
119  more = low.GetLow () && (places < 20);
120  }
121 
122  NS_LOG_LOGIC ((more ? "+" : " ")
123  << (floatfield ? "f" : " ")
124  << "[" << places << "] " << digit
125  << HEXHILOW (low.GetHigh (), low.GetLow ())
126  << std::dec << std::setfill (' ' ) << std::left);
127 
128  } while (more);
129 
130  // Check if we need to round the last printed digit,
131  // based on the first unprinted digit
132  std::string digits = oss.str ();
133  low *= 10;
134  int64_t nextDigit = low.GetHigh ();
135  if ( (nextDigit > 5) || ((nextDigit == 5) && (digit % 2 == 1)) )
136  {
137  // Walk backwards with the carry
138  bool carry = true;
139  for (std::string::reverse_iterator rit = digits.rbegin ();
140  rit != digits.rend ();
141  ++rit)
142  {
143  if (*rit == '.') // Skip over the decimal point
144  {
145  continue ;
146  }
147 
148  ++(*rit); // Add the carry
149  if (*rit <= '9') // Relies on character order...
150  {
151  carry = false;
152  break ; // Carry complete
153  }
154  else
155  {
156  *rit = '0'; // Continue carry to next higher digit
157  }
158  }
159  if (carry) // If we still have a carry...
160  {
161  digits.insert (digits.begin (), '1');
162  }
163  }
164  os << digits;
165 
166  os.flags (ff); // Restore stream flags
167  return os;
168 }
169 
179 static uint64_t ReadHiDigits (std::string str)
180 {
181  const char *buf = str.c_str ();
182  uint64_t retval = 0;
183  while (*buf != 0)
184  {
185  retval *= 10;
186  retval += *buf - '0';
187  buf++;
188  }
189  return retval;
190 }
191 
202 static uint64_t ReadLoDigits (std::string str)
203 {
204  int64x64_t low;
205  const int64x64_t round (0, 5); // Round last place in division
206 
207  for (std::string::const_reverse_iterator rit = str.rbegin ();
208  rit != str.rend ();
209  ++rit)
210  {
211  int digit = *rit - '0';
212  NS_ASSERT_MSG ( (0 <= digit) && (digit <= 9),
213  "digit " << digit << " out of range [0,9]"
214  << " streaming in low digits \"" << str << "\"");
215  low = (low + digit + round) / 10;
216  }
217 
218  return low.GetLow ();
219 }
220 
221 std::istream &operator >> (std::istream &is, int64x64_t &value)
222 {
223  std::string str;
224 
225  is >> str;
226  bool negative;
227  // skip heading spaces
228  std::string::size_type cur;
229  cur = str.find_first_not_of (" ");
230  std::string::size_type next;
231  // first, remove the sign.
232  next = str.find ("-", cur);
233  if (next != std::string::npos)
234  {
235  negative = true;
236  next++;
237  }
238  else
239  {
240  next = str.find ("+", cur);
241  if (next != std::string::npos)
242  {
243  next++;
244  }
245  else
246  {
247  next = cur;
248  }
249  negative = false;
250  }
251  cur = next;
252  int64_t hi;
253  uint64_t lo;
254  next = str.find (".", cur);
255  if (next != std::string::npos)
256  {
257  hi = ReadHiDigits (str.substr (cur, next-cur));
258  lo = ReadLoDigits (str.substr (next+1, str.size ()-(next+1)));
259  }
260  else if (cur != std::string::npos)
261  {
262  hi = ReadHiDigits (str.substr (cur, str.size ()-cur));
263  lo = 0;
264  }
265  else
266  {
267  hi = 0;
268  lo = 0;
269  }
270 
271  value = int64x64_t (hi, lo);
272  value = negative ? -value : value;
273 
274  return is;
275 }
276 
277 } // namespace ns3
std::istream & operator>>(std::istream &is, Angles &a)
initialize a struct Angles from input
Definition: angles.cc:48
NS_ASSERT_MSG(false, "Ipv4AddressGenerator::MaskToIndex(): Impossible")
High precision numerical type, implementing Q64.64 fixed precision.
Definition: int64x64-128.h:45
NS_ASSERT() and NS_ASSERT_MSG() macro definitions.
#define NS_LOG_COMPONENT_DEFINE_MASK(name, mask)
Define a logging component with a mask.
Definition: log.h:213
std::ostream & operator<<(std::ostream &os, const Angles &a)
print a struct Angles to output
Definition: angles.cc:42
static uint64_t ReadHiDigits(std::string str)
Read the integer portion of a number from a string containing just the integral digits (no decimal po...
Definition: int64x64.cc:179
Prefix all trace prints with simulation time.
Definition: log.h:116
Every class exported by the ns3 library is enclosed in the ns3 namespace.
#define HEXHILOW(hi, lo)
Print the high and low words of an int64x64 in hex, for debugging.
Definition: int64x64.cc:49
static uint64_t ReadLoDigits(std::string str)
Read the fractional part of a number from a string containing just the decimal digits of the fraction...
Definition: int64x64.cc:202
NS_LOG_LOGIC("Net device "<< nd<< " is not bridged")
uint64_t GetLow(void) const
Get the fractional portion of this value, unscaled.
Definition: int64x64-128.h:229
Debug message logging.
Declaration of the ns3::int64x64_t type and associated operators.
int64_t GetHigh(void) const
Get the integer portion.
Definition: int64x64-128.h:219