blob: dc1485c476429e917ee8c77bbc18ad37f5916261 [file] [log] [blame]
Guillaume Chatelet9a8f04b2020-10-12 11:50:35 +02001// 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)
Guillaume Chateletcdab59a2020-10-13 13:05:04 +020018// - DEFINE_TABLE_FEATURES, the list of FEATURE macros to be inserted.
Guillaume Chatelet9a8f04b2020-10-12 11:50:35 +020019
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,
Guillaume Chateletcdab59a2020-10-13 13:05:04 +020036static const char* kCpuInfoFlags[] = {DEFINE_TABLE_FEATURES};
Guillaume Chatelet9a8f04b2020-10-12 11:50:35 +020037#undef FEATURE
38
39#ifndef DEFINE_TABLE_DONT_GENERATE_HWCAPS
40#define FEATURE(ENUM, NAME, CPUINFO_FLAG, HWCAP, HWCAP2) \
41 [ENUM] = (HardwareCapabilities){HWCAP, HWCAP2},
42static const HardwareCapabilities kHardwareCapabilities[] = {
Guillaume Chateletcdab59a2020-10-13 13:05:04 +020043 DEFINE_TABLE_FEATURES};
Guillaume Chatelet9a8f04b2020-10-12 11:50:35 +020044#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 }
Guillaume Chateletcdab59a2020-10-13 13:05:04 +020054DEFINE_TABLE_FEATURES
Guillaume Chatelet9a8f04b2020-10-12 11:50:35 +020055#undef FEATURE
56
57#define FEATURE(ENUM, NAME, CPUINFO_FLAG, HWCAP, HWCAP2) [ENUM] = set_##ENUM,
Guillaume Chateletcdab59a2020-10-13 13:05:04 +020058static void (*const kSetters[])(DEFINE_TABLE_FEATURE_TYPE*,
59 bool) = {DEFINE_TABLE_FEATURES};
Guillaume Chatelet9a8f04b2020-10-12 11:50:35 +020060#undef FEATURE
61
62#define FEATURE(ENUM, NAME, CPUINFO_FLAG, HWCAP, HWCAP2) [ENUM] = get_##ENUM,
63static int (*const kGetters[])(const DEFINE_TABLE_FEATURE_TYPE*) = {
Guillaume Chateletcdab59a2020-10-13 13:05:04 +020064 DEFINE_TABLE_FEATURES};
Guillaume Chatelet9a8f04b2020-10-12 11:50:35 +020065#undef FEATURE
66
67#endif // SRC_DEFINE_TABLES_H_