linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Linus Torvalds <torvalds@osdl.org>
To: Andries.Brouwer@cwi.nl
Cc: linux-kernel@vger.kernel.org
Subject: Re: do_div considered harmful
Date: Sun, 3 Aug 2003 22:54:27 -0700 (PDT)	[thread overview]
Message-ID: <Pine.LNX.4.44.0308032249300.11175-100000@home.osdl.org> (raw)
In-Reply-To: <UTC200308040203.h7423rv14876.aeb@smtp.cwi.nl>


On Mon, 4 Aug 2003 Andries.Brouwer@cwi.nl wrote:
>
> Now that I compare, he wrote
> 	nativeMb = do_div(nativeMb, 1000000);
> to divide nativeMb by 1000000.
> 
> So, it seems natural to expect that do_div() gives the quotient.
> But it gives the remainder.
> (Strange, Erik showed correct output.)

Actually, the above is "undefined behaviour", since it has _two_ 
assignments in the same thing. Exactly because "do_div()" modifies both 
the first argument _and_ returns a value. So depending on the 
implementation of do_div() (whether there are any sequence points etc) and 
on random compiler behaviour (if there are no sequence points in do_div() 
internally), in the example above "nativeMb" migth be totally undefined 
after the above.

And yes, as a special case, it might be the divisor.

I agree that "do_div()" has strange semantics and is very likely misnamed,
but they are kind of forced upon us by the fact that C functions cannot
return two values. Renaming do_div() at this point is just going to make
it harder to write kernel- portable source, so I suspect we're better off
just commenting it, and making people aware of how do_div() works.

Not very many people should use "do_div()" directly (and a quick grep
shows that not very many people do). It's generally a mistake to do so, I
suspect. The thing was originally written explicitly for "printk()" and
nothing else.

		Linus


  parent reply	other threads:[~2003-08-04  5:54 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
2003-08-05  2:58   ` i_blksize Andries Brouwer
2003-08-05  6:10     ` i_blksize Andrew Morton
2003-08-04  5:54 ` Linus Torvalds [this message]
2003-08-04  5:31 do_div considered harmful 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=Pine.LNX.4.44.0308032249300.11175-100000@home.osdl.org \
    --to=torvalds@osdl.org \
    --cc=Andries.Brouwer@cwi.nl \
    --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).