mirror of
https://github.com/Dr-Noob/cpufetch.git
synced 2026-03-24 23:40:39 +01:00
[v0.99][X86] Improve CPU abbreviate code
This commit is contained in:
@@ -34,9 +34,11 @@ int64_t get_freq(struct frequency* freq) {
|
|||||||
|
|
||||||
#if defined(ARCH_X86) || defined(ARCH_PPC)
|
#if defined(ARCH_X86) || defined(ARCH_PPC)
|
||||||
char* get_str_cpu_name(struct cpuInfo* cpu, bool fcpuname) {
|
char* get_str_cpu_name(struct cpuInfo* cpu, bool fcpuname) {
|
||||||
if(cpu->cpu_vendor == CPU_VENDOR_INTEL && !fcpuname) {
|
#ifdef ARCH_X86
|
||||||
abbreviate_intel_cpu_name(&cpu->cpu_name);
|
if(!fcpuname) {
|
||||||
|
return get_str_cpu_name_abbreviated(cpu);
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
return cpu->cpu_name;
|
return cpu->cpu_name;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -73,6 +73,19 @@ int max(int a, int b) {
|
|||||||
return a > b ? a : b;
|
return a > b ? a : b;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
char *strremove(char *str, const char *sub) {
|
||||||
|
char *p, *q, *r;
|
||||||
|
if (*sub && (q = r = strstr(str, sub)) != NULL) {
|
||||||
|
size_t len = strlen(sub);
|
||||||
|
while ((r = strstr(p = r + len, sub)) != NULL) {
|
||||||
|
memmove(q, p, r - p);
|
||||||
|
q += r - p;
|
||||||
|
}
|
||||||
|
memmove(q, p, strlen(p) + 1);
|
||||||
|
}
|
||||||
|
return str;
|
||||||
|
}
|
||||||
|
|
||||||
void* emalloc(size_t size) {
|
void* emalloc(size_t size) {
|
||||||
void* ptr = malloc(size);
|
void* ptr = malloc(size);
|
||||||
|
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ void printWarn(const char *fmt, ...);
|
|||||||
void printErr(const char *fmt, ...);
|
void printErr(const char *fmt, ...);
|
||||||
void printBug(const char *fmt, ...);
|
void printBug(const char *fmt, ...);
|
||||||
int max(int a, int b);
|
int max(int a, int b);
|
||||||
|
char *strremove(char *str, const char *sub);
|
||||||
void* emalloc(size_t size);
|
void* emalloc(size_t size);
|
||||||
void* ecalloc(size_t nmemb, size_t size);
|
void* ecalloc(size_t nmemb, size_t size);
|
||||||
|
|
||||||
|
|||||||
@@ -116,7 +116,7 @@ char* get_str_cpu_name_internal() {
|
|||||||
return name;
|
return name;
|
||||||
}
|
}
|
||||||
|
|
||||||
void abbreviate_intel_cpu_name(char** name) {
|
bool abbreviate_intel_cpu_name(char** name) {
|
||||||
char* old_name = *name;
|
char* old_name = *name;
|
||||||
char* new_name = ecalloc(strlen(old_name) + 1, sizeof(char));
|
char* new_name = ecalloc(strlen(old_name) + 1, sizeof(char));
|
||||||
|
|
||||||
@@ -125,41 +125,39 @@ void abbreviate_intel_cpu_name(char** name) {
|
|||||||
char* aux_ptr = NULL;
|
char* aux_ptr = NULL;
|
||||||
|
|
||||||
// 1. Remove "(R)"
|
// 1. Remove "(R)"
|
||||||
if(strncmp(old_name_ptr, "Intel(R)", 8) != 0) return;
|
old_name_ptr = strstr(old_name_ptr, "Intel(R)");
|
||||||
|
if(old_name_ptr == NULL) return false;
|
||||||
strcpy(new_name_ptr, "Intel");
|
strcpy(new_name_ptr, "Intel");
|
||||||
new_name_ptr += 5;
|
new_name_ptr += strlen("Intel");
|
||||||
old_name_ptr += 8;
|
old_name_ptr += strlen("Intel(R)");
|
||||||
|
|
||||||
// 2. Remove "(R)" or "(TM)"
|
// 2. Remove "(R)" or "(TM)"
|
||||||
aux_ptr = strstr(old_name_ptr, "(");
|
aux_ptr = strstr(old_name_ptr, "(");
|
||||||
if(aux_ptr == NULL) return;
|
if(aux_ptr == NULL) return false;
|
||||||
strncpy(new_name_ptr, old_name_ptr, aux_ptr-old_name_ptr);
|
strncpy(new_name_ptr, old_name_ptr, aux_ptr-old_name_ptr);
|
||||||
|
|
||||||
new_name_ptr += aux_ptr-old_name_ptr;
|
new_name_ptr += aux_ptr-old_name_ptr;
|
||||||
strcpy(new_name_ptr, " ");
|
strcpy(new_name_ptr, " ");
|
||||||
new_name_ptr++;
|
new_name_ptr++;
|
||||||
old_name_ptr = strstr(aux_ptr, ")");
|
old_name_ptr = strstr(aux_ptr, ")");
|
||||||
if(old_name_ptr == NULL) return;
|
if(old_name_ptr == NULL) return false;
|
||||||
old_name_ptr += 2;
|
old_name_ptr++;
|
||||||
|
while(*old_name_ptr == ' ') old_name_ptr++;
|
||||||
|
|
||||||
// 3. Skip "CPU" (if exists)
|
// 3. Copy the CPU name
|
||||||
if(strncmp(old_name_ptr, "CPU", 3) == 0) {
|
aux_ptr = strstr(old_name_ptr, "@");
|
||||||
aux_ptr = strstr(old_name_ptr, " ");
|
if(aux_ptr == NULL) return false;
|
||||||
if(aux_ptr == NULL) return;
|
|
||||||
old_name_ptr = aux_ptr + 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 4. Copy the CPU name
|
|
||||||
aux_ptr = strstr(old_name_ptr, "CPU");
|
|
||||||
if(aux_ptr == NULL) {
|
|
||||||
// Name contains no "CPU" at the end, so ends with @
|
|
||||||
aux_ptr = strstr(old_name_ptr, "@");
|
|
||||||
if(aux_ptr == NULL) return;
|
|
||||||
}
|
|
||||||
strncpy(new_name_ptr, old_name_ptr, (aux_ptr-1)-old_name_ptr);
|
strncpy(new_name_ptr, old_name_ptr, (aux_ptr-1)-old_name_ptr);
|
||||||
|
|
||||||
|
// 4. Remove dummy strings in Intel CPU names
|
||||||
|
strremove(new_name, " CPU");
|
||||||
|
strremove(new_name, " Dual");
|
||||||
|
strremove(new_name, " 0");
|
||||||
|
|
||||||
free(old_name);
|
free(old_name);
|
||||||
*name = new_name;
|
*name = new_name;
|
||||||
|
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct uarch* get_cpu_uarch(struct cpuInfo* cpu) {
|
struct uarch* get_cpu_uarch(struct cpuInfo* cpu) {
|
||||||
@@ -748,6 +746,15 @@ struct frequency* get_frequency_info(struct cpuInfo* cpu) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// STRING FUNCTIONS
|
// STRING FUNCTIONS
|
||||||
|
char* get_str_cpu_name_abbreviated(struct cpuInfo* cpu) {
|
||||||
|
if(cpu->cpu_vendor == CPU_VENDOR_INTEL) {
|
||||||
|
if(!abbreviate_intel_cpu_name(&cpu->cpu_name)) {
|
||||||
|
printWarn("Failed to abbreviate CPU name");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return cpu->cpu_name;
|
||||||
|
}
|
||||||
|
|
||||||
char* get_str_topology(struct cpuInfo* cpu, struct topology* topo, bool dual_socket) {
|
char* get_str_topology(struct cpuInfo* cpu, struct topology* topo, bool dual_socket) {
|
||||||
int topo_sockets = dual_socket ? topo->sockets : 1;
|
int topo_sockets = dual_socket ? topo->sockets : 1;
|
||||||
char* string;
|
char* string;
|
||||||
|
|||||||
@@ -12,8 +12,7 @@ char* get_str_avx(struct cpuInfo* cpu);
|
|||||||
char* get_str_sse(struct cpuInfo* cpu);
|
char* get_str_sse(struct cpuInfo* cpu);
|
||||||
char* get_str_fma(struct cpuInfo* cpu);
|
char* get_str_fma(struct cpuInfo* cpu);
|
||||||
char* get_str_topology(struct cpuInfo* cpu, struct topology* topo, bool dual_socket);
|
char* get_str_topology(struct cpuInfo* cpu, struct topology* topo, bool dual_socket);
|
||||||
|
char* get_str_cpu_name_abbreviated(struct cpuInfo* cpu);
|
||||||
void abbreviate_intel_cpu_name(char** name);
|
|
||||||
|
|
||||||
void print_debug(struct cpuInfo* cpu);
|
void print_debug(struct cpuInfo* cpu);
|
||||||
void print_raw(struct cpuInfo* cpu);
|
void print_raw(struct cpuInfo* cpu);
|
||||||
|
|||||||
Reference in New Issue
Block a user