Blender V5.3
cpu_check.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2024 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8#include <string>
9
10#if defined(WIN32)
11# include <Windows.h>
12# include <intrin.h>
13#endif
14
15namespace blender {
16
17/* The code below is duplicated from system.c from bf_blenlib. This is on purpose, since bf_blenlib
18 * may be build with CPU flags that are not available on the current cpu so we can't link it. */
19
20#if !defined(_WIN32)
21static void __cpuid(
22 /* Cannot be const, because it is modified below.
23 * NOLINTNEXTLINE: readability-non-const-parameter. */
24 int data[4],
25 int selector)
26{
27# if defined(__x86_64__)
28 asm("cpuid" : "=a"(data[0]), "=b"(data[1]), "=c"(data[2]), "=d"(data[3]) : "a"(selector));
29# else
30 (void)selector;
31 data[0] = data[1] = data[2] = data[3] = 0;
32# endif
33}
34#endif
35
37{
38 int result[4], num;
39 __cpuid(result, 0);
40 num = result[0];
41
42 if (num >= 1) {
43 __cpuid(result, 0x00000001);
44 return (result[2] & (int(1) << 20)) != 0;
45 }
46 return 0;
47}
48
49static const char *cpu_brand_string()
50{
51 static char buf[49] = {0};
52 int result[4] = {0};
53 __cpuid(result, 0x80000000);
54 if (result[0] >= int(0x80000004)) {
55 __cpuid(reinterpret_cast<int *>(buf + 0), 0x80000002);
56 __cpuid(reinterpret_cast<int *>(buf + 16), 0x80000003);
57 __cpuid(reinterpret_cast<int *>(buf + 32), 0x80000004);
58 const char *buf_ptr = buf;
59 /* Trim any leading spaces. */
60 while (*buf_ptr == ' ') {
61 buf_ptr++;
62 }
63 return buf_ptr;
64 }
65 return nullptr;
66}
67
68#ifdef _MSC_VER
69extern "C" __declspec(dllexport) void cpu_check_win32()
70{
71# ifdef _M_X64
72 if (!cpu_supports_sse42()) {
73 std::string error_title = "Unsupported CPU - " + std::string(cpu_brand_string());
74 MessageBoxA(NULL,
75 "Blender requires a CPU with SSE42 support.",
76 error_title.c_str(),
77 MB_OK | MB_ICONERROR);
78 exit(-1);
79 }
80# endif
81}
82
83extern "C" BOOL WINAPI DllMain(HINSTANCE /*hinstDLL*/, DWORD fdwReason, LPVOID /*lpvReserved*/)
84{
85 switch (fdwReason) {
86 case DLL_PROCESS_ATTACH:
87 cpu_check_win32();
88 break;
89 }
90 return TRUE;
91}
92#else
93# include <cstdio>
94# include <cstdlib>
95
96static __attribute__((constructor)) void cpu_check()
97{
98# ifdef __x86_64
99 if (!cpu_supports_sse42()) {
100 printf("Unsupported CPU - %s\nBlender requires a CPU with SSE42 support.", cpu_brand_string());
101 exit(-1);
102 }
103 return;
104# endif
105}
106#endif
107
108} // namespace blender
__declspec(dllexport) DWORD NvOptimusEnablement=0x00000001
struct AtomicSpinLock __attribute__((aligned(32))) AtomicSpinLock
BMesh Iterators.
static int cpu_supports_sse42()
Definition cpu_check.cc:36
BMesh const char void * data
ATTR_WARN_UNUSED_RESULT const size_t num
static const char * cpu_brand_string()
Definition cpu_check.cc:49
static void __cpuid(int data[4], int selector)
Definition system.cc:122