xref: /aosp_15_r20/external/cpu_features/src/define_introspection.inl (revision eca53ba6d2e951e174b64682eaf56a36b8204c89)
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#ifndef INTROSPECTION_PREFIX
16#error "missing INTROSPECTION_PREFIX"
17#endif
18#ifndef INTROSPECTION_ENUM_PREFIX
19#error "missing INTROSPECTION_ENUM_PREFIX"
20#endif
21#ifndef INTROSPECTION_TABLE
22#error "missing INTROSPECTION_TABLE"
23#endif
24
25#include <stdbool.h>
26
27#define STRINGIZE_(s) #s
28#define STRINGIZE(s) STRINGIZE_(s)
29
30#define FEAT_TYPE_NAME__(X) X##Features
31#define FEAT_TYPE_NAME_(X) FEAT_TYPE_NAME__(X)
32#define FEAT_TYPE_NAME FEAT_TYPE_NAME_(INTROSPECTION_PREFIX)
33
34#define FEAT_ENUM_NAME__(X) X##FeaturesEnum
35#define FEAT_ENUM_NAME_(X) FEAT_ENUM_NAME__(X)
36#define FEAT_ENUM_NAME FEAT_ENUM_NAME_(INTROSPECTION_PREFIX)
37
38#define GET_FEAT_ENUM_VALUE__(X) Get##X##FeaturesEnumValue
39#define GET_FEAT_ENUM_VALUE_(X) GET_FEAT_ENUM_VALUE__(X)
40#define GET_FEAT_ENUM_VALUE GET_FEAT_ENUM_VALUE_(INTROSPECTION_PREFIX)
41
42#define GET_FEAT_ENUM_NAME__(X) Get##X##FeaturesEnumName
43#define GET_FEAT_ENUM_NAME_(X) GET_FEAT_ENUM_NAME__(X)
44#define GET_FEAT_ENUM_NAME GET_FEAT_ENUM_NAME_(INTROSPECTION_PREFIX)
45
46#define FEAT_ENUM_LAST__(X) X##_LAST_
47#define FEAT_ENUM_LAST_(X) FEAT_ENUM_LAST__(X)
48#define FEAT_ENUM_LAST FEAT_ENUM_LAST_(INTROSPECTION_ENUM_PREFIX)
49
50// Generate individual getters and setters.
51#define LINE(ENUM, NAME, A, B, C)                                \
52  static void set_##ENUM(FEAT_TYPE_NAME* features, bool value) { \
53    features->NAME = value;                                      \
54  }                                                              \
55  static int get_##ENUM(const FEAT_TYPE_NAME* features) {        \
56    return features->NAME;                                       \
57  }
58INTROSPECTION_TABLE
59#undef LINE
60
61// Generate getters table
62#define LINE(ENUM, NAME, A, B, C) [ENUM] = get_##ENUM,
63static int (*const kGetters[])(const FEAT_TYPE_NAME*) = {INTROSPECTION_TABLE};
64#undef LINE
65
66// Generate setters table
67#define LINE(ENUM, NAME, A, B, C) [ENUM] = set_##ENUM,
68static void (*const kSetters[])(FEAT_TYPE_NAME*, bool) = {INTROSPECTION_TABLE};
69#undef LINE
70
71// Implements the `GetXXXFeaturesEnumValue` API.
72int GET_FEAT_ENUM_VALUE(const FEAT_TYPE_NAME* features, FEAT_ENUM_NAME value) {
73  if (value >= FEAT_ENUM_LAST) return false;
74  return kGetters[value](features);
75}
76
77// Generate feature name table.
78#define LINE(ENUM, NAME, A, B, C) [ENUM] = STRINGIZE(NAME),
79static const char* kFeatureNames[] = {INTROSPECTION_TABLE};
80#undef LINE
81
82// Implements the `GetXXXFeaturesEnumName` API.
83const char* GET_FEAT_ENUM_NAME(FEAT_ENUM_NAME value) {
84  if (value >= FEAT_ENUM_LAST) return "unknown_feature";
85  return kFeatureNames[value];
86}
87