Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void get_vendor_id(char *vendor_id) // must be at least 13 bytes
- {
- FILE *cpuinfo = fopen("/proc/cpuinfo", "r");
- if(cpuinfo == NULL)
- ; // handle error
- char line[256];
- while(fgets(line, 256, cpuinfo))
- {
- if(strncmp(line, "vendor_id", 9) == 0)
- {
- char *colon = strchr(line, ':');
- if(colon == NULL || colon[1] == 0)
- ; // handle error
- strncpy(vendor_id, 12, colon + 2);
- fclose(cpuinfo);
- return;
- }
- }
- // if we got here, handle error
- fclose(cpuinfo);
- }
- void get_vendor_id(char *vendor_id) // must be at least 13 bytes
- {
- // GCC inline assembler
- __asm__ __volatile__
- ("movl $0, %%eaxnt"
- "cpuidnt"
- "movl %%ebx, %0nt"
- "movl %%edx, %1nt"
- "movl %%ecx, %2nt"
- : "=m"(vendor_id), "=m"(vendor_id + 4), "=m"(vendor_id + 8) // outputs
- : // no inputs
- : "%eax", "%ebx", "%edx", "%ecx", "memory"); // clobbered registers
- vendor_id[12] = 0;
- }
- int main(void)
- {
- char vendor_id[13];
- get_vendor_id(vendor_id);
- if(strcmp(vendor_id, "GenuineIntel") == 0)
- ; // it's Intel
- else if(strcmp(vendor_id, "AuthenticAMD") == 0)
- ; // it's AMD
- else
- ; // other
- return 0;
- }
- bool IsIntel() // returns true on an Intel processor, false on anything else
- {
- int id_str; // The first four characters of the vendor ID string
- __asm__ ("cpuid": // run the cpuid instruction with...
- "=c" (id_str) : // id_str set to the value of EBX after cpuid runs...
- "a" (0) : // and EAX set to 0 to run the proper cpuid function.
- "eax", "ebx", "edx"); // cpuid clobbers EAX, ECX, and EDX, in addition to EBX.
- if(id_str==0x6c65746e) // letn. little endian clobbering of GenuineI[ntel]
- return true;
- else
- return false;
- }
Add Comment
Please, Sign In to add comment