Blob Blame History Raw
From 40eeced3fb25780be27799b2a72a2264ef64163b Mon Sep 17 00:00:00 2001
From: Vladimir 'phcoder' Serbinenko <phcoder@gmail.com>
Date: Sat, 5 Jan 2013 18:37:34 +0100
Subject: [PATCH 088/482] 	* grub-core/fs/ext2.c (grub_ext2_read_block):
 Use shifts rather than 	divisions.

---
 ChangeLog           |  5 +++++
 grub-core/fs/ext2.c | 30 ++++++++++++++++--------------
 2 files changed, 21 insertions(+), 14 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 88fd763..af29161 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,10 @@
 2013-01-05  Vladimir Serbinenko  <phcoder@gmail.com>
 
+	* grub-core/fs/ext2.c (grub_ext2_read_block): Use shifts rather than
+	divisions.
+
+2013-01-05  Vladimir Serbinenko  <phcoder@gmail.com>
+
 	* grub-core/fs/ntfs.c: Eliminate useless divisions in favor of shifts.
 	* grub-core/fs/ntfscomp.c: Likewise.
 	* include/grub/ntfs.h (grub_ntfs_data): Replace spc with log_spc.
diff --git a/grub-core/fs/ext2.c b/grub-core/fs/ext2.c
index bd1ab24..cf2e2f4 100644
--- a/grub-core/fs/ext2.c
+++ b/grub-core/fs/ext2.c
@@ -454,11 +454,12 @@ grub_ext2_read_block (grub_fshelp_node_t node, grub_disk_addr_t fileblock)
       blknr = grub_le_to_cpu32 (indir[fileblock - INDIRECT_BLOCKS]);
     }
   /* Double indirect.  */
-  else if (fileblock < INDIRECT_BLOCKS + blksz / 4 * (blksz / 4 + 1))
+  else if (fileblock < INDIRECT_BLOCKS
+	   + blksz / 4 * ((grub_disk_addr_t) blksz / 4 + 1))
     {
-      unsigned int perblock = blksz / 4;
-      unsigned int rblock = fileblock - (INDIRECT_BLOCKS
-					 + blksz / 4);
+      int log_perblock = log2_blksz + 9 - 2;
+      grub_disk_addr_t rblock = fileblock - (INDIRECT_BLOCKS
+					     + blksz / 4);
       grub_uint32_t indir[blksz / 4];
 
       if (grub_disk_read (data->disk,
@@ -470,21 +471,22 @@ grub_ext2_read_block (grub_fshelp_node_t node, grub_disk_addr_t fileblock)
 
       if (grub_disk_read (data->disk,
 			  ((grub_disk_addr_t)
-			   grub_le_to_cpu32 (indir[rblock / perblock]))
+			   grub_le_to_cpu32 (indir[rblock >> log_perblock]))
 			  << log2_blksz,
 			  0, blksz, indir))
 	return grub_errno;
 
 
-      blknr = grub_le_to_cpu32 (indir[rblock % perblock]);
+      blknr = grub_le_to_cpu32 (indir[rblock & ((1 << log_perblock) - 1)]);
     }
   /* triple indirect.  */
-  else if (fileblock < INDIRECT_BLOCKS + blksz / 4 * (blksz / 4 + 1)
-	   + (blksz / 4) * (blksz / 4) * (blksz / 4 + 1))
+  else if (fileblock < INDIRECT_BLOCKS + blksz / 4 * ((grub_disk_addr_t) blksz / 4 + 1)
+	   + ((grub_disk_addr_t) blksz / 4) * ((grub_disk_addr_t) blksz / 4)
+	   * ((grub_disk_addr_t) blksz / 4 + 1))
     {
-      unsigned int perblock = blksz / 4;
-      unsigned int rblock = fileblock - (INDIRECT_BLOCKS + blksz / 4
-					 * (blksz / 4 + 1));
+      int log_perblock = log2_blksz + 9 - 2;
+      grub_disk_addr_t rblock = fileblock - (INDIRECT_BLOCKS + blksz / 4
+					     * (blksz / 4 + 1));
       grub_uint32_t indir[blksz / 4];
 
       if (grub_disk_read (data->disk,
@@ -496,19 +498,19 @@ grub_ext2_read_block (grub_fshelp_node_t node, grub_disk_addr_t fileblock)
 
       if (grub_disk_read (data->disk,
 			  ((grub_disk_addr_t)
-			   grub_le_to_cpu32 (indir[(rblock / perblock) / perblock]))
+			   grub_le_to_cpu32 (indir[(rblock >> log_perblock) >> log_perblock]))
 			  << log2_blksz,
 			  0, blksz, indir))
 	return grub_errno;
 
       if (grub_disk_read (data->disk,
 			  ((grub_disk_addr_t)
-			   grub_le_to_cpu32 (indir[(rblock / perblock) % perblock]))
+			   grub_le_to_cpu32 (indir[(rblock >> log_perblock) & ((1 << log_perblock) - 1)]))
 			  << log2_blksz,
 			  0, blksz, indir))
 	return grub_errno;
 
-      blknr = grub_le_to_cpu32 (indir[rblock % perblock]);
+      blknr = grub_le_to_cpu32 (indir[rblock  & ((1 << log_perblock) - 1)]);
     }
   else
     {
-- 
1.8.2.1