public inbox for linux-ext4@vger.kernel.org
 help / color / mirror / Atom feed
From: Alexandre Ratchov <alexandre.ratchov@bull.net>
To: linux-ext4@vger.kernel.org
Cc: Jean-Pierre Dion <jean-pierre.dion@bull.net>
Subject: [patch] small fix for e2p_percent()
Date: Wed, 20 Sep 2006 10:56:08 +0200	[thread overview]
Message-ID: <20060920085608.GA18351@openx1.frec.bull.fr> (raw)

hello,

e2p_percent() doesn't work for zero percent (``mke2fs -m0'' gets killed with
SIGFPE).

is there a reason for not simply using 64 bit arithmetic? perhaps to avoid
overflows for 7e+9TB file-systems :-), or just for correctness...

So, here is the patch that fixes this. In order to avoid integer overflows,
we pick 16 more bits to store ``base'' and do the exact division on 48 bits.
Since ``100 * base'' always fits in 48 bits, there's never overflow. This
still work if we remplace 'unsigned int' by 'unsigned long long'.

cheers,

-- Alexandre

Signed-off-by: Alexandre Ratchov <alexandre.ratchov@bull.net>

Index: e2fsprogs-1.39/lib/e2p/percent.c
===================================================================
--- e2fsprogs-1.39.orig/lib/e2p/percent.c	2006-09-20 12:07:05.000000000 +0200
+++ e2fsprogs-1.39/lib/e2p/percent.c	2006-09-20 12:33:20.000000000 +0200
@@ -14,18 +14,41 @@
 /*
  * We work really hard to calculate this accurately, while avoiding
  * an overflow.  "Is there a hyphen in anal-retentive?"  :-)
+ *
+ * -- "Yes there is, as in hair-splitting and nit-picking"
  */
 unsigned int e2p_percent(int percent, unsigned int base)
 {
-	unsigned int mask = ~((1 << (sizeof(unsigned int) - 1) * 8) - 1);
+	unsigned hi, lo, q, r;
 
-	if (100 % percent == 0)
-		return base / (100 / percent);
-	if (mask & base) 
-		return (base / 100) * percent;
-	return base * percent / 100;
+	/* 
+	 * in order to avoid overflow we write 'base' as:
+	 *
+	 *	base = hi * 2^16 + lo 
+	 * 
+	 * then we do all computations separately on 'hi' and 'lo'.
+	 * By using the definition of division:
+	 *
+	 *	precent * base = result * 100 + reminder
+	 *
+	 * (reminder < 100), we obtain the exact value of 'result' 
+	 * as follows:
+	 */
+#define BITS	16
+#define MASK	((1 << BITS) - 1)
+
+	hi = percent * (base >> BITS);
+	lo = percent * (base & MASK);
+
+	q = ((hi / 100) << BITS) + lo / 100;
+	r = ((hi % 100) << BITS) + lo % 100;
+		
+	return q + r / 100;
+#undef BITS
+#undef MASK
 }
 
+
 #ifdef DEBUG
 #include <unistd.h>
 #include <stdio.h>


                 reply	other threads:[~2006-09-20  8:56 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20060920085608.GA18351@openx1.frec.bull.fr \
    --to=alexandre.ratchov@bull.net \
    --cc=jean-pierre.dion@bull.net \
    --cc=linux-ext4@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox