Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.kernel > #1216092

Re: [PATCH 1/2] x86/bitops: implement __test_bit

From Ingo Molnar <mingo@kernel.org>
Newsgroups linux.kernel
Subject Re: [PATCH 1/2] x86/bitops: implement __test_bit
Date 2015-08-31 10:30 +0200
Message-ID <q3sDw-2eo-17@gated-at.bofh.it> (permalink)
References (1 earlier) <q3qs1-7CJ-9@gated-at.bofh.it> <q3qBI-7NY-11@gated-at.bofh.it> <q3sau-1r5-9@gated-at.bofh.it> <q3sau-1r5-17@gated-at.bofh.it> <q3stQ-235-23@gated-at.bofh.it>
Organization linux.* mail to news gateway

Show all headers | View raw


* yalin wang <yalin.wang2010@gmail.com> wrote:

> 
> > On Aug 31, 2015, at 15:59, Ingo Molnar <mingo@kernel.org> wrote:
> > 
> > 
> > * Michael S. Tsirkin <mst@redhat.com> 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 <mingo@kernel.org> wrote:
> >>>> 
> >>>> * Michael S. Tsirkin <mst@redhat.com> 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 ?

It's an artifact of testing it in isolation.

For constant bit positions GCC is able to do a fairly good job:

ffffffff8103d2a0 <vmx_get_rflags>:
ffffffff8103d2a0:       f6 87 4a 02 00 00 08    testb  $0x8,0x24a(%rdi)
...
ffffffff8103d2ab:       75 39                   jne    ffffffff8103d2e6 <vmx_get_rflags+0x46>


with just 2 instructions: a TESTB plus using the flag result in a JNE.

Using variable_test_bit() forces the result into a register, which is suboptimal.

Thanks,

	Ingo
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/

Back to linux.kernel | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

[PATCH 1/2] x86/bitops: implement __test_bit "Michael S. Tsirkin" <mst@redhat.com> - 2015-08-30 10:40 +0200
  [PATCH 2/2] kvm/x86: use __test_bit "Michael S. Tsirkin" <mst@redhat.com> - 2015-08-30 10:40 +0200
  Re: [PATCH 1/2] x86/bitops: implement __test_bit Ingo Molnar <mingo@kernel.org> - 2015-08-31 08:10 +0200
    Re: [PATCH 1/2] x86/bitops: implement __test_bit "H. Peter Anvin" <hpa@zytor.com> - 2015-08-31 08:20 +0200
      Re: [PATCH 1/2] x86/bitops: implement __test_bit Ingo Molnar <mingo@kernel.org> - 2015-08-31 10:00 +0200
        Re: [PATCH 1/2] x86/bitops: implement __test_bit Ingo Molnar <mingo@kernel.org> - 2015-08-31 10:20 +0200
        Re: [PATCH 1/2] x86/bitops: implement __test_bit yalin wang <yalin.wang2010@gmail.com> - 2015-08-31 10:20 +0200
          Re: [PATCH 1/2] x86/bitops: implement __test_bit Ingo Molnar <mingo@kernel.org> - 2015-08-31 10:30 +0200
        Re: [PATCH 1/2] x86/bitops: implement __test_bit "Michael S. Tsirkin" <mst@redhat.com> - 2015-08-31 13:20 +0200
          Re: [PATCH 1/2] x86/bitops: implement __test_bit Ingo Molnar <mingo@kernel.org> - 2015-09-01 11:30 +0200
            Re: [PATCH 1/2] x86/bitops: implement __test_bit "Michael S. Tsirkin" <mst@redhat.com> - 2015-09-01 11:50 +0200
              Re: [PATCH 1/2] x86/bitops: implement __test_bit Ingo Molnar <mingo@kernel.org> - 2015-09-01 13:40 +0200
                Re: [PATCH 1/2] x86/bitops: implement __test_bit "Michael S. Tsirkin" <mst@redhat.com> - 2015-09-01 17:10 +0200
                Re: [PATCH 1/2] x86/bitops: implement __test_bit "H. Peter Anvin" <hpa@zytor.com> - 2015-09-02 01:50 +0200
      Re: [PATCH 1/2] x86/bitops: implement __test_bit "Michael S. Tsirkin" <mst@redhat.com> - 2015-08-31 10:00 +0200

csiph-web