xref: /netbsd-src/common/lib/libc/string/bcmp.c (revision e7cb9801cec2842583be4c12ac76049b6bbeb02c)
1 /*	$NetBSD: bcmp.c,v 1.10 2020/01/29 09:18:26 ad Exp $	*/
2 
3 /*-
4  * Copyright (c) 2020 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Andrew Doran.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 /*
33  * Copyright (c) 1987, 1993
34  *	The Regents of the University of California.  All rights reserved.
35  *
36  * Redistribution and use in source and binary forms, with or without
37  * modification, are permitted provided that the following conditions
38  * are met:
39  * 1. Redistributions of source code must retain the above copyright
40  *    notice, this list of conditions and the following disclaimer.
41  * 2. Redistributions in binary form must reproduce the above copyright
42  *    notice, this list of conditions and the following disclaimer in the
43  *    documentation and/or other materials provided with the distribution.
44  * 3. Neither the name of the University nor the names of its contributors
45  *    may be used to endorse or promote products derived from this software
46  *    without specific prior written permission.
47  *
48  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58  * SUCH DAMAGE.
59  */
60 
61 #include <sys/cdefs.h>
62 #if defined(LIBC_SCCS) && !defined(lint)
63 #if 0
64 static char sccsid[] = "@(#)bcmp.c	8.1 (Berkeley) 6/4/93";
65 #else
66 __RCSID("$NetBSD: bcmp.c,v 1.10 2020/01/29 09:18:26 ad Exp $");
67 #endif
68 #endif /* LIBC_SCCS and not lint */
69 
70 
71 #if defined(_KERNEL) || defined(_STANDALONE)
72 #include <lib/libkern/libkern.h>
73 #if defined(_STANDALONE)
74 #include <lib/libsa/stand.h>
75 #endif
76 #else
77 #include <sys/types.h>
78 
79 #include <assert.h>
80 #include <string.h>
81 #endif
82 
83 /*
84  * bcmp -- vax cmpc3 instruction
85  */
86 int
bcmp(const void * s1,const void * s2,size_t n)87 bcmp(const void *s1, const void *s2, size_t n)
88 {
89 	const unsigned char *c1, *c2;
90 
91 #ifndef _STANDALONE
92 	const uintptr_t *b1, *b2;
93 
94 	b1 = s1;
95 	b2 = s2;
96 
97 #ifndef __NO_STRICT_ALIGNMENT
98 	if ((((uintptr_t)b1 | (uintptr_t)b2) & (sizeof(uintptr_t) - 1)) == 0)
99 #endif
100 	{
101 		while (n >= sizeof(uintptr_t)) {
102 			if (*b1++ != *b2++)
103 				return 1;
104 			n -= sizeof(uintptr_t);
105 		}
106 	}
107 
108 	c1 = (const unsigned char *)b1;
109 	c2 = (const unsigned char *)b2;
110 #else
111 	c1 = (const unsigned char *)s1;
112 	c2 = (const unsigned char *)s2;
113 #endif
114 
115 	if (n != 0) {
116 		do {
117 			if (*c1++ != *c2++)
118 				return 1;
119 		} while (--n != 0);
120 	}
121 
122 	return 0;
123 }
124