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