basisu_kernels_sse.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. // basisu_kernels_sse.cpp
  2. // Copyright (C) 2019-2021 Binomial LLC. All Rights Reserved.
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. #include "basisu_enc.h"
  16. #if BASISU_SUPPORT_SSE
  17. #define CPPSPMD_SSE2 0
  18. #ifdef _MSC_VER
  19. #include <intrin.h>
  20. #endif
  21. #if !defined(_MSC_VER)
  22. #if __AVX__ || __AVX2__ || __AVX512F__
  23. #error Please check your compiler options
  24. #endif
  25. #if CPPSPMD_SSE2
  26. #if __SSE4_1__ || __SSE3__ || __SSE4_2__ || __SSSE3__
  27. #error SSE4.1/SSE3/SSE4.2/SSSE3 cannot be enabled to use this file
  28. #endif
  29. #else
  30. #if !__SSE4_1__ || !__SSE3__ || !__SSSE3__
  31. #error Please check your compiler options
  32. #endif
  33. #endif
  34. #endif
  35. #include "cppspmd_sse.h"
  36. #include "cppspmd_type_aliases.h"
  37. using namespace basisu;
  38. #include "basisu_kernels_declares.h"
  39. #include "basisu_kernels_imp.h"
  40. namespace basisu
  41. {
  42. struct cpu_info
  43. {
  44. cpu_info() { memset(this, 0, sizeof(*this)); }
  45. bool m_has_fpu;
  46. bool m_has_mmx;
  47. bool m_has_sse;
  48. bool m_has_sse2;
  49. bool m_has_sse3;
  50. bool m_has_ssse3;
  51. bool m_has_sse41;
  52. bool m_has_sse42;
  53. bool m_has_avx;
  54. bool m_has_avx2;
  55. bool m_has_pclmulqdq;
  56. };
  57. static void extract_x86_flags(cpu_info &info, uint32_t ecx, uint32_t edx)
  58. {
  59. info.m_has_fpu = (edx & (1 << 0)) != 0;
  60. info.m_has_mmx = (edx & (1 << 23)) != 0;
  61. info.m_has_sse = (edx & (1 << 25)) != 0;
  62. info.m_has_sse2 = (edx & (1 << 26)) != 0;
  63. info.m_has_sse3 = (ecx & (1 << 0)) != 0;
  64. info.m_has_ssse3 = (ecx & (1 << 9)) != 0;
  65. info.m_has_sse41 = (ecx & (1 << 19)) != 0;
  66. info.m_has_sse42 = (ecx & (1 << 20)) != 0;
  67. info.m_has_pclmulqdq = (ecx & (1 << 1)) != 0;
  68. info.m_has_avx = (ecx & (1 << 28)) != 0;
  69. }
  70. static void extract_x86_extended_flags(cpu_info &info, uint32_t ebx)
  71. {
  72. info.m_has_avx2 = (ebx & (1 << 5)) != 0;
  73. }
  74. #ifndef _MSC_VER
  75. static void do_cpuid(uint32_t eax, uint32_t ecx, uint32_t* regs)
  76. {
  77. uint32_t ebx = 0, edx = 0;
  78. #if defined(__PIC__) && defined(__i386__)
  79. __asm__("movl %%ebx, %%edi;"
  80. "cpuid;"
  81. "xchgl %%ebx, %%edi;"
  82. : "=D"(ebx), "+a"(eax), "+c"(ecx), "=d"(edx));
  83. #else
  84. __asm__("cpuid;" : "+b"(ebx), "+a"(eax), "+c"(ecx), "=d"(edx));
  85. #endif
  86. regs[0] = eax; regs[1] = ebx; regs[2] = ecx; regs[3] = edx;
  87. }
  88. #endif
  89. static void get_cpuinfo(cpu_info &info)
  90. {
  91. int regs[4];
  92. #ifdef _MSC_VER
  93. __cpuid(regs, 0);
  94. #else
  95. do_cpuid(0, 0, (uint32_t *)regs);
  96. #endif
  97. const uint32_t max_eax = regs[0];
  98. if (max_eax >= 1U)
  99. {
  100. #ifdef _MSC_VER
  101. __cpuid(regs, 1);
  102. #else
  103. do_cpuid(1, 0, (uint32_t*)regs);
  104. #endif
  105. extract_x86_flags(info, regs[2], regs[3]);
  106. }
  107. if (max_eax >= 7U)
  108. {
  109. #ifdef _MSC_VER
  110. __cpuidex(regs, 7, 0);
  111. #else
  112. do_cpuid(7, 0, (uint32_t*)regs);
  113. #endif
  114. extract_x86_extended_flags(info, regs[1]);
  115. }
  116. }
  117. void detect_sse41()
  118. {
  119. cpu_info info;
  120. get_cpuinfo(info);
  121. // Check for everything from SSE to SSE 4.1
  122. g_cpu_supports_sse41 = info.m_has_sse && info.m_has_sse2 && info.m_has_sse3 && info.m_has_ssse3 && info.m_has_sse41;
  123. }
  124. } // namespace basisu
  125. #else // #if BASISU_SUPPORT_SSE
  126. namespace basisu
  127. {
  128. void detect_sse41()
  129. {
  130. }
  131. } // namespace basisu
  132. #endif // #if BASISU_SUPPORT_SSE