1*eca53ba6SRoland Levillain // Copyright 2017 Google LLC
2*eca53ba6SRoland Levillain //
3*eca53ba6SRoland Levillain // Licensed under the Apache License, Version 2.0 (the "License");
4*eca53ba6SRoland Levillain // you may not use this file except in compliance with the License.
5*eca53ba6SRoland Levillain // You may obtain a copy of the License at
6*eca53ba6SRoland Levillain //
7*eca53ba6SRoland Levillain // http://www.apache.org/licenses/LICENSE-2.0
8*eca53ba6SRoland Levillain //
9*eca53ba6SRoland Levillain // Unless required by applicable law or agreed to in writing, software
10*eca53ba6SRoland Levillain // distributed under the License is distributed on an "AS IS" BASIS,
11*eca53ba6SRoland Levillain // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*eca53ba6SRoland Levillain // See the License for the specific language governing permissions and
13*eca53ba6SRoland Levillain // limitations under the License.
14*eca53ba6SRoland Levillain
15*eca53ba6SRoland Levillain #ifndef CPU_FEATURES_INCLUDE_INTERNAL_BIT_UTILS_H_
16*eca53ba6SRoland Levillain #define CPU_FEATURES_INCLUDE_INTERNAL_BIT_UTILS_H_
17*eca53ba6SRoland Levillain
18*eca53ba6SRoland Levillain #include <assert.h>
19*eca53ba6SRoland Levillain #include <stdbool.h>
20*eca53ba6SRoland Levillain #include <stdint.h>
21*eca53ba6SRoland Levillain
22*eca53ba6SRoland Levillain #include "cpu_features_macros.h"
23*eca53ba6SRoland Levillain
24*eca53ba6SRoland Levillain CPU_FEATURES_START_CPP_NAMESPACE
25*eca53ba6SRoland Levillain
IsBitSet(uint32_t reg,uint32_t bit)26*eca53ba6SRoland Levillain inline static bool IsBitSet(uint32_t reg, uint32_t bit) {
27*eca53ba6SRoland Levillain return (reg >> bit) & 0x1;
28*eca53ba6SRoland Levillain }
29*eca53ba6SRoland Levillain
ExtractBitRange(uint32_t reg,uint32_t msb,uint32_t lsb)30*eca53ba6SRoland Levillain inline static uint32_t ExtractBitRange(uint32_t reg, uint32_t msb,
31*eca53ba6SRoland Levillain uint32_t lsb) {
32*eca53ba6SRoland Levillain const uint64_t bits = msb - lsb + 1ULL;
33*eca53ba6SRoland Levillain const uint64_t mask = (1ULL << bits) - 1ULL;
34*eca53ba6SRoland Levillain assert(msb >= lsb);
35*eca53ba6SRoland Levillain return (reg >> lsb) & mask;
36*eca53ba6SRoland Levillain }
37*eca53ba6SRoland Levillain
38*eca53ba6SRoland Levillain CPU_FEATURES_END_CPP_NAMESPACE
39*eca53ba6SRoland Levillain
40*eca53ba6SRoland Levillain #endif // CPU_FEATURES_INCLUDE_INTERNAL_BIT_UTILS_H_
41