A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
int64x64.cc
Go to the documentation of this file.
1 #include "int64x64.h"
2 #include <stdint.h>
3 #include <iostream>
4 #include <iomanip> // showpos
5 #include <sstream>
6 #include "assert.h"
7 #include "log.h"
8 
9 // Note: Logging in this file is largely avoided due to the
10 // number of calls that are made to these functions and the possibility
11 // of causing recursions leading to stack overflow
12 
13 NS_LOG_COMPONENT_DEFINE ("int64x64");
14 
15 namespace ns3 {
16 
17 static uint8_t MostSignificantDigit (uint64_t value)
18 {
19  uint8_t n = 0;
20  do
21  {
22  n++;
23  value /= 10;
24  } while (value != 0);
25  return n;
26 }
27 
28 static uint64_t PowerOfTen (uint8_t n)
29 {
30  uint64_t retval = 1;
31  while (n > 0)
32  {
33  retval *= 10;
34  n--;
35  }
36  return retval;
37 }
38 
39 std::ostream &operator << (std::ostream &os, const int64x64_t &value)
40 {
41  int64_t hi = value.GetHigh ();
42 
43  // Save stream format flags
44  std::ios_base::fmtflags ff = os.flags ();
45 
46  {
47  if (hi == 0)
49  {
50  os << '+';
51  }
52  else
53  {
54  os << std::showpos;
55  }
56  }
57 
58  os << hi << ".";
59  os.flags (ff); // Restore stream flags
60 
61  uint64_t low = value.GetLow ();
62  uint8_t msd = MostSignificantDigit (~((uint64_t)0));
63  do
64  {
65  msd--;
66  uint64_t pow = PowerOfTen (msd);
67  uint8_t digit = low / pow;
68  NS_ASSERT (digit < 10);
69  os << (uint16_t) digit;
70  low -= digit * pow;
71  } while (msd > 0 && low > 0);
72  return os;
73 }
74 
75 static uint64_t ReadDigits (std::string str)
76 {
77  const char *buf = str.c_str ();
78  uint64_t retval = 0;
79  while (*buf != 0)
80  {
81  retval *= 10;
82  retval += *buf - 0x30;
83  buf++;
84  }
85  return retval;
86 }
87 
88 std::istream &operator >> (std::istream &is, int64x64_t &value)
89 {
90  std::string str;
91 
92  is >> str;
93  bool negative;
94  // skip heading spaces
95  std::string::size_type cur;
96  cur = str.find_first_not_of (" ");
97  std::string::size_type next;
98  // first, remove the sign.
99  next = str.find ("-", cur);
100  if (next != std::string::npos)
101  {
102  negative = true;
103  next++;
104  }
105  else
106  {
107  next = str.find ("+", cur);
108  if (next != std::string::npos)
109  {
110  next++;
111  }
112  else
113  {
114  next = cur;
115  }
116  negative = false;
117  }
118  cur = next;
119  int64_t hi;
120  uint64_t lo;
121  next = str.find (".", cur);
122  if (next != std::string::npos)
123  {
124  hi = ReadDigits (str.substr (cur, next-cur));
125  lo = ReadDigits (str.substr (next+1, str.size ()-(next+1)));
126  }
127  else
128  {
129  hi = ReadDigits (str.substr (cur, str.size ()-cur));
130  lo = 0;
131  }
132  hi = negative ? -hi : hi;
133  value = int64x64_t (hi, lo);
134  return is;
135 }
136 
137 } // namespace ns3
std::istream & operator>>(std::istream &is, Angles &a)
initialize a struct Angles from input
Definition: angles.cc:49
#define NS_ASSERT(condition)
Definition: assert.h:64
static uint64_t ReadDigits(std::string str)
Definition: int64x64.cc:75
std::ostream & operator<<(std::ostream &os, const Angles &a)
print a struct Angles to output
Definition: angles.cc:43
static uint8_t MostSignificantDigit(uint64_t value)
Definition: int64x64.cc:17
NS_LOG_COMPONENT_DEFINE("int64x64")
static uint64_t PowerOfTen(uint8_t n)
Definition: int64x64.cc:28