dcavalca / rpms / grub2

Forked from rpms/grub2 3 years ago
Clone

Blame SOURCES/0087-grub-core-fs-ntfs.c-Eliminate-useless-divisions-in-f.patch

f96e0b
From e92606660e0fa6a9cf85eaf62677187965094506 Mon Sep 17 00:00:00 2001
f96e0b
From: Vladimir 'phcoder' Serbinenko <phcoder@gmail.com>
f96e0b
Date: Sat, 5 Jan 2013 17:36:04 +0100
f96e0b
Subject: [PATCH 087/482] 	* grub-core/fs/ntfs.c: Eliminate useless
f96e0b
 divisions in favor of shifts. 	* grub-core/fs/ntfscomp.c: Likewise. 	*
f96e0b
 include/grub/ntfs.h (grub_ntfs_data): Replace spc with log_spc. 
f96e0b
 (grub_ntfs_comp): Likewise.
f96e0b
f96e0b
---
f96e0b
 ChangeLog               |  7 +++++++
f96e0b
 grub-core/fs/ntfs.c     | 47 ++++++++++++++++++++++++-----------------------
f96e0b
 grub-core/fs/ntfscomp.c | 39 ++++++++++++++++++++-------------------
f96e0b
 include/grub/ntfs.h     |  6 ++++--
f96e0b
 4 files changed, 55 insertions(+), 44 deletions(-)
f96e0b
f96e0b
diff --git a/ChangeLog b/ChangeLog
f96e0b
index ea191b3..88fd763 100644
f96e0b
--- a/ChangeLog
f96e0b
+++ b/ChangeLog
f96e0b
@@ -1,5 +1,12 @@
f96e0b
 2013-01-05  Vladimir Serbinenko  <phcoder@gmail.com>
f96e0b
 
f96e0b
+	* grub-core/fs/ntfs.c: Eliminate useless divisions in favor of shifts.
f96e0b
+	* grub-core/fs/ntfscomp.c: Likewise.
f96e0b
+	* include/grub/ntfs.h (grub_ntfs_data): Replace spc with log_spc.
f96e0b
+	(grub_ntfs_comp): Likewise.
f96e0b
+
f96e0b
+2013-01-05  Vladimir Serbinenko  <phcoder@gmail.com>
f96e0b
+
f96e0b
 	* grub-core/fs/nilfs2.c (-grub_nilfs2_palloc_groups_per_desc_block):
f96e0b
 	Rename to ...
f96e0b
 	(grub_nilfs2_palloc_log_groups_per_desc_block): ... this. Return log
f96e0b
diff --git a/grub-core/fs/ntfs.c b/grub-core/fs/ntfs.c
f96e0b
index b9762b6..6004e1f 100644
f96e0b
--- a/grub-core/fs/ntfs.c
f96e0b
+++ b/grub-core/fs/ntfs.c
f96e0b
@@ -391,7 +391,7 @@ read_data (struct grub_ntfs_attr *at, char *pa, char *dest,
f96e0b
   grub_memset (&cc, 0, sizeof (cc));
f96e0b
   ctx = &cc;
f96e0b
   ctx->attr = at;
f96e0b
-  ctx->comp.spc = at->mft->data->spc;
f96e0b
+  ctx->comp.log_spc = at->mft->data->log_spc;
f96e0b
   ctx->comp.disk = at->mft->data->disk;
f96e0b
 
f96e0b
   if (pa[8] == 0)
f96e0b
@@ -440,11 +440,11 @@ read_data (struct grub_ntfs_attr *at, char *pa, char *dest,
f96e0b
 	  at->save_pos = 1;
f96e0b
 	}
f96e0b
 
f96e0b
-      vcn = ctx->target_vcn = (ofs >> GRUB_NTFS_COM_LOG_LEN) * (GRUB_NTFS_COM_SEC / ctx->comp.spc);
f96e0b
+      vcn = ctx->target_vcn = (ofs >> GRUB_NTFS_COM_LOG_LEN) * (GRUB_NTFS_COM_SEC >> ctx->comp.log_spc);
f96e0b
       ctx->target_vcn &= ~0xFULL;
f96e0b
     }
