Source-Changes-HG archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
[src/trunk]: src/common/lib/libc/arch/x86_64/string Rewrite bcmp() & memcmp()...
details: https://anonhg.NetBSD.org/src/rev/05c044eaed0e
branches: trunk
changeset: 467150:05c044eaed0e
user: ad <ad%NetBSD.org@localhost>
date: Wed Jan 15 10:56:49 2020 +0000
description:
Rewrite bcmp() & memcmp() to not use REP CMPS. Seems about 5-10x faster for
small strings on modern hardware.
diffstat:
common/lib/libc/arch/x86_64/string/bcmp.S | 77 ++++++++++++++++++++-----
common/lib/libc/arch/x86_64/string/memcmp.S | 86 ++++++++++++++++++++--------
2 files changed, 121 insertions(+), 42 deletions(-)
diffs (191 lines):
diff -r 4c7d5f2def7f -r 05c044eaed0e common/lib/libc/arch/x86_64/string/bcmp.S
--- a/common/lib/libc/arch/x86_64/string/bcmp.S Wed Jan 15 10:25:47 2020 +0000
+++ b/common/lib/libc/arch/x86_64/string/bcmp.S Wed Jan 15 10:56:49 2020 +0000
@@ -1,24 +1,67 @@
+/* $NetBSD: bcmp.S,v 1.4 2020/01/15 10:56:49 ad Exp $ */
+
+/*-
+ * Copyright (c) 2020 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Andrew Doran.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
#include <machine/asm.h>
#if defined(LIBC_SCCS)
- RCSID("$NetBSD: bcmp.S,v 1.3 2014/03/22 19:16:34 jakllsch Exp $")
+ RCSID("$NetBSD: bcmp.S,v 1.4 2020/01/15 10:56:49 ad Exp $")
#endif
ENTRY(bcmp)
- xorl %eax,%eax /* clear return value */
-
- movq %rdx,%rcx /* compare by words */
- shrq $3,%rcx
- repe
- cmpsq
- jne L1
-
- movq %rdx,%rcx /* compare remainder by bytes */
- andq $7,%rcx
- repe
- cmpsb
- je L2
-
-L1: incl %eax
-L2: ret
+ movq %rdx, %rcx /* compare by longs, equality only */
+ shrq $3, %rcx
+ jz 2f
+1:
+ movq (%rdi), %rax
+ cmpq %rax, (%rsi)
+ jne 5f
+ decq %rcx
+ leaq 8(%rdi), %rdi
+ leaq 8(%rsi), %rsi
+ jnz 1b
+2:
+ andl $7, %edx
+ jz 4f
+3:
+ movb (%rdi), %al /* compare by chars, equality only */
+ cmpb %al, (%rsi)
+ jne 5f
+ decl %edx
+ leaq 1(%rdi), %rdi
+ leaq 1(%rsi), %rsi
+ jnz 3b
+4:
+ xorl %eax, %eax
+ ret
+5:
+ movl $1, %eax
+ ret
END(bcmp)
diff -r 4c7d5f2def7f -r 05c044eaed0e common/lib/libc/arch/x86_64/string/memcmp.S
--- a/common/lib/libc/arch/x86_64/string/memcmp.S Wed Jan 15 10:25:47 2020 +0000
+++ b/common/lib/libc/arch/x86_64/string/memcmp.S Wed Jan 15 10:56:49 2020 +0000
@@ -1,3 +1,34 @@
+/* $NetBSD: memcmp.S,v 1.4 2020/01/15 10:56:49 ad Exp $ */
+
+/*-
+ * Copyright (c) 2020 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Andrew Doran.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
/*
* Written by J.T. Conklin <jtc%NetBSD.org@localhost>.
* Public domain.
@@ -7,34 +38,39 @@
#include <machine/asm.h>
#if defined(LIBC_SCCS)
- RCSID("$NetBSD: memcmp.S,v 1.3 2014/03/22 19:16:34 jakllsch Exp $")
+ RCSID("$NetBSD: memcmp.S,v 1.4 2020/01/15 10:56:49 ad Exp $")
#endif
ENTRY(memcmp)
- movq %rdx,%rcx /* compare by longs */
- shrq $3,%rcx
- repe
- cmpsq
- jne L5 /* do we match so far? */
-
- movq %rdx,%rcx /* compare remainder by bytes */
- andq $7,%rcx
- repe
- cmpsb
- jne L6 /* do we match? */
-
- xorl %eax,%eax /* we match, return zero */
+ movq %rdx, %rcx /* compare by longs, equality only */
+ shrq $3, %rcx
+ jz 2f
+1:
+ movq (%rdi), %rax
+ cmpq %rax, (%rsi)
+ jne 6f
+ decq %rcx
+ leaq 8(%rdi), %rdi
+ leaq 8(%rsi), %rsi
+ jnz 1b
+2:
+ andl $7, %edx
+ jz 5f
+3:
+ movb (%rdi), %al /* compare by chars, find difference */
+ subb (%rsi), %al
+ jne 4f
+ decl %edx
+ leaq 1(%rdi), %rdi
+ leaq 1(%rsi), %rsi
+ jnz 3b
+4:
+ movsbl %al, %eax
ret
-
-L5: movl $8,%ecx /* We know that one of the next */
- subq %rcx,%rdi /* eight pairs of bytes do not */
- subq %rcx,%rsi /* match. */
- repe
- cmpsb
-L6: xorl %eax,%eax /* Perform unsigned comparison */
- movb -1(%rdi),%al
- xorl %edx,%edx
- movb -1(%rsi),%dl
- subl %edx,%eax
+5:
+ xorl %eax, %eax
ret
+6:
+ movl $8, %edx
+ jmp 3b
END(memcmp)
Home |
Main Index |
Thread Index |
Old Index