• 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 #include "cpu_features_macros.h"
16 
17 #ifdef CPU_FEATURES_ARCH_X86
18 #if defined(CPU_FEATURES_OS_LINUX) || defined(CPU_FEATURES_OS_ANDROID)
19 
20 #include "impl_x86__base_implementation.inl"
21 
OverrideOsPreserves(OsPreserves * os_preserves)22 static void OverrideOsPreserves(OsPreserves* os_preserves) {
23   (void)os_preserves;
24   // No override
25 }
26 
27 #include "internal/filesystem.h"
28 #include "internal/stack_line_reader.h"
29 #include "internal/string_view.h"
DetectFeaturesFromOs(X86Info * info,X86Features * features)30 static void DetectFeaturesFromOs(X86Info* info, X86Features* features) {
31   (void)info;
32   // Handling Linux platform through /proc/cpuinfo.
33   const int fd = CpuFeatures_OpenFile("/proc/cpuinfo");
34   if (fd >= 0) {
35     StackLineReader reader;
36     StackLineReader_Initialize(&reader, fd);
37     for (bool stop = false; !stop;) {
38       const LineResult result = StackLineReader_NextLine(&reader);
39       if (result.eof) stop = true;
40       const StringView line = result.line;
41       StringView key, value;
42       if (!CpuFeatures_StringView_GetAttributeKeyValue(line, &key, &value))
43         continue;
44       if (!CpuFeatures_StringView_IsEquals(key, str("flags"))) continue;
45       features->sse = CpuFeatures_StringView_HasWord(value, "sse", ' ');
46       features->sse2 = CpuFeatures_StringView_HasWord(value, "sse2", ' ');
47       features->sse3 = CpuFeatures_StringView_HasWord(value, "pni", ' ');
48       features->ssse3 = CpuFeatures_StringView_HasWord(value, "ssse3", ' ');
49       features->sse4_1 = CpuFeatures_StringView_HasWord(value, "sse4_1", ' ');
50       features->sse4_2 = CpuFeatures_StringView_HasWord(value, "sse4_2", ' ');
51       break;
52     }
53     CpuFeatures_CloseFile(fd);
54   }
55 }
56 
57 #endif  // defined(CPU_FEATURES_OS_LINUX) || defined(CPU_FEATURES_OS_ANDROID)
58 #endif  // CPU_FEATURES_ARCH_X86
59