GCC中strlen()的实现在哪里?

Chr*_*son 19 c glibc strlen

有人能指出我strlen()在海湾合作委员会的定义吗?我现在大约半小时一直在试用版本4.4.2(当谷歌疯狂时),我似乎无法找到strlen()实际实现的位置.

Mar*_*off 30

您应该查看glibc,而不是GCC - 它似乎是在strlen.c- 这里是glibc版本2.7的strlen.c的链接...这里是strlen.cglibc SVN存储库链接.

你应该看glibc而不是gcc的原因是:

GNU C库用作GNU系统中 C库,大多数系统使用Linux内核.

  • Ahem,即可移植版本,检查sysdeps目录中是否有实际进入程序的版本.也就是说,如果GCC没有首先到达并用内联版本替换该调用,那么OP之前可能会看到它.http://cvs.savannah.gnu.org/viewvc/libc/sysdeps/x86_64/strlen.S?revision=1.2.2.2&root=libc&view=markup (7认同)
  • 嗯,那不是很优化.至少使用Visual C++,我们得到了一个像样的汇编语言strlen. (3认同)
  • “GNU C 库主要设计为可移植的高性能 C 库。” 我猜他们可能会更加重视便携性部分。 (2认同)
  • C版本实际上是非常优化的(虽然手动循环展开是相当愚蠢的).即使使用asm,你也很难打败它. (2认同)

tec*_*rus 12

我意识到这个问题已经过了4年了,但是如果你不这样做,gcc通常会包含自己的strlen副本,#include <string.h>并且没有任何答案(包括接受的答案).如果您忘了,您会收到警告:

file_name:line_number: warning: incompatible implicit declaration of built-in function 'strlen'

并且gcc将内联其副本,在x86上是repnz scasb asm变体,除非你传递-Werror或-fno-builtin.与此相关的文件位于gcc/config/<platform>/<platform>.{c,md}

它也由gcc/builtins.c控制.如果您想知道strlen()是否以及如何针对常量进行优化,请参阅tree c_strlen(tree src, int only_value)此文件中定义的函数.它还控制strlen(以及其他)如何扩展和折叠(基于前面提到的配置/平台)


hyp*_*gic 7

这是bsd实现

size_t
strlen(const char *str)
{
        const char *s;

        for (s = str; *s; ++s)
                ;
        return (s - str);
}
Run Code Online (Sandbox Code Playgroud)

  • 仍在等待编译器可以从中生成可用的快速机器代码的那一天....目前它不到优化的*C*版本速度的一半. (12认同)

X S*_*ish 5

glibc/string/strlen.c 中定义

#include <string.h>
#include <stdlib.h>

#undef strlen

#ifndef STRLEN
# define STRLEN strlen
#endif

/* Return the length of the null-terminated string STR.  Scan for
   the null terminator quickly by testing four bytes at a time.  */
size_t
STRLEN (const char *str)
{
  const char *char_ptr;
  const unsigned long int *longword_ptr;
  unsigned long int longword, himagic, lomagic;

  /* Handle the first few characters by reading one character at a time.
     Do this until CHAR_PTR is aligned on a longword boundary.  */
  for (char_ptr = str; ((unsigned long int) char_ptr
            & (sizeof (longword) - 1)) != 0;
       ++char_ptr)
    if (*char_ptr == '\0')
      return char_ptr - str;

  /* All these elucidatory comments refer to 4-byte longwords,
     but the theory applies equally well to 8-byte longwords.  */

  longword_ptr = (unsigned long int *) char_ptr;

  /* Bits 31, 24, 16, and 8 of this number are zero.  Call these bits
     the "holes."  Note that there is a hole just to the left of
     each byte, with an extra at the end:

     bits:  01111110 11111110 11111110 11111111
     bytes: AAAAAAAA BBBBBBBB CCCCCCCC DDDDDDDD

     The 1-bits make sure that carries propagate to the next 0-bit.
     The 0-bits provide holes for carries to fall into.  */
  himagic = 0x80808080L;
  lomagic = 0x01010101L;
  if (sizeof (longword) > 4)
    {
      /* 64-bit version of the magic.  */
      /* Do the shift in two steps to avoid a warning if long has 32 bits.  */
      himagic = ((himagic << 16) << 16) | himagic;
      lomagic = ((lomagic << 16) << 16) | lomagic;
    }
  if (sizeof (longword) > 8)
    abort ();

  /* Instead of the traditional loop which tests each character,
     we will test a longword at a time.  The tricky part is testing
     if *any of the four* bytes in the longword in question are zero.  */
  for (;;)
    {
      longword = *longword_ptr++;

      if (((longword - lomagic) & ~longword & himagic) != 0)
    {
      /* Which of the bytes was the zero?  If none of them were, it was
         a misfire; continue the search.  */

      const char *cp = (const char *) (longword_ptr - 1);

      if (cp[0] == 0)
        return cp - str;
      if (cp[1] == 0)
        return cp - str + 1;
      if (cp[2] == 0)
        return cp - str + 2;
      if (cp[3] == 0)
        return cp - str + 3;
      if (sizeof (longword) > 4)
        {
          if (cp[4] == 0)
        return cp - str + 4;
          if (cp[5] == 0)
        return cp - str + 5;
          if (cp[6] == 0)
        return cp - str + 6;
          if (cp[7] == 0)
        return cp - str + 7;
        }
    }
    }
}
libc_hidden_builtin_def (strlen)
Run Code Online (Sandbox Code Playgroud)

  • 这并没有回答这个问题。OP 不是在寻找自定义 strlen 实现。 (2认同)
  • 这不是一个自定义的 strlen 实现,它是 glibc 中的一个:(事实上,它在其他一些答案中提到过)。https://sourceware.org/git/?p=glibc.git;a=blob;f=string/strlen.c;h=0b8aefb81281a7a020351864ab21ddbcfe0f3ebb;hb=HEAD (2认同)

