【问题标题】:Where is the implementation of strlen() in GCC?GCC 中 strlen() 的实现在哪里?
【发布时间】:2010-12-16 12:55:41
【问题描述】:

谁能指出我在 GCC 中对strlen() 的定义?我已经 grepping 4.4.2 版本大约半小时了(同时谷歌搜索疯狂),我似乎无法找到 strlen() 的实际实现位置。

【问题讨论】:

    标签: c glibc strlen


    【解决方案1】:

    您应该查看 glibc,而不是 GCC——它似乎在 strlen.c 中定义——这是一个指向 strlen.c for glibc version 2.7 的链接...这里是一个指向 glibc SVN repository online for strlen.c 的链接。

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

    GNU C 库在 GNU 系统和大多数具有 Linux 内核的系统中用作 C 库。

    【讨论】:

    • 嗯,这不是很优化。至少使用 Visual C++,我们得到了一个不错的汇编语言 strlen。
    • “GNU C 库主要设计为可移植的高性能 C 库。”我猜他们可能会更加重视便携性。
    • 咳咳,即便携版本,请检查 sysdeps 目录以获取实际进入程序的版本。也就是说,如果 GCC 没有首先到达那里并将调用替换为内联版本,那么 OP 可能之前已经看到过它。 cvs.savannah.gnu.org/viewvc/libc/sysdeps/x86_64/…
    • 那个 C 版本实际上是非常优化的(尽管手动展开循环相当愚蠢)。即使使用 asm,您也很难击败它。
    • @toto 从 glibc 2.26 开始,这不再适用,现在所有主要拱门都有手动优化的组装实现:stackoverflow.com/a/50199212/895245
    【解决方案2】:

    这就是你要找的吗? strlen() source。请参阅git repository 了解更多信息。 glibc resources page 有指向 git 存储库的链接,如果您想获取它们而不是查看 Web 视图。

    【讨论】:

      【解决方案3】:

      这是bsd 的实现

      size_t
      strlen(const char *str)
      {
              const char *s;
      
              for (s = str; *s; ++s)
                      ;
              return (s - str);
      }
      

      【讨论】:

      • 仍在等待编译器可以从中生成可用的快速机器代码的那一天......目前它的速度还不到优化 C 版本的一半。
      • @R.. ICC 通常可以像这样自动矢量化循环。 gcc/clang 不能:它们只会对在第一次迭代之前已知行程计数的循环进行自动矢量化。 (即它们在搜索循环中毫无用处。)
      【解决方案4】:

      Google Code Search 是解决此类问题的一个很好的起点。它们通常指向函数的各种不同来源和实现。

      在您的特定情况下:GoogleCodeSearch(strlen)

      Google 代码搜索已于 2013 年 3 月完全关闭

      【讨论】:

        【解决方案5】:

        虽然最初的发布者可能不知道这个或一直在寻找这个,但 gcc 在内部内联了许多它自己定义的所谓“内置”c 函数,包括一些 mem*() 函数和 (取决于 gcc 版本)strlen。在这种情况下,库版本基本上从未使用过,并且将人员指向 glibc 中的版本严格来说并不正确。 (它这样做是出于性能原因——除了内联本身产生的改进之外,gcc 在提供函数时“知道”函数的某些事情,例如,strlen 是一个纯函数,因此它可以优化多个调用,或者在没有发生别名的 mem*() 函数的情况下。)

        有关这方面的更多信息,请参阅http://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html

        【讨论】:

          【解决方案6】:

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

          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(以及其他)如何展开和折叠(基于前面提到的配置/平台)

          【讨论】:

          【解决方案7】:

          我意识到这是个老问题,你可以在 github here 找到 linux 内核源代码,strlen() 的 32 位实现可以在 github 上的 strlen_32.c 找到。提到的文件有这个实现。

          #include <linux/types.h>
          #include <linux/string.h>
          #include <linux/module.h>
          
          size_t strlen(const char *s)
          {
              /* Get an aligned pointer. */
              const uintptr_t s_int = (uintptr_t) s;
              const uint32_t *p = (const uint32_t *)(s_int & -4);
          
              /* Read the first word, but force bytes before the string to be nonzero.
               * This expression works because we know shift counts are taken mod 32.
               */
              uint32_t v = *p | ((1 << (s_int << 3)) - 1);
          
              uint32_t bits;
              while ((bits = __insn_seqb(v, 0)) == 0)
                  v = *++p;
          
              return ((const char *)p) + (__insn_ctz(bits) >> 3) - s;
          }
          EXPORT_SYMBOL(strlen);
          

          【讨论】:

          • 发布一个 Tilera 特定的实现是相当晦涩的。
          【解决方案8】:

          你可以用这段代码,越简单越好!

          size_t Strlen ( const char * _str )
          {
              size_t i = 0;
              while(_str[i++]);
              return i;
          }
          

          【讨论】:

          • 此函数返回字符串长度加 1,这与排除 '\0' 的 strlen 的行为不一致
          【解决方案9】:

          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)
          

          【讨论】:

          • 这没有回答问题。 OP 不是在寻找自定义的 strlen 实现。
          【解决方案10】:

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

          截至glibc-2.26,快速:

          git ls-files | grep strlen.S
          

          在 glibc 树中显示了针对所有主要架构和变体的一打汇编手工优化实现。

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

          sysdeps/x86_64/multiarch/strlen-avx2.S
          sysdeps/x86_64/multiarch/strlen-sse2.S
          sysdeps/x86_64/strlen.S
          

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

          #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;
          }
          

          编译:

          gcc -ggdb3 -std=c99 -O0 a.c
          

          马上开始:

          disass main
          

          包含:

          callq  0x555555554590 <strlen@plt>
          

          所以 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.
          

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

          然后,我进一步确认:

          disass __strlen_avx2
          

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

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

          这就是 glibc 的核心部分来自于:它有很多经过拱优化的手写汇编代码。

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

          -O3

          TODO:使用-O3,gcc不使用glibc的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

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2020-05-24
            • 2019-10-14
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2012-04-13
            • 1970-01-01
            • 2014-08-05
            相关资源
            最近更新 更多