xref: /aosp_15_r20/art/runtime/oat/index_bss_mapping.cc (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1 /*
2  * Copyright (C) 2017 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 #include <algorithm>
18 
19 #include "index_bss_mapping.h"
20 
21 #include "base/bit_utils.h"
22 #include "base/length_prefixed_array.h"
23 
24 namespace art HIDDEN {
25 
GetBssOffset(size_t index_bits,uint32_t index,size_t slot_size) const26 size_t IndexBssMappingEntry::GetBssOffset(size_t index_bits,
27                                           uint32_t index,
28                                           size_t slot_size) const {
29   uint32_t diff = GetIndex(index_bits) - index;
30   if (diff == 0u) {
31     return bss_offset;
32   }
33   size_t mask_bits = 32u - index_bits;
34   if (diff > mask_bits) {
35     return IndexBssMappingLookup::npos;
36   }
37   // Shift out the index bits and bits for lower indexes.
38   // Note that `index_bits + (mask_bits - diff) == 32 - diff`.
39   uint32_t mask_from_index = index_and_mask >> (32u - diff);
40   if ((mask_from_index & 1u) != 0u) {
41     return bss_offset - POPCOUNT(mask_from_index) * slot_size;
42   } else {
43     return IndexBssMappingLookup::npos;
44   }
45 }
46 
GetBssOffset(const IndexBssMapping * mapping,uint32_t index,uint32_t number_of_indexes,size_t slot_size)47 size_t IndexBssMappingLookup::GetBssOffset(const IndexBssMapping* mapping,
48                                            uint32_t index,
49                                            uint32_t number_of_indexes,
50                                            size_t slot_size) {
51   DCHECK_LT(index, number_of_indexes);
52   if (mapping == nullptr) {
53     return npos;
54   }
55   size_t index_bits = IndexBssMappingEntry::IndexBits(number_of_indexes);
56   uint32_t index_mask = IndexBssMappingEntry::IndexMask(index_bits);
57   auto it = std::partition_point(
58       mapping->begin(),
59       mapping->end(),
60       [=](const struct IndexBssMappingEntry& entry) {
61         return (entry.index_and_mask & index_mask) < index;
62       });
63   if (it == mapping->end()) {
64     return npos;
65   }
66   const IndexBssMappingEntry& entry = *it;
67   return entry.GetBssOffset(index_bits, index, slot_size);
68 }
69 
70 }  // namespace art
71