rawmemchr.c 5.1 KB

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