Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752561AbcD1HV0 (ORCPT ); Thu, 28 Apr 2016 03:21:26 -0400 Received: from bombadil.infradead.org ([198.137.202.9]:37212 "EHLO bombadil.infradead.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750845AbcD1HVZ (ORCPT ); Thu, 28 Apr 2016 03:21:25 -0400 Date: Thu, 28 Apr 2016 09:21:18 +0200 From: Peter Zijlstra To: George Spelvin 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 Message-ID: <20160428072118.GR3430@twins.programming.kicks-ass.net> References: <1461744350-24156-1-git-send-email-zengzhaoxiu@163.com> <20160427162033.11271.qmail@ns.horizon.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20160427162033.11271.qmail@ns.horizon.com> User-Agent: Mutt/1.5.21 (2012-12-30) Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 1177 Lines: 58 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