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