From: David Laight <David.Laight@ACULAB.COM>
To: 'Yury Norov' <yury.norov@gmail.com>,
Andy Shevchenko <andy.shevchenko@gmail.com>
Cc: Linus Torvalds <torvalds@linux-foundation.org>,
Linux Kernel Mailing List <linux-kernel@vger.kernel.org>,
Guenter Roeck <linux@roeck-us.net>,
"Dennis Zhou" <dennis@kernel.org>,
Russell King <linux@armlinux.org.uk>,
"Catalin Marinas" <catalin.marinas@arm.com>,
Andy Shevchenko <andriy.shevchenko@linux.intel.com>,
Rasmus Villemoes <linux@rasmusvillemoes.dk>,
Alexey Klimov <aklimov@redhat.com>,
Kees Cook <keescook@chromium.org>,
Andy Whitcroft <apw@canonical.com>
Subject: RE: [PATCH v2 1/3] lib/find_bit: introduce FIND_FIRST_BIT() macro
Date: Wed, 24 Aug 2022 14:18:40 +0000 [thread overview]
Message-ID: <eed65809f99d49acbbbf3ba3ce6568a0@AcuMS.aculab.com> (raw)
In-Reply-To: <YwYlWjlWO3fFrtQp@yury-laptop>
...
> And generated code looks almost the same, except that
> on x86_64 your version is bigger. Compare before:
> 0000000000000000 <_find_first_bit>:
> 0: mov %rsi,%rax
> 3: test %rsi,%rsi
> 6: je 35 <_find_first_bit+0x35>
> 8: xor %edx,%edx
> a: jmp 19 <_find_first_bit+0x19>
> c: add $0x40,%rdx // Track bits and
> 10: add $0x8,%rdi // index separately
That add is free - happens in parallel with other instrutcions
> 14: cmp %rax,%rdx
> 17: jae 35 <_find_first_bit+0x35>
The instructions below will (probably/hopefully) be
speculatively executed in parallel with the cmp/jae above
> 19: mov (%rdi),%rcx
> 1c: test %rcx,%rcx
> 1f: je c <_find_first_bit+0xc>
> 21: tzcnt %rcx,%rcx
> 26: add %rdx,%rcx
> 29: cmp %rcx,%rax
> 2c: cmova %rcx,%rax
> 30: jmp 35 <_find_first_bit+0x35>
> 35: jmp 3a <_find_first_bit+0x3a>
> 3a: nopw 0x0(%rax,%rax,1)
>
> And after:
> 0000000000000000 <_find_first_bit>:
> 0: mov %rsi,%rax
> 3: test %rsi,%rsi
> 6: je 39 <_find_first_bit+0x39>
> 8: xor %edx,%edx
> a: jmp 15 <_find_first_bit+0x15>
> c: add $0x40,%rdx // Track bits only
> 10: cmp %rdx,%rax
> 13: jbe 39 <_find_first_bit+0x39>
> 15: mov %rdx,%rcx
> 18: shr $0x6,%rcx // But divide here
> 1c: mov (%rdi,%rcx,8),%rcx
> 20: test %rcx,%rcx
That is a long register dependency chain involving %cx.
It will limit the execution speed to (at least 6) clocks/iteration.
The older version might be 3 clocks/iteration.
So this could easily run at half the speed.
David
> 23: je c <_find_first_bit+0xc>
> 25: tzcnt %rcx,%rcx
> 2a: add %rcx,%rdx
> 2d: cmp %rdx,%rax
> 30: cmova %rdx,%rax
> 34: jmp 39 <_find_first_bit+0x39>
> 39: jmp 3e <_find_first_bit+0x3e>
> 3e: xchg %ax,%ax // Which adds 4 bytes to .text
>
> Thanks,
> Yury
>
> > > + val = (EXPRESSION); \
> > > + if (val) { \
> > > + sz = min(idx * BITS_PER_LONG + __ffs(word_op(val)), sz);\
> >
> > sz = min(idx + __ffs(...));
> >
> > > + break; \
> > > + } \
> > > + } \
> > > + \
> > > + sz; \
> > > +})
> >
> >
> > --
> > With Best Regards,
> > Andy Shevchenko
-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)
next prev parent reply other threads:[~2022-08-24 14:18 UTC|newest]
Thread overview: 23+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-08-24 1:26 [PATCH v2 0/4] lib: optimize find_bit() functions Yury Norov
2022-08-24 1:26 ` [PATCH v2 1/3] lib/find_bit: introduce FIND_FIRST_BIT() macro Yury Norov
2022-08-24 9:10 ` Andy Shevchenko
2022-08-24 13:19 ` Yury Norov
2022-08-24 14:18 ` David Laight [this message]
2022-08-24 17:45 ` Andy Shevchenko
2022-08-24 17:59 ` Linus Torvalds
2022-08-24 1:26 ` [PATCH v2 2/3] lib/find_bit: create find_first_zero_bit_le() Yury Norov
2022-08-24 9:22 ` Andy Shevchenko
2022-08-24 9:24 ` Andy Shevchenko
2022-08-24 13:37 ` Yury Norov
2022-08-24 17:50 ` Andy Shevchenko
2022-08-24 17:58 ` Russell King (Oracle)
2022-08-24 20:03 ` Yury Norov
2022-08-24 18:11 ` Linus Torvalds
2022-08-24 22:09 ` Yury Norov
2022-08-24 1:26 ` [PATCH v2 3/3] lib/find_bit: optimize find_next_bit() functions Yury Norov
2022-08-24 9:19 ` Andy Shevchenko
2022-08-24 13:53 ` Yury Norov
2022-08-24 17:54 ` Andy Shevchenko
2022-08-24 17:56 ` Andy Shevchenko
2022-08-24 21:27 ` Yury Norov
2022-08-24 9:00 ` [PATCH v2 0/4] lib: optimize find_bit() functions Andy Shevchenko
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=eed65809f99d49acbbbf3ba3ce6568a0@AcuMS.aculab.com \
--to=david.laight@aculab.com \
--cc=aklimov@redhat.com \
--cc=andriy.shevchenko@linux.intel.com \
--cc=andy.shevchenko@gmail.com \
--cc=apw@canonical.com \
--cc=catalin.marinas@arm.com \
--cc=dennis@kernel.org \
--cc=keescook@chromium.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux@armlinux.org.uk \
--cc=linux@rasmusvillemoes.dk \
--cc=linux@roeck-us.net \
--cc=torvalds@linux-foundation.org \
--cc=yury.norov@gmail.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.