f96e0b
   else
f96e0b
-    vcn = ctx->target_vcn = grub_divmod64 (ofs >> GRUB_NTFS_BLK_SHR, ctx->comp.spc, 0);
f96e0b
+    vcn = ctx->target_vcn = ofs >> (GRUB_NTFS_BLK_SHR + ctx->comp.log_spc);
f96e0b
 
f96e0b
   ctx->next_vcn = u32at (pa, 0x10);
f96e0b
   ctx->curr_lcn = 0;
f96e0b
@@ -459,17 +459,17 @@ read_data (struct grub_ntfs_attr *at, char *pa, char *dest,
f96e0b
       grub_disk_addr_t st0, st1;
f96e0b
       grub_uint64_t m;
f96e0b
 
f96e0b
-      grub_divmod64 (ofs >> GRUB_NTFS_BLK_SHR, ctx->comp.spc, &m);
f96e0b
+      m = (ofs >> GRUB_NTFS_BLK_SHR) & ((1 << ctx->comp.log_spc) - 1);
f96e0b
 
f96e0b
       st0 =
f96e0b
-	(ctx->target_vcn - ctx->curr_vcn + ctx->curr_lcn) * ctx->comp.spc + m;
f96e0b
+	((ctx->target_vcn - ctx->curr_vcn + ctx->curr_lcn) << ctx->comp.log_spc) + m;
f96e0b
       st1 = st0 + 1;
f96e0b
       if (st1 ==
f96e0b
-	  (ctx->next_vcn - ctx->curr_vcn + ctx->curr_lcn) * ctx->comp.spc)
f96e0b
+	  (ctx->next_vcn - ctx->curr_vcn + ctx->curr_lcn) << ctx->comp.log_spc)
f96e0b
 	{
f96e0b
 	  if (grub_ntfs_read_run_list (ctx))
f96e0b
 	    return grub_errno;
f96e0b
-	  st1 = ctx->curr_lcn * ctx->comp.spc;
f96e0b
+	  st1 = ctx->curr_lcn << ctx->comp.log_spc;
f96e0b
 	}
f96e0b
       grub_set_unaligned32 (dest, grub_cpu_to_le32 (st0));
f96e0b
       grub_set_unaligned32 (dest + 4, grub_cpu_to_le32 (st1));
