blob: 101fbd1c19bfe25079c2ba1028b7a02b6d45487b [file] [log] [blame]
Martin Stjernholmc15e7e42020-12-02 22:50:53 +00001/*
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
17#ifndef ART_LIBARTBASE_BASE_BIT_FIELD_H_
18#define ART_LIBARTBASE_BASE_BIT_FIELD_H_
19
20#include <android-base/logging.h>
21
22#include "globals.h"
23
24namespace art {
25
26static constexpr uintptr_t kUintPtrTOne = 1U;
27
28// BitField is a template for encoding and decoding a bit field inside
29// an unsigned machine word.
30template<typename T, size_t kPosition, size_t kSize>
31class BitField {
32 public:
android-t13d2c5b22022-10-12 13:43:18 +080033 using value_type = T;
Martin Stjernholmc15e7e42020-12-02 22:50:53 +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
42 // Tells whether the provided value fits into the bit field.
android-t13d2c5b22022-10-12 13:43:18 +080043 static constexpr bool IsValid(T value) {
Martin Stjernholmc15e7e42020-12-02 22:50:53 +000044 return (static_cast<uintptr_t>(value) & ~((kUintPtrTOne << size) - 1)) == 0;
45 }
46
47 // Returns a uword mask of the bit field.
android-t13d2c5b22022-10-12 13:43:18 +080048 static constexpr uintptr_t Mask() {
Martin Stjernholmc15e7e42020-12-02 22:50:53 +000049 return (kUintPtrTOne << size) - 1;
50 }
51
52 // Returns a uword mask of the bit field which can be applied directly to
53 // the raw unshifted bits.
android-t13d2c5b22022-10-12 13:43:18 +080054 static constexpr uintptr_t MaskInPlace() {
Martin Stjernholmc15e7e42020-12-02 22:50:53 +000055 return ((kUintPtrTOne << size) - 1) << position;
56 }
57
58 // Returns the shift count needed to right-shift the bit field to
59 // the least-significant bits.
android-t13d2c5b22022-10-12 13:43:18 +080060 static constexpr int Shift() {
Martin Stjernholmc15e7e42020-12-02 22:50:53 +000061 return position;
62 }
63
64 // Returns the size of the bit field.
android-t13d2c5b22022-10-12 13:43:18 +080065 static constexpr int BitSize() {
Martin Stjernholmc15e7e42020-12-02 22:50:53 +000066 return size;
67 }
68
69 // Returns a uword with the bit field value encoded.
android-t13d2c5b22022-10-12 13:43:18 +080070 static constexpr uintptr_t Encode(T value) {
Martin Stjernholmc15e7e42020-12-02 22:50:53 +000071 DCHECK(IsValid(value));
72 return static_cast<uintptr_t>(value) << position;
73 }
74
75 // Extracts the bit field from the value.
android-t13d2c5b22022-10-12 13:43:18 +080076 static constexpr T Decode(uintptr_t value) {
Martin Stjernholmc15e7e42020-12-02 22:50:53 +000077 return static_cast<T>((value >> position) & ((kUintPtrTOne << size) - 1));
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.
android-t13d2c5b22022-10-12 13:43:18 +080083 static constexpr uintptr_t Update(T value, uintptr_t original) {
Martin Stjernholmc15e7e42020-12-02 22:50:53 +000084 DCHECK(IsValid(value));
85 return (static_cast<uintptr_t>(value) << position) |
86 (~MaskInPlace() & original);
87 }
88};
89
90} // namespace art
91
92#endif // ART_LIBARTBASE_BASE_BIT_FIELD_H_