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