Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(222)

Side by Side Diff: net/base/int128.h

Issue 266243004: Clang format slam. Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef NET_BASE_INT128_H_ 5 #ifndef NET_BASE_INT128_H_
6 #define NET_BASE_INT128_H_ 6 #define NET_BASE_INT128_H_
7 7
8 #include <iosfwd> 8 #include <iosfwd>
9 #include "base/basictypes.h" 9 #include "base/basictypes.h"
10 #include "net/base/net_export.h" 10 #include "net/base/net_export.h"
11 11
12 struct uint128_pod; 12 struct uint128_pod;
13 13
14 // An unsigned 128-bit integer type. Thread-compatible. 14 // An unsigned 128-bit integer type. Thread-compatible.
15 class uint128 { 15 class uint128 {
16 public: 16 public:
17 uint128(); // Sets to 0, but don't trust on this behavior. 17 uint128(); // Sets to 0, but don't trust on this behavior.
18 uint128(uint64 top, uint64 bottom); 18 uint128(uint64 top, uint64 bottom);
19 uint128(int bottom); 19 uint128(int bottom);
20 uint128(uint32 bottom); // Top 96 bits = 0 20 uint128(uint32 bottom); // Top 96 bits = 0
21 uint128(uint64 bottom); // hi_ = 0 21 uint128(uint64 bottom); // hi_ = 0
22 uint128(const uint128 &val); 22 uint128(const uint128& val);
23 uint128(const uint128_pod &val); 23 uint128(const uint128_pod& val);
24 24
25 void Initialize(uint64 top, uint64 bottom); 25 void Initialize(uint64 top, uint64 bottom);
26 26
27 uint128& operator=(const uint128& b); 27 uint128& operator=(const uint128& b);
28 28
29 // Arithmetic operators. 29 // Arithmetic operators.
30 // TODO: division, etc. 30 // TODO: division, etc.
31 uint128& operator+=(const uint128& b); 31 uint128& operator+=(const uint128& b);
32 uint128& operator-=(const uint128& b); 32 uint128& operator-=(const uint128& b);
33 uint128& operator*=(const uint128& b); 33 uint128& operator*=(const uint128& b);
34 uint128 operator++(int); 34 uint128 operator++(int);
35 uint128 operator--(int); 35 uint128 operator--(int);
36 uint128& operator<<=(int); 36 uint128& operator<<=(int);
37 uint128& operator>>=(int); 37 uint128& operator>>=(int);
38 uint128& operator&=(const uint128& b); 38 uint128& operator&=(const uint128& b);
39 uint128& operator|=(const uint128& b); 39 uint128& operator|=(const uint128& b);
40 uint128& operator^=(const uint128& b); 40 uint128& operator^=(const uint128& b);
41 uint128& operator++(); 41 uint128& operator++();
42 uint128& operator--(); 42 uint128& operator--();
43 43
44 friend uint64 Uint128Low64(const uint128& v); 44 friend uint64 Uint128Low64(const uint128& v);
45 friend uint64 Uint128High64(const uint128& v); 45 friend uint64 Uint128High64(const uint128& v);
46 46
47 // We add "std::" to avoid including all of port.h. 47 // We add "std::" to avoid including all of port.h.
48 friend NET_EXPORT_PRIVATE std::ostream& operator<<(std::ostream& o, 48 friend NET_EXPORT_PRIVATE std::ostream& operator<<(std::ostream& o,
49 const uint128& b); 49 const uint128& b);
50 50
51 private: 51 private:
52 // Little-endian memory order optimizations can benefit from 52 // Little-endian memory order optimizations can benefit from
53 // having lo_ first, hi_ last. 53 // having lo_ first, hi_ last.
54 // See util/endian/endian.h and Load128/Store128 for storing a uint128. 54 // See util/endian/endian.h and Load128/Store128 for storing a uint128.
55 uint64 lo_; 55 uint64 lo_;
56 uint64 hi_; 56 uint64 hi_;
57 57
58 // Not implemented, just declared for catching automatic type conversions. 58 // Not implemented, just declared for catching automatic type conversions.
59 uint128(uint8); 59 uint128(uint8);
60 uint128(uint16); 60 uint128(uint16);
61 uint128(float v); 61 uint128(float v);
62 uint128(double v); 62 uint128(double v);
63 }; 63 };
64 64
65 // This is a POD form of uint128 which can be used for static variables which 65 // This is a POD form of uint128 which can be used for static variables which
66 // need to be operated on as uint128. 66 // need to be operated on as uint128.
67 struct uint128_pod { 67 struct uint128_pod {
68 // Note: The ordering of fields is different than 'class uint128' but the 68 // Note: The ordering of fields is different than 'class uint128' but the
69 // same as its 2-arg constructor. This enables more obvious initialization 69 // same as its 2-arg constructor. This enables more obvious initialization
70 // of static instances, which is the primary reason for this struct in the 70 // of static instances, which is the primary reason for this struct in the
71 // first place. This does not seem to defeat any optimizations wrt 71 // first place. This does not seem to defeat any optimizations wrt
72 // operations involving this struct. 72 // operations involving this struct.
73 uint64 hi; 73 uint64 hi;
74 uint64 lo; 74 uint64 lo;
75 }; 75 };
76 76
77 NET_EXPORT_PRIVATE extern const uint128_pod kuint128max; 77 NET_EXPORT_PRIVATE extern const uint128_pod kuint128max;
78 78
79 // allow uint128 to be logged 79 // allow uint128 to be logged
80 NET_EXPORT_PRIVATE extern std::ostream& operator<<(std::ostream& o, 80 NET_EXPORT_PRIVATE extern std::ostream& operator<<(std::ostream& o,
81 const uint128& b); 81 const uint128& b);
82 82
83 // Methods to access low and high pieces of 128-bit value. 83 // Methods to access low and high pieces of 128-bit value.
84 // Defined externally from uint128 to facilitate conversion 84 // Defined externally from uint128 to facilitate conversion
85 // to native 128-bit types when compilers support them. 85 // to native 128-bit types when compilers support them.
86 inline uint64 Uint128Low64(const uint128& v) { return v.lo_; } 86 inline uint64 Uint128Low64(const uint128& v) {
87 inline uint64 Uint128High64(const uint128& v) { return v.hi_; } 87 return v.lo_;
88 }
89 inline uint64 Uint128High64(const uint128& v) {
90 return v.hi_;
91 }
88 92
89 // TODO: perhaps it would be nice to have int128, a signed 128-bit type? 93 // TODO: perhaps it would be nice to have int128, a signed 128-bit type?
90 94
91 // -------------------------------------------------------------------------- 95 // --------------------------------------------------------------------------
92 // Implementation details follow 96 // Implementation details follow
93 // -------------------------------------------------------------------------- 97 // --------------------------------------------------------------------------
94 inline bool operator==(const uint128& lhs, const uint128& rhs) { 98 inline bool operator==(const uint128& lhs, const uint128& rhs) {
95 return (Uint128Low64(lhs) == Uint128Low64(rhs) && 99 return (Uint128Low64(lhs) == Uint128Low64(rhs) &&
96 Uint128High64(lhs) == Uint128High64(rhs)); 100 Uint128High64(lhs) == Uint128High64(rhs));
97 } 101 }
98 inline bool operator!=(const uint128& lhs, const uint128& rhs) { 102 inline bool operator!=(const uint128& lhs, const uint128& rhs) {
99 return !(lhs == rhs); 103 return !(lhs == rhs);
100 } 104 }
101 inline uint128& uint128::operator=(const uint128& b) { 105 inline uint128& uint128::operator=(const uint128& b) {
102 lo_ = b.lo_; 106 lo_ = b.lo_;
103 hi_ = b.hi_; 107 hi_ = b.hi_;
104 return *this; 108 return *this;
105 } 109 }
106 110
107 inline uint128::uint128(): lo_(0), hi_(0) { } 111 inline uint128::uint128() : lo_(0), hi_(0) {
108 inline uint128::uint128(uint64 top, uint64 bottom) : lo_(bottom), hi_(top) { } 112 }
109 inline uint128::uint128(const uint128 &v) : lo_(v.lo_), hi_(v.hi_) { } 113 inline uint128::uint128(uint64 top, uint64 bottom) : lo_(bottom), hi_(top) {
110 inline uint128::uint128(const uint128_pod &v) : lo_(v.lo), hi_(v.hi) { } 114 }
111 inline uint128::uint128(uint64 bottom) : lo_(bottom), hi_(0) { } 115 inline uint128::uint128(const uint128& v) : lo_(v.lo_), hi_(v.hi_) {
112 inline uint128::uint128(uint32 bottom) : lo_(bottom), hi_(0) { } 116 }
117 inline uint128::uint128(const uint128_pod& v) : lo_(v.lo), hi_(v.hi) {
118 }
119 inline uint128::uint128(uint64 bottom) : lo_(bottom), hi_(0) {
120 }
121 inline uint128::uint128(uint32 bottom) : lo_(bottom), hi_(0) {
122 }
113 inline uint128::uint128(int bottom) : lo_(bottom), hi_(0) { 123 inline uint128::uint128(int bottom) : lo_(bottom), hi_(0) {
114 if (bottom < 0) { 124 if (bottom < 0) {
115 --hi_; 125 --hi_;
116 } 126 }
117 } 127 }
118 inline void uint128::Initialize(uint64 top, uint64 bottom) { 128 inline void uint128::Initialize(uint64 top, uint64 bottom) {
119 hi_ = top; 129 hi_ = top;
120 lo_ = bottom; 130 lo_ = bottom;
121 } 131 }
122 132
123 // Comparison operators. 133 // Comparison operators.
124 134
125 #define CMP128(op) \ 135 #define CMP128(op) \
126 inline bool operator op(const uint128& lhs, const uint128& rhs) { \ 136 inline bool operator op(const uint128& lhs, const uint128& rhs) { \
127 return (Uint128High64(lhs) == Uint128High64(rhs)) ? \ 137 return (Uint128High64(lhs) == Uint128High64(rhs)) \
128 (Uint128Low64(lhs) op Uint128Low64(rhs)) : \ 138 ? (Uint128Low64(lhs) op Uint128Low64(rhs)) \
129 (Uint128High64(lhs) op Uint128High64(rhs)); \ 139 : (Uint128High64(lhs) op Uint128High64(rhs)); \
130 } 140 }
131 141
132 CMP128(<) 142 CMP128(< )
133 CMP128(>) 143 CMP128(> )
134 CMP128(>=) 144 CMP128(>= )
135 CMP128(<=) 145 CMP128(<= )
136 146
137 #undef CMP128 147 #undef CMP128
138 148
139 // Unary operators 149 // Unary operators
140 150
141 inline uint128 operator-(const uint128& val) { 151 inline uint128 operator-(const uint128& val) {
142 const uint64 hi_flip = ~Uint128High64(val); 152 const uint64 hi_flip = ~Uint128High64(val);
143 const uint64 lo_flip = ~Uint128Low64(val); 153 const uint64 lo_flip = ~Uint128Low64(val);
144 const uint64 lo_add = lo_flip + 1; 154 const uint64 lo_add = lo_flip + 1;
145 if (lo_add < lo_flip) { 155 if (lo_add < lo_flip) {
146 return uint128(hi_flip + 1, lo_add); 156 return uint128(hi_flip + 1, lo_add);
147 } 157 }
148 return uint128(hi_flip, lo_add); 158 return uint128(hi_flip, lo_add);
149 } 159 }
150 160
151 inline bool operator!(const uint128& val) { 161 inline bool operator!(const uint128& val) {
152 return !Uint128High64(val) && !Uint128Low64(val); 162 return !Uint128High64(val) && !Uint128Low64(val);
153 } 163 }
154 164
155 // Logical operators. 165 // Logical operators.
156 166
157 inline uint128 operator~(const uint128& val) { 167 inline uint128 operator~(const uint128& val) {
158 return uint128(~Uint128High64(val), ~Uint128Low64(val)); 168 return uint128(~Uint128High64(val), ~Uint128Low64(val));
159 } 169 }
160 170
161 #define LOGIC128(op) \ 171 #define LOGIC128(op) \
162 inline uint128 operator op(const uint128& lhs, const uint128& rhs) { \ 172 inline uint128 operator op(const uint128& lhs, const uint128& rhs) { \
163 return uint128(Uint128High64(lhs) op Uint128High64(rhs), \ 173 return uint128(Uint128High64(lhs) op Uint128High64(rhs), \
164 Uint128Low64(lhs) op Uint128Low64(rhs)); \ 174 Uint128Low64(lhs) op Uint128Low64(rhs)); \
165 } 175 }
166 176
167 LOGIC128(|) 177 LOGIC128(| )
168 LOGIC128(&) 178 LOGIC128(&)
169 LOGIC128(^) 179 LOGIC128 (^)
170 180
171 #undef LOGIC128 181 #undef LOGIC128
172 182
173 #define LOGICASSIGN128(op) \ 183 #define LOGICASSIGN128(op) \
174 inline uint128& uint128::operator op(const uint128& other) { \ 184 inline uint128& uint128::operator op(const uint128& other) { \
175 hi_ op other.hi_; \ 185 hi_ op other.hi_; \
176 lo_ op other.lo_; \ 186 lo_ op other.lo_; \
177 return *this; \ 187 return *this; \
178 } 188 }
179 189
180 LOGICASSIGN128(|=) 190 LOGICASSIGN128(|= )
181 LOGICASSIGN128(&=) 191 LOGICASSIGN128(&= )
182 LOGICASSIGN128(^=) 192 LOGICASSIGN128(^= )
183 193
184 #undef LOGICASSIGN128 194 #undef LOGICASSIGN128
185 195
186 // Shift operators. 196 // Shift operators.
187 197
188 inline uint128 operator<<(const uint128& val, int amount) { 198 inline uint128 operator<<(const uint128& val, int amount) {
189 // uint64 shifts of >= 64 are undefined, so we will need some special-casing. 199 // uint64 shifts of >= 64 are undefined, so we will need some special-casing.
190 if (amount < 64) { 200 if (amount < 64) {
191 if (amount == 0) { 201 if (amount == 0) {
192 return val; 202 return val;
193 } 203 }
194 uint64 new_hi = (Uint128High64(val) << amount) | 204 uint64 new_hi =
195 (Uint128Low64(val) >> (64 - amount)); 205 (Uint128High64(val) << amount) | (Uint128Low64(val) >> (64 - amount));
196 uint64 new_lo = Uint128Low64(val) << amount; 206 uint64 new_lo = Uint128Low64(val) << amount;
197 return uint128(new_hi, new_lo); 207 return uint128(new_hi, new_lo);
198 } else if (amount < 128) { 208 } else if (amount < 128) {
199 return uint128(Uint128Low64(val) << (amount - 64), 0); 209 return uint128(Uint128Low64(val) << (amount - 64), 0);
200 } else { 210 } else {
201 return uint128(0, 0); 211 return uint128(0, 0);
202 } 212 }
203 } 213 }
204 214
205 inline uint128 operator>>(const uint128& val, int amount) { 215 inline uint128 operator>>(const uint128& val, int amount) {
206 // uint64 shifts of >= 64 are undefined, so we will need some special-casing. 216 // uint64 shifts of >= 64 are undefined, so we will need some special-casing.
207 if (amount < 64) { 217 if (amount < 64) {
208 if (amount == 0) { 218 if (amount == 0) {
209 return val; 219 return val;
210 } 220 }
211 uint64 new_hi = Uint128High64(val) >> amount; 221 uint64 new_hi = Uint128High64(val) >> amount;
212 uint64 new_lo = (Uint128Low64(val) >> amount) | 222 uint64 new_lo =
213 (Uint128High64(val) << (64 - amount)); 223 (Uint128Low64(val) >> amount) | (Uint128High64(val) << (64 - amount));
214 return uint128(new_hi, new_lo); 224 return uint128(new_hi, new_lo);
215 } else if (amount < 128) { 225 } else if (amount < 128) {
216 return uint128(0, Uint128High64(val) >> (amount - 64)); 226 return uint128(0, Uint128High64(val) >> (amount - 64));
217 } else { 227 } else {
218 return uint128(0, 0); 228 return uint128(0, 0);
219 } 229 }
220 } 230 }
221 231
222 inline uint128& uint128::operator<<=(int amount) { 232 inline uint128& uint128::operator<<=(int amount) {
223 // uint64 shifts of >= 64 are undefined, so we will need some special-casing. 233 // uint64 shifts of >= 64 are undefined, so we will need some special-casing.
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after
321 *this += 1; 331 *this += 1;
322 return *this; 332 return *this;
323 } 333 }
324 334
325 inline uint128& uint128::operator--() { 335 inline uint128& uint128::operator--() {
326 *this -= 1; 336 *this -= 1;
327 return *this; 337 return *this;
328 } 338 }
329 339
330 #endif // NET_BASE_INT128_H_ 340 #endif // NET_BASE_INT128_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698