All of lore.kernel.org
 help / color / mirror / Atom feed
From: "H. Peter Anvin" <hpa@zytor.com>
To: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Ingo Molnar <mingo@elte.hu>,
	linux-kernel@vger.kernel.org,
	Borislav Petkov <petkovbb@googlemail.com>,
	Peter Zijlstra <a.p.zijlstra@chello.nl>,
	Thomas Gleixner <tglx@linutronix.de>,
	Andrew Morton <akpm@linux-foundation.org>
Subject: Re: [GIT PULL] core/hweight changes for v2.6.35
Date: Mon, 17 May 2010 15:06:22 -0700	[thread overview]
Message-ID: <4BF1BDDE.3060305@zytor.com> (raw)
In-Reply-To: <alpine.LFD.2.00.1005171443060.4195@i5.linux-foundation.org>

On 05/17/2010 02:46 PM, Linus Torvalds wrote:
> 
> 
> On Mon, 17 May 2010, Ingo Molnar wrote:
>>
>> +#ifdef CONFIG_64BIT
>> +/* popcnt %rdi, %rax */
>> +#define POPCNT ".byte 0xf3,0x48,0x0f,0xb8,0xc7"
>> +#define REG_IN "D"
>> +#define REG_OUT "a"
> ...
>> +/*
>> + * __sw_hweightXX are called from within the alternatives below
>> + * and callee-clobbered registers need to be taken care of. See
>> + * ARCH_HWEIGHT_CFLAGS in <arch/x86/Kconfig> for the respective
>> + * compiler switches.
>> + */
>> +static inline unsigned int __arch_hweight32(unsigned int w)
>> +{
>> +	unsigned int res = 0;
>> +
>> +	asm (ALTERNATIVE("call __sw_hweight32", POPCNT, X86_FEATURE_POPCNT)
>> +		     : "="REG_OUT (res)
>> +		     : REG_IN (w));
>> +
>> +	return res;
>> +}
> 
> I do not believe this is correct.
> 
> On x86-64, you are using a 64-bit instruction, but
> 
> 	REG_IN (w)
> 
> does _not_ guarantee that the register is zero in the high bits.
> 
> Yes, yes, in practice it _probably_ is, because the register almost 
> certainly got loaded with some kind of zero-extending mov instruction. But 
> as far as I can tell, the code is buggy. You're telling gcc that you are 
> using a 32-bit register, but you're actually counting bits in the full 64 
> bits.
> 
> Am I missing something?
> 

No, you're absolutely right; the input can be any value including
sign-extended.  The best fix for this is probably to use a 32-bit
instruction in this case -- I will check in such a patch and update the
pull branch.  If that's okay with you we'll send you a new pull request
in a bit.

The embarrassing part is I think I spotted this problem at one point,
but forgot about it in dealing with a conflict between this and another
patchset.

	-hpa


  reply	other threads:[~2010-05-17 22:07 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-05-17 21:21 [GIT PULL] core/hweight changes for v2.6.35 Ingo Molnar
2010-05-17 21:46 ` Linus Torvalds
2010-05-17 22:06   ` H. Peter Anvin [this message]
2010-05-17 22:57   ` [tip:core/hweight] x86, hweight: Use a 32-bit popcnt for __arch_hweight32() tip-bot for H. Peter Anvin
2010-05-17 23:04   ` [GIT PULL] UPDATED - core/hweight changes for v2.6.35 H. Peter Anvin
2010-08-13 21:42 ` [GIT PULL] " Mike Frysinger
2010-08-13 21:56   ` H. Peter Anvin
2010-08-13 21:59     ` Mike Frysinger
2010-08-14  0:11       ` H. Peter Anvin
2010-08-14 13:44         ` Borislav Petkov
2010-08-14  0:32   ` [PATCH] arch/tile: Rename the hweight() implementations to __arch_hweight() Chris Metcalf
2010-08-14  1:02     ` Mike Frysinger

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=4BF1BDDE.3060305@zytor.com \
    --to=hpa@zytor.com \
    --cc=a.p.zijlstra@chello.nl \
    --cc=akpm@linux-foundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@elte.hu \
    --cc=petkovbb@googlemail.com \
    --cc=tglx@linutronix.de \
    --cc=torvalds@linux-foundation.org \
    /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.