xref: /netbsd-src/common/lib/libc/string/memcmp.c (revision e7cb9801cec2842583be4c12ac76049b6bbeb02c)
1*e7cb9801Sad /*	$NetBSD: memcmp.c,v 1.8 2020/01/29 09:18:26 ad Exp $	*/
242a88f8eSad 
342a88f8eSad /*-
442a88f8eSad  * Copyright (c) 2020 The NetBSD Foundation, Inc.
542a88f8eSad  * All rights reserved.
642a88f8eSad  *
742a88f8eSad  * This code is derived from software contributed to The NetBSD Foundation
842a88f8eSad  * by Andrew Doran.
942a88f8eSad  *
1042a88f8eSad  * Redistribution and use in source and binary forms, with or without
1142a88f8eSad  * modification, are permitted provided that the following conditions
1242a88f8eSad  * are met:
1342a88f8eSad  * 1. Redistributions of source code must retain the above copyright
1442a88f8eSad  *    notice, this list of conditions and the following disclaimer.
1542a88f8eSad  * 2. Redistributions in binary form must reproduce the above copyright
1642a88f8eSad  *    notice, this list of conditions and the following disclaimer in the
1742a88f8eSad  *    documentation and/or other materials provided with the distribution.
1842a88f8eSad  *
1942a88f8eSad  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2042a88f8eSad  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2142a88f8eSad  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2242a88f8eSad  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2342a88f8eSad  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2442a88f8eSad  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2542a88f8eSad  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2642a88f8eSad  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2742a88f8eSad  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2842a88f8eSad  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2942a88f8eSad  * POSSIBILITY OF SUCH DAMAGE.
3042a88f8eSad  */
3137c9f0a6Schristos 
3237c9f0a6Schristos /*-
3337c9f0a6Schristos  * Copyright (c) 1990, 1993
3437c9f0a6Schristos  *	The Regents of the University of California.  All rights reserved.
3537c9f0a6Schristos  *
3637c9f0a6Schristos  * This code is derived from software contributed to Berkeley by
3737c9f0a6Schristos  * Chris Torek.
3837c9f0a6Schristos  *
3937c9f0a6Schristos  * Redistribution and use in source and binary forms, with or without
4037c9f0a6Schristos  * modification, are permitted provided that the following conditions
4137c9f0a6Schristos  * are met:
4237c9f0a6Schristos  * 1. Redistributions of source code must retain the above copyright
4337c9f0a6Schristos  *    notice, this list of conditions and the following disclaimer.
4437c9f0a6Schristos  * 2. Redistributions in binary form must reproduce the above copyright
4537c9f0a6Schristos  *    notice, this list of conditions and the following disclaimer in the
4637c9f0a6Schristos  *    documentation and/or other materials provided with the distribution.
4737c9f0a6Schristos  * 3. Neither the name of the University nor the names of its contributors
4837c9f0a6Schristos  *    may be used to endorse or promote products derived from this software
4937c9f0a6Schristos  *    without specific prior written permission.
5037c9f0a6Schristos  *
5137c9f0a6Schristos  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
5237c9f0a6Schristos  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
5337c9f0a6Schristos  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
5437c9f0a6Schristos  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
5537c9f0a6Schristos  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
5637c9f0a6Schristos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
5737c9f0a6Schristos  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
5837c9f0a6Schristos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
5937c9f0a6Schristos  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
6037c9f0a6Schristos  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
6137c9f0a6Schristos  * SUCH DAMAGE.
6237c9f0a6Schristos  */
6337c9f0a6Schristos 
6437c9f0a6Schristos #include <sys/cdefs.h>
6537c9f0a6Schristos #if defined(LIBC_SCCS) && !defined(lint)
6637c9f0a6Schristos #if 0
6737c9f0a6Schristos static char sccsid[] = "@(#)memcmp.c	8.1 (Berkeley) 6/4/93";
6837c9f0a6Schristos #else
69*e7cb9801Sad __RCSID("$NetBSD: memcmp.c,v 1.8 2020/01/29 09:18:26 ad Exp $");
7037c9f0a6Schristos #endif
7137c9f0a6Schristos #endif /* LIBC_SCCS and not lint */
7237c9f0a6Schristos 
7337c9f0a6Schristos #if !defined(_KERNEL) && !defined(_STANDALONE)
7442a88f8eSad #include <sys/types.h>
7542a88f8eSad 
7637c9f0a6Schristos #include <assert.h>
7737c9f0a6Schristos #include <string.h>
7837c9f0a6Schristos #else
7937c9f0a6Schristos #include <lib/libkern/libkern.h>
8037c9f0a6Schristos #endif
8137c9f0a6Schristos 
824041630eSjoerg #undef memcmp
8337c9f0a6Schristos /*
8437c9f0a6Schristos  * Compare memory regions.
8537c9f0a6Schristos  */
8637c9f0a6Schristos int
memcmp(const void * s1,const void * s2,size_t n)87a8565cf9Schristos memcmp(const void *s1, const void *s2, size_t n)
8837c9f0a6Schristos {
8942a88f8eSad 	const unsigned char *c1, *c2;
9042a88f8eSad 
91*e7cb9801Sad #ifndef _STANDALONE
92*e7cb9801Sad 	const uintptr_t *b1, *b2;
93*e7cb9801Sad 
9442a88f8eSad 	b1 = s1;
9542a88f8eSad 	b2 = s2;
9642a88f8eSad 
9703821078Sad #ifndef __NO_STRICT_ALIGNMENT
9803821078Sad 	if ((((uintptr_t)b1 | (uintptr_t)b2) & (sizeof(uintptr_t) - 1)) == 0)
9903821078Sad #endif
10003821078Sad 	{
10142a88f8eSad 		while (n >= sizeof(uintptr_t)) {
10242a88f8eSad 			if (*b1 != *b2)
10342a88f8eSad 				break;
10442a88f8eSad 			b1++;
10542a88f8eSad 			b2++;
10642a88f8eSad 			n -= sizeof(uintptr_t);
10742a88f8eSad 		}
10842a88f8eSad 	}
10942a88f8eSad 
11042a88f8eSad 	c1 = (const unsigned char *)b1;
11142a88f8eSad 	c2 = (const unsigned char *)b2;
112*e7cb9801Sad #else
113*e7cb9801Sad 	c1 = (const unsigned char *)s1;
114*e7cb9801Sad 	c2 = (const unsigned char *)s2;
115*e7cb9801Sad #endif
11637c9f0a6Schristos 
11737c9f0a6Schristos 	if (n != 0) {
11837c9f0a6Schristos 		do {
11942a88f8eSad 			if (*c1++ != *c2++)
12042a88f8eSad 				return *--c1 - *--c2;
12137c9f0a6Schristos 		} while (--n != 0);
12237c9f0a6Schristos 	}
12342a88f8eSad 
12442a88f8eSad 	return 0;
12537c9f0a6Schristos }
12696f5bbd2Sjoerg 
12796f5bbd2Sjoerg #if defined(__ARM_EABI__)
12896f5bbd2Sjoerg __strong_alias(__aeabi_memcmp, memcmp)
12996f5bbd2Sjoerg #endif
130