blob: 101fbd1c19bfe25079c2ba1028b7a02b6d45487b [file] [log] [blame]
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001/*
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 Sehrc431b9d2018-03-02 12:01:51 -080017#ifndef ART_LIBARTBASE_BASE_BIT_FIELD_H_
18#define ART_LIBARTBASE_BASE_BIT_FIELD_H_
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010019
Andreas Gampe57943812017-12-06 21:39:13 -080020#include <android-base/logging.h>
21
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010022#include "globals.h"
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010023
24namespace art {
25
Ian Rogers13735952014-10-08 12:43:28 -070026static constexpr uintptr_t kUintPtrTOne = 1U;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010027
28// BitField is a template for encoding and decoding a bit field inside
29// an unsigned machine word.
Vladimir Markoa1de9182016-02-25 11:37:38 +000030template<typename T, size_t kPosition, size_t kSize>
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010031class BitField {
32 public:
Vladimir Marko4f990712021-07-14 12:45:13 +010033 using value_type = T;
Vladimir Markoa1de9182016-02-25 11:37:38 +000034 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 Geoffray01bc96d2014-04-11 17:43:50 +010042 // Tells whether the provided value fits into the bit field.
Vladimir Markoce2a3442021-11-24 15:10:26 +000043 static constexpr bool IsValid(T value) {
Ian Rogers13735952014-10-08 12:43:28 -070044 return (static_cast<uintptr_t>(value) & ~((kUintPtrTOne << size) - 1)) == 0;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010045 }
46
47 // Returns a uword mask of the bit field.
Vladimir Markoce2a3442021-11-24 15:10:26 +000048 static constexpr uintptr_t Mask() {
Ian Rogers13735952014-10-08 12:43:28 -070049 return (kUintPtrTOne << size) - 1;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010050 }
51
52 // Returns a uword mask of the bit field which can be applied directly to
53 // the raw unshifted bits.
Vladimir Markoce2a3442021-11-24 15:10:26 +000054 static constexpr uintptr_t MaskInPlace() {
Ian Rogers13735952014-10-08 12:43:28 -070055 return ((kUintPtrTOne << size) - 1) << position;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010056 }
57
58 // Returns the shift count needed to right-shift the bit field to
59 // the least-significant bits.
Vladimir Markoce2a3442021-11-24 15:10:26 +000060 static constexpr int Shift() {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010061 return position;
62 }
63
64 // Returns the size of the bit field.
Vladimir Markoce2a3442021-11-24 15:10:26 +000065 static constexpr int BitSize() {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010066 return size;
67 }
68
69 // Returns a uword with the bit field value encoded.
Vladimir Markoce2a3442021-11-24 15:10:26 +000070 static constexpr uintptr_t Encode(T value) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010071 DCHECK(IsValid(value));
Ian Rogers13735952014-10-08 12:43:28 -070072 return static_cast<uintptr_t>(value) << position;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010073 }
74
75 // Extracts the bit field from the value.
Vladimir Markoce2a3442021-11-24 15:10:26 +000076 static constexpr T Decode(uintptr_t value) {
Ian Rogers13735952014-10-08 12:43:28 -070077 return static_cast<T>((value >> position) & ((kUintPtrTOne << size) - 1));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010078 }
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 Markoce2a3442021-11-24 15:10:26 +000083 static constexpr uintptr_t Update(T value, uintptr_t original) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010084 DCHECK(IsValid(value));
Ian Rogers13735952014-10-08 12:43:28 -070085 return (static_cast<uintptr_t>(value) << position) |
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010086 (~MaskInPlace() & original);
87 }
88};
89
90} // namespace art
91
David Sehrc431b9d2018-03-02 12:01:51 -080092#endif // ART_LIBARTBASE_BASE_BIT_FIELD_H_