Path: blob/master/platform/windows/cpu_feature_validation.c
10277 views
/**************************************************************************/1/* cpu_feature_validation.c */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include <windows.h>31#ifdef _MSC_VER32#include <intrin.h> // For builtin __cpuid.33#else34void __cpuid(int *r_cpuinfo, int p_info) {35// Note: Some compilers have a buggy `__cpuid` intrinsic, using inline assembly (based on LLVM-20 implementation) instead.36__asm__ __volatile__(37"xchgq %%rbx, %q1;"38"cpuid;"39"xchgq %%rbx, %q1;"40: "=a"(r_cpuinfo[0]), "=r"(r_cpuinfo[1]), "=c"(r_cpuinfo[2]), "=d"(r_cpuinfo[3])41: "0"(p_info));42}43#endif4445#ifndef PF_SSE4_2_INSTRUCTIONS_AVAILABLE46#define PF_SSE4_2_INSTRUCTIONS_AVAILABLE 3847#endif4849#ifdef WINDOWS_SUBSYSTEM_CONSOLE50extern int WINAPI mainCRTStartup();51#else52extern int WINAPI WinMainCRTStartup();53#endif5455#if defined(__GNUC__) || defined(__clang__)56extern int WINAPI ShimMainCRTStartup() __attribute__((used));57#endif5859extern int WINAPI ShimMainCRTStartup() {60BOOL win_sse42_supported = FALSE;61BOOL cpuid_sse42_supported = FALSE;6263int cpuinfo[4];64__cpuid(cpuinfo, 0x01);6566win_sse42_supported = IsProcessorFeaturePresent(PF_SSE4_2_INSTRUCTIONS_AVAILABLE);67cpuid_sse42_supported = cpuinfo[2] & (1 << 20);6869if (win_sse42_supported || cpuid_sse42_supported) {70#ifdef WINDOWS_SUBSYSTEM_CONSOLE71return mainCRTStartup();72#else73return WinMainCRTStartup();74#endif75} else {76MessageBoxW(NULL, L"A CPU with SSE4.2 instruction set support is required.", L"Godot Engine", MB_OK | MB_ICONEXCLAMATION | MB_TASKMODAL);77return -1;78}79}808182