bcmp() / memcmp(): compare in uintptr_t sized chunks when it's easy to.diff -r1.7 -r1.8 src/common/lib/libc/string/bcmp.c
(ad)
--- src/common/lib/libc/string/bcmp.c 2012/03/09 15:41:16 1.7
+++ src/common/lib/libc/string/bcmp.c 2020/01/27 22:13:39 1.8
@@ -1,14 +1,43 @@ | @@ -1,14 +1,43 @@ | |||
1 | /* $NetBSD: bcmp.c,v 1.7 2012/03/09 15:41:16 christos Exp $ */ | 1 | /* $NetBSD: bcmp.c,v 1.8 2020/01/27 22:13:39 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 | */ | |||
2 | 31 | |||
3 | /* | 32 | /* | |
4 | * Copyright (c) 1987, 1993 | 33 | * Copyright (c) 1987, 1993 | |
5 | * The Regents of the University of California. All rights reserved. | 34 | * The Regents of the University of California. All rights reserved. | |
6 | * | 35 | * | |
7 | * Redistribution and use in source and binary forms, with or without | 36 | * Redistribution and use in source and binary forms, with or without | |
8 | * modification, are permitted provided that the following conditions | 37 | * modification, are permitted provided that the following conditions | |
9 | * are met: | 38 | * are met: | |
10 | * 1. Redistributions of source code must retain the above copyright | 39 | * 1. Redistributions of source code must retain the above copyright | |
11 | * notice, this list of conditions and the following disclaimer. | 40 | * notice, this list of conditions and the following disclaimer. | |
12 | * 2. Redistributions in binary form must reproduce the above copyright | 41 | * 2. Redistributions in binary form must reproduce the above copyright | |
13 | * notice, this list of conditions and the following disclaimer in the | 42 | * notice, this list of conditions and the following disclaimer in the | |
14 | * documentation and/or other materials provided with the distribution. | 43 | * documentation and/or other materials provided with the distribution. | |
@@ -24,47 +53,62 @@ | @@ -24,47 +53,62 @@ | |||
24 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | 53 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
25 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | 54 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | 55 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
27 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | 56 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
28 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | 57 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
29 | * SUCH DAMAGE. | 58 | * SUCH DAMAGE. | |
30 | */ | 59 | */ | |
31 | 60 | |||
32 | #include <sys/cdefs.h> | 61 | #include <sys/cdefs.h> | |
33 | #if defined(LIBC_SCCS) && !defined(lint) | 62 | #if defined(LIBC_SCCS) && !defined(lint) | |
34 | #if 0 | 63 | #if 0 | |
35 | static char sccsid[] = "@(#)bcmp.c 8.1 (Berkeley) 6/4/93"; | 64 | static char sccsid[] = "@(#)bcmp.c 8.1 (Berkeley) 6/4/93"; | |
36 | #else | 65 | #else | |
37 | __RCSID("$NetBSD: bcmp.c,v 1.7 2012/03/09 15:41:16 christos Exp $"); | 66 | __RCSID("$NetBSD: bcmp.c,v 1.8 2020/01/27 22:13:39 ad Exp $"); | |
38 | #endif | 67 | #endif | |
39 | #endif /* LIBC_SCCS and not lint */ | 68 | #endif /* LIBC_SCCS and not lint */ | |
40 | 69 | |||
41 | 70 | |||
42 | #if defined(_KERNEL) || defined(_STANDALONE) | 71 | #if defined(_KERNEL) || defined(_STANDALONE) | |
43 | #include <lib/libkern/libkern.h> | 72 | #include <lib/libkern/libkern.h> | |
44 | #if defined(_STANDALONE) | 73 | #if defined(_STANDALONE) | |
45 | #include <lib/libsa/stand.h> | 74 | #include <lib/libsa/stand.h> | |
46 | #endif | 75 | #endif | |
47 | #else | 76 | #else | |
77 | #include <sys/types.h> | |||
78 | ||||
48 | #include <assert.h> | 79 | #include <assert.h> | |
49 | #include <string.h> | 80 | #include <string.h> | |
50 | #endif | 81 | #endif | |
51 | 82 | |||
52 | /* | 83 | /* | |
53 | * bcmp -- vax cmpc3 instruction | 84 | * bcmp -- vax cmpc3 instruction | |
54 | */ | 85 | */ | |
55 | int | 86 | int | |
56 | bcmp(const void *b1, const void *b2, size_t length) | 87 | bcmp(const void *s1, const void *s2, size_t n) | |
57 | { | 88 | { | |
58 | const char *p1 = b1, *p2 = b2; | 89 | const uintptr_t *b1, *b2; | |
90 | const unsigned char *c1, *c2; | |||
91 | ||||
92 | b1 = s1; | |||
93 | b2 = s2; | |||
59 | 94 | |||
60 | _DIAGASSERT(b1 != 0); | 95 | if ((((uintptr_t)b1 | (uintptr_t)b2) & (sizeof(uintptr_t) - 1)) == 0) { | |
61 | _DIAGASSERT(b2 != 0); | 96 | while (n >= sizeof(uintptr_t)) { | |
97 | if (*b1++ != *b2++) | |||
98 | return 1; | |||
99 | n -= sizeof(uintptr_t); | |||
100 | } | |||
101 | } | |||
102 | ||||
103 | c1 = (const unsigned char *)b1; | |||
104 | c2 = (const unsigned char *)b2; | |||
105 | ||||
106 | if (n != 0) { | |||
107 | do { | |||
108 | if (*c1++ != *c2++) | |||
109 | return 1; | |||
110 | } while (--n != 0); | |||
111 | } | |||
62 | 112 | |||
63 | if (length == 0) | 113 | return 0; | |
64 | return(0); | |||
65 | do | |||
66 | if (*p1++ != *p2++) | |||
67 | break; | |||
68 | while (--length); | |||
69 | return length != 0; | |||
70 | } | 114 | } |
--- src/common/lib/libc/string/memcmp.c 2018/02/04 20:22:17 1.5
+++ src/common/lib/libc/string/memcmp.c 2020/01/27 22:13:39 1.6
@@ -1,14 +1,43 @@ | @@ -1,14 +1,43 @@ | |||
1 | /* $NetBSD: memcmp.c,v 1.5 2018/02/04 20:22:17 mrg Exp $ */ | 1 | /* $NetBSD: memcmp.c,v 1.6 2020/01/27 22:13:39 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 | */ | |||
2 | 31 | |||
3 | /*- | 32 | /*- | |
4 | * Copyright (c) 1990, 1993 | 33 | * Copyright (c) 1990, 1993 | |
5 | * The Regents of the University of California. All rights reserved. | 34 | * The Regents of the University of California. All rights reserved. | |
6 | * | 35 | * | |
7 | * This code is derived from software contributed to Berkeley by | 36 | * This code is derived from software contributed to Berkeley by | |
8 | * Chris Torek. | 37 | * Chris Torek. | |
9 | * | 38 | * | |
10 | * Redistribution and use in source and binary forms, with or without | 39 | * Redistribution and use in source and binary forms, with or without | |
11 | * modification, are permitted provided that the following conditions | 40 | * modification, are permitted provided that the following conditions | |
12 | * are met: | 41 | * are met: | |
13 | * 1. Redistributions of source code must retain the above copyright | 42 | * 1. Redistributions of source code must retain the above copyright | |
14 | * notice, this list of conditions and the following disclaimer. | 43 | * notice, this list of conditions and the following disclaimer. | |
@@ -27,46 +56,65 @@ | @@ -27,46 +56,65 @@ | |||
27 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | 56 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
28 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | 57 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
29 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | 58 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
30 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | 59 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
31 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | 60 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
32 | * SUCH DAMAGE. | 61 | * SUCH DAMAGE. | |
33 | */ | 62 | */ | |
34 | 63 | |||
35 | #include <sys/cdefs.h> | 64 | #include <sys/cdefs.h> | |
36 | #if defined(LIBC_SCCS) && !defined(lint) | 65 | #if defined(LIBC_SCCS) && !defined(lint) | |
37 | #if 0 | 66 | #if 0 | |
38 | static char sccsid[] = "@(#)memcmp.c 8.1 (Berkeley) 6/4/93"; | 67 | static char sccsid[] = "@(#)memcmp.c 8.1 (Berkeley) 6/4/93"; | |
39 | #else | 68 | #else | |
40 | __RCSID("$NetBSD: memcmp.c,v 1.5 2018/02/04 20:22:17 mrg Exp $"); | 69 | __RCSID("$NetBSD: memcmp.c,v 1.6 2020/01/27 22:13:39 ad Exp $"); | |
41 | #endif | 70 | #endif | |
42 | #endif /* LIBC_SCCS and not lint */ | 71 | #endif /* LIBC_SCCS and not lint */ | |
43 | 72 | |||
44 | #if !defined(_KERNEL) && !defined(_STANDALONE) | 73 | #if !defined(_KERNEL) && !defined(_STANDALONE) | |
74 | #include <sys/types.h> | |||
75 | ||||
45 | #include <assert.h> | 76 | #include <assert.h> | |
46 | #include <string.h> | 77 | #include <string.h> | |
47 | #else | 78 | #else | |
48 | #include <lib/libkern/libkern.h> | 79 | #include <lib/libkern/libkern.h> | |
49 | #endif | 80 | #endif | |
50 | 81 | |||
51 | #undef memcmp | 82 | #undef memcmp | |
52 | /* | 83 | /* | |
53 | * Compare memory regions. | 84 | * Compare memory regions. | |
54 | */ | 85 | */ | |
55 | int | 86 | int | |
56 | memcmp(const void *s1, const void *s2, size_t n) | 87 | memcmp(const void *s1, const void *s2, size_t n) | |
57 | { | 88 | { | |
89 | const uintptr_t *b1, *b2; | |||
90 | const unsigned char *c1, *c2; | |||
58 | 91 | |||
59 | if (n != 0) { | 92 | b1 = s1; | |
60 | const unsigned char *p1 = s1, *p2 = s2; | 93 | b2 = s2; | |
61 | 94 | |||
95 | if ((((uintptr_t)b1 | (uintptr_t)b2) & (sizeof(uintptr_t) - 1)) == 0) { | |||
96 | while (n >= sizeof(uintptr_t)) { | |||
97 | if (*b1 != *b2) | |||
98 | break; | |||
99 | b1++; | |||
100 | b2++; | |||
101 | n -= sizeof(uintptr_t); | |||
102 | } | |||
103 | } | |||
104 | ||||
105 | c1 = (const unsigned char *)b1; | |||
106 | c2 = (const unsigned char *)b2; | |||
107 | ||||
108 | if (n != 0) { | |||
62 | do { | 109 | do { | |
63 | if (*p1++ != *p2++) | 110 | if (*c1++ != *c2++) | |
64 | return (*--p1 - *--p2); | 111 | return *--c1 - *--c2; | |
65 | } while (--n != 0); | 112 | } while (--n != 0); | |
66 | } | 113 | } | |
67 | return (0); | 114 | ||
115 | return 0; | |||
68 | } | 116 | } | |
69 | 117 | |||
70 | #if defined(__ARM_EABI__) | 118 | #if defined(__ARM_EABI__) | |
71 | __strong_alias(__aeabi_memcmp, memcmp) | 119 | __strong_alias(__aeabi_memcmp, memcmp) | |
72 | #endif | 120 | #endif |