From: Randolph Chung <tausq@debian.org>
To: linux-kernel@vger.kernel.org
Subject: [patch] fix __div64_32 to do division properly
Date: Thu, 23 Oct 2003 21:20:03 -0700 [thread overview]
Message-ID: <20031024042002.GA24406@tausq.org> (raw)
The generic __div64_32 in lib/div64.c only handles "small" divisors. If
the divisor is full 32-bits, it returns invalid results. This patch
fixes it. (this is used e.g. in nanosleep)
thanks
randolph
Index: lib/div64.c
===================================================================
RCS file: /var/cvs/linux-2.6/lib/div64.c,v
retrieving revision 1.1
diff -u -p -r1.1 div64.c
--- lib/div64.c 29 Jul 2003 17:02:19 -0000 1.1
+++ lib/div64.c 24 Oct 2003 04:10:59 -0000
@@ -25,25 +25,27 @@
uint32_t __div64_32(uint64_t *n, uint32_t base)
{
- uint32_t low, low2, high, rem;
+ uint64_t rem = *n;
+ uint64_t b = base;
+ uint64_t res = 0, d = 1;
- low = *n & 0xffffffff;
- high = *n >> 32;
- rem = high % (uint32_t)base;
- high = high / (uint32_t)base;
- low2 = low >> 16;
- low2 += rem << 16;
- rem = low2 % (uint32_t)base;
- low2 = low2 / (uint32_t)base;
- low = low & 0xffff;
- low += rem << 16;
- rem = low % (uint32_t)base;
- low = low / (uint32_t)base;
+ if (b > 0) {
+ while (b < rem) {
+ b <<= 1;
+ d <<= 1;
+ }
+ }
- *n = low +
- ((uint64_t)low2 << 16) +
- ((uint64_t)high << 32);
+ do {
+ if (rem >= b) {
+ rem -= b;
+ res += d;
+ }
+ b >>= 1;
+ d >>= 1;
+ } while (d);
+ *n = res;
return rem;
}
--
Randolph Chung
Debian GNU/Linux Developer, hppa/ia64 ports
http://www.tausq.org/
next reply other threads:[~2003-10-24 4:16 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2003-10-24 4:20 Randolph Chung [this message]
[not found] <20031026152412.GK24406@tausq.org>
2003-10-26 18:01 ` [patch] fix __div64_32 to do division properly Linus Torvalds
2003-10-26 19:10 ` Randolph Chung
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=20031024042002.GA24406@tausq.org \
--to=tausq@debian.org \
--cc=linux-kernel@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;
as well as URLs for NNTP newsgroup(s).