Path: blob/master/thirdparty/basis_universal/encoder/basisu_kernels_sse.cpp
9903 views
// basisu_kernels_sse.cpp1// Copyright (C) 2019-2024 Binomial LLC. All Rights Reserved.2//3// Licensed under the Apache License, Version 2.0 (the "License");4// you may not use this file except in compliance with the License.5// You may obtain a copy of the License at6//7// http://www.apache.org/licenses/LICENSE-2.08//9// Unless required by applicable law or agreed to in writing, software10// distributed under the License is distributed on an "AS IS" BASIS,11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.12// See the License for the specific language governing permissions and13// limitations under the License.14#include "basisu_enc.h"1516#if BASISU_SUPPORT_SSE1718#define CPPSPMD_SSE2 01920#ifdef _MSC_VER21#include <intrin.h>22#endif2324#include "cppspmd_sse.h"2526#include "cppspmd_type_aliases.h"2728using namespace basisu;2930#include "basisu_kernels_declares.h"31#include "basisu_kernels_imp.h"3233namespace basisu34{3536struct cpu_info37{38cpu_info() { memset(this, 0, sizeof(*this)); }3940bool m_has_fpu;41bool m_has_mmx;42bool m_has_sse;43bool m_has_sse2;44bool m_has_sse3;45bool m_has_ssse3;46bool m_has_sse41;47bool m_has_sse42;48bool m_has_avx;49bool m_has_avx2;50bool m_has_pclmulqdq;51};5253static void extract_x86_flags(cpu_info &info, uint32_t ecx, uint32_t edx)54{55info.m_has_fpu = (edx & (1 << 0)) != 0;56info.m_has_mmx = (edx & (1 << 23)) != 0;57info.m_has_sse = (edx & (1 << 25)) != 0;58info.m_has_sse2 = (edx & (1 << 26)) != 0;59info.m_has_sse3 = (ecx & (1 << 0)) != 0;60info.m_has_ssse3 = (ecx & (1 << 9)) != 0;61info.m_has_sse41 = (ecx & (1 << 19)) != 0;62info.m_has_sse42 = (ecx & (1 << 20)) != 0;63info.m_has_pclmulqdq = (ecx & (1 << 1)) != 0;64info.m_has_avx = (ecx & (1 << 28)) != 0;65}6667static void extract_x86_extended_flags(cpu_info &info, uint32_t ebx)68{69info.m_has_avx2 = (ebx & (1 << 5)) != 0;70}7172#ifndef _MSC_VER73static void do_cpuid(uint32_t eax, uint32_t ecx, uint32_t* regs)74{75uint32_t ebx = 0, edx = 0;7677#if defined(__PIC__) && defined(__i386__)78__asm__("movl %%ebx, %%edi;"79"cpuid;"80"xchgl %%ebx, %%edi;"81: "=D"(ebx), "+a"(eax), "+c"(ecx), "=d"(edx));82#else83__asm__("cpuid;" : "+b"(ebx), "+a"(eax), "+c"(ecx), "=d"(edx));84#endif8586regs[0] = eax; regs[1] = ebx; regs[2] = ecx; regs[3] = edx;87}88#endif8990static void get_cpuinfo(cpu_info &info)91{92int regs[4];9394#ifdef _MSC_VER95__cpuid(regs, 0);96#else97do_cpuid(0, 0, (uint32_t *)regs);98#endif99100const uint32_t max_eax = regs[0];101102if (max_eax >= 1U)103{104#ifdef _MSC_VER105__cpuid(regs, 1);106#else107do_cpuid(1, 0, (uint32_t*)regs);108#endif109extract_x86_flags(info, regs[2], regs[3]);110}111112if (max_eax >= 7U)113{114#ifdef _MSC_VER115__cpuidex(regs, 7, 0);116#else117do_cpuid(7, 0, (uint32_t*)regs);118#endif119120extract_x86_extended_flags(info, regs[1]);121}122}123124void detect_sse41()125{126cpu_info info;127get_cpuinfo(info);128129// Check for everything from SSE to SSE 4.1130g_cpu_supports_sse41 = info.m_has_sse && info.m_has_sse2 && info.m_has_sse3 && info.m_has_ssse3 && info.m_has_sse41;131}132133} // namespace basisu134#else // #if BASISU_SUPPORT_SSE135namespace basisu136{137138void detect_sse41()139{140}141142} // namespace basisu143#endif // #if BASISU_SUPPORT_SSE144145146147