blob: 1b43a362ac2e645f9bc31e21106d0b68cb04e53f [file] [log] [blame]
Guillaume Chatelet439d3712018-02-01 10:03:09 +01001// Copyright 2017 Google Inc.
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
Guillaume Chateletdfdac6a2019-01-17 18:00:21 +010015#include "internal/unix_features_aggregator.h"
Guillaume Chatelet439d3712018-02-01 10:03:09 +010016#include "internal/string_view.h"
17
Arvid Gerstmannd9689912018-05-04 09:32:17 +020018void CpuFeatures_SetFromFlags(const size_t configs_size,
19 const CapabilityConfig* configs,
20 const StringView flags_line,
21 void* const features) {
Guillaume Chatelet439d3712018-02-01 10:03:09 +010022 size_t i = 0;
23 for (; i < configs_size; ++i) {
24 const CapabilityConfig config = configs[i];
Arvid Gerstmannd9689912018-05-04 09:32:17 +020025 config.set_bit(features, CpuFeatures_StringView_HasWord(
26 flags_line, config.proc_cpuinfo_flag));
Guillaume Chatelet439d3712018-02-01 10:03:09 +010027 }
28}
29
30static bool IsSet(const uint32_t mask, const uint32_t value) {
Guillaume Chatelet9917e842019-01-21 10:39:24 +010031 if (mask == 0) return false;
Guillaume Chatelet439d3712018-02-01 10:03:09 +010032 return (value & mask) == mask;
33}
34
35static bool IsHwCapsSet(const HardwareCapabilities hwcaps_mask,
36 const HardwareCapabilities hwcaps) {
Guillaume Chatelet9917e842019-01-21 10:39:24 +010037 return IsSet(hwcaps_mask.hwcaps, hwcaps.hwcaps) ||
Guillaume Chatelet439d3712018-02-01 10:03:09 +010038 IsSet(hwcaps_mask.hwcaps2, hwcaps.hwcaps2);
39}
40
Arvid Gerstmanna1ffdcb2018-04-26 10:31:03 +020041void CpuFeatures_OverrideFromHwCaps(const size_t configs_size,
Arvid Gerstmannd9689912018-05-04 09:32:17 +020042 const CapabilityConfig* configs,
43 const HardwareCapabilities hwcaps,
44 void* const features) {
Guillaume Chatelet439d3712018-02-01 10:03:09 +010045 size_t i = 0;
46 for (; i < configs_size; ++i) {
47 const CapabilityConfig* config = &configs[i];
48 if (IsHwCapsSet(config->hwcaps_mask, hwcaps)) {
49 config->set_bit(features, true);
50 }
51 }
52}