memchr.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /* Copyright (C) 1991, 1993, 1996, 1997, 1999, 2000, 2003, 2004, 2006 Free
  2. Software Foundation, Inc.
  3. Based on strlen implementation by Torbjorn Granlund (tege@sics.se),
  4. with help from Dan Sahlin (dan@sics.se) and
  5. commentary by Jim Blandy (jimb@ai.mit.edu);
  6. adaptation to memchr suggested by Dick Karpinski (dick@cca.ucsf.edu),
  7. and implemented by Roland McGrath (roland@ai.mit.edu).
  8. NOTE: The canonical source of this file is maintained with the GNU C Library.
  9. Bugs can be reported to bug-glibc@prep.ai.mit.edu.
  10. This program is free software; you can redistribute it and/or modify it
  11. under the terms of the GNU General Public License as published by the
  12. Free Software Foundation; either version 2, or (at your option) any
  13. later version.
  14. This program is distributed in the hope that it will be useful,
  15. but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. GNU General Public License for more details.
  18. You should have received a copy of the GNU General Public License
  19. along with this program; if not, write to the Free Software Foundation,
  20. Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
  21. #ifndef _LIBC
  22. # include <config.h>
  23. #endif
  24. #include <string.h>
  25. #include <stddef.h>
  26. #if defined _LIBC
  27. # include <memcopy.h>
  28. #else
  29. # define reg_char char
  30. #endif
  31. #include <limits.h>
  32. #if HAVE_BP_SYM_H || defined _LIBC
  33. # include <bp-sym.h>
  34. #else
  35. # define BP_SYM(sym) sym
  36. #endif
  37. #undef memchr
  38. #undef __memchr
  39. /* Search no more than N bytes of S for C. */
  40. void *
  41. __memchr (void const *s, int c_in, size_t n)
  42. {
  43. const unsigned char *char_ptr;
  44. const unsigned long int *longword_ptr;
  45. unsigned long int longword, magic_bits, charmask;
  46. unsigned reg_char c;
  47. int i;
  48. c = (unsigned char) c_in;
  49. /* Handle the first few characters by reading one character at a time.
  50. Do this until CHAR_PTR is aligned on a longword boundary. */
  51. for (char_ptr = (const unsigned char *) s;
  52. n > 0 && (size_t) char_ptr % sizeof longword != 0;
  53. --n, ++char_ptr)
  54. if (*char_ptr == c)
  55. return (void *) char_ptr;
  56. /* All these elucidatory comments refer to 4-byte longwords,
  57. but the theory applies equally well to any size longwords. */
  58. longword_ptr = (const unsigned long int *) char_ptr;
  59. /* Bits 31, 24, 16, and 8 of this number are zero. Call these bits
  60. the "holes." Note that there is a hole just to the left of
  61. each byte, with an extra at the end:
  62. bits: 01111110 11111110 11111110 11111111
  63. bytes: AAAAAAAA BBBBBBBB CCCCCCCC DDDDDDDD
  64. The 1-bits make sure that carries propagate to the next 0-bit.
  65. The 0-bits provide holes for carries to fall into. */
  66. /* Set MAGIC_BITS to be this pattern of 1 and 0 bits.
  67. Set CHARMASK to be a longword, each of whose bytes is C. */
  68. magic_bits = 0xfefefefe;
  69. charmask = c | (c << 8);
  70. charmask |= charmask << 16;
  71. #if 0xffffffffU < ULONG_MAX
  72. magic_bits |= magic_bits << 32;
  73. charmask |= charmask << 32;
  74. if (8 < sizeof longword)
  75. for (i = 64; i < sizeof longword * 8; i *= 2)
  76. {
  77. magic_bits |= magic_bits << i;
  78. charmask |= charmask << i;
  79. }
  80. #endif
  81. magic_bits = (ULONG_MAX >> 1) & (magic_bits | 1);
  82. /* Instead of the traditional loop which tests each character,
  83. we will test a longword at a time. The tricky part is testing
  84. if *any of the four* bytes in the longword in question are zero. */
  85. while (n >= sizeof longword)
  86. {
  87. /* We tentatively exit the loop if adding MAGIC_BITS to
  88. LONGWORD fails to change any of the hole bits of LONGWORD.
  89. 1) Is this safe? Will it catch all the zero bytes?
  90. Suppose there is a byte with all zeros. Any carry bits
  91. propagating from its left will fall into the hole at its
  92. least significant bit and stop. Since there will be no
  93. carry from its most significant bit, the LSB of the
  94. byte to the left will be unchanged, and the zero will be
  95. detected.
  96. 2) Is this worthwhile? Will it ignore everything except
  97. zero bytes? Suppose every byte of LONGWORD has a bit set
  98. somewhere. There will be a carry into bit 8. If bit 8
  99. is set, this will carry into bit 16. If bit 8 is clear,
  100. one of bits 9-15 must be set, so there will be a carry
  101. into bit 16. Similarly, there will be a carry into bit
  102. 24. If one of bits 24-30 is set, there will be a carry
  103. into bit 31, so all of the hole bits will be changed.
  104. The one misfire occurs when bits 24-30 are clear and bit
  105. 31 is set; in this case, the hole at bit 31 is not
  106. changed. If we had access to the processor carry flag,
  107. we could close this loophole by putting the fourth hole
  108. at bit 32!
  109. So it ignores everything except 128's, when they're aligned
  110. properly.
  111. 3) But wait! Aren't we looking for C, not zero?
  112. Good point. So what we do is XOR LONGWORD with a longword,
  113. each of whose bytes is C. This turns each byte that is C
  114. into a zero. */
  115. longword = *longword_ptr++ ^ charmask;
  116. /* Add MAGIC_BITS to LONGWORD. */
  117. if ((((longword + magic_bits)
  118. /* Set those bits that were unchanged by the addition. */
  119. ^ ~longword)
  120. /* Look at only the hole bits. If any of the hole bits
  121. are unchanged, most likely one of the bytes was a
  122. zero. */
  123. & ~magic_bits) != 0)
  124. {
  125. /* Which of the bytes was C? If none of them were, it was
  126. a misfire; continue the search. */
  127. const unsigned char *cp = (const unsigned char *) (longword_ptr - 1);
  128. if (cp[0] == c)
  129. return (void *) cp;
  130. if (cp[1] == c)
  131. return (void *) &cp[1];
  132. if (cp[2] == c)
  133. return (void *) &cp[2];
  134. if (cp[3] == c)
  135. return (void *) &cp[3];
  136. if (4 < sizeof longword && cp[4] == c)
  137. return (void *) &cp[4];
  138. if (5 < sizeof longword && cp[5] == c)
  139. return (void *) &cp[5];
  140. if (6 < sizeof longword && cp[6] == c)
  141. return (void *) &cp[6];
  142. if (7 < sizeof longword && cp[7] == c)
  143. return (void *) &cp[7];
  144. if (8 < sizeof longword)
  145. for (i = 8; i < sizeof longword; i++)
  146. if (cp[i] == c)
  147. return (void *) &cp[i];
  148. }
  149. n -= sizeof longword;
  150. }
  151. char_ptr = (const unsigned char *) longword_ptr;
  152. while (n-- > 0)
  153. {
  154. if (*char_ptr == c)
  155. return (void *) char_ptr;
  156. else
  157. ++char_ptr;
  158. }
  159. return 0;
  160. }
  161. #ifdef weak_alias
  162. weak_alias (__memchr, BP_SYM (memchr))
  163. #endif