memchr2.c 6.3 KB

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