nalika / rpms / grub2

Forked from rpms/grub2 2 years ago
Clone

Blame SOURCES/0458-png-Optimize-by-avoiding-divisions.patch

d18179
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
d18179
From: Vladimir Serbinenko <phcoder@gmail.com>
d18179
Date: Thu, 26 Feb 2015 18:27:43 +0100
d18179
Subject: [PATCH] png: Optimize by avoiding divisions.
d18179
d18179
(cherry picked from commit 18125877eec4e6d7b0b6567b6c92eab1df946f47)
d18179
---
d18179
 grub-core/video/readers/png.c | 29 ++++++++++++++++++++---------
d18179
 1 file changed, 20 insertions(+), 9 deletions(-)
d18179
d18179
diff --git a/grub-core/video/readers/png.c b/grub-core/video/readers/png.c
d18179
index a9066658f2..71bb76f9c4 100644
d18179
--- a/grub-core/video/readers/png.c
d18179
+++ b/grub-core/video/readers/png.c
d18179
@@ -236,7 +236,7 @@ grub_png_decode_image_palette (struct grub_png_data *data,
d18179
 {
d18179
   unsigned i = 0, j;
d18179
 
d18179
-  if (len == 0 || len % 3 != 0)
d18179
+  if (len == 0)
d18179
     return GRUB_ERR_NONE;
d18179
 
d18179
   grub_errno = GRUB_ERR_NONE;
d18179
@@ -900,15 +900,26 @@ grub_png_convert_image (struct grub_png_data *data)
d18179
       int mask = (1 << data->color_bits) - 1;
d18179
       unsigned j;
d18179
       if (data->is_gray)
d18179
-	for (i = 0; i < (1U << data->color_bits); i++)
d18179
-	  {
d18179
-	    grub_uint8_t col = (0xff * i) / ((1U << data->color_bits) - 1);
d18179
-	    palette[i][0] = col;
d18179
-	    palette[i][1] = col;
d18179
-	    palette[i][2] = col;
d18179
-	  }
d18179
+	{
d18179
+	  /* Generic formula is
d18179
+	     (0xff * i) / ((1U << data->color_bits) - 1)
d18179
+	     but for allowed bit depth of 1, 2 and for it's
d18179
+	     equivalent to
d18179
+	     (0xff / ((1U << data->color_bits) - 1)) * i
d18179
+	     Precompute the multipliers to avoid division.
d18179
+	  */
d18179
+
d18179
+	  const grub_uint8_t multipliers[5] = { 0xff, 0xff, 0x55, 0x24, 0x11 };
d18179
+	  for (i = 0; i < (1U << data->color_bits); i++)
d18179
+	    {
d18179
+	      grub_uint8_t col = multipliers[data->color_bits] * i;
d18179
+	      palette[i][0] = col;
d18179
+	      palette[i][1] = col;
d18179
+	      palette[i][2] = col;
d18179
+	    }
d18179
+	}
d18179
       else
d18179
-	grub_memcpy (palette, data->palette, 16 * 3);
d18179
+	grub_memcpy (palette, data->palette, 3 << data->color_bits);
d18179
       d1c = d1;
d18179
       d2c = d2;
d18179
       for (j = 0; j < data->image_height; j++, d1c += data->image_width * 3,