Patchwork vga: optimize ppm_save() divisions

login
register
mail settings
Submitter Avi Kivity
Date Jan. 3, 2012, 1:32 p.m.
Message ID <1325597577-29385-1-git-send-email-avi@redhat.com>
Download mbox | patch
Permalink /patch/134002/
State New
Headers show

Comments

Avi Kivity - Jan. 3, 2012, 1:32 p.m.
ppm_save() spends upwards of 50% of its time doing divisions. Replace them
with shifts.

Signed-off-by: Avi Kivity <avi@redhat.com>
---
 hw/vga.c |   10 ++++------
 1 files changed, 4 insertions(+), 6 deletions(-)
Avi Kivity - Jan. 12, 2012, 1:46 p.m.
On 01/03/2012 03:32 PM, Avi Kivity wrote:
> ppm_save() spends upwards of 50% of its time doing divisions. Replace them
> with shifts.
>
>


Pings.
Alon Levy - Jan. 12, 2012, 3:16 p.m.
On Tue, Jan 03, 2012 at 03:32:57PM +0200, Avi Kivity wrote:
> ppm_save() spends upwards of 50% of its time doing divisions. Replace them
> with shifts.
> 

Reviewed-by: Alon Levy <alevy@redhat.com>

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

> Signed-off-by: Avi Kivity <avi@redhat.com>
> ---
>  hw/vga.c |   10 ++++------
>  1 files changed, 4 insertions(+), 6 deletions(-)
> 
> diff --git a/hw/vga.c b/hw/vga.c
> index ca79aa1..a228cde 100644
> --- a/hw/vga.c
> +++ b/hw/vga.c
> @@ -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;
> -- 
> 1.7.7.1
> 
>
Alon Levy - Jan. 12, 2012, 3:19 p.m.
On Thu, Jan 12, 2012 at 05:16:03PM +0200, Alon Levy wrote:
> On Tue, Jan 03, 2012 at 03:32:57PM +0200, Avi Kivity wrote:
> > ppm_save() spends upwards of 50% of its time doing divisions. Replace them
> > with shifts.
> > 
> 
> Reviewed-by: Alon Levy <alevy@redhat.com>
> 
> rmax/bmax/gmax are all uint8_t atm, could add a compilation error if
> sizeof(bmax)!=1 ever.

meant {r,g,b}bits.

> 
> > Signed-off-by: Avi Kivity <avi@redhat.com>
> > ---
> >  hw/vga.c |   10 ++++------
> >  1 files changed, 4 insertions(+), 6 deletions(-)
> > 
> > diff --git a/hw/vga.c b/hw/vga.c
> > index ca79aa1..a228cde 100644
> > --- a/hw/vga.c
> > +++ b/hw/vga.c
> > @@ -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;
> > -- 
> > 1.7.7.1
> > 
> > 
>
Anthony Liguori - Jan. 12, 2012, 4:54 p.m.
On 01/03/2012 07:32 AM, Avi Kivity wrote:
> ppm_save() spends upwards of 50% of its time doing divisions. Replace them
> with shifts.
>
> Signed-off-by: Avi Kivity<avi@redhat.com>

Applied.  Thanks.

Regards,

Anthony Liguori

> ---
>   hw/vga.c |   10 ++++------
>   1 files changed, 4 insertions(+), 6 deletions(-)
>
> diff --git a/hw/vga.c b/hw/vga.c
> index ca79aa1..a228cde 100644
> --- a/hw/vga.c
> +++ b/hw/vga.c
> @@ -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;

Patch

diff --git a/hw/vga.c b/hw/vga.c
index ca79aa1..a228cde 100644
--- a/hw/vga.c
+++ b/hw/vga.c
@@ -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;