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

Side by Side Diff: src/utils.h

Issue 14643004: Provide BitField64 utility class. (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Created 7 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
« no previous file with comments | « no previous file | test/cctest/test-conversions.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2012 the V8 project authors. All rights reserved. 1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without 2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are 3 // modification, are permitted provided that the following conditions are
4 // met: 4 // met:
5 // 5 //
6 // * Redistributions of source code must retain the above copyright 6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer. 7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above 8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following 9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided 10 // disclaimer in the documentation and/or other materials provided
(...skipping 224 matching lines...) Expand 10 before | Expand all | Expand 10 after
235 inline int StrLength(const char* string) { 235 inline int StrLength(const char* string) {
236 size_t length = strlen(string); 236 size_t length = strlen(string);
237 ASSERT(length == static_cast<size_t>(static_cast<int>(length))); 237 ASSERT(length == static_cast<size_t>(static_cast<int>(length)));
238 return static_cast<int>(length); 238 return static_cast<int>(length);
239 } 239 }
240 240
241 241
242 // ---------------------------------------------------------------------------- 242 // ----------------------------------------------------------------------------
243 // BitField is a help template for encoding and decode bitfield with 243 // BitField is a help template for encoding and decode bitfield with
244 // unsigned content. 244 // unsigned content.
245 template<class T, int shift, int size> 245
246 class BitField { 246 template<class T, int shift, int size, class U>
247 class BitFieldBase {
247 public: 248 public:
248 // A uint32_t mask of bit field. To use all bits of a uint32 in a 249 // A type U mask of bit field. To use all bits of a type U of x bits
249 // bitfield without compiler warnings we have to compute 2^32 without 250 // in a bitfield without compiler warnings we have to compute 2^x
250 // using a shift count of 32. 251 // without using a shift count of x in the computation.
251 static const uint32_t kMask = ((1U << shift) << size) - (1U << shift); 252 static const U kOne = static_cast<U>(1U);
252 static const uint32_t kShift = shift; 253 static const U kMask = ((kOne << shift) << size) - (kOne << shift);
253 static const uint32_t kSize = size; 254 static const U kShift = shift;
255 static const U kSize = size;
254 256
255 // Value for the field with all bits set. 257 // Value for the field with all bits set.
256 static const T kMax = static_cast<T>((1U << size) - 1); 258 static const T kMax = static_cast<T>((1U << size) - 1);
257 259
258 // Tells whether the provided value fits into the bit field. 260 // Tells whether the provided value fits into the bit field.
259 static bool is_valid(T value) { 261 static bool is_valid(T value) {
260 return (static_cast<uint32_t>(value) & ~static_cast<uint32_t>(kMax)) == 0; 262 return (static_cast<U>(value) & ~static_cast<U>(kMax)) == 0;
261 } 263 }
262 264
263 // Returns a uint32_t with the bit field value encoded. 265 // Returns a type U with the bit field value encoded.
264 static uint32_t encode(T value) { 266 static U encode(T value) {
265 ASSERT(is_valid(value)); 267 ASSERT(is_valid(value));
266 return static_cast<uint32_t>(value) << shift; 268 return static_cast<U>(value) << shift;
267 } 269 }
268 270
269 // Returns a uint32_t with the bit field value updated. 271 // Returns a type U with the bit field value updated.
270 static uint32_t update(uint32_t previous, T value) { 272 static U update(U previous, T value) {
271 return (previous & ~kMask) | encode(value); 273 return (previous & ~kMask) | encode(value);
272 } 274 }
273 275
274 // Extracts the bit field from the value. 276 // Extracts the bit field from the value.
275 static T decode(uint32_t value) { 277 static T decode(U value) {
276 return static_cast<T>((value & kMask) >> shift); 278 return static_cast<T>((value & kMask) >> shift);
277 } 279 }
278 }; 280 };
279 281
280 282
283 template<class T, int shift, int size>
284 class BitField : public BitFieldBase<T, shift, size, uint32_t> { };
285
286
287 template<class T, int shift, int size>
288 class BitField64 : public BitFieldBase<T, shift, size, uint64_t> { };
289
290
281 // ---------------------------------------------------------------------------- 291 // ----------------------------------------------------------------------------
282 // Hash function. 292 // Hash function.
283 293
284 static const uint32_t kZeroHashSeed = 0; 294 static const uint32_t kZeroHashSeed = 0;
285 295
286 // Thomas Wang, Integer Hash Functions. 296 // Thomas Wang, Integer Hash Functions.
287 // http://www.concentric.net/~Ttwang/tech/inthash.htm 297 // http://www.concentric.net/~Ttwang/tech/inthash.htm
288 inline uint32_t ComputeIntegerHash(uint32_t key, uint32_t seed) { 298 inline uint32_t ComputeIntegerHash(uint32_t key, uint32_t seed) {
289 uint32_t hash = key; 299 uint32_t hash = key;
290 hash = hash ^ seed; 300 hash = hash ^ seed;
(...skipping 800 matching lines...) Expand 10 before | Expand all | Expand 10 after
1091 1101
1092 // Every compiled stub starts with this id. 1102 // Every compiled stub starts with this id.
1093 static const int kStubEntryId = 5; 1103 static const int kStubEntryId = 5;
1094 1104
1095 int id_; 1105 int id_;
1096 }; 1106 };
1097 1107
1098 } } // namespace v8::internal 1108 } } // namespace v8::internal
1099 1109
1100 #endif // V8_UTILS_H_ 1110 #endif // V8_UTILS_H_
OLDNEW
« no previous file with comments | « no previous file | test/cctest/test-conversions.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698