Guillaume Chatelet | 439d371 | 2018-02-01 10:03:09 +0100 | [diff] [blame] | 1 | // 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 Chatelet | dfdac6a | 2019-01-17 18:00:21 +0100 | [diff] [blame] | 15 | #include "internal/unix_features_aggregator.h" |
Guillaume Chatelet | 439d371 | 2018-02-01 10:03:09 +0100 | [diff] [blame] | 16 | #include "internal/string_view.h" |
| 17 | |
Arvid Gerstmann | d968991 | 2018-05-04 09:32:17 +0200 | [diff] [blame] | 18 | void CpuFeatures_SetFromFlags(const size_t configs_size, |
| 19 | const CapabilityConfig* configs, |
| 20 | const StringView flags_line, |
| 21 | void* const features) { |
Guillaume Chatelet | 439d371 | 2018-02-01 10:03:09 +0100 | [diff] [blame] | 22 | size_t i = 0; |
| 23 | for (; i < configs_size; ++i) { |
| 24 | const CapabilityConfig config = configs[i]; |
Arvid Gerstmann | d968991 | 2018-05-04 09:32:17 +0200 | [diff] [blame] | 25 | config.set_bit(features, CpuFeatures_StringView_HasWord( |
| 26 | flags_line, config.proc_cpuinfo_flag)); |
Guillaume Chatelet | 439d371 | 2018-02-01 10:03:09 +0100 | [diff] [blame] | 27 | } |
| 28 | } |
| 29 | |
| 30 | static bool IsSet(const uint32_t mask, const uint32_t value) { |
Guillaume Chatelet | 9917e84 | 2019-01-21 10:39:24 +0100 | [diff] [blame^] | 31 | if (mask == 0) return false; |
Guillaume Chatelet | 439d371 | 2018-02-01 10:03:09 +0100 | [diff] [blame] | 32 | return (value & mask) == mask; |
| 33 | } |
| 34 | |
| 35 | static bool IsHwCapsSet(const HardwareCapabilities hwcaps_mask, |
| 36 | const HardwareCapabilities hwcaps) { |
Guillaume Chatelet | 9917e84 | 2019-01-21 10:39:24 +0100 | [diff] [blame^] | 37 | return IsSet(hwcaps_mask.hwcaps, hwcaps.hwcaps) || |
Guillaume Chatelet | 439d371 | 2018-02-01 10:03:09 +0100 | [diff] [blame] | 38 | IsSet(hwcaps_mask.hwcaps2, hwcaps.hwcaps2); |
| 39 | } |
| 40 | |
Arvid Gerstmann | a1ffdcb | 2018-04-26 10:31:03 +0200 | [diff] [blame] | 41 | void CpuFeatures_OverrideFromHwCaps(const size_t configs_size, |
Arvid Gerstmann | d968991 | 2018-05-04 09:32:17 +0200 | [diff] [blame] | 42 | const CapabilityConfig* configs, |
| 43 | const HardwareCapabilities hwcaps, |
| 44 | void* const features) { |
Guillaume Chatelet | 439d371 | 2018-02-01 10:03:09 +0100 | [diff] [blame] | 45 | 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 | } |