blob: 8d9d8cfe63f1252ea13ebfdedec1d1a36bb68fd6 [file] [log] [blame]
Vladimir Markof3c52b42017-11-17 17:32:12 +00001/*
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
24namespace art {
25
26size_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
47constexpr size_t IndexBssMappingLookup::npos;
48
49size_t IndexBssMappingLookup::GetBssOffset(const IndexBssMapping* mapping,
50 uint32_t index,
51 uint32_t number_of_indexes,
52 size_t slot_size) {
53 DCHECK_LT(index, number_of_indexes);
54 if (mapping == nullptr) {
55 return npos;
56 }
57 size_t index_bits = IndexBssMappingEntry::IndexBits(number_of_indexes);
58 uint32_t index_mask = IndexBssMappingEntry::IndexMask(index_bits);
59 auto it = std::partition_point(
60 mapping->begin(),
61 mapping->end(),
62 [=](const struct IndexBssMappingEntry& entry) {
63 return (entry.index_and_mask & index_mask) < index;
64 });
65 if (it == mapping->end()) {
66 return npos;
67 }
68 const IndexBssMappingEntry& entry = *it;
69 return entry.GetBssOffset(index_bits, index, slot_size);
70}
71
72} // namespace art