xref: /freebsd/lib/libc/string/memchr.c (revision 559a218c9b257775fb249b67945fe4a05b7a6b9f)
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  */
256b62f424SEd Maste #include <limits.h>
26*4874ddfdSEd Maste #include <stdint.h>
27*4874ddfdSEd Maste #include <string.h>
2858f0484fSRodney W. Grimes 
296b62f424SEd Maste #define SS (sizeof(size_t))
306b62f424SEd Maste #define ALIGN (sizeof(size_t) - 1)
316b62f424SEd Maste #define ONES ((size_t)-1 / UCHAR_MAX)
326b62f424SEd Maste #define HIGHS (ONES * (UCHAR_MAX / 2 + 1))
336b62f424SEd Maste #define HASZERO(x) (((x)-ONES) & ~(x)&HIGHS)
346b62f424SEd Maste 
35*4874ddfdSEd Maste void *
memchr(const void * src,int c,size_t n)36*4874ddfdSEd Maste memchr(const void *src, int c, size_t n)
3758f0484fSRodney W. Grimes {
386b62f424SEd Maste 	const unsigned char *s = src;
396b62f424SEd Maste 	c = (unsigned char)c;
406b62f424SEd Maste #ifdef __GNUC__
41*4874ddfdSEd Maste 	for (; ((uintptr_t)s & ALIGN) && n && *s != c; s++, n--)
42*4874ddfdSEd Maste 		;
436b62f424SEd Maste 	if (n && *s != c) {
446b62f424SEd Maste 		typedef size_t __attribute__((__may_alias__)) word;
456b62f424SEd Maste 		const word *w;
466b62f424SEd Maste 		size_t k = ONES * c;
47*4874ddfdSEd Maste 		for (w = (const void *)s; n >= SS && !HASZERO(*w ^ k);
48*4874ddfdSEd Maste 		     w++, n -= SS)
49*4874ddfdSEd Maste 			;
506b62f424SEd Maste 		s = (const void *)w;
5158f0484fSRodney W. Grimes 	}
526b62f424SEd Maste #endif
53*4874ddfdSEd Maste 	for (; n && *s != c; s++, n--)
54*4874ddfdSEd Maste 		;
556b62f424SEd Maste 	return n ? (void *)s : 0;
5658f0484fSRodney W. Grimes }
57