Cir*_*四事件 5

glibc 2.26 有几个手动优化的汇编实现 strlen

截至glibc-2.26,快速:

git ls-files | grep strlen.S
Run Code Online (Sandbox Code Playgroud)

在 glibc 树中显示了针对所有主要架构和变体的十几种程序集手动优化实现。

特别是,仅 x86_64 就有 3 种变体:

sysdeps/x86_64/multiarch/strlen-avx2.S
sysdeps/x86_64/multiarch/strlen-sse2.S
sysdeps/x86_64/strlen.S
Run Code Online (Sandbox Code Playgroud)

确定使用哪个的一种快速而肮脏的方法是逐步调试测试程序:

#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>

int main(void) {
    size_t size = 0x80000000, i, result;
    char *s = malloc(size);
    for (i = 0; i < size; ++i)
        s[i] = 'a';
    s[size - 1] = '\0';
    result = strlen(s);
    assert(result == size - 1);
    return EXIT_SUCCESS;
}
Run Code Online (Sandbox Code Playgroud)

编译:

gcc -ggdb3 -std=c99 -O0 a.c
Run Code Online (Sandbox Code Playgroud)

立即行动:

disass main
Run Code Online (Sandbox Code Playgroud)

包含:

callq  0x555555554590 <strlen@plt>
Run Code Online (Sandbox Code Playgroud)

所以正在调用 libc 版本。

经过几个si指令级别的步骤后,GDB 达到:

__strlen_avx2 () at ../sysdeps/x86_64/multiarch/strlen-avx2.S:52                                         
52      ../sysdeps/x86_64/multiarch/strlen-avx2.S: No such file or directory.
Run Code Online (Sandbox Code Playgroud)

这告诉我strlen-avx2.S被使用了。

然后,我进一步确认:

disass __strlen_avx2
Run Code Online (Sandbox Code Playgroud)

并将反汇编与glibc源进行比较。

使用 AVX2 版本并不奇怪,因为我有一个i7-7820HQ CPU,发布日期为 2017 年第一季度并支持 AVX2,而AVX2是最先进的组件实现,发布日期为 2013 年第二季度,而SSE2则更多从 2004 年开始。

这就是 glibc 核心部分的很大一部分:它有很多经过架构优化的手写汇编代码。

在 Ubuntu 17.10、gcc 7.2.0、glibc 2.26 中测试。

-O3

TODO: with -O3, gcc 不使用 glibc's strlen,它只是生成内联程序集,在:https : //stackoverflow.com/a/19885891/895245 中提到

是因为它可以优化得更好吗?但是它的输出不包含AVX2指令,所以我觉得不是这样。

https://www.gnu.org/software/gcc/projects/optimize.html提到:

GCC优化器的不足

glibc 具有各种字符串函数的内联汇编版本;GCC 在相同的架构上有一些,但不一定相同。可以为更多函数提供额外的 optab 条目,例如 ffs 和 strlen 的条目,包括 memset、strchr、strcpy 和 strrchr。

我的简单测试表明该-O3版本实际上更快,因此GCC做出了正确的选择。

询问:https : //www.quora.com/unanswered/How-does-GCC-know-that-its-builtin-implementation-of-strlen-is-faster-than-glibcs​​-when-using-optimization-level -O3