glibc | strlen

 

brief:返回以 null 结尾的字符串的长度。通过一次性测试 4(sizeof(long int)) 字节来快速扫描至 null 字符。(Return the length of the null-terminated string STR. Scan for
the null terminator quickly by testing four bytes at a time.)

这样的优化的前提是字节对齐的保证:

Handle the first few characters by reading one character at a time.
Do this until CHAR_PTR is aligned on a longword boundary.

  /* 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;  /* 判断是否能 sizeof(longword) 被整除。能->到达对齐的边界了。
       ++char_ptr)
    if (*char_ptr == '\0')  /* null terminate */
      return char_ptr - str;

待写……

接下来:magic_bits,the real magic

posted on 2015-10-01 22:52  Excavator  阅读(177)  评论(0编辑  收藏  举报

导航