9# if (!defined(FREE_WINDOWS))
13#elif defined(__APPLE__)
14# include <sys/ioctl.h>
15# include <sys/sysctl.h>
16# include <sys/types.h>
19# include <sys/ioctl.h>
30 CONSOLE_SCREEN_BUFFER_INFO csbi;
31 if (GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi)) {
32 columns = csbi.dwSize.X;
36 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &
w) == 0) {
41 return (columns > 0) ? columns : 80;
45#if (!defined(_WIN32) || defined(FREE_WINDOWS)) && (defined(__x86_64__) || defined(__i386__))
48# if defined(__x86_64__)
49 asm(
"cpuid" :
"=a"(
data[0]),
"=b"(
data[1]),
"=c"(
data[2]),
"=d"(
data[3]) :
"a"(selector));
50# elif defined(__i386__)
51 asm(
"pushl %%ebx \n\t"
68 char modelname[512] =
"";
69 size_t bufferlen = 512;
70 if (sysctlbyname(
"machdep.cpu.brand_string", &modelname, &bufferlen,
nullptr, 0) == 0) {
73#elif (defined(WIN32) || defined(__x86_64__) || defined(__i386__)) && !defined(_M_ARM64)
81 __cpuid((
int *)(buf + 0), 0x80000002);
82 __cpuid((
int *)(buf + 16), 0x80000003);
83 __cpuid((
int *)(buf + 32), 0x80000004);
92#elif defined(_M_ARM64)
93 DWORD processorNameStringLength = 255;
94 char processorNameString[255];
95 if (RegGetValueA(HKEY_LOCAL_MACHINE,
96 "HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0",
97 "ProcessorNameString",
100 &processorNameString,
101 &processorNameStringLength) == ERROR_SUCCESS)
103 return processorNameString;
107 FILE *cpuinfo = fopen(
"/proc/cpuinfo",
"r");
108 if (cpuinfo !=
nullptr) {
109 char cpuinfo_buf[513] =
"";
110 fread(cpuinfo_buf,
sizeof(cpuinfo_buf) - 1, 1, cpuinfo);
113 char *modelname = strstr(cpuinfo_buf,
"model name");
114 if (modelname !=
nullptr) {
115 modelname = strchr(modelname,
':');
116 if (modelname !=
nullptr) {
118 char *modelname_end = strchr(modelname,
'\n');
119 if (modelname_end !=
nullptr) {
120 *modelname_end =
'\0';
127 return "Unknown CPU";
132 return (
sizeof(
void *) * 8);
135#if defined(__x86_64__) || defined(_M_X64) || defined(__i386__) || defined(_M_IX86)
137struct CPUCapabilities {
142static CPUCapabilities &system_cpu_capabilities()
144 static CPUCapabilities caps = {};
145 static bool caps_init =
false;
155 const bool sse = (
result[3] & ((int)1 << 25)) != 0;
156 const bool sse2 = (
result[3] & ((int)1 << 26)) != 0;
157 const bool sse3 = (
result[2] & ((int)1 << 0)) != 0;
159 const bool ssse3 = (
result[2] & ((int)1 << 9)) != 0;
160 const bool sse41 = (
result[2] & ((int)1 << 19)) != 0;
161 const bool sse42 = (
result[2] & ((int)1 << 20)) != 0;
163 const bool fma3 = (
result[2] & ((int)1 << 12)) != 0;
164 const bool os_uses_xsave_xrestore = (
result[2] & ((int)1 << 27)) != 0;
165 const bool cpu_avx_support = (
result[2] & ((int)1 << 28)) != 0;
168 caps.sse42 = sse && sse2 && sse3 && ssse3 && sse41 && sse42;
170 if (os_uses_xsave_xrestore && cpu_avx_support) {
173# if defined(__GNUC__)
176 __asm__(
".byte 0x0f, 0x01, 0xd0" :
"=a"(xcr_feature_mask),
"=d"(edx) :
"c"(0));
177# elif defined(_MSC_VER) && defined(_XCR_XFEATURE_ENABLED_MASK)
179 xcr_feature_mask = (
uint32_t)_xgetbv(_XCR_XFEATURE_ENABLED_MASK);
181 xcr_feature_mask = 0;
183 const bool avx = (xcr_feature_mask & 0x6) == 0x6;
184 const bool f16c = (
result[2] & ((int)1 << 29)) != 0;
187 bool bmi1 = (
result[1] & ((int)1 << 3)) != 0;
188 bool bmi2 = (
result[1] & ((int)1 << 8)) != 0;
189 bool avx2 = (
result[1] & ((int)1 << 5)) != 0;
191 caps.avx2 = sse && sse2 && sse3 && ssse3 && sse41 && sse42 && avx && f16c && avx2 &&
192 fma3 && bmi1 && bmi2;
204 CPUCapabilities &caps = system_cpu_capabilities();
210 CPUCapabilities &caps = system_cpu_capabilities();
231 ram.dwLength =
sizeof(ram);
232 GlobalMemoryStatusEx(&ram);
233 return ram.ullTotalPhys;
234#elif defined(__APPLE__)
236 size_t len =
sizeof(ram);
237 if (sysctlbyname(
"hw.memsize", &ram, &
len,
nullptr, 0) == 0) {
242 size_t ps = sysconf(_SC_PAGESIZE);
243 size_t pn = sysconf(_SC_PHYS_PAGES);
251 return GetCurrentProcessId();
ATTR_WARN_UNUSED_RESULT const size_t num
BMesh const char void * data
unsigned long long int uint64_t
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
#define CCL_NAMESPACE_END
string string_remove_trademark(const string &s)
static void __cpuid(int data[4], int selector)
size_t system_physical_ram()
bool system_cpu_support_avx2()
CCL_NAMESPACE_BEGIN int system_console_width()
bool system_cpu_support_sse42()
string system_cpu_brand_string()
uint64_t system_self_process_id()