| OLD | NEW |
| 1 // Copyright 2011 the V8 project authors. All rights reserved. | 1 // Copyright 2011 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 185 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 196 return static_cast<int>(length); | 196 return static_cast<int>(length); |
| 197 } | 197 } |
| 198 | 198 |
| 199 | 199 |
| 200 // ---------------------------------------------------------------------------- | 200 // ---------------------------------------------------------------------------- |
| 201 // BitField is a help template for encoding and decode bitfield with | 201 // BitField is a help template for encoding and decode bitfield with |
| 202 // unsigned content. | 202 // unsigned content. |
| 203 template<class T, int shift, int size> | 203 template<class T, int shift, int size> |
| 204 class BitField { | 204 class BitField { |
| 205 public: | 205 public: |
| 206 // A uint32_t mask of bit field. To use all bits of a uint32 in a |
| 207 // bitfield without compiler warnings we have to compute 2^32 without |
| 208 // using a shift count of 32. |
| 209 static const uint32_t kMask = ((1U << shift) << size) - (1U << shift); |
| 210 |
| 211 // Value for the field with all bits set. |
| 212 static const T kMax = static_cast<T>((1U << size) - 1); |
| 213 |
| 206 // Tells whether the provided value fits into the bit field. | 214 // Tells whether the provided value fits into the bit field. |
| 207 static bool is_valid(T value) { | 215 static bool is_valid(T value) { |
| 208 return (static_cast<uint32_t>(value) & ~((1U << (size)) - 1)) == 0; | 216 return (static_cast<uint32_t>(value) & ~kMax) == 0; |
| 209 } | |
| 210 | |
| 211 // Returns a uint32_t mask of bit field. | |
| 212 static uint32_t mask() { | |
| 213 // To use all bits of a uint32 in a bitfield without compiler warnings we | |
| 214 // have to compute 2^32 without using a shift count of 32. | |
| 215 return ((1U << shift) << size) - (1U << shift); | |
| 216 } | 217 } |
| 217 | 218 |
| 218 // Returns a uint32_t with the bit field value encoded. | 219 // Returns a uint32_t with the bit field value encoded. |
| 219 static uint32_t encode(T value) { | 220 static uint32_t encode(T value) { |
| 220 ASSERT(is_valid(value)); | 221 ASSERT(is_valid(value)); |
| 221 return static_cast<uint32_t>(value) << shift; | 222 return static_cast<uint32_t>(value) << shift; |
| 222 } | 223 } |
| 223 | 224 |
| 224 // Returns a uint32_t with the bit field value updated. | 225 // Returns a uint32_t with the bit field value updated. |
| 225 static uint32_t update(uint32_t previous, T value) { | 226 static uint32_t update(uint32_t previous, T value) { |
| 226 return (previous & ~mask()) | encode(value); | 227 return (previous & ~kMask) | encode(value); |
| 227 } | 228 } |
| 228 | 229 |
| 229 // Extracts the bit field from the value. | 230 // Extracts the bit field from the value. |
| 230 static T decode(uint32_t value) { | 231 static T decode(uint32_t value) { |
| 231 return static_cast<T>((value & mask()) >> shift); | 232 return static_cast<T>((value & kMask) >> shift); |
| 232 } | |
| 233 | |
| 234 // Value for the field with all bits set. | |
| 235 static T max() { | |
| 236 return decode(mask()); | |
| 237 } | 233 } |
| 238 }; | 234 }; |
| 239 | 235 |
| 240 | 236 |
| 241 // ---------------------------------------------------------------------------- | 237 // ---------------------------------------------------------------------------- |
| 242 // Hash function. | 238 // Hash function. |
| 243 | 239 |
| 244 // Thomas Wang, Integer Hash Functions. | 240 // Thomas Wang, Integer Hash Functions. |
| 245 // http://www.concentric.net/~Ttwang/tech/inthash.htm | 241 // http://www.concentric.net/~Ttwang/tech/inthash.htm |
| 246 static inline uint32_t ComputeIntegerHash(uint32_t key) { | 242 static inline uint32_t ComputeIntegerHash(uint32_t key) { |
| (...skipping 668 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 915 ASSERT(element < static_cast<int>(sizeof(T) * CHAR_BIT)); | 911 ASSERT(element < static_cast<int>(sizeof(T) * CHAR_BIT)); |
| 916 return 1 << element; | 912 return 1 << element; |
| 917 } | 913 } |
| 918 | 914 |
| 919 T bits_; | 915 T bits_; |
| 920 }; | 916 }; |
| 921 | 917 |
| 922 } } // namespace v8::internal | 918 } } // namespace v8::internal |
| 923 | 919 |
| 924 #endif // V8_UTILS_H_ | 920 #endif // V8_UTILS_H_ |
| OLD | NEW |