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