[strlen的GNU實現]有趣的代碼

來源:互聯網
上載者:User
先擱這吧,晚上回去分析。
  1. #include <string.h>
  2. #include <stdlib.h>
  3. #undef strlen
  4. /*
  5. Return the length of the null-terminated string STR.  Scan for
  6. the null terminator quickly by testing four bytes at a time.  
  7. */
  8. size_t strlen (str)
  9. const char *str;
  10. {
  11.     const char *char_ptr;
  12.     const unsigned long int *longword_ptr;
  13.     unsigned long int longword, magic_bits, himagic, lomagic;
  14.     /*
  15.     Handle the first few characters by reading one character at a time.
  16.     Do this until CHAR_PTR is aligned on a longword boundary.  
  17.     */
  18.     for (char_ptr = str; ((unsigned long int) char_ptr
  19.             & (sizeof (longword) - 1)) != 0;++char_ptr)
  20.     if (*char_ptr == '/0')
  21.             return char_ptr - str;
  22.     /*
  23.     All these elucidatory comments refer to 4-byte longwords,
  24.     but the theory applies equally well to 8-byte longwords.  
  25.     */
  26.     longword_ptr = (unsigned long int *) char_ptr;
  27.     /*
  28.     Bits 31, 24, 16, and 8 of this number are zero.  Call these bits
  29.     the "holes."  Note that there is a hole just to the left of
  30.     each byte, with an extra at the end:
  31.     bits:  01111110 11111110 11111110 11111111
  32.     bytes: AAAAAAAA BBBBBBBB CCCCCCCC DDDDDDDD
  33.     The 1-bits make sure that carries propagate to the next 0-bit.
  34.     The 0-bits provide holes for carries to fall into.  
  35.     */
  36.     magic_bits = 0x7efefeffL;
  37.     himagic = 0x80808080L;
  38.     lomagic = 0x01010101L;
  39.     if (sizeof (longword) > 4)
  40.     {
  41.             /* 64-bit version of the magic.  */
  42.             /* Do the shift in two steps to avoid a warning if long has 32 bits.  */
  43.             magic_bits = ((0x7efefefeL << 16) << 16) | 0xfefefeffL;
  44.             himagic = ((himagic << 16) << 16) | himagic;
  45.             lomagic = ((lomagic << 16) << 16) | lomagic;
  46.     }
  47.     if (sizeof (longword) > 8)
  48.             abort ();
  49.     /*
  50.     Instead of the traditional loop which tests each character,
  51.     we will test a longword at a time.  The tricky part is testing
  52.     if *any of the four* bytes in the longword in question are zero.  
  53.     */
  54.     for (;;)
  55.     {
  56.         /* We tentatively exit the loop if adding MAGIC_BITS to
  57.         LONGWORD fails to change any of the hole bits of LONGWORD.
  58.         1) Is this safe?  Will it catch all the zero bytes?
  59.         Suppose there is a byte with all zeros.  Any carry bits
  60.         propagating from its left will fall into the hole at its
  61.         least significant bit and stop.  Since there will be no
  62.         carry from its most significant bit, the LSB of the
  63.         byte to the left will be unchanged, and the zero will be
  64.         detected.
  65.         2) Is this worthwhile?  Will it ignore everything except
  66.         zero bytes?  Suppose every byte of LONGWORD has a bit set
  67.         somewhere.  There will be a carry into bit 8.  If bit 8
  68.         is set, this will carry into bit 16.  If bit 8 is clear,
  69.         one of bits 9-15 must be set, so there will be a carry
  70.         into bit 16.  Similarly, there will be a carry into bit
  71.         24.  If one of bits 24-30 is set, there will be a carry
  72.         into bit 31, so all of the hole bits will be changed.
  73.         The one misfire occurs when bits 24-30 are clear and bit
  74.         31 is set; in this case, the hole at bit 31 is not
  75.         changed.  If we had access to the processor carry flag,
  76.         we could close this loophole by putting the fourth hole
  77.         at bit 32!
  78.         So it ignores everything except 128's, when they're aligned
  79.         properly.  */
  80.         longword = *longword_ptr++;
  81.         if (
  82.         #if 0
  83.         /* Add MAGIC_BITS to LONGWORD.  */
  84.         (((longword + magic_bits)
  85.         /* Set those bits that were unchanged by the addition.  */
  86.         ^ ~longword)
  87.         /* Look at only the hole bits.  If any of the hole bits
  88.         are unchanged, most likely one of the bytes was a
  89.         zero.  */
  90.         & ~magic_bits)
  91.         #else
  92.         ((longword - lomagic) & himagic)
  93.         #endif
  94.         != 0)
  95.         {
  96.             /*
  97.             Which of the bytes was the zero?  If none of them were, it was
  98.             a misfire; continue the search.  
  99.             */
  100.             const char *cp = (const char *) (longword_ptr - 1);
  101.             if (cp[0] == 0)
  102.             return cp - str;
  103.             if (cp[1] == 0)
  104.             return cp - str + 1;
  105.             if (cp[2] == 0)
  106.             return cp - str + 2;
  107.             if (cp[3] == 0)
  108.             return cp - str + 3;
  109.             if (sizeof (longword) > 4)
  110.             {
  111.                   if (cp[4] == 0)
  112.                 return cp - str + 4;
  113.                   if (cp[5] == 0)
  114.                 return cp - str + 5;
  115.                   if (cp[6] == 0)
  116.                 return cp - str + 6;
  117.                   if (cp[7] == 0)
  118.                 return cp - str + 7;
  119.             }
  120.         }
  121.     }
  122. }
  123. libc_hidden_builtin_def (strlen)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.