xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame^] | 1 | /* Copyright (C) 2010-2016 Free Software Foundation, Inc. |
| 2 | This file is part of the GNU C Library. |
| 3 | |
| 4 | The GNU C Library is free software; you can redistribute it and/or |
| 5 | modify it under the terms of the GNU Lesser General Public |
| 6 | License as published by the Free Software Foundation; either |
| 7 | version 2.1 of the License, or (at your option) any later version. |
| 8 | |
| 9 | The GNU C Library 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 GNU |
| 12 | Lesser General Public License for more details. |
| 13 | |
| 14 | You should have received a copy of the GNU Lesser General Public |
| 15 | License along with the GNU C Library. If not, see |
| 16 | <http://www.gnu.org/licenses/>. */ |
| 17 | |
| 18 | #include <string.h> |
| 19 | |
| 20 | typedef unsigned long word; |
| 21 | |
| 22 | static inline word |
| 23 | ldq_u(const void *s) |
| 24 | { |
| 25 | return *(const word *)((word)s & -8); |
| 26 | } |
| 27 | |
| 28 | #define unlikely(X) __builtin_expect ((X), 0) |
| 29 | #define prefetch(X) __builtin_prefetch ((void *)(X), 0) |
| 30 | |
| 31 | #define cmpbeq0(X) __builtin_alpha_cmpbge(0, (X)) |
| 32 | #define find(X, Y) cmpbeq0 ((X) ^ (Y)) |
| 33 | |
| 34 | /* Search no more than N bytes of S for C. */ |
| 35 | |
| 36 | void * |
| 37 | __memchr (const void *s, int xc, size_t n) |
| 38 | { |
| 39 | const word *s_align; |
| 40 | word t, current, found, mask, offset; |
| 41 | |
| 42 | if (unlikely (n == 0)) |
| 43 | return 0; |
| 44 | |
| 45 | current = ldq_u (s); |
| 46 | |
| 47 | /* Replicate low byte of XC into all bytes of C. */ |
| 48 | t = xc & 0xff; /* 0000000c */ |
| 49 | t = (t << 8) | t; /* 000000cc */ |
| 50 | t = (t << 16) | t; /* 0000cccc */ |
| 51 | const word c = (t << 32) | t; /* cccccccc */ |
| 52 | |
| 53 | /* Align the source, and decrement the count by the number |
| 54 | of bytes searched in the first word. */ |
| 55 | s_align = (const word *)((word)s & -8); |
| 56 | n += ((word)s & 7); |
| 57 | |
| 58 | /* Deal with misalignment in the first word for the comparison. */ |
| 59 | mask = (1ul << ((word)s & 7)) - 1; |
| 60 | |
| 61 | /* If the entire string fits within one word, we may need masking |
| 62 | at both the front and the back of the string. */ |
| 63 | if (unlikely (n <= 8)) |
| 64 | { |
| 65 | mask |= -1ul << n; |
| 66 | goto last_quad; |
| 67 | } |
| 68 | |
| 69 | found = find (current, c) & ~mask; |
| 70 | if (unlikely (found)) |
| 71 | goto found_it; |
| 72 | |
| 73 | s_align++; |
| 74 | n -= 8; |
| 75 | |
| 76 | /* If the block is sufficiently large, align to cacheline and prefetch. */ |
| 77 | if (unlikely (n >= 256)) |
| 78 | { |
| 79 | /* Prefetch 3 cache lines beyond the one we're working on. */ |
| 80 | prefetch (s_align + 8); |
| 81 | prefetch (s_align + 16); |
| 82 | prefetch (s_align + 24); |
| 83 | |
| 84 | while ((word)s_align & 63) |
| 85 | { |
| 86 | current = *s_align; |
| 87 | found = find (current, c); |
| 88 | if (found) |
| 89 | goto found_it; |
| 90 | s_align++; |
| 91 | n -= 8; |
| 92 | } |
| 93 | |
| 94 | /* Within each cacheline, advance the load for the next word |
| 95 | before the test for the previous word is complete. This |
| 96 | allows us to hide the 3 cycle L1 cache load latency. We |
| 97 | only perform this advance load within a cacheline to prevent |
| 98 | reading across page boundary. */ |
| 99 | #define CACHELINE_LOOP \ |
| 100 | do { \ |
| 101 | word i, next = s_align[0]; \ |
| 102 | for (i = 0; i < 7; ++i) \ |
| 103 | { \ |
| 104 | current = next; \ |
| 105 | next = s_align[1]; \ |
| 106 | found = find (current, c); \ |
| 107 | if (unlikely (found)) \ |
| 108 | goto found_it; \ |
| 109 | s_align++; \ |
| 110 | } \ |
| 111 | current = next; \ |
| 112 | found = find (current, c); \ |
| 113 | if (unlikely (found)) \ |
| 114 | goto found_it; \ |
| 115 | s_align++; \ |
| 116 | n -= 64; \ |
| 117 | } while (0) |
| 118 | |
| 119 | /* While there's still lots more data to potentially be read, |
| 120 | continue issuing prefetches for the 4th cacheline out. */ |
| 121 | while (n >= 256) |
| 122 | { |
| 123 | prefetch (s_align + 24); |
| 124 | CACHELINE_LOOP; |
| 125 | } |
| 126 | |
| 127 | /* Up to 3 cache lines remaining. Continue issuing advanced |
| 128 | loads, but stop prefetching. */ |
| 129 | while (n >= 64) |
| 130 | CACHELINE_LOOP; |
| 131 | |
| 132 | /* We may have exhausted the buffer. */ |
| 133 | if (n == 0) |
| 134 | return NULL; |
| 135 | } |
| 136 | |
| 137 | /* Quadword aligned loop. */ |
| 138 | current = *s_align; |
| 139 | while (n > 8) |
| 140 | { |
| 141 | found = find (current, c); |
| 142 | if (unlikely (found)) |
| 143 | goto found_it; |
| 144 | current = *++s_align; |
| 145 | n -= 8; |
| 146 | } |
| 147 | |
| 148 | /* The last word may need masking at the tail of the compare. */ |
| 149 | mask = -1ul << n; |
| 150 | last_quad: |
| 151 | found = find (current, c) & ~mask; |
| 152 | if (found == 0) |
| 153 | return NULL; |
| 154 | |
| 155 | found_it: |
| 156 | #ifdef __alpha_cix__ |
| 157 | offset = __builtin_alpha_cttz (found); |
| 158 | #else |
| 159 | /* Extract LSB. */ |
| 160 | found &= -found; |
| 161 | |
| 162 | /* Binary search for the LSB. */ |
| 163 | offset = (found & 0x0f ? 0 : 4); |
| 164 | offset += (found & 0x33 ? 0 : 2); |
| 165 | offset += (found & 0x55 ? 0 : 1); |
| 166 | #endif |
| 167 | |
| 168 | return (void *)((word)s_align + offset); |
| 169 | } |
| 170 | |
| 171 | #ifdef weak_alias |
| 172 | weak_alias (__memchr, memchr) |
| 173 | #endif |
| 174 | libc_hidden_builtin_def (memchr) |