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