【问题标题】:Understanding code in strlen implementation理解 strlen 实现中的代码
【发布时间】:2016-04-11 03:01:17
【问题描述】:

关于在 glibc 中 string.h 中的 strlen 的实现,我有两个问题。

  1. 该实现使用带有“孔”的幻数。我无法理解这是如何工作的。有人可以帮我理解这个sn-p:

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

    幻数的用途是什么?

  2. 为什么不简单地增加指针直到 NULL 字符并返回计数?这种方法更快吗?为什么会这样?

【问题讨论】:

  • 在大多数架构上,glibc 将使用更快的函数。例如,在现代英特尔芯片上,它使用 SIMD 扩展来矢量化检查。

标签: c string algorithm pointers gcc


【解决方案1】:

这用于一次查看 4 个字节(32 位)甚至 8 个(64 位),以检查其中一个是否为零(字符串结尾),而不是单独检查每个字节。

这是一个检查空字节的示例:

unsigned int v; // 32-bit word to check if any 8-bit byte in it is 0
bool hasZeroByte = ~((((v & 0x7F7F7F7F) + 0x7F7F7F7F) | v) | 0x7F7F7F7F);

更多信息请参见Bit Twiddling Hacks。

此处使用的那个(32 位示例):

还有一种更快的方法——使用 hasless(v, 1),它的定义是 以下;它适用于 4 次操作,不需要后续操作 确认。它简化为

#define haszero(v) (((v) - 0x01010101UL) &amp; ~(v) &amp; 0x80808080UL)

子表达式 (v - 0x01010101UL),计算结果为设置的高位 每当 v 中的相应字节为零或大于时的任何字节 0x80。子表达式 ~v & 0x80808080UL 计算为高位集 以字节为单位,其中 v 的字节没有设置其高位(因此 字节小于 0x80)。最后,通过与这两个子表达式 结果是在 v 中的字节为零时设置的高位,因为 由于第一个值大于 0x80 而设置的高位 子表达式被第二个屏蔽掉。

一次查看一个字节的 CPU 周期至少与查看一个完整的整数值(寄存器范围)一样多。在此算法中,检查完整整数以查看它们是否包含零。如果不是,则使用很少的指令,并且可以跳转 到下一个完整整数。如果里面有一个零字节,则进一步检查它的确切位置。

【讨论】:

  • 此外,gcc strlen 实现所做的是优化以利用支持 8 字节整数的架构。在上面,您只能一次查找 4 个字节的空值。 strlen 中的 if (sizeof (longword) &gt; 4) 比较将比较扩展了额外的 4 个字节。任何一种方式的好处都是提高了strlen 对于长度超过大约 32 个字符的字符串的性能。 (高于您通过 逐字符检查 获得的结果)。很好的答案。
猜你喜欢
  • 2014-04-26
  • 1970-01-01
  • 2012-08-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-05
  • 2010-12-16
  • 2018-11-12
相关资源
最近更新 更多