158f0484fSRodney W. Grimes /*- 2*6b62f424SEd Maste * SPDX-License-Identifier: MIT 38a16b7a1SPedro F. Giffuni * 4*6b62f424SEd Maste * Copyright (c) 2005-2014 Rich Felker, et al. 558f0484fSRodney W. Grimes * 6*6b62f424SEd Maste * Permission is hereby granted, free of charge, to any person obtaining 7*6b62f424SEd Maste * a copy of this software and associated documentation files (the 8*6b62f424SEd Maste * "Software"), to deal in the Software without restriction, including 9*6b62f424SEd Maste * without limitation the rights to use, copy, modify, merge, publish, 10*6b62f424SEd Maste * distribute, sublicense, and/or sell copies of the Software, and to 11*6b62f424SEd Maste * permit persons to whom the Software is furnished to do so, subject to 12*6b62f424SEd Maste * the following conditions: 1358f0484fSRodney W. Grimes * 14*6b62f424SEd Maste * The above copyright notice and this permission notice shall be 15*6b62f424SEd Maste * included in all copies or substantial portions of the Software. 1658f0484fSRodney W. Grimes * 17*6b62f424SEd Maste * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18*6b62f424SEd Maste * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19*6b62f424SEd Maste * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 20*6b62f424SEd Maste * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 21*6b62f424SEd Maste * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 22*6b62f424SEd Maste * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 23*6b62f424SEd Maste * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 2458f0484fSRodney W. Grimes */ 2558f0484fSRodney W. Grimes #include <sys/cdefs.h> 26de5fe5d5SDavid E. O'Brien __FBSDID("$FreeBSD$"); 27de5fe5d5SDavid E. O'Brien 2858f0484fSRodney W. Grimes #include <string.h> 29*6b62f424SEd Maste #include <stdint.h> 30*6b62f424SEd Maste #include <limits.h> 3158f0484fSRodney W. Grimes 32*6b62f424SEd Maste #define SS (sizeof(size_t)) 33*6b62f424SEd Maste #define ALIGN (sizeof(size_t)-1) 34*6b62f424SEd Maste #define ONES ((size_t)-1/UCHAR_MAX) 35*6b62f424SEd Maste #define HIGHS (ONES * (UCHAR_MAX/2+1)) 36*6b62f424SEd Maste #define HASZERO(x) (((x)-ONES) & ~(x) & HIGHS) 37*6b62f424SEd Maste 38*6b62f424SEd Maste void *memchr(const void *src, int c, size_t n) 3958f0484fSRodney W. Grimes { 40*6b62f424SEd Maste const unsigned char *s = src; 41*6b62f424SEd Maste c = (unsigned char)c; 42*6b62f424SEd Maste #ifdef __GNUC__ 43*6b62f424SEd Maste for (; ((uintptr_t)s & ALIGN) && n && *s != c; s++, n--); 44*6b62f424SEd Maste if (n && *s != c) { 45*6b62f424SEd Maste typedef size_t __attribute__((__may_alias__)) word; 46*6b62f424SEd Maste const word *w; 47*6b62f424SEd Maste size_t k = ONES * c; 48*6b62f424SEd Maste for (w = (const void *)s; n>=SS && !HASZERO(*w^k); w++, n-=SS); 49*6b62f424SEd Maste s = (const void *)w; 5058f0484fSRodney W. Grimes } 51*6b62f424SEd Maste #endif 52*6b62f424SEd Maste for (; n && *s != c; s++, n--); 53*6b62f424SEd Maste return n ? (void *)s : 0; 5458f0484fSRodney W. Grimes } 55