cpu_feature_validation.c 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. #include <windows.h>
  31. #ifdef _MSC_VER
  32. #include <intrin.h> // For builtin __cpuid.
  33. #else
  34. void __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. #endif
  44. #ifndef PF_SSE4_2_INSTRUCTIONS_AVAILABLE
  45. #define PF_SSE4_2_INSTRUCTIONS_AVAILABLE 38
  46. #endif
  47. #ifdef WINDOWS_SUBSYSTEM_CONSOLE
  48. extern int WINAPI mainCRTStartup();
  49. #else
  50. extern int WINAPI WinMainCRTStartup();
  51. #endif
  52. #if defined(__GNUC__) || defined(__clang__)
  53. extern int WINAPI ShimMainCRTStartup() __attribute__((used));
  54. #endif
  55. extern int WINAPI ShimMainCRTStartup() {
  56. BOOL win_sse42_supported = FALSE;
  57. BOOL cpuid_sse42_supported = FALSE;
  58. int cpuinfo[4];
  59. __cpuid(cpuinfo, 0x01);
  60. win_sse42_supported = IsProcessorFeaturePresent(PF_SSE4_2_INSTRUCTIONS_AVAILABLE);
  61. cpuid_sse42_supported = cpuinfo[2] & (1 << 20);
  62. if (win_sse42_supported || cpuid_sse42_supported) {
  63. #ifdef WINDOWS_SUBSYSTEM_CONSOLE
  64. return mainCRTStartup();
  65. #else
  66. return WinMainCRTStartup();
  67. #endif
  68. } else {
  69. MessageBoxW(NULL, L"A CPU with SSE4.2 instruction set support is required.", L"Godot Engine", MB_OK | MB_ICONEXCLAMATION | MB_TASKMODAL);
  70. return -1;
  71. }
  72. }