> -----邮件原件-----
> 发件人: Florian Westphal [mailto:[email protected]]
> 发送时间: 2019年2月25日 19:27
> 收件人: Li,Rongqing <[email protected]>
> 抄送: [email protected]
> 主题: Re: [PATCH][nf-next] netfilter: replace modulo operation with bitwise
> AND
> 
> Li RongQing <[email protected]> wrote:
> > CONNTRACK_LOCKS is 1024 and power of 2, so modulo operations can be
> > replaced with AND (CONNTRACK_LOCKS - 1)
> >
> > and bitwise AND operation is quicker than module operation
> 
> Uh.  What kind of compiler doesn't figure that out?!
> 
> I would prefer to keep it as-is and let compiler do the optimization.


gcc version 7.3.0 (GCC)


main()
{
    int i=1000000000;

         i= i % 1024;

        return i;
}

00000000004004a7 <main>:
  4004a7:       55                      push   %rbp
  4004a8:       48 89 e5                mov    %rsp,%rbp
  4004ab:       c7 45 fc 00 ca 9a 3b    movl   $0x3b9aca00,-0x4(%rbp)
  4004b2:       8b 45 fc                mov    -0x4(%rbp),%eax
  4004b5:       99                      cltd
  4004b6:       c1 ea 16                shr    $0x16,%edx
  4004b9:       01 d0                   add    %edx,%eax
  4004bb:       25 ff 03 00 00          and    $0x3ff,%eax
  4004c0:       29 d0                   sub    %edx,%eax
  4004c2:       89 45 fc                mov    %eax,-0x4(%rbp)
  4004c5:       8b 45 fc                mov    -0x4(%rbp),%eax
  4004c8:       5d                      pop    %rbp
  4004c9:       c3                      retq
  4004ca:       66 0f 1f 44 00 00       nopw   0x0(%rax,%rax,1)

=================================
main()
{
    int i=1000000000;

         i= i & (1024-1);

        return i;
}

00000000004004a7 <main>:
  4004a7:       55                      push   %rbp
  4004a8:       48 89 e5                mov    %rsp,%rbp
  4004ab:       c7 45 fc 00 ca 9a 3b    movl   $0x3b9aca00,-0x4(%rbp)
  4004b2:       81 65 fc ff 03 00 00    andl   $0x3ff,-0x4(%rbp)
  4004b9:       8b 45 fc                mov    -0x4(%rbp),%eax
  4004bc:       5d                      pop    %rbp
  4004bd:       c3                      retq
  4004be:       66 90                   xchg   %ax,%ax


Similar patch:

commit 1a1d74d378b13ad3f93e8975a0ade0980a49d28b
Author: Jakub Kicinski <[email protected]>
Date:   Mon Oct 31 20:43:17 2016 +0000

    nfp: use AND instead of modulo to get ring indexes


thanks

-RongQing

Reply via email to