public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Peter Zijlstra <peterz@infradead.org>
To: George Spelvin <linux@horizon.com>
Cc: akpm@linux-foundation.org, zengzhaoxiu@163.com,
	linux-kernel@vger.kernel.org, zhaoxiu.zeng@gmail.com
Subject: Re: [patch V2] lib: GCD: add binary GCD algorithm
Date: Thu, 28 Apr 2016 09:21:18 +0200	[thread overview]
Message-ID: <20160428072118.GR3430@twins.programming.kicks-ass.net> (raw)
In-Reply-To: <20160427162033.11271.qmail@ns.horizon.com>

On Wed, Apr 27, 2016 at 12:20:33PM -0400, George Spelvin wrote:
> The frequent "if (USE_FFS)" conditions seem to clutter the code.
> Would it be easier to read if the two variants were just separated?
> The function is short enough that the duplication isn't too severe.

Yes please, this is _MUCH_ more readable.

> #if USE_FFS
> /* If __ffs is available, the even/odd algorithm benchmarks slower. */
> unsigned long gcd(unsigned long a, unsigned long b)
> {
> 	unsigned long r = a | b;
> 
> 	if (!a || !b)
> 		return r;
> 
> 	b >>= __ffs(b);
> 
> 	for (;;) {
> 		a >>= __ffs(a);
> 		if (a == b)
> 			return a << __ffs(r);
> 		if (a < b)
> 			swap(a, b);
> 		a -= b;
> 	}
> }
> 
> #else /* !USE_FFS */
> 
> /* If normalization is done by loops, the even/odd algorithm is a win. */
> unsigned long gcd(unsigned long a, unsigned long b)
> {
> 	unsigned long r = a | b;
> 
> 	if (!a || !b)
> 		return r;
> 
> 	r &= -r;	/* Isolate lsbit of r */
> 
> 	while (!(b & r))
> 		b >>= 1;
> 
> 	for (;;) {
> 		while (!(a & r))
> 			a >>= 1;
> 		if (a == b)
> 			return a;
> 		if (a < b)
> 			swap(a, b);
> 		a -= b;
> 		a >>= 1;
> 		if (a & r)
> 			a += b;
> 		a >>= 1;
> 	}
> }
> #endif

  reply	other threads:[~2016-04-28  7:21 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-04-27  8:05 [patch V2] lib: GCD: add binary GCD algorithm zengzhaoxiu
2016-04-27 16:20 ` George Spelvin
2016-04-28  7:21   ` Peter Zijlstra [this message]
2016-04-27 20:08 ` Andrew Morton

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=20160428072118.GR3430@twins.programming.kicks-ass.net \
    --to=peterz@infradead.org \
    --cc=akpm@linux-foundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux@horizon.com \
    --cc=zengzhaoxiu@163.com \
    --cc=zhaoxiu.zeng@gmail.com \
    /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