123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149 |
- #include <string.h>
- #include <stdlib.h>
- size_t strlen (const char *str)
- {
- const char *char_ptr;
- const unsigned long int *longword_ptr;
- unsigned long int longword, magic_bits, himagic, lomagic;
-
- for (char_ptr = str; ((unsigned long int) char_ptr
- & (sizeof (longword) - 1)) != 0;
- ++char_ptr)
- if (*char_ptr == '\0')
- return char_ptr - str;
-
- longword_ptr = (unsigned long int *) char_ptr;
-
- magic_bits = 0x7efefeffL;
- himagic = 0x80808080L;
- lomagic = 0x01010101L;
- if (sizeof (longword) > 4)
- {
-
-
- magic_bits = ((0x7efefefeL << 16) << 16) | 0xfefefeffL;
- himagic = ((himagic << 16) << 16) | himagic;
- lomagic = ((lomagic << 16) << 16) | lomagic;
- }
- if (sizeof (longword) > 8)
- abort ();
-
- for (;;)
- {
-
- longword = *longword_ptr++;
- if (
- #if 0
-
- (((longword + magic_bits)
-
- ^ ~longword)
-
- & ~magic_bits)
- #else
- ((longword - lomagic) & himagic)
- #endif
- != 0)
- {
-
- 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_weak(strlen)
|