memrchr.cc 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /* memrchr -- find the last occurrence of a byte in a memory block
  2. Copyright (C) 1991, 1993, 1996-1997, 1999-2000, 2003-2015 Free Software
  3. Foundation, Inc.
  4. Based on strlen implementation by Torbjorn Granlund (tege@sics.se),
  5. with help from Dan Sahlin (dan@sics.se) and
  6. commentary by Jim Blandy (jimb@ai.mit.edu);
  7. adaptation to memchr suggested by Dick Karpinski (dick@cca.ucsf.edu),
  8. and implemented by Roland McGrath (roland@ai.mit.edu).
  9. This program is free software: you can redistribute it and/or modify
  10. it under the terms of the GNU General Public License as published by
  11. the Free Software Foundation; either version 3 of the License, or
  12. (at your option) any 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. #ifndef HAVE_MEMRCHR
  21. #define reg_char char
  22. #include <string.h>
  23. #include <limits.h>
  24. #undef __memrchr
  25. #ifdef _LIBC
  26. # undef memrchr
  27. #endif
  28. #ifndef weak_alias
  29. # define __memrchr memrchr
  30. #endif
  31. /* Search no more than N bytes of S for C. */
  32. extern "C" void *
  33. memrchr (const void *s, int c_in, size_t n)
  34. {
  35. /* On 32-bit hardware, choosing longword to be a 32-bit unsigned
  36. long instead of a 64-bit uintmax_t tends to give better
  37. performance. On 64-bit hardware, unsigned long is generally 64
  38. bits already. Change this typedef to experiment with
  39. performance. */
  40. typedef unsigned long int longword;
  41. const unsigned char *char_ptr;
  42. const longword *longword_ptr;
  43. longword repeated_one;
  44. longword repeated_c;
  45. unsigned reg_char c;
  46. c = (unsigned char) c_in;
  47. /* Handle the last few bytes by reading one byte at a time.
  48. Do this until CHAR_PTR is aligned on a longword boundary. */
  49. for (char_ptr = (const unsigned char *) s + n;
  50. n > 0 && (size_t) char_ptr % sizeof (longword) != 0;
  51. --n)
  52. if (*--char_ptr == c)
  53. return (void *) char_ptr;
  54. longword_ptr = (const longword *) char_ptr;
  55. /* All these elucidatory comments refer to 4-byte longwords,
  56. but the theory applies equally well to any size longwords. */
  57. /* Compute auxiliary longword values:
  58. repeated_one is a value which has a 1 in every byte.
  59. repeated_c has c in every byte. */
  60. repeated_one = 0x01010101;
  61. repeated_c = c | (c << 8);
  62. repeated_c |= repeated_c << 16;
  63. if (0xffffffffU < (longword) -1)
  64. {
  65. repeated_one |= repeated_one << 31 << 1;
  66. repeated_c |= repeated_c << 31 << 1;
  67. if (8 < sizeof (longword))
  68. {
  69. size_t i;
  70. for (i = 64; i < sizeof (longword) * 8; i *= 2)
  71. {
  72. repeated_one |= repeated_one << i;
  73. repeated_c |= repeated_c << i;
  74. }
  75. }
  76. }
  77. /* Instead of the traditional loop which tests each byte, we will test a
  78. longword at a time. The tricky part is testing if *any of the four*
  79. bytes in the longword in question are equal to c. We first use an xor
  80. with repeated_c. This reduces the task to testing whether *any of the
  81. four* bytes in longword1 is zero.
  82. We compute tmp =
  83. ((longword1 - repeated_one) & ~longword1) & (repeated_one << 7).
  84. That is, we perform the following operations:
  85. 1. Subtract repeated_one.
  86. 2. & ~longword1.
  87. 3. & a mask consisting of 0x80 in every byte.
  88. Consider what happens in each byte:
  89. - If a byte of longword1 is zero, step 1 and 2 transform it into 0xff,
  90. and step 3 transforms it into 0x80. A carry can also be propagated
  91. to more significant bytes.
  92. - If a byte of longword1 is nonzero, let its lowest 1 bit be at
  93. position k (0 <= k <= 7); so the lowest k bits are 0. After step 1,
  94. the byte ends in a single bit of value 0 and k bits of value 1.
  95. After step 2, the result is just k bits of value 1: 2^k - 1. After
  96. step 3, the result is 0. And no carry is produced.
  97. So, if longword1 has only non-zero bytes, tmp is zero.
  98. Whereas if longword1 has a zero byte, call j the position of the least
  99. significant zero byte. Then the result has a zero at positions 0, ...,
  100. j-1 and a 0x80 at position j. We cannot predict the result at the more
  101. significant bytes (positions j+1..3), but it does not matter since we
  102. already have a non-zero bit at position 8*j+7.
  103. So, the test whether any byte in longword1 is zero is equivalent to
  104. testing whether tmp is nonzero. */
  105. while (n >= sizeof (longword))
  106. {
  107. longword longword1 = *--longword_ptr ^ repeated_c;
  108. if ((((longword1 - repeated_one) & ~longword1)
  109. & (repeated_one << 7)) != 0)
  110. {
  111. longword_ptr++;
  112. break;
  113. }
  114. n -= sizeof (longword);
  115. }
  116. char_ptr = (const unsigned char *) longword_ptr;
  117. /* At this point, we know that either n < sizeof (longword), or one of the
  118. sizeof (longword) bytes starting at char_ptr is == c. On little-endian
  119. machines, we could determine the first such byte without any further
  120. memory accesses, just by looking at the tmp result from the last loop
  121. iteration. But this does not work on big-endian machines. Choose code
  122. that works in both cases. */
  123. while (n-- > 0)
  124. {
  125. if (*--char_ptr == c)
  126. return (void *) char_ptr;
  127. }
  128. return NULL;
  129. }
  130. #endif