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