public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Andrew Morton <akpm@linux-foundation.org>
To: Eric Paris <eparis@redhat.com>
Cc: Randy Dunlap <randy.dunlap@oracle.com>,
	linux-kernel@vger.kernel.org, selinux@tycho.nsa.gov,
	sds@tycho.nsa.gov, jmorris@namei.org
Subject: Re: [PATCH] kernel: rounddown helper function
Date: Thu, 5 Aug 2010 12:03:12 -0700	[thread overview]
Message-ID: <20100805120312.e166a041.akpm@linux-foundation.org> (raw)
In-Reply-To: <1281030996.2604.17.camel@dhcp231-200.rdu.redhat.com>

On Thu, 05 Aug 2010 13:56:36 -0400
Eric Paris <eparis@redhat.com> wrote:

> > > I'm more used to seeing it like
> > > 
> > > #define DIV_ROUND_DOWN(n, d)	(((n) / (d)) * (d))
> > > 
> > > but since multiply/divide/modulus are usually slower, your (SELinux) way is better,
> > > I suppose.
> > > 
> > > and the usual caveats apply:  don't use these macros with expressions (nor with y
> > > or d == 0).
> > 
> > Yes, it really shouldn't reference its argument twice.  And that's easy
> > to fix.
> 
> Are you suggesting something like
> 
> #define rounddown(n, d) ({ typeof(n) __n = (n); __n - (__n % (d)); })

looks good.

> If that's what you are hoping for, would you also like to see a patch
> doing the same thing for roundup() ?

Sure.  I doubt if anything accidentally depends on the curent behavior,
although that would be amusing.

> > A fancy version would detect constant-power-of-two and do an `& (d - 1)'
> > instead of the modulus.  But probably the compiler does optimisatons in
> > that case - for unsigned types, at least.
> 
> I don't think we really need to.  My quick test shows:
> 
> #define rounddown(n, d) ({typeof((n)) __n = (n); (__n - (__n % (d)));})
> 
> int round7(unsigned int a)
> {
> 	return rounddown(a, 7);
> }
> 
> int round4(unsigned int a)
> {
> 	return rounddown(a, 4);
> }
> 
> 0000000000400504 <round7>:
>   400504:       b9 07 00 00 00          mov    $0x7,%ecx
>   400509:       89 f8                   mov    %edi,%eax
>   40050b:       31 d2                   xor    %edx,%edx
>   40050d:       f7 f1                   div    %ecx
>   40050f:       89 f8                   mov    %edi,%eax
>   400511:       29 d0                   sub    %edx,%eax
>   400513:       c3                      retq
> 
> 0000000000400514 <round4>:
>   400514:       89 f8                   mov    %edi,%eax
>   400516:       83 e0 fc                and    $0xfffffffffffffffc,%eax
>   400519:       c3                      retq
> 

OK, thanks for checking.

      reply	other threads:[~2010-08-05 19:04 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-08-03 18:16 [PATCH] kernel: rounddown helper function Eric Paris
2010-08-03 18:23 ` Randy Dunlap
2010-08-04 21:35   ` Andrew Morton
2010-08-05 17:56     ` Eric Paris
2010-08-05 19:03       ` Andrew Morton [this message]

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=20100805120312.e166a041.akpm@linux-foundation.org \
    --to=akpm@linux-foundation.org \
    --cc=eparis@redhat.com \
    --cc=jmorris@namei.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=randy.dunlap@oracle.com \
    --cc=sds@tycho.nsa.gov \
    --cc=selinux@tycho.nsa.gov \
    /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