Attempting to replace 64-bit mask with bool array

This commit is contained in:
Dr-Noob
2025-10-30 08:33:27 +01:00
parent 2116f19073
commit e823c769ff
4 changed files with 27 additions and 15 deletions

View File

@@ -138,7 +138,7 @@ struct features {
struct extensions {
char* str;
uint64_t mask;
bool* mask; // allocated at runtime with size RISCV_ISA_EXT_ID_MAX-1
};
struct cpuInfo {

View File

@@ -949,14 +949,7 @@ bool print_cpufetch_arm(struct cpuInfo* cpu, STYLE s, struct color** cs, struct
#endif
#ifdef ARCH_RISCV
// https://stackoverflow.com/a/2709523
uint64_t number_of_bits(uint64_t i) {
i = i - ((i >> 1) & 0x5555555555555555);
i = (i & 0x3333333333333333) + ((i >> 2) & 0x3333333333333333);
return (((i + (i >> 4)) & 0xF0F0F0F0F0F0F0F) * 0x101010101010101) >> 56;
}
void print_ascii_riscv(struct ascii* art, uint32_t la, int32_t termw, bool use_short, uint64_t extensions_mask) {
void print_ascii_riscv(struct ascii* art, uint32_t la, int32_t termw, bool use_short, bool* extensions_mask) {
struct ascii_logo* logo = art->art;
int attr_to_print = 0;
int attr_type;
@@ -966,7 +959,7 @@ void print_ascii_riscv(struct ascii* art, uint32_t la, int32_t termw, bool use_s
int32_t ext_list_size = sizeof(extension_list)/sizeof(extension_list[0]);
int32_t ext_num = 0;
int32_t ext_to_print = 0;
int32_t num_extensions = number_of_bits(extensions_mask);
int32_t num_extensions = get_num_extensions(extensions_mask);
int32_t space_up = ((int)logo->height - (int)(art->n_attributes_set + num_extensions))/2;
int32_t space_down = (int)logo->height - (int)(art->n_attributes_set + num_extensions) - (int)space_up;
uint32_t logo_pos = 0;
@@ -1012,7 +1005,11 @@ void print_ascii_riscv(struct ascii* art, uint32_t la, int32_t termw, bool use_s
// Print extension
if(attr_to_print > 0 && art->attributes[attr_to_print-1]->type == ATTRIBUTE_EXTENSIONS && ext_num != num_extensions) {
// Search for the extension to print
while(ext_to_print < ext_list_size && !((extensions_mask >> extension_list[ext_to_print].id) & 1U)) ext_to_print++;
printf("ext_to_print: %d\n", ext_to_print);
while (ext_to_print < ext_list_size && !((extensions_mask[extension_list[ext_to_print].id])))
ext_to_print++;
if(ext_to_print == ext_list_size) {
printBug("print_ascii_riscv: Unable to find the extension to print");
}

View File

@@ -12,7 +12,7 @@
#define SET_ISA_EXT_MAP(name, bit) \
if(strncmp(multi_letter_extension, name, \
multi_letter_extension_len) == 0) { \
ext->mask |= 1UL << bit; \
ext->mask[bit] = true; \
maskset = true; \
} \
@@ -152,7 +152,7 @@ bool valid_extension(char ext) {
struct extensions* get_extensions_from_str(char* str) {
struct extensions* ext = emalloc(sizeof(struct extensions));
ext->mask = 0;
ext->mask = ecalloc((RISCV_ISA_EXT_ID_MAX-1) * sizeof(bool));
ext->str = NULL;
if(str == NULL) {
@@ -165,6 +165,8 @@ struct extensions* get_extensions_from_str(char* str) {
// Code inspired in Linux kernel (riscv_fill_hwcap):
// https://elixir.bootlin.com/linux/v6.2.10/source/arch/riscv/kernel/cpufeature.c
// Now it seems to be here in riscv_parse_isa_string:
// https://elixir.bootlin.com/linux/v6.16/source/arch/riscv/kernel/cpufeature.c
char* isa = str;
if (!strncmp(isa, "rv32", 4))
isa += 4;
@@ -196,7 +198,7 @@ struct extensions* get_extensions_from_str(char* str) {
// adding it to the mask
if(valid_extension(*e)) {
int n = *e - 'a';
ext->mask |= 1UL << n;
ext->mask[n] = true;
}
else {
printBug("get_extensions_from_str: Invalid extension: '%c'", *e);
@@ -207,6 +209,18 @@ struct extensions* get_extensions_from_str(char* str) {
return ext;
}
uint32 get_num_extensions(bool* mask) {
uint32 num = 0;
for (int i=0; i < RISCV_ISA_EXT_ID_MAX-1; i++) {
if (mask[i]) num++;
}
return num;
}
bool is_mask_empty(bool* mask) {
return get_num_extensions(mask) == 0;
}
struct cpuInfo* get_cpu_info(void) {
struct cpuInfo* cpu = malloc(sizeof(struct cpuInfo));
//init_cpu_info(cpu);
@@ -219,7 +233,7 @@ struct cpuInfo* get_cpu_info(void) {
cpu->hv = emalloc(sizeof(struct hypervisor));
cpu->hv->present = false;
cpu->ext = get_extensions_from_str(ext_str);
if(cpu->ext->str != NULL && cpu->ext->mask == 0) return NULL;
if(cpu->ext->str != NULL && is_mask_empty(cpu->ext->mask)) return NULL;
cpu->arch = get_uarch(cpu);
cpu->soc = get_soc(cpu);
cpu->freq = get_frequency_info(0);

View File

@@ -196,6 +196,7 @@ static const struct extension extension_list[] = {
struct cpuInfo* get_cpu_info(void);
char* get_str_topology(struct cpuInfo* cpu, struct topology* topo);
char* get_str_extensions(struct cpuInfo* cpu);
uint32 get_num_extensions(bool* mask);
void print_debug(struct cpuInfo* cpu);
#endif