All of lore.kernel.org
 help / color / mirror / Atom feed
From: Thomas Rohwer <tr@ohwer.de>
To: Andi Kleen <andi@firstfloor.org>, akpm@linux-foundation.org
Cc: linux-kernel@vger.kernel.org, davidlohr.bueso@hp.com,
	rafael.j.wysocki@intel.com, lenb@kernel.org,
	Andi Kleen <ak@linux.intel.com>
Subject: Re: [PATCH] Optimize int_sqrt for small values for faster idle
Date: Sun, 31 Jan 2016 15:27:48 +0800	[thread overview]
Message-ID: <56ADB774.8080708@gmail.com> (raw)
In-Reply-To: <1454017365-8509-1-git-send-email-andi@firstfloor.org>

Hello,

 > -	m = 1UL << (BITS_PER_LONG - 2);
 > +	if (x <= 0xffff) {
 > +		if (m <= 0xff)
 > +			m = 1UL << (8 - 2);
 > +		else
 > +			m = 1UL << (16 - 2);
 > +	} else if (x <= 0xffffffff)
 > +		m = 1UL << (32 - 2);
 > +	else
 > +		m = 1UL << (BITS_PER_LONG - 2);
 >   	while (m != 0) {
 >   		b = y + m;
 >   		y >>= 1;
 >


I think, m can be initialized with

1 << (greatest multiple of 2 less than or equal to (position of most significant bit of x))

i.e. 1 << ((position of most significant bit of x) & 62)

without changing the outcome of the original algorithm (as long as x<m the loop does just m >>= 2).

I believe, that for (position of most significant bit of x) there is an efficient macro, and
some processors directly have an instruction for it. So this would probably be faster than your suggestion
for an initial starting value and give an even better starting value (cutting in some cases further on the number of
while loop interations).

If one just wants to achieve a result with a certain relative error in terms of the fraction of the input, one can
probably only look at the most significant bit and a few following bits of x.

Sincerely,

Thomas

  parent reply	other threads:[~2016-01-31  7:27 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-01-28 21:42 [PATCH] Optimize int_sqrt for small values for faster idle Andi Kleen
2016-01-28 22:04 ` kbuild test robot
2016-01-28 22:11 ` kbuild test robot
2016-01-28 22:15 ` Joe Perches
2016-01-28 22:40   ` Andi Kleen
2016-01-28 22:22 ` Joe Perches
2016-01-28 22:29 ` Eric Dumazet
2016-01-28 22:30 ` Andi Kleen
2016-01-29  3:59 ` Rafael J. Wysocki
2016-01-31  7:27 ` Thomas Rohwer [this message]
2016-02-01 21:25 ` Rasmus Villemoes
2016-02-01 21:36   ` Andi Kleen
2016-02-01 23:08     ` Rasmus Villemoes
2016-02-02  0:00       ` Andi Kleen
2016-02-02  0:36       ` Eric Dumazet
2016-02-02 20:46         ` Rasmus Villemoes
2016-02-02 21:30           ` Eric Dumazet
2017-07-20 10:10         ` Peter Zijlstra
2017-07-24 13:28           ` Eric Dumazet
2016-02-07 21:32     ` Rasmus Villemoes
2016-02-09 20:44       ` Andi Kleen
2016-02-10 13:31         ` Fengguang Wu

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=56ADB774.8080708@gmail.com \
    --to=tr@ohwer.de \
    --cc=ak@linux.intel.com \
    --cc=akpm@linux-foundation.org \
    --cc=andi@firstfloor.org \
    --cc=davidlohr.bueso@hp.com \
    --cc=lenb@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=rafael.j.wysocki@intel.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.