| [2556] | 1 | /* Copyright (C) 1991,93,96,97,99,2000 Free Software Foundation, Inc.
|
|---|
| 2 | Based on strlen implementation by Torbjorn Granlund ([email protected]),
|
|---|
| 3 | with help from Dan Sahlin ([email protected]) and
|
|---|
| 4 | commentary by Jim Blandy ([email protected]);
|
|---|
| 5 | adaptation to memchr suggested by Dick Karpinski ([email protected]),
|
|---|
| 6 | and implemented by Roland McGrath ([email protected]).
|
|---|
| 7 |
|
|---|
| 8 | NOTE: The canonical source of this file is maintained with the GNU C Library.
|
|---|
| 9 | Bugs can be reported to [email protected].
|
|---|
| 10 |
|
|---|
| 11 | This program is free software; you can redistribute it and/or modify it
|
|---|
| 12 | under the terms of the GNU General Public License as published by the
|
|---|
| 13 | Free Software Foundation; either version 2, or (at your option) any
|
|---|
| 14 | later version.
|
|---|
| 15 |
|
|---|
| 16 | This program is distributed in the hope that it will be useful,
|
|---|
| 17 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 18 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|---|
| 19 | GNU General Public License for more details.
|
|---|
| 20 |
|
|---|
| 21 | You should have received a copy of the GNU General Public License
|
|---|
| 22 | along with this program; if not, write to the Free Software
|
|---|
| 23 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
|
|---|
| 24 | USA. */
|
|---|
| 25 |
|
|---|
| 26 | #ifdef HAVE_CONFIG_H
|
|---|
| 27 | # include <config.h>
|
|---|
| 28 | #endif
|
|---|
| 29 |
|
|---|
| 30 | #undef __ptr_t
|
|---|
| 31 | #if defined (__cplusplus) || (defined (__STDC__) && __STDC__)
|
|---|
| 32 | # define __ptr_t void *
|
|---|
| 33 | #else /* Not C++ or ANSI C. */
|
|---|
| 34 | # define __ptr_t char *
|
|---|
| 35 | #endif /* C++ or ANSI C. */
|
|---|
| 36 |
|
|---|
| 37 | #if defined _LIBC
|
|---|
| 38 | # include <string.h>
|
|---|
| 39 | # include <memcopy.h>
|
|---|
| 40 | #else
|
|---|
| 41 | # define reg_char char
|
|---|
| 42 | #endif
|
|---|
| 43 |
|
|---|
| 44 | #if HAVE_STDLIB_H || defined _LIBC
|
|---|
| 45 | # include <stdlib.h>
|
|---|
| 46 | #endif
|
|---|
| 47 |
|
|---|
| 48 | #if HAVE_LIMITS_H || defined _LIBC
|
|---|
| 49 | # include <limits.h>
|
|---|
| 50 | #endif
|
|---|
| 51 |
|
|---|
| 52 | #define LONG_MAX_32_BITS 2147483647
|
|---|
| 53 |
|
|---|
| 54 | #ifndef LONG_MAX
|
|---|
| 55 | # define LONG_MAX LONG_MAX_32_BITS
|
|---|
| 56 | #endif
|
|---|
| 57 |
|
|---|
| 58 | #include <sys/types.h>
|
|---|
| 59 | #if HAVE_BP_SYM_H || defined _LIBC
|
|---|
| 60 | # include <bp-sym.h>
|
|---|
| 61 | #else
|
|---|
| 62 | # define BP_SYM(sym) sym
|
|---|
| 63 | #endif
|
|---|
| 64 |
|
|---|
| 65 | #undef memchr
|
|---|
| 66 | #undef __memchr
|
|---|
| 67 |
|
|---|
| 68 | /* Search no more than N bytes of S for C. */
|
|---|
| 69 | __ptr_t
|
|---|
| 70 | __memchr (s, c_in, n)
|
|---|
| 71 | const __ptr_t s;
|
|---|
| 72 | int c_in;
|
|---|
| 73 | size_t n;
|
|---|
| 74 | {
|
|---|
| 75 | const unsigned char *char_ptr;
|
|---|
| 76 | const unsigned long int *longword_ptr;
|
|---|
| 77 | unsigned long int longword, magic_bits, charmask;
|
|---|
| 78 | unsigned reg_char c;
|
|---|
| 79 |
|
|---|
| 80 | c = (unsigned char) c_in;
|
|---|
| 81 |
|
|---|
| 82 | /* Handle the first few characters by reading one character at a time.
|
|---|
| 83 | Do this until CHAR_PTR is aligned on a longword boundary. */
|
|---|
| 84 | for (char_ptr = (const unsigned char *) s;
|
|---|
| 85 | n > 0 && ((unsigned long int) char_ptr
|
|---|
| 86 | & (sizeof (longword) - 1)) != 0;
|
|---|
| 87 | --n, ++char_ptr)
|
|---|
| 88 | if (*char_ptr == c)
|
|---|
| 89 | return (__ptr_t) char_ptr;
|
|---|
| 90 |
|
|---|
| 91 | /* All these elucidatory comments refer to 4-byte longwords,
|
|---|
| 92 | but the theory applies equally well to 8-byte longwords. */
|
|---|
| 93 |
|
|---|
| 94 | longword_ptr = (unsigned long int *) char_ptr;
|
|---|
| 95 |
|
|---|
| 96 | /* Bits 31, 24, 16, and 8 of this number are zero. Call these bits
|
|---|
| 97 | the "holes." Note that there is a hole just to the left of
|
|---|
| 98 | each byte, with an extra at the end:
|
|---|
| 99 |
|
|---|
| 100 | bits: 01111110 11111110 11111110 11111111
|
|---|
| 101 | bytes: AAAAAAAA BBBBBBBB CCCCCCCC DDDDDDDD
|
|---|
| 102 |
|
|---|
| 103 | The 1-bits make sure that carries propagate to the next 0-bit.
|
|---|
| 104 | The 0-bits provide holes for carries to fall into. */
|
|---|
| 105 |
|
|---|
| 106 | if (sizeof (longword) != 4 && sizeof (longword) != 8)
|
|---|
| 107 | abort ();
|
|---|
| 108 |
|
|---|
| 109 | #if LONG_MAX <= LONG_MAX_32_BITS
|
|---|
| 110 | magic_bits = 0x7efefeff;
|
|---|
| 111 | #else
|
|---|
| 112 | magic_bits = ((unsigned long int) 0x7efefefe << 32) | 0xfefefeff;
|
|---|
| 113 | #endif
|
|---|
| 114 |
|
|---|
| 115 | /* Set up a longword, each of whose bytes is C. */
|
|---|
| 116 | charmask = c | (c << 8);
|
|---|
| 117 | charmask |= charmask << 16;
|
|---|
| 118 | #if LONG_MAX > LONG_MAX_32_BITS
|
|---|
| 119 | charmask |= charmask << 32;
|
|---|
| 120 | #endif
|
|---|
| |
|---|