Received: from eggs.gnu.org ([2001:4830:134:3::10]:49000) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eulzp-0004ua-55 for qemu-devel@nongnu.org; Sat, 10 Mar 2018 16:25:42 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1eulzm-0003vj-0L for qemu-devel@nongnu.org; Sat, 10 Mar 2018 16:25:41 -0500 Received: from mail-wm0-x242.google.com ([2a00:1450:400c:c09::242]:39932) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1eulzl-0003vE-Pu for qemu-devel@nongnu.org; Sat, 10 Mar 2018 16:25:37 -0500 Received: by mail-wm0-x242.google.com with SMTP id i3so9566286wmi.4 for ; Sat, 10 Mar 2018 13:25:37 -0800 (PST) Sender: =?UTF-8?Q?Philippe_Mathieu=2DDaud=C3=A9?= References: <1520629272-98060-1-git-send-email-mjc@sifive.com> From: =?UTF-8?Q?Philippe_Mathieu-Daud=c3=a9?= Message-ID: Date: Sat, 10 Mar 2018 22:25:35 +0100 MIME-Version: 1.0 In-Reply-To: <1520629272-98060-1-git-send-email-mjc@sifive.com> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 8bit Subject: Re: [Qemu-devel] [PATCH v2] RISC-V: Fix riscv_isa_string, use popcount to count bits List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Michael Clark , qemu-devel@nongnu.org Cc: Peter Maydell , Palmer Dabbelt On 03/09/2018 10:01 PM, Michael Clark wrote: > 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 > Cc: Peter Maydell > Cc: Eric Blake > Signed-off-by: Michael Clark > --- > target/riscv/cpu.c | 2 +- > 1 file changed, 1 insertion(+), 1 deletion(-) > > diff --git a/target/riscv/cpu.c b/target/riscv/cpu.c > index 4851890..1456535 100644 > --- a/target/riscv/cpu.c > +++ b/target/riscv/cpu.c > @@ -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); > char *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): > for (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.