Skip to contentSkip to conversation

vga: optimize ppm_save() divisions

PATCH Applied Avi Kivity 3 participants 5 messages Jan 3, 2012 – Jan 12, 2012

lore
Share & tools
lore

Avi Kivity

author
#0 lore raw

PATCH 1/1 vga: optimize ppm_save() divisions

ppm_save() spends upwards of 50% of its time doing divisions. Replace them with shifts.

---
 hw/vga.c |   10 ++++------
 1 files changed, 4 insertions(+), 6 deletions(-)
hw/vga.c +4 -6
index ca79aa1..a228cde 100644
@@ -2370,12 +2370,10 @@ int ppm_save(const char *filename, struct DisplaySurface *ds)
                 v = *(uint32_t *)d;
             else
                 v = (uint32_t) (*(uint16_t *)d);
-            r = ((v >> ds->pf.rshift) & ds->pf.rmax) * 256 /
-                (ds->pf.rmax + 1);
-            g = ((v >> ds->pf.gshift) & ds->pf.gmax) * 256 /
-                (ds->pf.gmax + 1);
-            b = ((v >> ds->pf.bshift) & ds->pf.bmax) * 256 /
-                (ds->pf.bmax + 1);
+            /* Limited to 8 or fewer bits per channel: */
+            r = ((v >> ds->pf.rshift) & ds->pf.rmax) << (8 - ds->pf.rbits);
+            g = ((v >> ds->pf.gshift) & ds->pf.gmax) << (8 - ds->pf.gbits);
+            b = ((v >> ds->pf.bshift) & ds->pf.bmax) << (8 - ds->pf.bbits);
             *pbuf++ = r;
             *pbuf++ = g;
             *pbuf++ = b;
Signature · 1 line
1.7.7.1
author
#1 lore raw
Quoted 4 lines · Avi Kivity

ppm_save() spends upwards of 50% of its time doing divisions. Replace them with shifts.

Pings.

Signature · 1 line
error compiling committee.c: too many arguments to function
Reviewed-by on v1 1/1
#2 lore raw
In reply to Avi Kivity
Quoted 3 lines · Avi Kivity

ppm_save() spends upwards of 50% of its time doing divisions. Replace them with shifts.

rmax/bmax/gmax are all uint8_t atm, could add a compilation error if sizeof(bmax)!=1 ever.

Quoted 30 lines
---
 hw/vga.c |   10 ++++------
 1 files changed, 4 insertions(+), 6 deletions(-)
hw/vga.c +4 -6
index ca79aa1..a228cde 100644
@@ -2370,12 +2370,10 @@ int ppm_save(const char *filename, struct DisplaySurface *ds)
                 v = *(uint32_t *)d;
             else
                 v = (uint32_t) (*(uint16_t *)d);
-            r = ((v >> ds->pf.rshift) & ds->pf.rmax) * 256 /
-                (ds->pf.rmax + 1);
-            g = ((v >> ds->pf.gshift) & ds->pf.gmax) * 256 /
-                (ds->pf.gmax + 1);
-            b = ((v >> ds->pf.bshift) & ds->pf.bmax) * 256 /
-                (ds->pf.bmax + 1);
+            /* Limited to 8 or fewer bits per channel: */
+            r = ((v >> ds->pf.rshift) & ds->pf.rmax) << (8 - ds->pf.rbits);
+            g = ((v >> ds->pf.gshift) & ds->pf.gmax) << (8 - ds->pf.gbits);
+            b = ((v >> ds->pf.bshift) & ds->pf.bmax) << (8 - ds->pf.bbits);
             *pbuf++ = r;
             *pbuf++ = g;
             *pbuf++ = b;
Signature · 1 line
1.7.7.1
#3 lore raw
Quoted 9 lines · Alon Levy
Quoted 3 lines · Avi Kivity

ppm_save() spends upwards of 50% of its time doing divisions. Replace them with shifts.

rmax/bmax/gmax are all uint8_t atm, could add a compilation error if sizeof(bmax)!=1 ever.

meant {r,g,b}bits.

Quoted 32 lines
Quoted 31 lines
---
 hw/vga.c |   10 ++++------
 1 files changed, 4 insertions(+), 6 deletions(-)
hw/vga.c +4 -6
index ca79aa1..a228cde 100644
@@ -2370,12 +2370,10 @@ int ppm_save(const char *filename, struct DisplaySurface *ds)
                 v = *(uint32_t *)d;
             else
                 v = (uint32_t) (*(uint16_t *)d);
-            r = ((v >> ds->pf.rshift) & ds->pf.rmax) * 256 /
-                (ds->pf.rmax + 1);
-            g = ((v >> ds->pf.gshift) & ds->pf.gmax) * 256 /
-                (ds->pf.gmax + 1);
-            b = ((v >> ds->pf.bshift) & ds->pf.bmax) * 256 /
-                (ds->pf.bmax + 1);
+            /* Limited to 8 or fewer bits per channel: */
+            r = ((v >> ds->pf.rshift) & ds->pf.rmax) << (8 - ds->pf.rbits);
+            g = ((v >> ds->pf.gshift) & ds->pf.gmax) << (8 - ds->pf.gbits);
+            b = ((v >> ds->pf.bshift) & ds->pf.bmax) << (8 - ds->pf.bbits);
             *pbuf++ = r;
             *pbuf++ = g;
             *pbuf++ = b;
Signature · 1 line
1.7.7.1
#4 lore raw
In reply to Avi Kivity
Quoted 4 lines · Avi Kivity

ppm_save() spends upwards of 50% of its time doing divisions. Replace them with shifts.

Applied. Thanks.

Regards,

Anthony Liguori

Quoted 25 lines
---
  hw/vga.c |   10 ++++------
  1 files changed, 4 insertions(+), 6 deletions(-)
hw/vga.c +4 -6
index ca79aa1..a228cde 100644
@@ -2370,12 +2370,10 @@ int ppm_save(const char *filename, struct DisplaySurface *ds)
                  v = *(uint32_t *)d;
              else
                  v = (uint32_t) (*(uint16_t *)d);
-            r = ((v>>  ds->pf.rshift)&  ds->pf.rmax) * 256 /
-                (ds->pf.rmax + 1);
-            g = ((v>>  ds->pf.gshift)&  ds->pf.gmax) * 256 /
-                (ds->pf.gmax + 1);
-            b = ((v>>  ds->pf.bshift)&  ds->pf.bmax) * 256 /
-                (ds->pf.bmax + 1);
+            /* Limited to 8 or fewer bits per channel: */
+            r = ((v>>  ds->pf.rshift)&  ds->pf.rmax)<<  (8 - ds->pf.rbits);
+            g = ((v>>  ds->pf.gshift)&  ds->pf.gmax)<<  (8 - ds->pf.gbits);
+            b = ((v>>  ds->pf.bshift)&  ds->pf.bmax)<<  (8 - ds->pf.bbits);
              *pbuf++ = r;
              *pbuf++ = g;
              *pbuf++ = b;