Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
David Sehr | c431b9d | 2018-03-02 12:01:51 -0800 | [diff] [blame] | 17 | #ifndef ART_LIBARTBASE_BASE_BIT_FIELD_H_ |
| 18 | #define ART_LIBARTBASE_BASE_BIT_FIELD_H_ |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 19 | |
Andreas Gampe | 5794381 | 2017-12-06 21:39:13 -0800 | [diff] [blame] | 20 | #include <android-base/logging.h> |
| 21 | |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 22 | #include "globals.h" |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 23 | |
| 24 | namespace art { |
| 25 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 26 | static constexpr uintptr_t kUintPtrTOne = 1U; |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 27 | |
| 28 | // BitField is a template for encoding and decoding a bit field inside |
| 29 | // an unsigned machine word. |
Vladimir Marko | a1de918 | 2016-02-25 11:37:38 +0000 | [diff] [blame] | 30 | template<typename T, size_t kPosition, size_t kSize> |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 31 | class BitField { |
| 32 | public: |
Vladimir Marko | 4f99071 | 2021-07-14 12:45:13 +0100 | [diff] [blame] | 33 | using value_type = T; |
Vladimir Marko | a1de918 | 2016-02-25 11:37:38 +0000 | [diff] [blame] | 34 | static constexpr size_t position = kPosition; |
| 35 | static constexpr size_t size = kSize; |
| 36 | |
| 37 | static_assert(position < sizeof(uintptr_t) * kBitsPerByte, "Invalid position."); |
| 38 | static_assert(size != 0u, "Invalid size."); |
| 39 | static_assert(size <= sizeof(uintptr_t) * kBitsPerByte, "Invalid size."); |
| 40 | static_assert(size + position <= sizeof(uintptr_t) * kBitsPerByte, "Invalid position + size."); |
| 41 | |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 42 | // Tells whether the provided value fits into the bit field. |
Vladimir Marko | ce2a344 | 2021-11-24 15:10:26 +0000 | [diff] [blame] | 43 | static constexpr bool IsValid(T value) { |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 44 | return (static_cast<uintptr_t>(value) & ~((kUintPtrTOne << size) - 1)) == 0; |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | // Returns a uword mask of the bit field. |
Vladimir Marko | ce2a344 | 2021-11-24 15:10:26 +0000 | [diff] [blame] | 48 | static constexpr uintptr_t Mask() { |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 49 | return (kUintPtrTOne << size) - 1; |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | // Returns a uword mask of the bit field which can be applied directly to |
| 53 | // the raw unshifted bits. |
Vladimir Marko | ce2a344 | 2021-11-24 15:10:26 +0000 | [diff] [blame] | 54 | static constexpr uintptr_t MaskInPlace() { |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 55 | return ((kUintPtrTOne << size) - 1) << position; |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | // Returns the shift count needed to right-shift the bit field to |
| 59 | // the least-significant bits. |
Vladimir Marko | ce2a344 | 2021-11-24 15:10:26 +0000 | [diff] [blame] | 60 | static constexpr int Shift() { |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 61 | return position; |
| 62 | } |
| 63 | |
| 64 | // Returns the size of the bit field. |
Vladimir Marko | ce2a344 | 2021-11-24 15:10:26 +0000 | [diff] [blame] | 65 | static constexpr int BitSize() { |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 66 | return size; |
| 67 | } |
| 68 | |
| 69 | // Returns a uword with the bit field value encoded. |
Vladimir Marko | ce2a344 | 2021-11-24 15:10:26 +0000 | [diff] [blame] | 70 | static constexpr uintptr_t Encode(T value) { |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 71 | DCHECK(IsValid(value)); |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 72 | return static_cast<uintptr_t>(value) << position; |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | // Extracts the bit field from the value. |
Vladimir Marko | ce2a344 | 2021-11-24 15:10:26 +0000 | [diff] [blame] | 76 | static constexpr T Decode(uintptr_t value) { |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 77 | return static_cast<T>((value >> position) & ((kUintPtrTOne << size) - 1)); |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | // Returns a uword with the bit field value encoded based on the |
| 81 | // original value. Only the bits corresponding to this bit field |
| 82 | // will be changed. |
Vladimir Marko | ce2a344 | 2021-11-24 15:10:26 +0000 | [diff] [blame] | 83 | static constexpr uintptr_t Update(T value, uintptr_t original) { |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 84 | DCHECK(IsValid(value)); |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 85 | return (static_cast<uintptr_t>(value) << position) | |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 86 | (~MaskInPlace() & original); |
| 87 | } |
| 88 | }; |
| 89 | |
| 90 | } // namespace art |
| 91 | |
David Sehr | c431b9d | 2018-03-02 12:01:51 -0800 | [diff] [blame] | 92 | #endif // ART_LIBARTBASE_BASE_BIT_FIELD_H_ |