c - 理解 strlen 实现中的代码

标签 c string algorithm pointers gcc

<分区>

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

  1. 实现使用带有“漏洞”的魔数(Magic Number)。我无法理解这是如何工作的。有人可以帮我理解这个片段:

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

    魔数(Magic Number)的用途是什么?

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

最佳答案

这用于一次查看 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 位示例):

There is yet a faster method — use hasless(v, 1), which is defined below; it works in 4 operations and requires no subsquent verification. It simplifies to

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

The subexpression (v - 0x01010101UL), evaluates to a high bit set in any byte whenever the corresponding byte in v is zero or greater than 0x80. The sub-expression ~v & 0x80808080UL evaluates to high bits set in bytes where the byte of v doesn't have its high bit set (so the byte was less than 0x80). Finally, by ANDing these two sub-expressions the result is the high bits set where the bytes in v were zero, since the high bits set due to a value greater than 0x80 in the first sub-expression are masked off by the second.

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

关于c - 理解 strlen 实现中的代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34642994/

相关文章:

c++ - 没有循环的算法复杂度?

javascript - jQuery - 在色轮上获取白色或深色文本

c - 为什么可以将 int 的地址视为指向 int 的指针?

c - 接受用户输入时数组超出限制

CSFML 意外的重复纹理行为

python - 如何使用 lambda 函数使 'str' 对象可调用?

c - 通过递归反转整数顺序

Javascript - 网页内容转字符串

php - 伪随机字符串

algorithm - 一个动态规划问题