RISC-V: Fix riscv_isa_string, use popcount to count bits
PATCH v2 1/1 RISC-V: Fix riscv_isa_string, use popcount to count bits
Logic bug caused the string size calculation for the RISC-V format ISA string to be small. This fix allows slack for rv128.
--- target/riscv/cpu.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
target/riscv/cpu.c +1 -1
@@ -391,7 +391,7 @@ static const TypeInfo riscv_cpu_type_info = {char *riscv_isa_string (RISCVCPU *cpu) {int i; -size_t maxlen =5 +ctz32 (cpu->env.misa); +size_t maxlen =8 +ctpop64 (cpu->env.misa);char *isa_string =g_new0 (char, maxlen);snprintf (isa_string, maxlen,"rv%d" ,TARGET_LONG_BITS );for (i =0 ; i <sizeof (riscv_exts); i++) {
Signature · 1 line
2.7.0
Quoted 16 lines · Michael Clark
Logic bug caused the string size calculation for the RISC-V format ISA string to be small. This fix allows slack for rv128.
Cc Palmer Dabbelt <[email protected]>Cc Peter Maydell <[email protected]>Cc Eric Blake <[email protected]>Signed-off-by Michael Clark <[email protected]>--- target/riscv/cpu.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)target/riscv/cpu.c +0
-0
target/riscv/cpu.c static const TypeInfo riscv_cpu_type_info = { · line 394
Quoted 5 lines
target/riscv/cpu.c +1
-1@@ -391,7 +391,7 @@ static const TypeInfo riscv_cpu_type_info = {char *riscv_isa_string (RISCVCPU *cpu) {int i; -size_t maxlen =5 +ctz32 (cpu->env.misa); +size_t maxlen =8 +ctpop64 (cpu->env.misa);
Can you document the magic 5/8?
This looks nice, but this seems to me too much optimization to save few bytes, using sizeof(riscv_exts) is overflow-free.
Maybe this is enough and self-explanatory:
const size_t maxlen = sizeof("rv128") + sizeof(riscv_exts);target/riscv/cpu.c static const TypeInfo riscv_cpu_type_info = { · line 396
Quoted 2 lines
target/riscv/cpu.c +0
-0char *isa_string =g_new0 (char, maxlen);snprintf (isa_string, maxlen,"rv%d" ,TARGET_LONG_BITS );
Also, if you keep the snprintf() return value, you can (naming it 'n') simplify (also easier to review):
target/riscv/cpu.c static const TypeInfo riscv_cpu_type_info = { · line 397
Quoted 2 lines
target/riscv/cpu.c +0
-0for (i =0 ; i <sizeof (riscv_exts); i++) {
if (cpu->env.misa & RV(riscv_exts[i])) {
- isa_string[strlen(isa_string)] = riscv_exts[i] - 'A' + 'a';
+ isa_string[n++] = tolower(riscv_exts[i]);
}
}and simply use g_new() with:
+ isa_string[n] = '\0';
return isa_string; }
Regards,
Phil.
Quoted 25 lines · Philippe Mathieu-Daudé
Quoted 16 lines · Michael Clark
Logic bug caused the string size calculation for the RISC-V format ISA string to be small. This fix allows slack for rv128.
Cc Palmer Dabbelt <[email protected]>Cc Peter Maydell <[email protected]>Cc Eric Blake <[email protected]>Signed-off-by Michael Clark <[email protected]>--- target/riscv/cpu.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)target/riscv/cpu.c +0
-0target/riscv/cpu.c static const TypeInfo riscv_cpu_type_info = { · line 394
Quoted 5 lines
target/riscv/cpu.c +1
-1@@ -391,7 +391,7 @@ static const TypeInfo riscv_cpu_type_info = {char *riscv_isa_string (RISCVCPU *cpu) {int i; -size_t maxlen =5 +ctz32 (cpu->env.misa); +size_t maxlen =8 +ctpop64 (cpu->env.misa);Can you document the magic 5/8?
Quoted 6 lines
This looks nice, but this seems to me too much optimization to save few bytes, using
sizeof(riscv_exts)is overflow-free.Maybe this is enough and self-explanatory:
const size_t maxlen = sizeof("rv128") + sizeof(riscv_exts);
Oops:
const size_t maxlen = sizeof("rv128") + sizeof(riscv_exts) + 1;Quoted 26 lines
target/riscv/cpu.c static const TypeInfo riscv_cpu_type_info = { · line 396
Quoted 3 lines
target/riscv/cpu.c +0
-0char *isa_string =g_new0 (char, maxlen);snprintf (isa_string, maxlen,"rv%d" ,TARGET_LONG_BITS );Also, if you keep the
snprintf()return value, you can (naming it 'n') simplify (also easier to review):target/riscv/cpu.c static const TypeInfo riscv_cpu_type_info = { · line 397
Quoted 2 lines
target/riscv/cpu.c +0
-0for (i =0 ; i <sizeof (riscv_exts); i++) {if (cpu->env.misa & RV(riscv_exts[i])) {(quoted diff) +1
-1- isa_string[strlen(isa_string)] = riscv_exts[i] - 'A' + 'a'; + isa_string[n++] = tolower(riscv_exts[i]); } }and simply use
g_new()with:+ isa_string[n] = '\0';
return isa_string; }Regards,
Phil.
Quoted 46 lines · Philippe Mathieu-Daudé
Quoted 16 lines · Michael Clark
Logic bug caused the string size calculation for the RISC-V format ISA string to be small. This fix allows slack for rv128.
Cc Palmer Dabbelt <[email protected]>Cc Peter Maydell <[email protected]>Cc Eric Blake <[email protected]>Signed-off-by Michael Clark <[email protected]>--- target/riscv/cpu.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)target/riscv/cpu.c +0
-0target/riscv/cpu.c static const TypeInfo riscv_cpu_type_info = { · line 394
Quoted 5 lines
target/riscv/cpu.c +1
-1@@ -391,7 +391,7 @@ static const TypeInfo riscv_cpu_type_info = {char *riscv_isa_string (RISCVCPU *cpu) {int i; -size_t maxlen =5 +ctz32 (cpu->env.misa); +size_t maxlen =8 +ctpop64 (cpu->env.misa);Can you document the magic 5/8?
This looks nice, but this seems to me too much optimization to save few bytes, using
sizeof(riscv_exts)is overflow-free.Maybe this is enough and self-explanatory:
const size_t maxlen = sizeof("rv128") + sizeof(riscv_exts);target/riscv/cpu.c static const TypeInfo riscv_cpu_type_info = { · line 396
Quoted 2 lines
target/riscv/cpu.c +0
-0char *isa_string =g_new0 (char, maxlen);snprintf (isa_string, maxlen,"rv%d" ,TARGET_LONG_BITS );Also, if you keep the
snprintf()return value, you can (naming it 'n') simplify (also easier to review):target/riscv/cpu.c static const TypeInfo riscv_cpu_type_info = { · line 397
Quoted 2 lines
target/riscv/cpu.c +0
-0for (i =0 ; i <sizeof (riscv_exts); i++) {if (cpu->env.misa & RV(riscv_exts[i])) {(quoted diff) +1
-1- isa_string[strlen(isa_string)] = riscv_exts[i] - 'A' + 'a'; + isa_string[n++] = tolower(riscv_exts[i]); } }
Quoted 6 lines
and simply use
g_new()with:+ isa_string[n] = '\0';
return isa_string; }
Hi -- any chance of a respin of this patch that addresses Philippe's review comments so we can fix it before rc0? This is causing my merge-build tests to fail about 50% of the time on OpenBSD at the moment...
thanks
-- PMM
Quoted 51 lines · Peter Maydell
Quoted 50 lines · Philippe Mathieu-Daudé
Quoted 16 lines · Michael Clark
Logic bug caused the string size calculation for the RISC-V format ISA string to be small. This fix allows slack for rv128.
Cc Palmer Dabbelt <[email protected]>Cc Peter Maydell <[email protected]>Cc Eric Blake <[email protected]>Signed-off-by Michael Clark <[email protected]>--- target/riscv/cpu.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)target/riscv/cpu.c +0
-0target/riscv/cpu.c static const TypeInfo riscv_cpu_type_info = { · line 394
Quoted 5 lines
target/riscv/cpu.c +1
-1@@ -391,7 +391,7 @@ static const TypeInfo riscv_cpu_type_info = {char *riscv_isa_string (RISCVCPU *cpu) {int i; -size_t maxlen =5 +ctz32 (cpu->env.misa); +size_t maxlen =8 +ctpop64 (cpu->env.misa);Can you document the magic 5/8?
This looks nice, but this seems to me too much optimization to save few bytes, using
sizeof(riscv_exts)is overflow-free.Maybe this is enough and self-explanatory:
const size_t maxlen = sizeof("rv128") + sizeof(riscv_exts);target/riscv/cpu.c static const TypeInfo riscv_cpu_type_info = { · line 396
Quoted 2 lines
target/riscv/cpu.c +0
-0char *isa_string =g_new0 (char, maxlen);snprintf (isa_string, maxlen,"rv%d" ,TARGET_LONG_BITS );Also, if you keep the
snprintf()return value, you can (naming it 'n') simplify (also easier to review):target/riscv/cpu.c static const TypeInfo riscv_cpu_type_info = { · line 397
Quoted 2 lines
target/riscv/cpu.c +0
-0for (i =0 ; i <sizeof (riscv_exts); i++) {if (cpu->env.misa & RV(riscv_exts[i])) {(quoted diff) +1
-1- isa_string[strlen(isa_string)] = riscv_exts[i] - 'A' + 'a'; + isa_string[n++] = tolower(riscv_exts[i]); } }and simply use
g_new()with:+ isa_string[n] = '\0';
Quoted 7 lines
Quoted 2 lines
return isa_string; }Hi -- any chance of a respin of this patch that addresses Philippe's review comments so we can fix it before rc0? This is causing my merge-build tests to fail about 50% of the time on OpenBSD at the moment...
I'll respin asap.
I was out earlier this week as I was at the RISC-V Hackathon at the Embedded Linux Conference in Portland. I also have to go through the review backlog for the post merge spec conformance and cleanup series...
Quoted 65 lines · Michael Clark
Quoted 52 lines · Peter Maydell
Quoted 50 lines · Philippe Mathieu-Daudé
Quoted 16 lines · Michael Clark
Logic bug caused the string size calculation for the RISC-V format ISA string to be small. This fix allows slack for rv128.
Cc Palmer Dabbelt <[email protected]>Cc Peter Maydell <[email protected]>Cc Eric Blake <[email protected]>Signed-off-by Michael Clark <[email protected]>--- target/riscv/cpu.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)target/riscv/cpu.c +0
-0target/riscv/cpu.c static const TypeInfo riscv_cpu_type_info = { · line 394
Quoted 5 lines
target/riscv/cpu.c +1
-1@@ -391,7 +391,7 @@ static const TypeInfo riscv_cpu_type_info = {char *riscv_isa_string (RISCVCPU *cpu) {int i; -size_t maxlen =5 +ctz32 (cpu->env.misa); +size_t maxlen =8 +ctpop64 (cpu->env.misa);Can you document the magic 5/8?
This looks nice, but this seems to me too much optimization to save few bytes, using
sizeof(riscv_exts)is overflow-free.Maybe this is enough and self-explanatory:
const size_t maxlen = sizeof("rv128") + sizeof(riscv_exts);target/riscv/cpu.c static const TypeInfo riscv_cpu_type_info = { · line 396
Quoted 2 lines
target/riscv/cpu.c +0
-0char *isa_string =g_new0 (char, maxlen);snprintf (isa_string, maxlen,"rv%d" ,TARGET_LONG_BITS );Also, if you keep the
snprintf()return value, you can (naming it 'n') simplify (also easier to review):target/riscv/cpu.c static const TypeInfo riscv_cpu_type_info = { · line 397
Quoted 2 lines
target/riscv/cpu.c +0
-0for (i =0 ; i <sizeof (riscv_exts); i++) {if (cpu->env.misa & RV(riscv_exts[i])) {(quoted diff) +1
-1- isa_string[strlen(isa_string)] = riscv_exts[i] - 'A' + 'a'; + isa_string[n++] = tolower(riscv_exts[i]); } }and simply use
g_new()with:+ isa_string[n] = '\0';
Quoted 7 lines
Quoted 2 lines
return isa_string; }Hi -- any chance of a respin of this patch that addresses Philippe's review comments so we can fix it before rc0? This is causing my merge-build tests to fail about 50% of the time on OpenBSD at the moment...
Quoted 6 lines
I'll respin asap.
I was out earlier this week as I was at the RISC-V Hackathon at the Embedded Linux Conference in Portland. I also have to go through the review backlog for the post merge spec conformance and cleanup series...
I've sent out the riscv_isa_string fix patch as well as the RISC-V Post-merge spec conformance and cleanup series independently. I could combine them so we have one post-merge PR that contains the riscv_isa_string string fix, the spec conformance fixes and cleanup. Many of the cleanup patches have been reviewed by Phil, but the patches that haven't been reviewed are spec conformance fixes and likely require a reading of the RISC-V Privileged ISA Specification, which may not happen, unless anyone with RISC-V knowledge has time. They have sign off.
If I submit a PR it will include the checkpatch fixes that I noticed after sending the patches. The series has been tested pretty extensively and the earlier revision is what we have in the riscv.org repo and in SiFive's freedom-u-sdk. i.e. it is what folk are using. I've been working inside of QEMU RISC-V quite a lot with this series applied, running the latest Fedora image with SMP Linux, building QEMU inside of QEMU as I'm working on a TCG backend for RISC-V that I started this Monday during the RISC-V Hackathon.
FYI: This is the work in progress branch on the TCG backend for RISC-V (it's not quite complete but the bones are there):
- https://github.com/michaeljclark/riscv-qemu/tree/riscv-tcg-backend