Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754241AbcDGDza (ORCPT ); Wed, 6 Apr 2016 23:55:30 -0400 Received: from mail-pf0-f195.google.com ([209.85.192.195]:34095 "EHLO mail-pf0-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750985AbcDGDz2 (ORCPT ); Wed, 6 Apr 2016 23:55:28 -0400 Subject: Re: [PATCH v2 10/30] Add x86-specific parity functions To: Borislav Petkov , zengzhaoxiu@163.com References: <57031D9D.801@gmail.com> <1459934085-7152-1-git-send-email-zengzhaoxiu@163.com> <20160406101300.GA13719@pd.tnic> Cc: tglx@linutronix.de, mingo@redhat.com, hpa@zytor.com, dvlasenk@redhat.com, akpm@linux-foundation.org, dvyukov@google.com, keescook@chromium.org, linux-kernel@vger.kernel.org From: Zeng Zhaoxiu Message-ID: <5705DA2A.70308@gmail.com> Date: Thu, 7 Apr 2016 11:55:22 +0800 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.6.0 MIME-Version: 1.0 In-Reply-To: <20160406101300.GA13719@pd.tnic> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 1356 Lines: 39 在 2016年04月06日 18:13, Borislav Petkov 写道: > On Wed, Apr 06, 2016 at 05:14:45PM +0800, zengzhaoxiu@163.com wrote: >> From: Zhaoxiu Zeng >> >> Use alternatives, lifted from arch_hweight >> >> Signed-off-by: Zhaoxiu Zeng >> --- >> arch/x86/include/asm/arch_hweight.h | 5 ++ >> arch/x86/include/asm/arch_parity.h | 102 ++++++++++++++++++++++++++++++++++++ >> arch/x86/include/asm/bitops.h | 4 +- >> arch/x86/lib/Makefile | 8 +++ >> arch/x86/lib/parity.c | 32 ++++++++++++ >> 5 files changed, 150 insertions(+), 1 deletion(-) >> create mode 100644 arch/x86/include/asm/arch_parity.h >> create mode 100644 arch/x86/lib/parity.c > ... > >> +static __always_inline unsigned int __arch_parity32(unsigned int w) >> +{ >> + unsigned int res; >> + >> + asm(ALTERNATIVE("call __sw_parity32", POPCNT32 "; and $1, %0", X86_FEATURE_POPCNT) >> + : "="REG_OUT (res) >> + : REG_IN (w) >> + : "cc"); > So why all that churn instead of simply doing: > > static __always_inline unsigned int __arch_parity32(unsigned int w) > { > return hweight32(w) & 1; > } > > Ditto for the 64-bit version. > __sw_parity32 is faster than __sw_hweight32. I don't know how many CPUs do not support the popc, if they are outdated, use __arch_hweight32 is the easiest way.