From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752489AbbHaIPO (ORCPT ); Mon, 31 Aug 2015 04:15:14 -0400 Received: from mail-pa0-f43.google.com ([209.85.220.43]:33442 "EHLO mail-pa0-f43.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750922AbbHaIPM convert rfc822-to-8bit (ORCPT ); Mon, 31 Aug 2015 04:15:12 -0400 Content-Type: text/plain; charset=us-ascii Mime-Version: 1.0 (Mac OS X Mail 8.2 \(2104\)) Subject: Re: [PATCH 1/2] x86/bitops: implement __test_bit From: yalin wang In-Reply-To: <20150831075947.GA9974@gmail.com> Date: Mon, 31 Aug 2015 16:15:02 +0800 Cc: "Michael S. Tsirkin" , "H. Peter Anvin" , lkml , Thomas Gleixner , Ingo Molnar , x86@kernel.org, Rusty Russell , Linus Torvalds Content-Transfer-Encoding: 8BIT Message-Id: <663BA998-C2EF-4FEA-964A-72BA2521E62D@gmail.com> References: <1440776707-22016-1-git-send-email-mst@redhat.com> <20150831060549.GB7093@gmail.com> <0779C35A-141F-4019-942A-CD3F861048A3@zytor.com> <20150831105355-mutt-send-email-mst@redhat.com> <20150831075947.GA9974@gmail.com> To: Ingo Molnar X-Mailer: Apple Mail (2.2104) Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org > On Aug 31, 2015, at 15:59, Ingo Molnar wrote: > > > * Michael S. Tsirkin wrote: > >> On Sun, Aug 30, 2015 at 11:13:20PM -0700, H. Peter Anvin wrote: >>> Presumably because gcc can't generate bt... whether or not it is worth it is another matter. >>> >>> On August 30, 2015 11:05:49 PM PDT, Ingo Molnar wrote: >>>> >>>> * Michael S. Tsirkin wrote: >>>> >>>>> +static __always_inline int __constant_test_bit(long nr, const >>>> unsigned long *addr) >>>>> +{ >>>>> + return ((1UL << (nr & (BITS_PER_LONG-1))) & >>>>> + (addr[nr >> _BITOPS_LONG_SHIFT])) != 0; >>>>> +} >>>>> + >>>>> +static inline int __variable_test_bit(long nr, const unsigned long >>>> *addr) >>>>> +{ >>>>> + int oldbit; >>>>> + >>>>> + asm volatile("bt %2,%1\n\t" >>>>> + "sbb %0,%0" >>>>> + : "=r" (oldbit) >>>>> + : "m" (*addr), "Ir" (nr)); >>>>> + >>>>> + return oldbit; >>>>> +} >>>> >>>> Color me confused, why use assembly for this at all? >>>> >>>> Why not just use C for testing the bit (i.e. turn __constant_test_bit() >>>> into >>>> __test_bit()) - that would also allow the compiler to propagate the >>>> result, >>>> potentially more optimally than we can do it via SBB... >>>> >>>> Thanks, >>>> >>>> Ingo >> >> Exactly: >> >> >> Disassembly of section .text: >> >> 00000000 <__variable_test_bit>: >> __variable_test_bit(): >> 0: 8b 54 24 08 mov 0x8(%esp),%edx >> 4: 8b 44 24 04 mov 0x4(%esp),%eax >> 8: 0f a3 02 bt %eax,(%edx) >> b: 19 c0 sbb %eax,%eax >> d: c3 ret >> e: 66 90 xchg %ax,%ax >> >> 00000010 <__constant_test_bit>: >> __constant_test_bit(): >> 10: 8b 4c 24 04 mov 0x4(%esp),%ecx >> 14: 8b 44 24 08 mov 0x8(%esp),%eax >> 18: 89 ca mov %ecx,%edx >> 1a: c1 fa 04 sar $0x4,%edx >> 1d: 8b 04 90 mov (%eax,%edx,4),%eax >> 20: d3 e8 shr %cl,%eax >> 22: 83 e0 01 and $0x1,%eax >> 25: c3 ret > > But that's due to the forced interface of generating a return code. Please compare > it at an inlined usage site, where GCC is free to do the comparison directly and > use the result in flags. just curious : it seems __variable_test_bit() use less instructions, why not always use __variable_test_bit() , remove __constant_test_bit() version ? Thanks