| 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 VM_BITFIELD_H_ | 5 #ifndef VM_BITFIELD_H_ |
| 6 #define VM_BITFIELD_H_ | 6 #define 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 bit field inside | 12 // BitField is a template for encoding and decoding a value of type T |
| 13 // an unsigned machine word. | 13 // inside a storage of type S. |
| 14 template<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<uword>(value) & ~((kUwordOne << size) - 1)) == 0; | 21 return (static_cast<S>(value) & ~((kUwordOne << size) - 1)) == 0; |
| 22 } | 22 } |
| 23 | 23 |
| 24 // Returns a uword mask of the bit field. | 24 // Returns a S mask of the bit field. |
| 25 static uword mask() { | 25 static S mask() { |
| 26 return (kUwordOne << size) - 1; | 26 return (kUwordOne << size) - 1; |
| 27 } | 27 } |
| 28 | 28 |
| 29 // Returns a uword mask of the bit field which can be applied directly to | 29 // Returns a S mask of the bit field which can be applied directly to |
| 30 // to the raw unshifted bits. | 30 // to the raw unshifted bits. |
| 31 static uword mask_in_place() { | 31 static S mask_in_place() { |
| 32 return ((kUwordOne << size) - 1) << position; | 32 return ((kUwordOne << size) - 1) << position; |
| 33 } | 33 } |
| 34 | 34 |
| 35 // Returns the shift count needed to right-shift the bit field to | 35 // Returns the shift count needed to right-shift the bit field to |
| 36 // the least-significant bits. | 36 // the least-significant bits. |
| 37 static int shift() { | 37 static int shift() { |
| 38 return position; | 38 return position; |
| 39 } | 39 } |
| 40 | 40 |
| 41 // Returns the size of the bit field. | 41 // Returns the size of the bit field. |
| 42 static int bitsize() { | 42 static int bitsize() { |
| 43 return size; | 43 return size; |
| 44 } | 44 } |
| 45 | 45 |
| 46 // Returns a uword with the bit field value encoded. | 46 // Returns an S with the bit field value encoded. |
| 47 static uword encode(T value) { | 47 static S encode(T value) { |
| 48 COMPILE_ASSERT((sizeof(S) * kBitsPerByte) >= (position + size)); |
| 48 ASSERT(is_valid(value)); | 49 ASSERT(is_valid(value)); |
| 49 return static_cast<uword>(value) << position; | 50 return static_cast<S>(value) << position; |
| 50 } | 51 } |
| 51 | 52 |
| 52 // Extracts the bit field from the value. | 53 // Extracts the bit field from the value. |
| 53 static T decode(uword value) { | 54 static T decode(S value) { |
| 54 return static_cast<T>((value >> position) & ((kUwordOne << size) - 1)); | 55 return static_cast<T>((value >> position) & ((kUwordOne << size) - 1)); |
| 55 } | 56 } |
| 56 | 57 |
| 57 // Returns a uword with the bit field value encoded based on the | 58 // Returns an S with the bit field value encoded based on the |
| 58 // original value. Only the bits corresponding to this bit field | 59 // original value. Only the bits corresponding to this bit field |
| 59 // will be changed. | 60 // will be changed. |
| 60 static uword update(T value, uword original) { | 61 static S update(T value, S original) { |
| 61 ASSERT(is_valid(value)); | 62 ASSERT(is_valid(value)); |
| 62 return (static_cast<uword>(value) << position) | | 63 return (static_cast<S>(value) << position) | |
| 63 (~mask_in_place() & original); | 64 (~mask_in_place() & original); |
| 64 } | 65 } |
| 65 }; | 66 }; |
| 66 | 67 |
| 67 } // namespace dart | 68 } // namespace dart |
| 68 | 69 |
| 69 #endif // VM_BITFIELD_H_ | 70 #endif // VM_BITFIELD_H_ |
| OLD | NEW |