rawmemchr.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /* Searching in a string.
  2. Copyright (C) 2008-2023 Free Software Foundation, Inc.
  3. This file is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Lesser General Public License as
  5. published by the Free Software Foundation; either version 2.1 of the
  6. License, or (at your option) any later version.
  7. This file 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 Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public License
  12. along with this program. If not, see <https://www.gnu.org/licenses/>. */
  13. #include <config.h>
  14. /* Specification. */
  15. #include <string.h>
  16. /* A function definition is only needed if HAVE_RAWMEMCHR is not defined. */
  17. #if !HAVE_RAWMEMCHR
  18. # include <limits.h>
  19. # include <stdint.h>
  20. /* Find the first occurrence of C in S. */
  21. void *
  22. rawmemchr (const void *s, int c_in)
  23. {
  24. /* Change this typedef to experiment with performance. */
  25. typedef uintptr_t longword;
  26. /* If you change the "uintptr_t", you should change UINTPTR_WIDTH to match.
  27. This verifies that the type does not have padding bits. */
  28. static_assert (UINTPTR_WIDTH == UCHAR_WIDTH * sizeof (longword));
  29. const unsigned char *char_ptr;
  30. unsigned char c = c_in;
  31. /* Handle the first few bytes by reading one byte at a time.
  32. Do this until CHAR_PTR is aligned on a longword boundary. */
  33. for (char_ptr = (const unsigned char *) s;
  34. (uintptr_t) char_ptr % alignof (longword) != 0;
  35. ++char_ptr)
  36. if (*char_ptr == c)
  37. return (void *) char_ptr;
  38. longword const *longword_ptr = s = char_ptr;
  39. /* Compute auxiliary longword values:
  40. repeated_one is a value which has a 1 in every byte.
  41. repeated_c has c in every byte. */
  42. longword repeated_one = (longword) -1 / UCHAR_MAX;
  43. longword repeated_c = repeated_one * c;
  44. longword repeated_hibit = repeated_one * (UCHAR_MAX / 2 + 1);
  45. /* Instead of the traditional loop which tests each byte, we will
  46. test a longword at a time. The tricky part is testing if any of
  47. the bytes in the longword in question are equal to
  48. c. We first use an xor with repeated_c. This reduces the task
  49. to testing whether any of the bytes in longword1 is zero.
  50. (The following comments assume 8-bit bytes, as POSIX requires;
  51. the code's use of UCHAR_MAX should work even if bytes have more
  52. than 8 bits.)
  53. We compute tmp =
  54. ((longword1 - repeated_one) & ~longword1) & (repeated_one * 0x80).
  55. That is, we perform the following operations:
  56. 1. Subtract repeated_one.
  57. 2. & ~longword1.
  58. 3. & a mask consisting of 0x80 in every byte.
  59. Consider what happens in each byte:
  60. - If a byte of longword1 is zero, step 1 and 2 transform it into 0xff,
  61. and step 3 transforms it into 0x80. A carry can also be propagated
  62. to more significant bytes.
  63. - If a byte of longword1 is nonzero, let its lowest 1 bit be at
  64. position k (0 <= k <= 7); so the lowest k bits are 0. After step 1,
  65. the byte ends in a single bit of value 0 and k bits of value 1.
  66. After step 2, the result is just k bits of value 1: 2^k - 1. After
  67. step 3, the result is 0. And no carry is produced.
  68. So, if longword1 has only non-zero bytes, tmp is zero.
  69. Whereas if longword1 has a zero byte, call j the position of the least
  70. significant zero byte. Then the result has a zero at positions 0, ...,
  71. j-1 and a 0x80 at position j. We cannot predict the result at the more
  72. significant bytes (positions j+1..3), but it does not matter since we
  73. already have a non-zero bit at position 8*j+7.
  74. The test whether any byte in longword1 is zero is equivalent
  75. to testing whether tmp is nonzero.
  76. This test can read beyond the end of a string, depending on where
  77. C_IN is encountered. However, this is considered safe since the
  78. initialization phase ensured that the read will be aligned,
  79. therefore, the read will not cross page boundaries and will not
  80. cause a fault. */
  81. while (1)
  82. {
  83. longword longword1 = *longword_ptr ^ repeated_c;
  84. if ((((longword1 - repeated_one) & ~longword1) & repeated_hibit) != 0)
  85. break;
  86. longword_ptr++;
  87. }
  88. char_ptr = s = longword_ptr;
  89. /* At this point, we know that one of the sizeof (longword) bytes
  90. starting at char_ptr is == c. If we knew endianness, we
  91. could determine the first such byte without any further memory
  92. accesses, just by looking at the tmp result from the last loop
  93. iteration. However, the following simple and portable code does
  94. not attempt this potential optimization. */
  95. while (*char_ptr != c)
  96. char_ptr++;
  97. return (void *) char_ptr;
  98. }
  99. #endif