memchr.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /* Copyright (C) 1991, 1993, 1996-1997, 1999-2000, 2003-2004, 2006, 2008-2021
  2. Free 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 3 of the License, or 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, see <https://www.gnu.org/licenses/>. */
  20. #ifndef _LIBC
  21. # include <config.h>
  22. #endif
  23. #include <string.h>
  24. #include <stddef.h>
  25. #if defined _LIBC
  26. # include <memcopy.h>
  27. #else
  28. # define reg_char char
  29. #endif
  30. #include <limits.h>
  31. #if HAVE_BP_SYM_H || defined _LIBC
  32. # include <bp-sym.h>
  33. #else
  34. # define BP_SYM(sym) sym
  35. #endif
  36. #undef __memchr
  37. #ifdef _LIBC
  38. # undef memchr
  39. #endif
  40. #ifndef weak_alias
  41. # define __memchr memchr
  42. #endif
  43. /* Search no more than N bytes of S for C. */
  44. void *
  45. __memchr (void const *s, int c_in, size_t n)
  46. {
  47. /* On 32-bit hardware, choosing longword to be a 32-bit unsigned
  48. long instead of a 64-bit uintmax_t tends to give better
  49. performance. On 64-bit hardware, unsigned long is generally 64
  50. bits already. Change this typedef to experiment with
  51. performance. */
  52. typedef unsigned long int longword;
  53. const unsigned char *char_ptr;
  54. const longword *longword_ptr;
  55. longword repeated_one;
  56. longword repeated_c;
  57. unsigned reg_char c;
  58. c = (unsigned char) c_in;
  59. /* Handle the first few bytes by reading one byte at a time.
  60. Do this until CHAR_PTR is aligned on a longword boundary. */
  61. for (char_ptr = (const unsigned char *) s;
  62. n > 0 && (size_t) char_ptr % sizeof (longword) != 0;
  63. --n, ++char_ptr)
  64. if (*char_ptr == c)
  65. return (void *) char_ptr;
  66. longword_ptr = (const longword *) char_ptr;
  67. /* All these elucidatory comments refer to 4-byte longwords,
  68. but the theory applies equally well to any size longwords. */
  69. /* Compute auxiliary longword values:
  70. repeated_one is a value which has a 1 in every byte.
  71. repeated_c has c in every byte. */
  72. repeated_one = 0x01010101;
  73. repeated_c = c | (c << 8);
  74. repeated_c |= repeated_c << 16;
  75. if (0xffffffffU < (longword) -1)
  76. {
  77. repeated_one |= repeated_one << 31 << 1;
  78. repeated_c |= repeated_c << 31 << 1;
  79. if (8 < sizeof (longword))
  80. {
  81. size_t i;
  82. for (i = 64; i < sizeof (longword) * 8; i *= 2)
  83. {
  84. repeated_one |= repeated_one << i;
  85. repeated_c |= repeated_c << i;
  86. }
  87. }
  88. }
  89. /* Instead of the traditional loop which tests each byte, we will test a
  90. longword at a time. The tricky part is testing if *any of the four*
  91. bytes in the longword in question are equal to c. We first use an xor
  92. with repeated_c. This reduces the task to testing whether *any of the
  93. four* bytes in longword1 is zero.
  94. We compute tmp =
  95. ((longword1 - repeated_one) & ~longword1) & (repeated_one << 7).
  96. That is, we perform the following operations:
  97. 1. Subtract repeated_one.
  98. 2. & ~longword1.
  99. 3. & a mask consisting of 0x80 in every byte.
  100. Consider what happens in each byte:
  101. - If a byte of longword1 is zero, step 1 and 2 transform it into 0xff,
  102. and step 3 transforms it into 0x80. A carry can also be propagated
  103. to more significant bytes.
  104. - If a byte of longword1 is nonzero, let its lowest 1 bit be at
  105. position k (0 <= k <= 7); so the lowest k bits are 0. After step 1,
  106. the byte ends in a single bit of value 0 and k bits of value 1.
  107. After step 2, the result is just k bits of value 1: 2^k - 1. After
  108. step 3, the result is 0. And no carry is produced.
  109. So, if longword1 has only non-zero bytes, tmp is zero.
  110. Whereas if longword1 has a zero byte, call j the position of the least
  111. significant zero byte. Then the result has a zero at positions 0, ...,
  112. j-1 and a 0x80 at position j. We cannot predict the result at the more
  113. significant bytes (positions j+1..3), but it does not matter since we
  114. already have a non-zero bit at position 8*j+7.
  115. So, the test whether any byte in longword1 is zero is equivalent to
  116. testing whether tmp is nonzero. */
  117. while (n >= sizeof (longword))
  118. {
  119. longword longword1 = *longword_ptr ^ repeated_c;
  120. if ((((longword1 - repeated_one) & ~longword1)
  121. & (repeated_one << 7)) != 0)
  122. break;
  123. longword_ptr++;
  124. n -= sizeof (longword);
  125. }
  126. char_ptr = (const unsigned char *) longword_ptr;
  127. /* At this point, we know that either n < sizeof (longword), or one of the
  128. sizeof (longword) bytes starting at char_ptr is == c. On little-endian
  129. machines, we could determine the first such byte without any further
  130. memory accesses, just by looking at the tmp result from the last loop
  131. iteration. But this does not work on big-endian machines. Choose code
  132. that works in both cases. */
  133. for (; n > 0; --n, ++char_ptr)
  134. {
  135. if (*char_ptr == c)
  136. return (void *) char_ptr;
  137. }
  138. return NULL;
  139. }
  140. #ifdef weak_alias
  141. weak_alias (__memchr, BP_SYM (memchr))
  142. #endif