/ externals / oaknut / tests / _feature_detect.cpp
_feature_detect.cpp
 1  // SPDX-FileCopyrightText: Copyright (c) 2022 merryhime <https://mary.rs>
 2  // SPDX-License-Identifier: MIT
 3  
 4  #include <cstdio>
 5  
 6  #include <catch2/catch_test_macros.hpp>
 7  
 8  #include "architecture.hpp"
 9  
10  #ifdef ON_ARM64
11  
12  #    include "oaknut/feature_detection/feature_detection.hpp"
13  #    include "oaknut/feature_detection/feature_detection_idregs.hpp"
14  
15  using namespace oaknut;
16  
17  TEST_CASE("Print CPU features (Default)")
18  {
19      CpuFeatures features = detect_features();
20  
21      std::fputs("CPU Features: ", stdout);
22  
23  #    define OAKNUT_CPU_FEATURE(name)        \
24          if (features.has(CpuFeature::name)) \
25              std::fputs(#name " ", stdout);
26  #    include "oaknut/impl/cpu_feature.inc.hpp"
27  #    undef OAKNUT_CPU_FEATURE
28  
29      std::fputs("\n", stdout);
30  }
31  
32  #    if OAKNUT_SUPPORTS_READING_ID_REGISTERS == 1
33  
34  TEST_CASE("Print CPU features (Using CPUID)")
35  {
36      std::optional<id::IdRegisters> id_regs = read_id_registers();
37      REQUIRE(!!id_regs);
38  
39      CpuFeatures features = detect_features_via_id_registers(*id_regs);
40  
41      std::fputs("CPU Features (CPUID method): ", stdout);
42  
43  #        define OAKNUT_CPU_FEATURE(name)        \
44              if (features.has(CpuFeature::name)) \
45                  std::fputs(#name " ", stdout);
46  #        include "oaknut/impl/cpu_feature.inc.hpp"
47  #        undef OAKNUT_CPU_FEATURE
48  
49      std::fputs("\n", stdout);
50  }
51  
52  #    elif OAKNUT_SUPPORTS_READING_ID_REGISTERS == 2
53  
54  TEST_CASE("Print CPU features (Using CPUID)")
55  {
56      const std::size_t core_count = get_core_count();
57      for (std::size_t core_index = 0; core_index < core_count; core_index++) {
58          std::optional<id::IdRegisters> id_regs = read_id_registers(core_index);
59          REQUIRE(!!id_regs);
60  
61          CpuFeatures features = detect_features_via_id_registers(*id_regs);
62  
63          std::printf("CPU Features (CPUID method - Core %zu): ", core_index);
64  
65  #        define OAKNUT_CPU_FEATURE(name)        \
66              if (features.has(CpuFeature::name)) \
67                  std::fputs(#name " ", stdout);
68  #        include "oaknut/impl/cpu_feature.inc.hpp"
69  #        undef OAKNUT_CPU_FEATURE
70  
71          std::fputs("\n", stdout);
72      }
73  }
74  
75  #    endif
76  
77  #endif