GraphLab: Distributed Graph-Parallel API  2.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
uint128.hpp
1 /**
2  * Copyright (c) 2009 Carnegie Mellon University.
3  * All rights reserved.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing,
12  * software distributed under the License is distributed on an "AS
13  * IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
14  * express or implied. See the License for the specific language
15  * governing permissions and limitations under the License.
16  *
17  * For more about this software visit:
18  *
19  * http://www.graphlab.ml.cmu.edu
20  *
21  */
22 
23 
24 #ifndef GRAPHLAB_UINT128_HPP
25 #define GRAPHLAB_UINT128_HPP
26 #include <stdint.h>
27 #include <iostream>
28 #include <iomanip>
29 #include <graphlab/serialization/serialization_includes.hpp>
30 
31 namespace graphlab {
32 
33 /**
34  * A 128 bit numeric type. This type is a union of a 16-byte character array (bytes),
35  * and struct of two 64-bit integers (ints.high and ints.low).
36  */
37 union gl_uint128_t {
38  struct {
39  uint64_t high;
40  uint64_t low;
41  } ints;
42  char bytes[16];
43 
44  gl_uint128_t() { }
45 
46  /**
47  * Constructs a 128-bit type from a 64-bit value.
48  * It simply clears the "high" 64 bits of the 128-bit integer, and sets
49  * the low 64-bits to the input
50  */
51  explicit gl_uint128_t(uint64_t val) {
52  ints.high = 0;
53  ints.low = val;
54  }
55 };
56 
57 /**
58  * Sets all 128bits of the the gl_uint128_t to 'true'.
59  * Or the 128-bit integer representation of "-1"
60  */
62  gl_uint128_t i;
63  i.ints.high = (uint64_t)(-1);
64  i.ints.low = (uint64_t)(-1);
65  return i;
66 }
67 
68 /**
69  * Prints the 128-bit integer as hexadecimal
70  */
71 inline std::ostream& operator<<(std::ostream& out, const gl_uint128_t &val) {
72  static char hexchar[17] = "0123456789abcdef";
73 
74  for (size_t i = 0;i < 16; ++i) {
75  out << hexchar[(val.bytes[i] >> 4) & 15];
76  out << hexchar[val.bytes[i] & 15];
77  }
78  return out;
79 }
80 
81 }
82 
83 SERIALIZABLE_POD(graphlab::gl_uint128_t);
84 
85 #endif
86