158f0484fSRodney W. Grimes /*- 26b62f424SEd Maste * SPDX-License-Identifier: MIT 38a16b7a1SPedro F. Giffuni * 46b62f424SEd Maste * Copyright (c) 2005-2014 Rich Felker, et al. 558f0484fSRodney W. Grimes * 66b62f424SEd Maste * Permission is hereby granted, free of charge, to any person obtaining 76b62f424SEd Maste * a copy of this software and associated documentation files (the 86b62f424SEd Maste * "Software"), to deal in the Software without restriction, including 96b62f424SEd Maste * without limitation the rights to use, copy, modify, merge, publish, 106b62f424SEd Maste * distribute, sublicense, and/or sell copies of the Software, and to 116b62f424SEd Maste * permit persons to whom the Software is furnished to do so, subject to 126b62f424SEd Maste * the following conditions: 1358f0484fSRodney W. Grimes * 146b62f424SEd Maste * The above copyright notice and this permission notice shall be 156b62f424SEd Maste * included in all copies or substantial portions of the Software. 1658f0484fSRodney W. Grimes * 176b62f424SEd Maste * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 186b62f424SEd Maste * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 196b62f424SEd Maste * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 206b62f424SEd Maste * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 216b62f424SEd Maste * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 226b62f424SEd Maste * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 236b62f424SEd 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 286b62f424SEd Maste #include <limits.h> 29*4874ddfdSEd Maste #include <stdint.h> 30*4874ddfdSEd Maste #include <string.h> 3158f0484fSRodney W. Grimes 326b62f424SEd Maste #define SS (sizeof(size_t)) 336b62f424SEd Maste #define ALIGN (sizeof(size_t) - 1) 346b62f424SEd Maste #define ONES ((size_t)-1 / UCHAR_MAX) 356b62f424SEd Maste #define HIGHS (ONES * (UCHAR_MAX / 2 + 1)) 366b62f424SEd Maste #define HASZERO(x) (((x)-ONES) & ~(x)&HIGHS) 376b62f424SEd Maste 38*4874ddfdSEd Maste void * 39*4874ddfdSEd Maste memchr(const void *src, int c, size_t n) 4058f0484fSRodney W. Grimes { 416b62f424SEd Maste const unsigned char *s = src; 426b62f424SEd Maste c = (unsigned char)c; 436b62f424SEd Maste #ifdef __GNUC__ 44*4874ddfdSEd Maste for (; ((uintptr_t)s & ALIGN) && n && *s != c; s++, n--) 45*4874ddfdSEd Maste ; 466b62f424SEd Maste if (n && *s != c) { 476b62f424SEd Maste typedef size_t __attribute__((__may_alias__)) word; 486b62f424SEd Maste const word *w; 496b62f424SEd Maste size_t k = ONES * c; 50*4874ddfdSEd Maste for (w = (const void *)s; n >= SS && !HASZERO(*w ^ k); 51*4874ddfdSEd Maste w++, n -= SS) 52*4874ddfdSEd Maste ; 536b62f424SEd Maste s = (const void *)w; 5458f0484fSRodney W. Grimes } 556b62f424SEd Maste #endif 56*4874ddfdSEd Maste for (; n && *s != c; s++, n--) 57*4874ddfdSEd Maste ; 586b62f424SEd Maste return n ? (void *)s : 0; 5958f0484fSRodney W. Grimes } 60