From: Andrew Morton <akpm@osdl.org>
To: Andries.Brouwer@cwi.nl
Cc: linux-kernel@vger.kernel.org, torvalds@osdl.org
Subject: Re: do_div considered harmful
Date: Sun, 3 Aug 2003 19:29:19 -0700 [thread overview]
Message-ID: <20030803192919.0d7d881c.akpm@osdl.org> (raw)
In-Reply-To: <UTC200308040203.h7423rv14876.aeb@smtp.cwi.nl>
Andries.Brouwer@cwi.nl wrote:
>
> Writing this ide capacity patch an hour ago or so
> I split off a helper sectors_to_MB() since Erik's recent
> patch uses this also.
> Now that I compare, he wrote
> nativeMb = do_div(nativeMb, 1000000);
> to divide nativeMb by 1000000.
> Similarly, I find in fs/cifs/inode.c
> inode->i_blocks = do_div(findData.NumOfBytes, inode->i_blksize);
This should be
int blocksize = 1 << inode->i_blkbits;
inode->i_blocks = (findData.NumOfBytes + blocksize - 1)
>> inode->i_blkbits;
and inode.i_blksize should probably be removed from the kernel.
> So, it seems natural to expect that do_div() gives the quotient.
> But it gives the remainder.
> (Strange, Erik showed correct output.)
>
> Since the semantics of this object are very unlike that of a C function,
> I wonder whether we should write DO_DIV instead, or DO_DIV_AND_REM
> to show that a remainder is returned.
Sometimes the slash-star operator comes in handy.
--- 25/include/asm-i386/div64.h~do_div-comment 2003-08-03 19:20:58.000000000 -0700
+++ 25-akpm/include/asm-i386/div64.h 2003-08-03 19:21:11.000000000 -0700
@@ -1,6 +1,16 @@
#ifndef __I386_DIV64
#define __I386_DIV64
+/*
+ * The semantics of do_div() are:
+ *
+ * uint32_t do_div(uint64_t *n, uint32_t base)
+ * {
+ * uint32_t remainder = *n % base;
+ * *n = *n / base;
+ * return remainder;
+ * }
+ */
#define do_div(n,base) ({ \
unsigned long __upper, __low, __high, __mod; \
asm("":"=a" (__low), "=d" (__high):"A" (n)); \
_
next prev parent reply other threads:[~2003-08-04 2:28 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2003-08-04 2:03 do_div considered harmful Andries.Brouwer
2003-08-04 2:29 ` Andrew Morton [this message]
2003-08-05 2:58 ` i_blksize Andries Brouwer
2003-08-05 6:10 ` i_blksize Andrew Morton
2003-08-04 5:54 ` do_div considered harmful Linus Torvalds
2003-08-04 5:31 Steve French
2003-08-04 9:46 Andries.Brouwer
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=20030803192919.0d7d881c.akpm@osdl.org \
--to=akpm@osdl.org \
--cc=Andries.Brouwer@cwi.nl \
--cc=linux-kernel@vger.kernel.org \
--cc=torvalds@osdl.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).