1 // Copyright 2020 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 // The following preprocessor constants must be defined before including this
16 // file:
17 //  - DEFINE_TABLE_FEATURE_TYPE, the underlying type (e.g. X86Features)
18 //  - DEFINE_TABLE_FEATURES, the list of FEATURE macros to be inserted.
19 
20 // This file is to be included once per `cpuinfo_XXX.c` in order to construct
21 // feature getters and setters functions as well as several enum indexed tables
22 // from the db file.
23 // - `kGetters` a table of getters function pointers from feature enum to
24 // retrieve a feature,
25 // - `kSetters` a table of setters function pointers from feature enum to set a
26 // feature,
27 // - `kCpuInfoFlags` a table of strings from feature enum to /proc/cpuinfo
28 // flags,
29 // - `kHardwareCapabilities` a table of HardwareCapabilities structs indexed by
30 // their feature enum.
31 
32 #ifndef SRC_DEFINE_TABLES_H_
33 #define SRC_DEFINE_TABLES_H_
34 
35 #define FEATURE(ENUM, NAME, CPUINFO_FLAG, HWCAP, HWCAP2) [ENUM] = CPUINFO_FLAG,
36 static const char* kCpuInfoFlags[] = {DEFINE_TABLE_FEATURES};
37 #undef FEATURE
38 
39 #ifndef DEFINE_TABLE_DONT_GENERATE_HWCAPS
40 #define FEATURE(ENUM, NAME, CPUINFO_FLAG, HWCAP, HWCAP2) \
41   [ENUM] = (HardwareCapabilities){HWCAP, HWCAP2},
42 static const HardwareCapabilities kHardwareCapabilities[] = {
43     DEFINE_TABLE_FEATURES};
44 #undef FEATURE
45 #endif  // DEFINE_TABLE_DONT_GENERATE_HWCAPS
46 
47 #define FEATURE(ENUM, NAME, CPUINFO_FLAG, HWCAP, HWCAP2)                    \
48   static void set_##ENUM(DEFINE_TABLE_FEATURE_TYPE* features, bool value) { \
49     features->NAME = value;                                                 \
50   }                                                                         \
51   static int get_##ENUM(const DEFINE_TABLE_FEATURE_TYPE* features) {        \
52     return features->NAME;                                                  \
53   }
54 DEFINE_TABLE_FEATURES
55 #undef FEATURE
56 
57 #define FEATURE(ENUM, NAME, CPUINFO_FLAG, HWCAP, HWCAP2) [ENUM] = set_##ENUM,
58 static void (*const kSetters[])(DEFINE_TABLE_FEATURE_TYPE*,
59                                 bool) = {DEFINE_TABLE_FEATURES};
60 #undef FEATURE
61 
62 #define FEATURE(ENUM, NAME, CPUINFO_FLAG, HWCAP, HWCAP2) [ENUM] = get_##ENUM,
63 static int (*const kGetters[])(const DEFINE_TABLE_FEATURE_TYPE*) = {
64     DEFINE_TABLE_FEATURES};
65 #undef FEATURE
66 
67 #endif  // SRC_DEFINE_TABLES_H_
68