astcenc_platform_isa_detection.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. // SPDX-License-Identifier: Apache-2.0
  2. // ----------------------------------------------------------------------------
  3. // Copyright 2020-2022 Arm Limited
  4. //
  5. // Licensed under the Apache License, Version 2.0 (the "License"); you may not
  6. // use this file except in compliance with the License. You may obtain a copy
  7. // of the License at:
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing, software
  12. // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  13. // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  14. // License for the specific language governing permissions and limitations
  15. // under the License.
  16. // ----------------------------------------------------------------------------
  17. /**
  18. * @brief Platform-specific function implementations.
  19. *
  20. * This module contains functions for querying the host extended ISA support.
  21. */
  22. // Include before the defines below to pick up any auto-setup based on compiler
  23. // built-in config, if not being set explicitly by the build system
  24. #include "astcenc_internal.h"
  25. #if (ASTCENC_SSE > 0) || (ASTCENC_AVX > 0) || \
  26. (ASTCENC_POPCNT > 0) || (ASTCENC_F16C > 0)
  27. static bool g_init { false };
  28. /** Does this CPU support SSE 4.1? Set to -1 if not yet initialized. */
  29. static bool g_cpu_has_sse41 { false };
  30. /** Does this CPU support AVX2? Set to -1 if not yet initialized. */
  31. static bool g_cpu_has_avx2 { false };
  32. /** Does this CPU support POPCNT? Set to -1 if not yet initialized. */
  33. static bool g_cpu_has_popcnt { false };
  34. /** Does this CPU support F16C? Set to -1 if not yet initialized. */
  35. static bool g_cpu_has_f16c { false };
  36. /* ============================================================================
  37. Platform code for Visual Studio
  38. ============================================================================ */
  39. #if !defined(__clang__) && defined(_MSC_VER)
  40. #define WIN32_LEAN_AND_MEAN
  41. #include <windows.h>
  42. #include <intrin.h>
  43. /**
  44. * @brief Detect platform CPU ISA support and update global trackers.
  45. */
  46. static void detect_cpu_isa()
  47. {
  48. int data[4];
  49. __cpuid(data, 0);
  50. int num_id = data[0];
  51. if (num_id >= 1)
  52. {
  53. __cpuidex(data, 1, 0);
  54. // SSE41 = Bank 1, ECX, bit 19
  55. g_cpu_has_sse41 = data[2] & (1 << 19) ? true : false;
  56. // POPCNT = Bank 1, ECX, bit 23
  57. g_cpu_has_popcnt = data[2] & (1 << 23) ? true : false;
  58. // F16C = Bank 1, ECX, bit 29
  59. g_cpu_has_f16c = data[2] & (1 << 29) ? true : false;
  60. }
  61. if (num_id >= 7)
  62. {
  63. __cpuidex(data, 7, 0);
  64. // AVX2 = Bank 7, EBX, bit 5
  65. g_cpu_has_avx2 = data[1] & (1 << 5) ? true : false;
  66. }
  67. // Ensure state bits are updated before init flag is updated
  68. MemoryBarrier();
  69. g_init = true;
  70. }
  71. /* ============================================================================
  72. Platform code for GCC and Clang
  73. ============================================================================ */
  74. #else
  75. #include <cpuid.h>
  76. /**
  77. * @brief Detect platform CPU ISA support and update global trackers.
  78. */
  79. static void detect_cpu_isa()
  80. {
  81. unsigned int data[4];
  82. if (__get_cpuid_count(1, 0, &data[0], &data[1], &data[2], &data[3]))
  83. {
  84. // SSE41 = Bank 1, ECX, bit 19
  85. g_cpu_has_sse41 = data[2] & (1 << 19) ? true : false;
  86. // POPCNT = Bank 1, ECX, bit 23
  87. g_cpu_has_popcnt = data[2] & (1 << 23) ? true : false;
  88. // F16C = Bank 1, ECX, bit 29
  89. g_cpu_has_f16c = data[2] & (1 << 29) ? true : false;
  90. }
  91. g_cpu_has_avx2 = 0;
  92. if (__get_cpuid_count(7, 0, &data[0], &data[1], &data[2], &data[3]))
  93. {
  94. // AVX2 = Bank 7, EBX, bit 5
  95. g_cpu_has_avx2 = data[1] & (1 << 5) ? true : false;
  96. }
  97. // Ensure state bits are updated before init flag is updated
  98. __sync_synchronize();
  99. g_init = true;
  100. }
  101. #endif
  102. /* See header for documentation. */
  103. bool cpu_supports_popcnt()
  104. {
  105. if (!g_init)
  106. {
  107. detect_cpu_isa();
  108. }
  109. return g_cpu_has_popcnt;
  110. }
  111. /* See header for documentation. */
  112. bool cpu_supports_f16c()
  113. {
  114. if (!g_init)
  115. {
  116. detect_cpu_isa();
  117. }
  118. return g_cpu_has_f16c;
  119. }
  120. /* See header for documentation. */
  121. bool cpu_supports_sse41()
  122. {
  123. if (!g_init)
  124. {
  125. detect_cpu_isa();
  126. }
  127. return g_cpu_has_sse41;
  128. }
  129. /* See header for documentation. */
  130. bool cpu_supports_avx2()
  131. {
  132. if (!g_init)
  133. {
  134. detect_cpu_isa();
  135. }
  136. return g_cpu_has_avx2;
  137. }
  138. #endif