Blob Blame History Raw
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Vladimir Serbinenko <phcoder@gmail.com>
Date: Thu, 26 Feb 2015 21:27:32 +0100
Subject: [PATCH] jpeg: Optimise by replacing division with shifts.

(cherry picked from commit 7213c1e02807e760febec06a0f19b11173abc55a)
---
 grub-core/video/readers/jpeg.c | 25 ++++++++++++++-----------
 1 file changed, 14 insertions(+), 11 deletions(-)

diff --git a/grub-core/video/readers/jpeg.c b/grub-core/video/readers/jpeg.c
index 84cf5ceec9..55bd95b797 100644
--- a/grub-core/video/readers/jpeg.c
+++ b/grub-core/video/readers/jpeg.c
@@ -94,7 +94,7 @@ struct grub_jpeg_data
   jpeg_data_unit_t crdu;
   jpeg_data_unit_t cbdu;
 
-  unsigned vs, hs;
+  unsigned log_vs, log_hs;
   int dri;
   unsigned r1;
 
@@ -321,11 +321,14 @@ grub_jpeg_decode_sof (struct grub_jpeg_data *data)
       ss = grub_jpeg_get_byte (data);	/* Sampling factor.  */
       if (!id)
 	{
-	  data->vs = ss & 0xF;	/* Vertical sampling.  */
-	  data->hs = ss >> 4;	/* Horizontal sampling.  */
-	  if ((data->vs > 2) || (data->hs > 2) || (data->vs == 0) || (data->hs == 0))
+	  grub_uint8_t vs, hs;
+	  vs = ss & 0xF;	/* Vertical sampling.  */
+	  hs = ss >> 4;	/* Horizontal sampling.  */
+	  if ((vs > 2) || (hs > 2) || (vs == 0) || (hs == 0))
 	    return grub_error (GRUB_ERR_BAD_FILE_TYPE,
 			       "jpeg: sampling method not supported");
+	  data->log_vs = (vs == 2);
+	  data->log_hs = (hs == 2);
 	}
       else if (ss != JPEG_SAMPLING_1x1)
 	return grub_error (GRUB_ERR_BAD_FILE_TYPE,
@@ -638,10 +641,10 @@ grub_jpeg_decode_data (struct grub_jpeg_data *data)
   unsigned c1, vb, hb, nr1, nc1;
   int rst = data->dri;
 
-  vb = data->vs * 8;
-  hb = data->hs * 8;
-  nr1 = (data->image_height + vb - 1) / vb;
-  nc1 = (data->image_width + hb - 1) / hb;
+  vb = 8 << data->log_vs;
+  hb = 8 << data->log_hs;
+  nr1 = (data->image_height + vb - 1) >> (3 + data->log_vs);
+  nc1 = (data->image_width + hb - 1)  >> (3 + data->log_hs);
 
   if (data->bitmap_ptr == NULL)
     return grub_error(GRUB_ERR_BAD_FILE_TYPE,
@@ -655,8 +658,8 @@ grub_jpeg_decode_data (struct grub_jpeg_data *data)
 	unsigned r2, c2, nr2, nc2;
 	grub_uint8_t *ptr2;
 
-	for (r2 = 0; r2 < data->vs; r2++)
-	  for (c2 = 0; c2 < data->hs; c2++)
+	for (r2 = 0; r2 < (1U << data->log_vs); r2++)
+	  for (c2 = 0; c2 < (1U << data->log_hs); c2++)
 	    grub_jpeg_decode_du (data, 0, data->ydu[r2 * 2 + c2]);
 
 	if (data->color_components >= 3)
@@ -678,7 +681,7 @@ grub_jpeg_decode_data (struct grub_jpeg_data *data)
 	      unsigned i0;
 	      int yy;
 
-	      i0 = (r2 / data->vs) * 8 + (c2 / data->hs);
+	      i0 = (r2 >> data->log_vs) * 8 + (c2 >> data->log_hs);
 	      yy = data->ydu[(r2 / 8) * 2 + (c2 / 8)][(r2 % 8) * 8 + (c2 % 8)];
 
 	      if (data->color_components >= 3)