[Qemu-devel] [PATCH 1/5] coccinelle: replace code with ROUND_UP macro

Marc-André Lureau posted 5 patches 8 years, 5 months ago
[Qemu-devel] [PATCH 1/5] coccinelle: replace code with ROUND_UP macro
Posted by Marc-André Lureau 8 years, 5 months ago
I used a the following coccinelle script:

@@
expression e1;
@@
- ((e1) + (3)) / (4) * (4)
+ ROUND_UP(e1,4)

@@
expression e1;
expression e2;
@@
-(ROUND_UP(e1,e2))
+ROUND_UP(e1,e2)

I tried with various other values (4, 8, 16, 32), but got only the
matches in this patch.

Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
---
 target/i386/arch_dump.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/target/i386/arch_dump.c b/target/i386/arch_dump.c
index fe0aa36932..e6788250b8 100644
--- a/target/i386/arch_dump.c
+++ b/target/i386/arch_dump.c
@@ -84,9 +84,9 @@ static int x86_64_write_elf64_note(WriteCoreDumpFunction f,
     note->n_descsz = cpu_to_le32(descsz);
     note->n_type = cpu_to_le32(NT_PRSTATUS);
     buf = (char *)note;
-    buf += ((sizeof(Elf64_Nhdr) + 3) / 4) * 4;
+    buf += ROUND_UP(sizeof(Elf64_Nhdr), 4);
     memcpy(buf, name, name_size);
-    buf += ((name_size + 3) / 4) * 4;
+    buf += ROUND_UP(name_size, 4);
     memcpy(buf + 32, &id, 4); /* pr_pid */
     buf += descsz - sizeof(x86_64_user_regs_struct)-sizeof(target_ulong);
     memcpy(buf, &regs, sizeof(x86_64_user_regs_struct));
@@ -163,9 +163,9 @@ static int x86_write_elf64_note(WriteCoreDumpFunction f, CPUX86State *env,
     note->n_descsz = cpu_to_le32(descsz);
     note->n_type = cpu_to_le32(NT_PRSTATUS);
     buf = (char *)note;
-    buf += ((sizeof(Elf64_Nhdr) + 3) / 4) * 4;
+    buf += ROUND_UP(sizeof(Elf64_Nhdr), 4);
     memcpy(buf, name, name_size);
-    buf += ((name_size + 3) / 4) * 4;
+    buf += ROUND_UP(name_size, 4);
     memcpy(buf, &prstatus, sizeof(prstatus));
 
     ret = f(note, note_size, opaque);
@@ -218,9 +218,9 @@ int x86_cpu_write_elf32_note(WriteCoreDumpFunction f, CPUState *cs,
     note->n_descsz = cpu_to_le32(descsz);
     note->n_type = cpu_to_le32(NT_PRSTATUS);
     buf = (char *)note;
-    buf += ((sizeof(Elf32_Nhdr) + 3) / 4) * 4;
+    buf += ROUND_UP(sizeof(Elf32_Nhdr), 4);
     memcpy(buf, name, name_size);
-    buf += ((name_size + 3) / 4) * 4;
+    buf += ROUND_UP(name_size, 4);
     memcpy(buf, &prstatus, sizeof(prstatus));
 
     ret = f(note, note_size, opaque);
-- 
2.13.0.91.g00982b8dd


Re: [Qemu-devel] [PATCH 1/5] coccinelle: replace code with ROUND_UP macro
Posted by Juan Quintela 8 years, 5 months ago
Marc-André Lureau <marcandre.lureau@redhat.com> wrote:
> I used a the following coccinelle script:
>
> @@
> expression e1;
> @@
> - ((e1) + (3)) / (4) * (4)
> + ROUND_UP(e1,4)
>
> @@
> expression e1;
> expression e2;
> @@
> -(ROUND_UP(e1,e2))
> +ROUND_UP(e1,e2)
>
> I tried with various other values (4, 8, 16, 32), but got only the
> matches in this patch.
>
> Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>

Reviewed-by: Juan Quintela <quintela@redhat.com>

Re: [Qemu-devel] [PATCH 1/5] coccinelle: replace code with ROUND_UP macro
Posted by Eric Blake 8 years, 5 months ago
On 06/07/2017 02:46 AM, Marc-André Lureau wrote:
> I used a the following coccinelle script:
> 
> @@
> expression e1;
> @@
> - ((e1) + (3)) / (4) * (4)
> + ROUND_UP(e1,4)


Can't you also search for:

@@
expression e1, e2
@@
- ((e1) + (e2 - 1)) / (e2) * (e2)
+ ROUND_UP(e1, e2)

to catch cases where we are rounding by a non-constant?

-- 
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org