Impala
Impalaistheopensource,nativeanalyticdatabaseforApacheHadoop.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
multi-precision.cc
Go to the documentation of this file.
1 // Copyright 2012 Cloudera Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
16 
17 #include "common/logging.h"
18 
19 #include "common/names.h"
20 
21 namespace impala {
22 
23 static const uint32_t ONE_BILLION = 1000000000;
24 
25 // Print the value in base 10 by converting v into parts that are base
26 // 1 billion (large multiple of 10 that's easy to work with).
27 ostream& operator<<(ostream& os, const int128_t& val) {
28  int128_t v = val;
29  if (v == 0) {
30  os << "0";
31  return os;
32  }
33 
34  if (v < 0) {
35  v = -v;
36  os << "-";
37  }
38 
39  // 1B^5 covers the range for int128_t
40  // parts[0] is the least significant place.
41  uint32_t parts[5];
42  int index = 0;
43  while (v > 0) {
44  parts[index++] = v % ONE_BILLION;
45  v /= ONE_BILLION;
46  }
47  --index;
48 
49  // Accumulate into a temporary stringstream so format options on 'os' do
50  // not mess up printing val.
51  // TODO: This is likely pretty expensive with the string copies. We don't
52  // do this in paths we care about currently but might need to revisit.
53  stringstream ss;
54  ss << parts[index];
55  for (int i = index - 1; i >= 0; --i) {
56  // The remaining parts need to be padded with leading zeros.
57  ss << setfill('0') << setw(9) << parts[i];
58  }
59  os << ss.str();
60  return os;
61 }
62 
63 }
static const uint32_t ONE_BILLION
ostream & operator<<(ostream &os, const map< TNetworkAddress, llama::TAllocatedResource > &resources)
__int128_t int128_t
We use the c++ int128_t type. This is stored using 16 bytes and very performant.