f96e0b
@@ -478,12 +478,10 @@ read_data (struct grub_ntfs_attr *at, char *pa, char *dest,
f96e0b
 
f96e0b
   if (!(ctx->flags & GRUB_NTFS_RF_COMP))
f96e0b
     {
f96e0b
-      unsigned int pow;
f96e0b
-
f96e0b
-      if (!grub_fshelp_log2blksize (ctx->comp.spc, &pow))
f96e0b
-	grub_fshelp_read_file (ctx->comp.disk, (grub_fshelp_node_t) ctx,
f96e0b
-			       read_hook, ofs, len, dest,
f96e0b
-			       grub_ntfs_read_block, ofs + len, pow, 0);
f96e0b
+      grub_fshelp_read_file (ctx->comp.disk, (grub_fshelp_node_t) ctx,
f96e0b
+			     read_hook, ofs, len, dest,
f96e0b
+			     grub_ntfs_read_block, ofs + len,
f96e0b
+			     ctx->comp.log_spc, 0);
f96e0b
       return grub_errno;
f96e0b
     }
f96e0b
 
f96e0b
@@ -515,11 +513,11 @@ read_attr (struct grub_ntfs_attr *at, char *dest, grub_disk_addr_t ofs,
f96e0b
 
f96e0b
       /* If compression is possible make sure that we include possible
f96e0b
 	 compressed block size.  */
f96e0b
-      if (GRUB_NTFS_COM_SEC >= at->mft->data->spc)
f96e0b
+      if (GRUB_NTFS_LOG_COM_SEC >= at->mft->data->log_spc)
f96e0b
 	vcn = ((ofs >> GRUB_NTFS_COM_LOG_LEN)
f96e0b
-	       * (GRUB_NTFS_COM_SEC / at->mft->data->spc)) & ~0xFULL;
f96e0b
+	       << (GRUB_NTFS_LOG_COM_SEC - at->mft->data->log_spc)) & ~0xFULL;
f96e0b
       else
f96e0b
-	vcn = grub_divmod64 (ofs, at->mft->data->spc << GRUB_NTFS_BLK_SHR, 0);
f96e0b
+	vcn = ofs >> (at->mft->data->log_spc + GRUB_NTFS_BLK_SHR);
f96e0b
       pa = at->attr_nxt + u16at (at->attr_nxt, 4);
f96e0b
       while (pa < at->attr_end)
f96e0b
 	{
f96e0b
@@ -934,6 +932,7 @@ grub_ntfs_mount (grub_disk_t disk)
f96e0b
 {
f96e0b
   struct grub_ntfs_bpb bpb;
f96e0b
   struct grub_ntfs_data *data = 0;
f96e0b
+  grub_uint32_t spc;
f96e0b
 
f96e0b
   if (!disk)
f96e0b
     goto fail;
f96e0b
@@ -955,23 +954,25 @@ grub_ntfs_mount (grub_disk_t disk)
f96e0b
       || (bpb.bytes_per_sector & (bpb.bytes_per_sector - 1)) != 0)
f96e0b
     goto fail;
f96e0b
 
f96e0b
-  data->spc = (((grub_uint32_t) bpb.sectors_per_cluster
f96e0b
-		* (grub_uint32_t) grub_le_to_cpu16 (bpb.bytes_per_sector))
f96e0b
-	       >> GRUB_NTFS_BLK_SHR);
f96e0b
-  if (!data->spc)
f96e0b
+  spc = (((grub_uint32_t) bpb.sectors_per_cluster
f96e0b
+	  * (grub_uint32_t) grub_le_to_cpu16 (bpb.bytes_per_sector))
f96e0b
+	 >> GRUB_NTFS_BLK_SHR);
f96e0b
+  if (spc == 0 || (spc & (spc - 1)))
f96e0b
     goto fail;
f96e0b
 
f96e0b
+  for (data->log_spc = 0; (1U << data->log_spc) < spc; data->log_spc++);
f96e0b
+
f96e0b
   if (bpb.clusters_per_mft > 0)
f96e0b
-    data->mft_size = data->spc * bpb.clusters_per_mft;
f96e0b
+    data->mft_size = bpb.clusters_per_mft << data->log_spc;
f96e0b
   else
f96e0b
     data->mft_size = 1 << (-bpb.clusters_per_mft - GRUB_NTFS_BLK_SHR);
f96e0b
 
f96e0b
   if (bpb.clusters_per_index > 0)
f96e0b
-    data->idx_size = data->spc * bpb.clusters_per_index;
f96e0b
+    data->idx_size = bpb.clusters_per_index << data->log_spc;
f96e0b
   else
f96e0b
     data->idx_size = 1 << (-bpb.clusters_per_index - GRUB_NTFS_BLK_SHR);
f96e0b
 
f96e0b
-  data->mft_start = grub_le_to_cpu64 (bpb.mft_lcn) * data->spc;
f96e0b
+  data->mft_start = grub_le_to_cpu64 (bpb.mft_lcn) << data->log_spc;
f96e0b
 
f96e0b
   if ((data->mft_size > GRUB_NTFS_MAX_MFT) || (data->idx_size > GRUB_NTFS_MAX_IDX))
f96e0b
     goto fail;
f96e0b
diff --git a/grub-core/fs/ntfscomp.c b/grub-core/fs/ntfscomp.c
f96e0b
index ec359fa..9b3b75d 100644
f96e0b
--- a/grub-core/fs/ntfscomp.c
f96e0b
+++ b/grub-core/fs/ntfscomp.c
f96e0b
@@ -33,8 +33,9 @@ decomp_nextvcn (struct grub_ntfs_comp *cc)
f96e0b
   if (grub_disk_read
f96e0b
       (cc->disk,
f96e0b
        (cc->comp_table[cc->comp_head].next_lcn -
f96e0b
-	(cc->comp_table[cc->comp_head].next_vcn - cc->cbuf_vcn)) * cc->spc, 0,
f96e0b
-       cc->spc << GRUB_NTFS_BLK_SHR, cc->cbuf))
f96e0b
+	(cc->comp_table[cc->comp_head].next_vcn - cc->cbuf_vcn)) << cc->log_spc,
f96e0b
+       0,
f96e0b
+       1 << (cc->log_spc + GRUB_NTFS_BLK_SHR), cc->cbuf))
f96e0b
     return grub_errno;
