MRPT  2.0.4
cpu.h
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2020, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
10 #pragma once
11 
12 #include <mrpt/core/common.h>
13 #include <array>
14 #include <string>
15 
16 namespace mrpt::cpu
17 {
18 /** OS-portable set of CPU feature definitions, for usage in mrpt::cpu::supports
19  * \ingroup mrpt_core_grp
20  */
21 enum class feature : unsigned int
22 {
23  MMX = 0,
24  POPCNT,
25  SSE,
26  SSE2,
27  SSE3,
28  SSSE3,
29  SSE4_1,
30  SSE4_2,
31  AVX,
32  AVX2,
33  // ---- end of list ----
35 };
36 
37 namespace internal
38 {
39 /** Auxiliary class. Users should use mrpt::core::supports() instead.
40  * \ingroup mrpt_core_grp
41  */
43 {
44  public:
45  static CPU_analyzer& Instance() noexcept;
46 
47  std::array<
48  bool, static_cast<std::size_t>(mrpt::cpu::feature::FEATURE_COUNT)>
50 
51  inline bool& feat(mrpt::cpu::feature f) noexcept
52  {
53  return feat_detected[static_cast<std::size_t>(f)];
54  }
55  inline bool feat(mrpt::cpu::feature f) const noexcept
56  {
57  return feat_detected[static_cast<std::size_t>(f)];
58  }
59 
60  private:
61  // Ctor: runs all the checks and fills in the vector of features:
62  CPU_analyzer() noexcept
63  {
64  // Start with all falses:
65  feat_detected.fill(false);
66  detect_impl();
67  }
68  void detect_impl() noexcept;
69 };
70 } // namespace internal
71 
72 /** Returns true if the current CPU (and OS) supports the given CPU feature.
73  * \ingroup mrpt_core_grp
74  */
75 inline bool supports(feature f) noexcept
76 {
77  const auto& o = internal::CPU_analyzer::Instance();
78  return o.feat(f);
79 }
80 
81 /** Blindly enables/disables a CPU feature flag in the list
82  * of detected features to be reported in subsequent calls to
83  * mrpt::cpu::supports(). Could be used to disable a given CPU feature for
84  * benchmarking dynamically-dispatched functions.
85  *
86  * \note Enabling a feature that is not actually supported by the current CPU
87  * would probably lead to program crashes.
88  *
89  * \ingroup mrpt_core_grp
90  */
91 inline void overrideDetectedFeature(feature f, bool newValue) noexcept
92 {
94  o.feat(f) = newValue;
95 }
96 
97 /** Returns a string with detected features: "MMX:1 SSE2:0 etc."
98  * \ingroup mrpt_core_grp
99  */
100 std::string features_as_string() noexcept;
101 
102 } // namespace mrpt::cpu
bool feat(mrpt::cpu::feature f) const noexcept
Definition: cpu.h:55
void overrideDetectedFeature(feature f, bool newValue) noexcept
Blindly enables/disables a CPU feature flag in the list of detected features to be reported in subseq...
Definition: cpu.h:91
bool & feat(mrpt::cpu::feature f) noexcept
Definition: cpu.h:51
feature
OS-portable set of CPU feature definitions, for usage in mrpt::cpu::supports.
Definition: cpu.h:21
bool supports(feature f) noexcept
Returns true if the current CPU (and OS) supports the given CPU feature.
Definition: cpu.h:75
Auxiliary class.
Definition: cpu.h:42
std::array< bool, static_cast< std::size_t >mrpt::cpu::feature::FEATURE_COUNT)> feat_detected
Definition: cpu.h:49
Definition: cpu.h:16
std::string features_as_string() noexcept
Returns a string with detected features: "MMX:1 SSE2:0 etc.".
Definition: cpu.cpp:95
void detect_impl() noexcept
Definition: cpu.cpp:69
static CPU_analyzer & Instance() noexcept
Definition: cpu.cpp:22



Page generated by Doxygen 1.8.14 for MRPT 2.0.4 Git: 33de1d0ad Sat Jun 20 11:02:42 2020 +0200 at sáb jun 20 17:35:17 CEST 2020