| OLD | NEW |
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef RUNTIME_VM_BITFIELD_H_ | 5 #ifndef RUNTIME_VM_BITFIELD_H_ |
| 6 #define RUNTIME_VM_BITFIELD_H_ | 6 #define RUNTIME_VM_BITFIELD_H_ |
| 7 | 7 |
| 8 namespace dart { | 8 namespace dart { |
| 9 | 9 |
| 10 static const uword kUwordOne = 1U; | 10 static const uword kUwordOne = 1U; |
| 11 | 11 |
| 12 // BitField is a template for encoding and decoding a value of type T | 12 // BitField is a template for encoding and decoding a value of type T |
| 13 // inside a storage of type S. | 13 // inside a storage of type S. |
| 14 template<typename S, typename T, int position, int size> | 14 template <typename S, typename T, int position, int size> |
| 15 class BitField { | 15 class BitField { |
| 16 public: | 16 public: |
| 17 static const intptr_t kNextBit = position + size; | 17 static const intptr_t kNextBit = position + size; |
| 18 | 18 |
| 19 // Tells whether the provided value fits into the bit field. | 19 // Tells whether the provided value fits into the bit field. |
| 20 static bool is_valid(T value) { | 20 static bool is_valid(T value) { |
| 21 return (static_cast<S>(value) & ~((kUwordOne << size) - 1)) == 0; | 21 return (static_cast<S>(value) & ~((kUwordOne << size) - 1)) == 0; |
| 22 } | 22 } |
| 23 | 23 |
| 24 // Returns a S mask of the bit field. | 24 // Returns a S mask of the bit field. |
| 25 static S mask() { | 25 static S mask() { return (kUwordOne << size) - 1; } |
| 26 return (kUwordOne << size) - 1; | |
| 27 } | |
| 28 | 26 |
| 29 // Returns a S mask of the bit field which can be applied directly to | 27 // Returns a S mask of the bit field which can be applied directly to |
| 30 // to the raw unshifted bits. | 28 // to the raw unshifted bits. |
| 31 static S mask_in_place() { | 29 static S mask_in_place() { return ((kUwordOne << size) - 1) << position; } |
| 32 return ((kUwordOne << size) - 1) << position; | |
| 33 } | |
| 34 | 30 |
| 35 // Returns the shift count needed to right-shift the bit field to | 31 // Returns the shift count needed to right-shift the bit field to |
| 36 // the least-significant bits. | 32 // the least-significant bits. |
| 37 static int shift() { | 33 static int shift() { return position; } |
| 38 return position; | |
| 39 } | |
| 40 | 34 |
| 41 // Returns the size of the bit field. | 35 // Returns the size of the bit field. |
| 42 static int bitsize() { | 36 static int bitsize() { return size; } |
| 43 return size; | |
| 44 } | |
| 45 | 37 |
| 46 // Returns an S with the bit field value encoded. | 38 // Returns an S with the bit field value encoded. |
| 47 static S encode(T value) { | 39 static S encode(T value) { |
| 48 COMPILE_ASSERT((sizeof(S) * kBitsPerByte) >= (position + size)); | 40 COMPILE_ASSERT((sizeof(S) * kBitsPerByte) >= (position + size)); |
| 49 ASSERT(is_valid(value)); | 41 ASSERT(is_valid(value)); |
| 50 return static_cast<S>(value) << position; | 42 return static_cast<S>(value) << position; |
| 51 } | 43 } |
| 52 | 44 |
| 53 // Extracts the bit field from the value. | 45 // Extracts the bit field from the value. |
| 54 static T decode(S value) { | 46 static T decode(S value) { |
| 55 return static_cast<T>((value >> position) & ((kUwordOne << size) - 1)); | 47 return static_cast<T>((value >> position) & ((kUwordOne << size) - 1)); |
| 56 } | 48 } |
| 57 | 49 |
| 58 // Returns an S with the bit field value encoded based on the | 50 // Returns an S with the bit field value encoded based on the |
| 59 // original value. Only the bits corresponding to this bit field | 51 // original value. Only the bits corresponding to this bit field |
| 60 // will be changed. | 52 // will be changed. |
| 61 static S update(T value, S original) { | 53 static S update(T value, S original) { |
| 62 ASSERT(is_valid(value)); | 54 ASSERT(is_valid(value)); |
| 63 return (static_cast<S>(value) << position) | | 55 return (static_cast<S>(value) << position) | (~mask_in_place() & original); |
| 64 (~mask_in_place() & original); | |
| 65 } | 56 } |
| 66 }; | 57 }; |
| 67 | 58 |
| 68 } // namespace dart | 59 } // namespace dart |
| 69 | 60 |
| 70 #endif // RUNTIME_VM_BITFIELD_H_ | 61 #endif // RUNTIME_VM_BITFIELD_H_ |
| OLD | NEW |