cpu_info.hpp 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. //
  2. // Copyright (c) 2017 Vinnie Falco (vinnie dot falco at gmail dot com)
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. // Official repository: https://github.com/boostorg/beast
  8. //
  9. #ifndef BOOST_BEAST_DETAIL_CPU_INFO_HPP
  10. #define BOOST_BEAST_DETAIL_CPU_INFO_HPP
  11. #include <boost/config.hpp>
  12. #ifndef BOOST_BEAST_NO_INTRINSICS
  13. # if defined(BOOST_MSVC) || ((defined(BOOST_GCC) || defined(BOOST_CLANG)) && defined(__SSE4_2__))
  14. # define BOOST_BEAST_NO_INTRINSICS 0
  15. # else
  16. # define BOOST_BEAST_NO_INTRINSICS 1
  17. # endif
  18. #endif
  19. #if ! BOOST_BEAST_NO_INTRINSICS
  20. #ifdef BOOST_MSVC
  21. #include <intrin.h> // __cpuid
  22. #else
  23. #include <cpuid.h> // __get_cpuid
  24. #endif
  25. namespace boost {
  26. namespace beast {
  27. namespace detail {
  28. /* Portions from Boost,
  29. Copyright Andrey Semashev 2007 - 2015.
  30. */
  31. template<class = void>
  32. void
  33. cpuid(
  34. std::uint32_t id,
  35. std::uint32_t& eax,
  36. std::uint32_t& ebx,
  37. std::uint32_t& ecx,
  38. std::uint32_t& edx)
  39. {
  40. #ifdef BOOST_MSVC
  41. int regs[4];
  42. __cpuid(regs, id);
  43. eax = regs[0];
  44. ebx = regs[1];
  45. ecx = regs[2];
  46. edx = regs[3];
  47. #else
  48. __get_cpuid(id, &eax, &ebx, &ecx, &edx);
  49. #endif
  50. }
  51. struct cpu_info
  52. {
  53. bool sse42 = false;
  54. cpu_info();
  55. };
  56. inline
  57. cpu_info::
  58. cpu_info()
  59. {
  60. constexpr std::uint32_t SSE42 = 1 << 20;
  61. std::uint32_t eax = 0;
  62. std::uint32_t ebx = 0;
  63. std::uint32_t ecx = 0;
  64. std::uint32_t edx = 0;
  65. cpuid(0, eax, ebx, ecx, edx);
  66. if(eax >= 1)
  67. {
  68. cpuid(1, eax, ebx, ecx, edx);
  69. sse42 = (ecx & SSE42) != 0;
  70. }
  71. }
  72. template<class = void>
  73. cpu_info const&
  74. get_cpu_info()
  75. {
  76. static cpu_info const ci;
  77. return ci;
  78. }
  79. } // detail
  80. } // beast
  81. } // boost
  82. #endif
  83. #endif