xref: /netbsd-src/common/lib/libc/string/bcmp.c (revision e7cb9801cec2842583be4c12ac76049b6bbeb02c)
1*e7cb9801Sad /*	$NetBSD: bcmp.c,v 1.10 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) 1987, 1993
3437c9f0a6Schristos  *	The Regents of the University of California.  All rights reserved.
3537c9f0a6Schristos  *
3637c9f0a6Schristos  * Redistribution and use in source and binary forms, with or without
3737c9f0a6Schristos  * modification, are permitted provided that the following conditions
3837c9f0a6Schristos  * are met:
3937c9f0a6Schristos  * 1. Redistributions of source code must retain the above copyright
4037c9f0a6Schristos  *    notice, this list of conditions and the following disclaimer.
4137c9f0a6Schristos  * 2. Redistributions in binary form must reproduce the above copyright
4237c9f0a6Schristos  *    notice, this list of conditions and the following disclaimer in the
4337c9f0a6Schristos  *    documentation and/or other materials provided with the distribution.
4437c9f0a6Schristos  * 3. Neither the name of the University nor the names of its contributors
4537c9f0a6Schristos  *    may be used to endorse or promote products derived from this software
4637c9f0a6Schristos  *    without specific prior written permission.
4737c9f0a6Schristos  *
4837c9f0a6Schristos  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
4937c9f0a6Schristos  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
5037c9f0a6Schristos  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
5137c9f0a6Schristos  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
5237c9f0a6Schristos  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
5337c9f0a6Schristos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
5437c9f0a6Schristos  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
5537c9f0a6Schristos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
5637c9f0a6Schristos  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
5737c9f0a6Schristos  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
5837c9f0a6Schristos  * SUCH DAMAGE.
5937c9f0a6Schristos  */
6037c9f0a6Schristos 
6137c9f0a6Schristos #include <sys/cdefs.h>
6237c9f0a6Schristos #if defined(LIBC_SCCS) && !defined(lint)
6337c9f0a6Schristos #if 0
6437c9f0a6Schristos static char sccsid[] = "@(#)bcmp.c	8.1 (Berkeley) 6/4/93";
6537c9f0a6Schristos #else
66*e7cb9801Sad __RCSID("$NetBSD: bcmp.c,v 1.10 2020/01/29 09:18:26 ad Exp $");
6737c9f0a6Schristos #endif
6837c9f0a6Schristos #endif /* LIBC_SCCS and not lint */
6937c9f0a6Schristos 
7037c9f0a6Schristos 
71d8d70d1bShe #if defined(_KERNEL) || defined(_STANDALONE)
72d0b9e6c9Stsutsui #include <lib/libkern/libkern.h>
73d0b9e6c9Stsutsui #if defined(_STANDALONE)
74d0b9e6c9Stsutsui #include <lib/libsa/stand.h>
750e68e441Sskrll #endif
76d0b9e6c9Stsutsui #else
7742a88f8eSad #include <sys/types.h>
7842a88f8eSad 
7937c9f0a6Schristos #include <assert.h>
8037c9f0a6Schristos #include <string.h>
8137c9f0a6Schristos #endif
82245ee9afShe 
8337c9f0a6Schristos /*
8437c9f0a6Schristos  * bcmp -- vax cmpc3 instruction
8537c9f0a6Schristos  */
8637c9f0a6Schristos int
bcmp(const void * s1,const void * s2,size_t n)8742a88f8eSad bcmp(const void *s1, const void *s2, size_t n)
8837c9f0a6Schristos {
8942a88f8eSad 	const unsigned char *c1, *c2;
9037c9f0a6Schristos 
91*e7cb9801Sad #ifndef _STANDALONE
92*e7cb9801Sad 	const uintptr_t *b1, *b2;
93*e7cb9801Sad 
9442a88f8eSad 	b1 = s1;
9542a88f8eSad 	b2 = s2;
9637c9f0a6Schristos 
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 				return 1;
10442a88f8eSad 			n -= sizeof(uintptr_t);
10542a88f8eSad 		}
10642a88f8eSad 	}
10742a88f8eSad 
10842a88f8eSad 	c1 = (const unsigned char *)b1;
10942a88f8eSad 	c2 = (const unsigned char *)b2;
110*e7cb9801Sad #else
111*e7cb9801Sad 	c1 = (const unsigned char *)s1;
112*e7cb9801Sad 	c2 = (const unsigned char *)s2;
113*e7cb9801Sad #endif
11442a88f8eSad 
11542a88f8eSad 	if (n != 0) {
11642a88f8eSad 		do {
11742a88f8eSad 			if (*c1++ != *c2++)
11842a88f8eSad 				return 1;
11942a88f8eSad 		} while (--n != 0);
12042a88f8eSad 	}
12142a88f8eSad 
12242a88f8eSad 	return 0;
12337c9f0a6Schristos }
124