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