f96e0b
   cc->cbuf_vcn++;
f96e0b
   if ((cc->cbuf_vcn >= cc->comp_table[cc->comp_head].next_vcn))
f96e0b
@@ -46,7 +47,7 @@ decomp_nextvcn (struct grub_ntfs_comp *cc)
f96e0b
 static grub_err_t
f96e0b
 decomp_getch (struct grub_ntfs_comp *cc, unsigned char *res)
f96e0b
 {
f96e0b
-  if (cc->cbuf_ofs >= (cc->spc << GRUB_NTFS_BLK_SHR))
f96e0b
+  if (cc->cbuf_ofs >= (1U << (cc->log_spc + GRUB_NTFS_BLK_SHR)))
f96e0b
     {
f96e0b
       if (decomp_nextvcn (cc))
f96e0b
 	return grub_errno;
f96e0b
@@ -159,7 +160,7 @@ decomp_block (struct grub_ntfs_comp *cc, char *dest)
f96e0b
     {
f96e0b
       int n;
f96e0b
 
f96e0b
-      n = (cc->spc << GRUB_NTFS_BLK_SHR) - cc->cbuf_ofs;
f96e0b
+      n = (1 << (cc->log_spc + GRUB_NTFS_BLK_SHR)) - cc->cbuf_ofs;
f96e0b
       if (n > cnt)
f96e0b
 	n = cnt;
f96e0b
       if ((dest) && (n))
f96e0b
@@ -178,7 +179,7 @@ decomp_block (struct grub_ntfs_comp *cc, char *dest)
f96e0b
 static grub_err_t
f96e0b
 read_block (struct grub_ntfs_rlst *ctx, char *buf, grub_size_t num)
f96e0b
 {
f96e0b
-  int cpb = GRUB_NTFS_COM_SEC / ctx->comp.spc;
f96e0b
+  int log_cpb = GRUB_NTFS_LOG_COM_SEC - ctx->comp.log_spc;
f96e0b
 
f96e0b
   while (num)
f96e0b
     {
f96e0b
@@ -192,7 +193,7 @@ read_block (struct grub_ntfs_rlst *ctx, char *buf, grub_size_t num)
f96e0b
 	    return grub_error (GRUB_ERR_BAD_FS, "invalid compression block");
f96e0b
 	  ctx->comp.comp_head = ctx->comp.comp_tail = 0;
f96e0b
 	  ctx->comp.cbuf_vcn = ctx->target_vcn;
f96e0b
-	  ctx->comp.cbuf_ofs = (ctx->comp.spc << GRUB_NTFS_BLK_SHR);
f96e0b
+	  ctx->comp.cbuf_ofs = (1 << (ctx->comp.log_spc + GRUB_NTFS_BLK_SHR));
f96e0b
 	  if (ctx->target_vcn >= ctx->next_vcn)
f96e0b
 	    {
f96e0b
 	      if (grub_ntfs_read_run_list (ctx))
f96e0b
@@ -211,14 +212,14 @@ read_block (struct grub_ntfs_rlst *ctx, char *buf, grub_size_t num)
f96e0b
 	    }
f96e0b
 	}
f96e0b
 
f96e0b
-      nn = (16 - (unsigned) (ctx->target_vcn & 0xF)) / cpb;
f96e0b
+      nn = (16 - (unsigned) (ctx->target_vcn & 0xF)) >> log_cpb;
f96e0b
       if (nn > num)
f96e0b
 	nn = num;
f96e0b
       num -= nn;
f96e0b
 
f96e0b
       if (ctx->flags & GRUB_NTFS_RF_BLNK)
f96e0b
 	{
f96e0b
-	  ctx->target_vcn += nn * cpb;
f96e0b
+	  ctx->target_vcn += nn << log_cpb;
f96e0b
 	  if (ctx->comp.comp_tail == 0)
f96e0b
 	    {
f96e0b
 	      if (buf)
f96e0b
@@ -241,7 +242,7 @@ read_block (struct grub_ntfs_rlst *ctx, char *buf, grub_size_t num)
f96e0b
 	}
f96e0b
       else
f96e0b
 	{
f96e0b
-	  nn *= cpb;
f96e0b
+	  nn <<= log_cpb;
f96e0b
 	  while ((ctx->comp.comp_head < ctx->comp.comp_tail) && (nn))
f96e0b
 	    {
f96e0b
 	      grub_disk_addr_t tt;
f96e0b
@@ -258,10 +259,10 @@ read_block (struct grub_ntfs_rlst *ctx, char *buf, grub_size_t num)
f96e0b
 		      (ctx->comp.disk,
f96e0b
 		       (ctx->comp.comp_table[ctx->comp.comp_head].next_lcn -
f96e0b
 			(ctx->comp.comp_table[ctx->comp.comp_head].next_vcn -
f96e0b
-			 ctx->target_vcn)) * ctx->comp.spc, 0,
f96e0b
-		       tt * (ctx->comp.spc << GRUB_NTFS_BLK_SHR), buf))
f96e0b
+			 ctx->target_vcn)) << ctx->comp.log_spc, 0,
f96e0b
+		       tt << (ctx->comp.log_spc + GRUB_NTFS_BLK_SHR), buf))
f96e0b
 		    return grub_errno;
f96e0b
-		  buf += tt * (ctx->comp.spc << GRUB_NTFS_BLK_SHR);
f96e0b
+		  buf += tt << (ctx->comp.log_spc + GRUB_NTFS_BLK_SHR);
f96e0b
 		}
f96e0b
 	      nn -= tt;
f96e0b
 	      if (ctx->target_vcn >=
f96e0b
@@ -275,10 +276,10 @@ read_block (struct grub_ntfs_rlst *ctx, char *buf, grub_size_t num)
f96e0b
 		  if (grub_disk_read
f96e0b
 		      (ctx->comp.disk,
f96e0b
 		       (ctx->target_vcn - ctx->curr_vcn +
f96e0b
-			ctx->curr_lcn) * ctx->comp.spc, 0,
f96e0b
-		       nn * (ctx->comp.spc << GRUB_NTFS_BLK_SHR), buf))
f96e0b
+			ctx->curr_lcn) << ctx->comp.log_spc, 0,
f96e0b
+		       nn << (ctx->comp.log_spc + GRUB_NTFS_BLK_SHR), buf))
f96e0b
 		    return grub_errno;
