簡體   English   中英

GCC中strlen()的實現在哪里?

[英]Where is the implementation of strlen() in GCC?

誰能指點我在GCC中定義strlen() 我一直在試用版本4.4.2大約半小時(當谷歌瘋狂時),我似乎無法找到strlen()實際實現的位置。

您應該在glibc中查找,而不是GCC - 它似乎是在strlen.c定義的 - 這里是glibc版本2.7的strlen.c鏈接...這里是strlenglibc SVN存儲庫鏈接。 c

你應該看glibc而不是gcc的原因是:

GNU C庫用作GNU系統中 C庫,大多數系統使用Linux內核。

我意識到這個問題已經過了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(以及其他)如何擴展和折疊(基於前面提到的配置/平台)

這是bsd實現

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

        for (s = str; *s; ++s)
                ;
        return (s - str);
}

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)

這是你想要的? strlen()來源 有關更多信息,請參閱git存儲庫 如果你想抓住它們而不是查看網頁視圖,那么glibc資源頁面會鏈接到git存儲庫。

谷歌代碼搜索是這類問題的一個很好的起點。 它們通常指向函數的各種不同來源和實現。

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

Google Code Search於2013年3月完全關閉

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硬性的很大一部分來自:它有很多arch優化的手寫匯編代碼。

測試在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在同一架構上有一些但不一定相同。 可以為包括memset,strchr,strcpy和strrchr在內的多個函數提供其他optab條目,例如ffs和strlen的條目。

我的簡單測試表明-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

雖然原始海報可能不知道這個或者一直在尋找這個,但gcc內部內聯了一些所謂的“內置”c函數,它自己定義,包括一些mem *()函數和(取決於gcc版)strlen。 在這種情況下,庫版本基本上從不使用,並且將該人指向glibc中的版本並不嚴格地說是正確的。 (它出於性能原因這樣做 - 除了內聯本身產生的改進之外,gcc在提供函數時“知道”關於函數的某些事情,例如,strlen是一個純函數,因此它可以因此優化多個調用,或者在mem *()函數中沒有發生混疊。)

有關這方面的更多信息,請參閱http://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html

我意識到這是一個老問題,你可以在這里找到github上的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);

你可以使用這個代碼,越簡單就越好!

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM