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