f96e0b
-		  buf += nn * (ctx->comp.spc << GRUB_NTFS_BLK_SHR);
f96e0b
+		  buf += nn << (ctx->comp.log_spc + GRUB_NTFS_BLK_SHR);
f96e0b
 		}
f96e0b
 	      ctx->target_vcn += nn;
f96e0b
 	    }
f96e0b
@@ -294,7 +295,7 @@ ntfscomp (struct grub_ntfs_attr *at, char *dest, grub_disk_addr_t ofs,
f96e0b
   grub_err_t ret;
f96e0b
 
f96e0b
   ctx->comp.comp_head = ctx->comp.comp_tail = 0;
f96e0b
-  ctx->comp.cbuf = grub_malloc ((ctx->comp.spc) << GRUB_NTFS_BLK_SHR);
f96e0b
+  ctx->comp.cbuf = grub_malloc (1 << (ctx->comp.log_spc + GRUB_NTFS_BLK_SHR));
f96e0b
   if (!ctx->comp.cbuf)
f96e0b
     return 0;
f96e0b
 
f96e0b
@@ -304,7 +305,7 @@ ntfscomp (struct grub_ntfs_attr *at, char *dest, grub_disk_addr_t ofs,
f96e0b
 
f96e0b
   if ((vcn > ctx->target_vcn) &&
f96e0b
       (read_block
f96e0b
-       (ctx, NULL, ((vcn - ctx->target_vcn) * ctx->comp.spc) / GRUB_NTFS_COM_SEC)))
f96e0b
+       (ctx, NULL, (vcn - ctx->target_vcn) >> (GRUB_NTFS_LOG_COM_SEC - ctx->comp.log_spc))))
f96e0b
     {
f96e0b
       ret = grub_errno;
f96e0b
       goto quit;
f96e0b
@@ -314,7 +315,7 @@ ntfscomp (struct grub_ntfs_attr *at, char *dest, grub_disk_addr_t ofs,
f96e0b
     {
f96e0b
       grub_uint32_t t, n, o;
f96e0b
 
f96e0b
-      t = ctx->target_vcn * (ctx->comp.spc << GRUB_NTFS_BLK_SHR);
f96e0b
+      t = ctx->target_vcn << (ctx->comp.log_spc + GRUB_NTFS_BLK_SHR);
f96e0b
       if (read_block (ctx, at->sbuf, 1))
f96e0b
 	{
f96e0b
 	  ret = grub_errno;
f96e0b
@@ -346,7 +347,7 @@ ntfscomp (struct grub_ntfs_attr *at, char *dest, grub_disk_addr_t ofs,
f96e0b
     {
f96e0b
       grub_uint32_t t;
f96e0b
 
f96e0b
-      t = ctx->target_vcn * (ctx->comp.spc << GRUB_NTFS_BLK_SHR);
f96e0b
+      t = ctx->target_vcn << (ctx->comp.log_spc + GRUB_NTFS_BLK_SHR);
f96e0b
       if (read_block (ctx, at->sbuf, 1))
f96e0b
 	{
f96e0b
 	  ret = grub_errno;
f96e0b
diff --git a/include/grub/ntfs.h b/include/grub/ntfs.h
f96e0b
index 0935342..cc28a01 100644
f96e0b
--- a/include/grub/ntfs.h
f96e0b
+++ b/include/grub/ntfs.h
f96e0b
@@ -87,6 +87,7 @@ enum
f96e0b
 #define GRUB_NTFS_COM_LEN		4096
f96e0b
 #define GRUB_NTFS_COM_LOG_LEN	12
f96e0b
 #define GRUB_NTFS_COM_SEC		(GRUB_NTFS_COM_LEN >> GRUB_NTFS_BLK_SHR)
f96e0b
+#define GRUB_NTFS_LOG_COM_SEC		(GRUB_NTFS_COM_LOG_LEN - GRUB_NTFS_BLK_SHR)
f96e0b
 
f96e0b
 enum
f96e0b
   {
f96e0b
@@ -156,7 +157,7 @@ struct grub_ntfs_data
f96e0b
   grub_disk_t disk;
f96e0b
   grub_uint32_t mft_size;
f96e0b
   grub_uint32_t idx_size;
f96e0b
-  grub_uint32_t spc;
f96e0b
+  int log_spc;
f96e0b
   grub_uint32_t mft_start;
f96e0b
   grub_uint64_t uuid;
f96e0b
 };
f96e0b
@@ -172,7 +173,8 @@ struct grub_ntfs_comp
f96e0b
   grub_disk_t disk;
f96e0b
   int comp_head, comp_tail;
f96e0b
   struct grub_ntfs_comp_table_element comp_table[16];
f96e0b
-  grub_uint32_t cbuf_ofs, cbuf_vcn, spc;
f96e0b
+  grub_uint32_t cbuf_ofs, cbuf_vcn;
f96e0b
+  int log_spc;
f96e0b
   char *cbuf;
f96e0b
 };
f96e0b
 
f96e0b
-- 
f96e0b
1.8.2.1
f96e0b