Re: [PATCH] Optimize is_power_of_2().

2007-06-15 Thread H. Peter Anvin
Robert P. J. Day wrote: > On Fri, 15 Jun 2007, H. Peter Anvin wrote: > >> Vegard Nossum wrote: >>> From: Vegard Nossum <[EMAIL PROTECTED]> >>> Date: Fri, 15 Jun 2007 18:35:49 +0200 >>> Subject: [PATCH] Optimize is_power_of_2(). >>> >>&g

Re: [PATCH] Optimize is_power_of_2().

2007-06-15 Thread David M. Lloyd
On Fri, 15 Jun 2007 14:54:20 -0500 "David M. Lloyd" <[EMAIL PROTECTED]> wrote: > On Fri, 15 Jun 2007 21:47:50 +0200 (CEST) > Jan Engelhardt <[EMAIL PROTECTED]> wrote: > > > On Jun 15 2007 18:56, Vegard Nossum wrote: > > > bool is_power_of_2(unsigned long n) > > > { > > >- return (n != 0 && ((n &

Re: [PATCH] Optimize is_power_of_2().

2007-06-15 Thread David M. Lloyd
On Fri, 15 Jun 2007 21:47:50 +0200 (CEST) Jan Engelhardt <[EMAIL PROTECTED]> wrote: > On Jun 15 2007 18:56, Vegard Nossum wrote: > > bool is_power_of_2(unsigned long n) > > { > >-return (n != 0 && ((n & (n - 1)) == 0)); > >+return n * !(n & (n - 1)); > > } > > There is a third way which u

Re: [PATCH] Optimize is_power_of_2().

2007-06-15 Thread Jan Engelhardt
On Jun 15 2007 18:56, Vegard Nossum wrote: > bool is_power_of_2(unsigned long n) > { >- return (n != 0 && ((n & (n - 1)) == 0)); >+ return n * !(n & (n - 1)); > } There is a third way which uses neither * nor &&, but []: bool is_power_of_2(unsigned long n) { static const bool

Re: [PATCH] Optimize is_power_of_2().

2007-06-15 Thread Robert P. J. Day
On Fri, 15 Jun 2007, H. Peter Anvin wrote: > Vegard Nossum wrote: > > From: Vegard Nossum <[EMAIL PROTECTED]> > > Date: Fri, 15 Jun 2007 18:35:49 +0200 > > Subject: [PATCH] Optimize is_power_of_2(). > > > > Rationale: Removes one conditional branch and reduc

Re: [PATCH] Optimize is_power_of_2().

2007-06-15 Thread H. Peter Anvin
Vegard Nossum wrote: > From: Vegard Nossum <[EMAIL PROTECTED]> > Date: Fri, 15 Jun 2007 18:35:49 +0200 > Subject: [PATCH] Optimize is_power_of_2(). > > Rationale: Removes one conditional branch and reduces icache footprint. > Proof: If n is false, the product of n and any

[PATCH] Optimize is_power_of_2().

2007-06-15 Thread Vegard Nossum
From: Vegard Nossum <[EMAIL PROTECTED]> Date: Fri, 15 Jun 2007 18:35:49 +0200 Subject: [PATCH] Optimize is_power_of_2(). Rationale: Removes one conditional branch and reduces icache footprint. Proof: If n is false, the product of n and any value is false. If n is true, the truth of (n * x)