10 #include <OpenImageIO/sysutil.h>
15 # if (!defined(FREE_WINDOWS))
19 #elif defined(__APPLE__)
20 # include <sys/ioctl.h>
21 # include <sys/sysctl.h>
22 # include <sys/types.h>
25 # include <sys/ioctl.h>
36 CONSOLE_SCREEN_BUFFER_INFO csbi;
37 if (GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi)) {
38 columns = csbi.dwSize.X;
42 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &
w) == 0) {
47 return (columns > 0) ? columns : 80;
51 #if (!defined(_WIN32) || defined(FREE_WINDOWS)) && (defined(__x86_64__) || defined(__i386__))
54 # if defined(__x86_64__)
55 asm(
"cpuid" :
"=a"(
data[0]),
"=b"(
data[1]),
"=c"(
data[2]),
"=d"(
data[3]) :
"a"(selector));
56 # elif defined(__i386__)
57 asm(
"pushl %%ebx \n\t"
72 #if defined(__APPLE__)
74 char modelname[512] =
"";
75 size_t bufferlen = 512;
76 if (sysctlbyname(
"machdep.cpu.brand_string", &modelname, &bufferlen,
NULL, 0) == 0) {
79 #elif defined(WIN32) || defined(__x86_64__) || defined(__i386__)
87 __cpuid((
int *)(buf + 0), 0x80000002);
88 __cpuid((
int *)(buf + 16), 0x80000003);
89 __cpuid((
int *)(buf + 32), 0x80000004);
100 FILE *cpuinfo = fopen(
"/proc/cpuinfo",
"r");
101 if (cpuinfo !=
nullptr) {
102 char cpuinfo_buf[513] =
"";
103 fread(cpuinfo_buf,
sizeof(cpuinfo_buf) - 1, 1, cpuinfo);
106 char *modelname = strstr(cpuinfo_buf,
"model name");
107 if (modelname !=
nullptr) {
108 modelname = strchr(modelname,
':');
109 if (modelname !=
nullptr) {
111 char *modelname_end = strchr(modelname,
'\n');
112 if (modelname_end !=
nullptr) {
113 *modelname_end =
'\0';
120 return "Unknown CPU";
125 return (
sizeof(
void *) * 8);
128 #if defined(__x86_64__) || defined(_M_X64) || defined(__i386__) || defined(_M_IX86)
130 struct CPUCapabilities {
138 static CPUCapabilities &system_cpu_capabilities()
140 static CPUCapabilities caps = {};
141 static bool caps_init =
false;
151 const bool sse = (
result[3] & ((int)1 << 25)) != 0;
152 const bool sse2 = (
result[3] & ((int)1 << 26)) != 0;
153 const bool sse3 = (
result[2] & ((int)1 << 0)) != 0;
155 const bool ssse3 = (
result[2] & ((int)1 << 9)) != 0;
156 const bool sse41 = (
result[2] & ((int)1 << 19)) != 0;
159 const bool fma3 = (
result[2] & ((int)1 << 12)) != 0;
160 const bool os_uses_xsave_xrestore = (
result[2] & ((int)1 << 27)) != 0;
161 const bool cpu_avx_support = (
result[2] & ((int)1 << 28)) != 0;
164 caps.sse2 = sse && sse2;
165 caps.sse3 = sse && sse2 && sse3 && ssse3;
166 caps.sse41 = sse && sse2 && sse3 && ssse3 && sse41;
168 if (os_uses_xsave_xrestore && cpu_avx_support) {
171 # if defined(__GNUC__)
174 __asm__(
".byte 0x0f, 0x01, 0xd0" :
"=a"(xcr_feature_mask),
"=d"(edx) :
"c"(0));
175 # elif defined(_MSC_VER) && defined(_XCR_XFEATURE_ENABLED_MASK)
177 xcr_feature_mask = (
uint32_t)_xgetbv(_XCR_XFEATURE_ENABLED_MASK);
179 xcr_feature_mask = 0;
181 const bool avx = (xcr_feature_mask & 0x6) == 0x6;
182 const bool f16c = (
result[2] & ((int)1 << 29)) != 0;
185 bool bmi1 = (
result[1] & ((int)1 << 3)) != 0;
186 bool bmi2 = (
result[1] & ((int)1 << 8)) != 0;
187 bool avx2 = (
result[1] & ((int)1 << 5)) != 0;
189 caps.avx = sse && sse2 && sse3 && ssse3 && sse41 && avx;
190 caps.avx2 = sse && sse2 && sse3 && ssse3 && sse41 && avx && f16c && avx2 && fma3 && bmi1 &&
203 CPUCapabilities &caps = system_cpu_capabilities();
209 CPUCapabilities &caps = system_cpu_capabilities();
215 CPUCapabilities &caps = system_cpu_capabilities();
221 CPUCapabilities &caps = system_cpu_capabilities();
227 CPUCapabilities &caps = system_cpu_capabilities();
262 ram.dwLength =
sizeof(ram);
263 GlobalMemoryStatusEx(&ram);
264 return ram.ullTotalPhys;
265 #elif defined(__APPLE__)
267 size_t len =
sizeof(ram);
268 if (sysctlbyname(
"hw.memsize", &ram, &
len,
NULL, 0) == 0) {
273 size_t ps = sysconf(_SC_PAGESIZE);
274 size_t pn = sysconf(_SC_PHYS_PAGES);
282 return GetCurrentProcessId();
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
#define CCL_NAMESPACE_END
unsigned __int64 uint64_t
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()
OIIO_NAMESPACE_USING CCL_NAMESPACE_BEGIN int system_console_width()
string system_cpu_brand_string()
bool system_cpu_support_avx()
uint64_t system_self_process_id()
bool system_cpu_support_sse3()
bool system_cpu_support_sse41()
bool system_cpu_support_sse2()