memchr.c 5.5 KB

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