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