内在的memcmp

Jus*_*tin 6 memory gcc intrinsics

根据gcc文档,memcmp不是GCC的内在功能.如果你想在gcc下加速glibc的memcmp,你需要使用文档中定义的较低级别的内在函数.然而,当在互联网上搜索时,似乎很多人都认为memcmp是一个内置函数.是针对某些编译器而不是针对其他编译器的?

Jus*_* L. 8

请注意,repz cmpsb例程可能不会比glibc的memcmp快.事实上,在我的测试中,即使比较几个字节,它也永远不会更快.

请参阅http://gcc.gnu.org/bugzilla/show_bug.cgi?id=43052


Lan*_*son 5

您的链接似乎是针对x86体系结构特定的内置函数,根据 memcmp实现为gcc内置的独立于体系结构.

编辑:

使用Cygwin gcc版本3.3.1为i686编译以下代码,-O2:

#include <stdlib.h>

struct foo {
    int a;
    int b;
} ;

int func(struct foo *x, struct foo *y)
{
    return memcmp(x, y, sizeof (struct foo));
}
Run Code Online (Sandbox Code Playgroud)

生成以下输出(请注意,对memcmp()的调用将转换为8字节"repz cmpsb"):

   0:   55                      push   %ebp
   1:   b9 08 00 00 00          mov    $0x8,%ecx
   6:   89 e5                   mov    %esp,%ebp
   8:   fc                      cld    
   9:   83 ec 08                sub    $0x8,%esp
   c:   89 34 24                mov    %esi,(%esp)
   f:   8b 75 08                mov    0x8(%ebp),%esi
  12:   89 7c 24 04             mov    %edi,0x4(%esp)
  16:   8b 7d 0c                mov    0xc(%ebp),%edi
  19:   f3 a6                   repz cmpsb %es:(%edi),%ds:(%esi)
  1b:   0f 92 c0                setb   %al
  1e:   8b 34 24                mov    (%esp),%esi
  21:   8b 7c 24 04             mov    0x4(%esp),%edi
  25:   0f 97 c2                seta   %dl
  28:   89 ec                   mov    %ebp,%esp
  2a:   5d                      pop    %ebp
  2b:   28 c2                   sub    %al,%dl
  2d:   0f be c2                movsbl %dl,%eax
  30:   c3                      ret    
  31:   90                      nop    
Run Code Online (Sandbox Code Playgroud)

  • 为什么不是一个2字的`repz cmpsl`?或者更好的是,只需`if(x-> a == y-> a && x-> b == y-> b)`?gcc糟透了...... (3认同)