| 1 | // basisu_kernels_sse.cpp |
| 2 | // Copyright (C) 2019-2024 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 | |
| 17 | #if BASISU_SUPPORT_SSE |
| 18 | |
| 19 | #define CPPSPMD_SSE2 0 |
| 20 | |
| 21 | #ifdef _MSC_VER |
| 22 | #include <intrin.h> |
| 23 | #endif |
| 24 | |
| 25 | #include "cppspmd_sse.h" |
| 26 | |
| 27 | #include "cppspmd_type_aliases.h" |
| 28 | |
| 29 | using namespace basisu; |
| 30 | |
| 31 | #include "basisu_kernels_declares.h" |
| 32 | #include "basisu_kernels_imp.h" |
| 33 | |
| 34 | namespace basisu |
| 35 | { |
| 36 | |
| 37 | struct cpu_info |
| 38 | { |
| 39 | cpu_info() { memset(this, 0, sizeof(*this)); } |
| 40 | |
| 41 | bool m_has_fpu; |
| 42 | bool m_has_mmx; |
| 43 | bool m_has_sse; |
| 44 | bool m_has_sse2; |
| 45 | bool m_has_sse3; |
| 46 | bool m_has_ssse3; |
| 47 | bool m_has_sse41; |
| 48 | bool m_has_sse42; |
| 49 | bool m_has_avx; |
| 50 | bool m_has_avx2; |
| 51 | bool m_has_pclmulqdq; |
| 52 | }; |
| 53 | |
| 54 | static void extract_x86_flags(cpu_info &info, uint32_t ecx, uint32_t edx) |
| 55 | { |
| 56 | info.m_has_fpu = (edx & (1 << 0)) != 0; |
| 57 | info.m_has_mmx = (edx & (1 << 23)) != 0; |
| 58 | info.m_has_sse = (edx & (1 << 25)) != 0; |
| 59 | info.m_has_sse2 = (edx & (1 << 26)) != 0; |
| 60 | info.m_has_sse3 = (ecx & (1 << 0)) != 0; |
| 61 | info.m_has_ssse3 = (ecx & (1 << 9)) != 0; |
| 62 | info.m_has_sse41 = (ecx & (1 << 19)) != 0; |
| 63 | info.m_has_sse42 = (ecx & (1 << 20)) != 0; |
| 64 | info.m_has_pclmulqdq = (ecx & (1 << 1)) != 0; |
| 65 | info.m_has_avx = (ecx & (1 << 28)) != 0; |
| 66 | } |
| 67 | |
| 68 | static void extract_x86_extended_flags(cpu_info &info, uint32_t ebx) |
| 69 | { |
| 70 | info.m_has_avx2 = (ebx & (1 << 5)) != 0; |
| 71 | } |
| 72 | |
| 73 | #ifndef _MSC_VER |
| 74 | static void do_cpuid(uint32_t eax, uint32_t ecx, uint32_t* regs) |
| 75 | { |
| 76 | uint32_t ebx = 0, edx = 0; |
| 77 | |
| 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 | |
| 87 | regs[0] = eax; regs[1] = ebx; regs[2] = ecx; regs[3] = edx; |
| 88 | } |
| 89 | #endif |
| 90 | |
| 91 | static void get_cpuinfo(cpu_info &info) |
| 92 | { |
| 93 | int regs[4]; |
| 94 | |
| 95 | #ifdef _MSC_VER |
| 96 | __cpuid(regs, 0); |
| 97 | #else |
| 98 | do_cpuid(0, 0, (uint32_t *)regs); |
| 99 | #endif |
| 100 | |
| 101 | const uint32_t max_eax = regs[0]; |
| 102 | |
| 103 | if (max_eax >= 1U) |
| 104 | { |
| 105 | #ifdef _MSC_VER |
| 106 | __cpuid(regs, 1); |
| 107 | #else |
| 108 | do_cpuid(1, 0, (uint32_t*)regs); |
| 109 | #endif |
| 110 | extract_x86_flags(info, regs[2], regs[3]); |
| 111 | } |
| 112 | |
| 113 | if (max_eax >= 7U) |
| 114 | { |
| 115 | #ifdef _MSC_VER |
| 116 | __cpuidex(regs, 7, 0); |
| 117 | #else |
| 118 | do_cpuid(7, 0, (uint32_t*)regs); |
| 119 | #endif |
| 120 | |
| 121 | extract_x86_extended_flags(info, regs[1]); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | void detect_sse41() |
| 126 | { |
| 127 | cpu_info info; |
| 128 | get_cpuinfo(info); |
| 129 | |
| 130 | // Check for everything from SSE to SSE 4.1 |
| 131 | g_cpu_supports_sse41 = info.m_has_sse && info.m_has_sse2 && info.m_has_sse3 && info.m_has_ssse3 && info.m_has_sse41; |
| 132 | } |
| 133 | |
| 134 | } // namespace basisu |
| 135 | #else // #if BASISU_SUPPORT_SSE |
| 136 | namespace basisu |
| 137 | { |
| 138 | |
| 139 | void detect_sse41() |
| 140 | { |
| 141 | } |
| 142 | |
| 143 | } // namespace basisu |
| 144 | #endif // #if BASISU_SUPPORT_SSE |
| 145 |