aarch64: MTE compatible strcmp

Message ID VI1PR08MB3663E2D23A8BAFE3ADF6EBDDED9D0@VI1PR08MB3663.eurprd08.prod.outlook.com
State Committed
Headers
Series aarch64: MTE compatible strcmp |

Commit Message

Alex Butler June 16, 2020, 12:42 p.m. UTC
  This patch adds an MTE compatible implementation of strcmp.

Please see the benchmark results for the performance uplift.

| length | align1 | align2 | uplift A72 | uplift A53 | uplift N1 |
|      1 |      1 |      1 |      0.87x |      1.03x |     1.06x |
|      1 |      1 |      1 |      0.95x |      1.03x |     0.90x |
|      1 |      1 |      1 |      0.95x |      1.03x |     0.90x |
|      2 |      2 |      2 |      0.95x |      1.03x |     1.08x |
|      2 |      2 |      2 |      0.95x |      1.08x |     1.08x |
|      2 |      2 |      2 |      0.83x |      1.03x |     1.08x |
|      3 |      3 |      3 |      0.95x |      1.03x |     1.17x |
|      3 |      3 |      3 |      0.95x |      1.03x |     1.08x |
|      3 |      3 |      3 |      0.95x |      1.04x |     1.08x |
|      4 |      4 |      4 |      0.61x |      1.10x |     0.95x |
|      4 |      4 |      4 |      0.95x |      1.03x |     1.05x |
|      4 |      4 |      4 |      0.95x |      1.02x |     1.08x |
|      5 |      5 |      5 |      0.59x |      1.09x |     0.95x |
|      5 |      5 |      5 |      0.59x |      1.10x |     0.95x |
|      5 |      5 |      5 |      0.59x |      1.10x |     0.95x |
|      6 |      6 |      6 |      0.62x |      1.08x |     0.95x |
|      6 |      6 |      6 |      0.62x |      1.10x |     0.78x |
|      6 |      6 |      6 |      0.62x |      1.10x |     0.95x |
|      7 |      7 |      7 |      0.62x |      1.09x |     0.95x |
|      7 |      7 |      7 |      0.62x |      1.10x |     0.95x |
|      7 |      7 |      7 |      0.62x |      1.11x |     1.01x |
|      8 |      8 |      8 |      1.68x |      0.95x |     0.96x |
|      8 |      8 |      8 |      0.93x |      0.96x |     1.03x |
|      8 |      8 |      8 |      0.95x |      0.92x |     1.03x |
|      9 |      9 |      9 |      0.62x |      1.10x |     0.96x |
|      9 |      9 |      9 |      0.62x |      1.10x |     0.95x |
|      9 |      9 |      9 |      0.60x |      1.10x |     0.98x |
|     10 |     10 |     10 |      0.59x |      1.10x |     0.96x |
|     10 |     10 |     10 |      0.59x |      1.10x |     0.96x |
|     10 |     10 |     10 |      0.59x |      1.09x |     0.96x |
|     11 |     11 |     11 |      0.59x |      1.10x |     0.96x |
|     11 |     11 |     11 |      0.59x |      1.10x |     0.96x |
|     11 |     11 |     11 |      0.59x |      1.10x |     0.96x |
|     12 |     12 |     12 |      0.98x |      1.06x |     1.00x |
|     12 |     12 |     12 |      0.62x |      1.10x |     0.80x |
|     12 |     12 |     12 |      0.61x |      1.10x |     0.96x |
|     13 |     13 |     13 |      1.00x |      1.05x |     1.00x |
|     13 |     13 |     13 |      1.00x |      1.07x |     1.00x |
|     13 |     13 |     13 |      1.00x |      1.05x |     1.00x |
|     14 |     14 |     14 |      1.00x |      1.07x |     1.00x |
|     14 |     14 |     14 |      1.01x |      1.05x |     1.00x |
|     14 |     14 |     14 |      1.00x |      1.07x |     1.00x |
|     15 |     15 |     15 |      0.99x |      1.05x |     1.12x |
|     15 |     15 |     15 |      1.00x |      1.07x |     1.12x |
|     15 |     15 |     15 |      1.00x |      1.05x |     1.12x |
|     16 |     16 |     16 |      1.00x |      1.03x |     1.01x |
|     16 |     16 |     16 |      1.76x |      1.00x |     0.97x |
|     16 |     16 |     16 |      1.86x |      1.00x |     0.97x |
|     17 |     17 |     17 |      1.00x |      1.06x |     1.00x |
|     17 |     17 |     17 |      1.00x |      1.06x |     1.01x |
|     17 |     17 |     17 |      1.00x |      1.05x |     1.00x |
|     18 |     18 |     18 |      1.00x |      1.07x |     1.00x |
|     18 |     18 |     18 |      1.01x |      1.05x |     1.00x |
|     18 |     18 |     18 |      1.00x |      1.06x |     1.00x |
|     19 |     19 |     19 |      1.00x |      1.05x |     1.00x |
|     19 |     19 |     19 |      1.01x |      1.06x |     1.00x |
|     19 |     19 |     19 |      1.00x |      1.06x |     1.00x |
|     20 |     20 |     20 |      1.03x |      1.07x |     1.04x |
|     20 |     20 |     20 |      1.00x |      1.07x |     1.00x |
|     20 |     20 |     20 |      0.99x |      1.06x |     1.00x |
|     21 |     21 |     21 |      1.03x |      1.07x |     1.04x |
|     21 |     21 |     21 |      1.03x |      1.07x |     1.04x |
|     21 |     21 |     21 |      1.05x |      1.07x |     1.04x |
|     22 |     22 |     22 |      1.03x |      1.08x |     1.04x |
|     22 |     22 |     22 |      1.03x |      1.07x |     1.04x |
|     22 |     22 |     22 |      1.03x |      1.07x |     1.04x |
|     23 |     23 |     23 |      1.02x |      1.07x |     1.03x |
|     23 |     23 |     23 |      1.03x |      1.07x |     1.04x |
|     23 |     23 |     23 |      1.03x |      1.07x |     1.04x |
|     24 |     24 |     24 |      1.04x |      1.04x |     1.05x |
|     24 |     24 |     24 |      1.00x |      1.03x |     0.82x |
|     24 |     24 |     24 |      1.01x |      0.98x |     1.01x |
|     25 |     25 |     25 |      1.03x |      1.07x |     1.04x |
|     25 |     25 |     25 |      1.03x |      1.07x |     1.04x |
|     25 |     25 |     25 |      1.03x |      1.07x |     1.04x |
|     26 |     26 |     26 |      1.03x |      1.07x |     1.04x |
|     26 |     26 |     26 |      1.03x |      1.07x |     1.04x |
|     26 |     26 |     26 |      1.03x |      1.07x |     1.04x |
|     27 |     27 |     27 |      1.03x |      1.07x |     1.04x |
|     27 |     27 |     27 |      1.03x |      1.07x |     1.04x |
|     27 |     27 |     27 |      1.03x |      1.08x |     1.04x |
|     28 |     28 |     28 |      1.05x |      1.09x |     1.06x |
|     28 |     28 |     28 |      1.03x |      1.08x |     1.04x |
|     28 |     28 |     28 |      1.02x |      1.07x |     1.04x |
|     29 |     29 |     29 |      1.06x |      1.09x |     1.09x |
|     29 |     29 |     29 |      1.06x |      1.09x |     1.06x |
|     29 |     29 |     29 |      1.06x |      1.08x |     1.06x |
|     30 |     30 |     30 |      1.06x |      1.09x |     1.06x |
|     30 |     30 |     30 |      1.06x |      1.09x |     1.05x |
|     30 |     30 |     30 |      1.07x |      1.09x |     1.06x |
|     31 |     31 |     31 |      1.06x |      1.09x |     1.06x |
|     31 |     31 |     31 |      1.06x |      1.09x |     1.06x |
|     31 |     31 |     31 |      1.06x |      1.09x |     1.05x |
|      4 |      0 |      0 |      0.94x |      0.96x |     1.01x |
|      4 |      0 |      0 |      0.94x |      0.96x |     1.00x |
|      4 |      0 |      0 |      0.94x |      0.95x |     0.90x |
|      4 |      0 |      0 |      0.94x |      0.96x |     0.90x |
|      4 |      0 |      0 |      0.94x |      0.96x |     0.91x |
|      4 |      0 |      0 |      0.94x |      0.96x |     0.91x |
|      4 |      0 |      1 |      0.92x |      0.93x |     0.73x |
|      4 |      1 |      2 |      0.87x |      0.98x |     0.84x |
|      8 |      0 |      0 |      1.00x |      1.00x |     0.76x |
|      8 |      0 |      0 |      1.00x |      0.96x |     0.75x |
|      8 |      0 |      0 |      0.92x |      0.90x |     1.01x |
|      8 |      0 |      0 |      0.94x |      0.96x |     1.00x |
|      8 |      0 |      0 |      0.94x |      0.97x |     1.01x |
|      8 |      0 |      0 |      0.94x |      0.90x |     0.99x |
|      8 |      0 |      2 |      0.86x |      0.95x |     0.78x |
|      8 |      2 |      3 |      0.99x |      0.96x |     1.22x |
|     16 |      0 |      0 |      1.01x |      1.02x |     1.01x |
|     16 |      0 |      0 |      1.00x |      0.97x |     1.01x |
|     16 |      0 |      0 |      1.00x |      1.00x |     0.76x |
|     16 |      0 |      0 |      1.00x |      1.00x |     0.97x |
|     16 |      0 |      0 |      1.00x |      1.00x |     0.97x |
|     16 |      0 |      0 |      1.00x |      1.00x |     0.97x |
|     16 |      0 |      3 |      0.86x |      1.00x |     0.88x |
|     16 |      3 |      4 |      1.00x |      0.93x |     1.10x |
|     32 |      0 |      0 |      1.07x |      1.04x |     1.08x |
|     32 |      0 |      0 |      1.08x |      1.04x |     1.08x |
|     32 |      0 |      0 |      1.04x |      1.05x |     1.05x |
|     32 |      0 |      0 |      1.04x |      0.96x |     1.05x |
|     32 |      0 |      0 |      1.04x |      0.96x |     1.05x |
|     32 |      0 |      0 |      1.04x |      0.98x |     1.05x |
|     32 |      0 |      4 |      0.91x |      1.03x |     0.93x |
|     32 |      4 |      5 |      0.94x |      1.00x |     1.00x |
|     64 |      0 |      0 |      1.12x |      1.03x |     1.16x |
|     64 |      0 |      0 |      1.11x |      1.03x |     1.16x |
|     64 |      0 |      0 |      1.11x |      1.04x |     1.15x |
|     64 |      0 |      0 |      1.11x |      1.03x |     1.15x |
|     64 |      0 |      0 |      1.11x |      1.03x |     1.02x |
|     64 |      0 |      0 |      1.11x |      1.03x |     1.02x |
|     64 |      0 |      5 |      1.05x |      1.19x |     1.15x |
|     64 |      5 |      6 |      1.04x |      1.14x |     1.12x |
|    128 |      0 |      0 |      1.11x |      0.99x |     1.18x |
|    128 |      0 |      0 |      1.11x |      0.99x |     1.18x |
|    128 |      0 |      0 |      1.09x |      0.99x |     1.18x |
|    128 |      0 |      0 |      1.09x |      0.99x |     1.18x |
|    128 |      0 |      0 |      1.09x |      0.99x |     1.18x |
|    128 |      0 |      0 |      1.09x |      0.99x |     1.18x |
|    128 |      0 |      6 |      1.15x |      1.04x |     1.26x |
|    128 |      6 |      7 |      1.22x |      1.05x |     1.27x |
|    256 |      0 |      0 |      1.13x |      1.00x |     1.19x |
|    256 |      0 |      0 |      1.13x |      1.00x |     1.19x |
|    256 |      0 |      0 |      1.12x |      0.99x |     1.19x |
|    256 |      0 |      0 |      1.12x |      1.00x |     1.19x |
|    256 |      0 |      0 |      1.12x |      1.00x |     1.19x |
|    256 |      0 |      0 |      1.12x |      0.99x |     1.19x |
|    256 |      0 |      7 |      1.53x |      1.08x |     1.33x |
|    256 |      7 |      8 |      1.51x |      1.07x |     1.33x |
|    512 |      0 |      0 |      1.16x |      1.00x |     1.19x |
|    512 |      0 |      0 |      1.15x |      1.00x |     1.19x |
|    512 |      0 |      0 |      1.15x |      1.00x |     1.19x |
|    512 |      0 |      0 |      1.15x |      1.00x |     1.19x |
|    512 |      0 |      0 |      1.18x |      1.00x |     1.19x |
|    512 |      0 |      0 |      1.16x |      1.00x |     1.19x |
|    512 |      0 |      8 |      1.15x |      1.00x |     1.19x |
|    512 |      8 |      9 |      1.35x |      1.10x |     1.37x |
|   1024 |      0 |      0 |      1.19x |      1.00x |     1.19x |
|   1024 |      0 |      0 |      1.20x |      1.00x |     1.19x |
|   1024 |      0 |      0 |      1.19x |      1.00x |     1.19x |
|   1024 |      0 |      0 |      1.16x |      1.00x |     1.19x |
|   1024 |      0 |      0 |      1.16x |      1.00x |     1.19x |
|   1024 |      0 |      0 |      1.17x |      1.00x |     1.19x |
|   1024 |      0 |      9 |      1.39x |      1.12x |     1.40x |
|   1024 |      9 |     10 |      1.34x |      1.11x |     1.35x |
|     16 |      1 |      2 |      0.96x |      0.96x |     1.08x |
|     16 |      2 |      1 |      0.86x |      0.95x |     0.97x |
|     16 |      1 |      2 |      0.97x |      0.96x |     1.08x |
|     16 |      2 |      1 |      0.86x |      0.95x |     0.97x |
|     16 |      1 |      2 |      0.95x |      0.95x |     1.19x |
|     16 |      2 |      1 |      0.86x |      0.95x |     1.07x |
|     32 |      2 |      4 |      0.91x |      0.98x |     1.00x |
|     32 |      4 |      2 |      0.92x |      0.93x |     0.97x |
|     32 |      2 |      4 |      0.89x |      0.96x |     1.00x |
|     32 |      4 |      2 |      0.92x |      0.92x |     0.97x |
|     32 |      2 |      4 |      0.91x |      0.96x |     1.00x |
|     32 |      4 |      2 |      0.92x |      1.00x |     0.97x |
|     64 |      3 |      6 |      1.00x |      1.11x |     1.10x |
|     64 |      6 |      3 |      1.03x |      1.15x |     1.09x |
|     64 |      3 |      6 |      1.01x |      1.11x |     1.11x |
|     64 |      6 |      3 |      1.03x |      1.15x |     1.09x |
|     64 |      3 |      6 |      1.00x |      1.11x |     1.10x |
|     64 |      6 |      3 |      1.02x |      1.16x |     1.09x |
|    128 |      4 |      8 |      1.17x |      1.03x |     1.23x |
|    128 |      8 |      4 |      1.17x |      1.06x |     1.26x |
|    128 |      4 |      8 |      1.20x |      1.05x |     1.24x |
|    128 |      8 |      4 |      1.16x |      1.04x |     1.26x |
|    128 |      4 |      8 |      1.16x |      1.03x |     1.24x |
|    128 |      8 |      4 |      1.15x |      1.04x |     1.26x |
|    256 |      5 |     10 |      1.52x |      1.07x |     1.31x |
|    256 |     10 |      5 |      1.48x |      1.07x |     1.31x |
|    256 |      5 |     10 |      1.52x |      1.07x |     1.33x |
|    256 |     10 |      5 |      1.47x |      1.07x |     1.30x |
|    256 |      5 |     10 |      1.52x |      1.07x |     1.33x |
|    256 |     10 |      5 |      1.48x |      1.08x |     1.30x |
|    512 |      6 |     12 |      1.26x |      1.10x |     1.37x |
|    512 |     12 |      6 |      1.33x |      1.11x |     1.35x |
|    512 |      6 |     12 |      1.27x |      1.10x |     1.37x |
|    512 |     12 |      6 |      1.33x |      1.11x |     1.35x |
|    512 |      6 |     12 |      1.27x |      1.10x |     1.37x |
|    512 |     12 |      6 |      1.33x |      1.11x |     1.35x |
|   1024 |      7 |     14 |      1.39x |      1.12x |     1.45x |
|   1024 |     14 |      7 |      1.32x |      1.13x |     1.41x |
|   1024 |      7 |     14 |      1.39x |      1.12x |     1.45x |
|   1024 |     14 |      7 |      1.32x |      1.13x |     1.42x |
|   1024 |      7 |     14 |      1.39x |      1.12x |     1.45x |
|   1024 |     14 |      7 |      1.33x |      1.13x |     1.41x |

This patch passes the tests with no regressions.

8< --- 8< --- 8<
Add support for MTE to strcmp. Regression tested with xcheck and benchmarked
with glibc's benchtests on the Cortex-A53, Cortex-A72, and Neoverse N1.

The existing implementation assumes that any access to the pages in which the
string resides is safe. This assumption is not true when MTE is enabled. This
patch updates the algorithm to ensure that accesses remain within the bounds
of an MTE tag (16-byte chunks) and improves overall performance.

Co-authored-by: Branislav Rankov <branislav.rankov@arm.com>
Co-authored-by: Wilco Dijkstra <wilco.dijkstra@arm.com>
  

Comments

Szabolcs Nagy June 23, 2020, 4:59 p.m. UTC | #1
The 06/16/2020 12:42, Alex Butler wrote:
> Add support for MTE to strcmp. Regression tested with xcheck and benchmarked
> with glibc's benchtests on the Cortex-A53, Cortex-A72, and Neoverse N1.
> 
> The existing implementation assumes that any access to the pages in which the
> string resides is safe. This assumption is not true when MTE is enabled. This
> patch updates the algorithm to ensure that accesses remain within the bounds
> of an MTE tag (16-byte chunks) and improves overall performance.
> 
> Co-authored-by: Branislav Rankov <branislav.rankov@arm.com>
> Co-authored-by: Wilco Dijkstra <wilco.dijkstra@arm.com>

thanks, committed.
  

Patch

From 2d8bba6aa91151b31472fc1444d7563fac35dfb5 Mon Sep 17 00:00:00 2001
From: Alex Butler <alex.butler@arm.com>
Date: Fri, 12 Jun 2020 13:51:43 +0100
Subject: [PATCH] aarch64: add MTE compatible strcmp

---
 sysdeps/aarch64/strcmp.S | 234 +++++++++++++++++++++++++----------------------
 1 file changed, 125 insertions(+), 109 deletions(-)

diff --git a/sysdeps/aarch64/strcmp.S b/sysdeps/aarch64/strcmp.S
index d044c29..77d7218 100644
--- a/sysdeps/aarch64/strcmp.S
+++ b/sysdeps/aarch64/strcmp.S
@@ -18,14 +18,14 @@ 
 
 /* Assumptions:
  *
- * ARMv8-a, AArch64
+ * ARMv8-a, AArch64.
+ * MTE compatible.
  */
 
 #include <sysdep.h>
 
 #define REP8_01 0x0101010101010101
 #define REP8_7f 0x7f7f7f7f7f7f7f7f
-#define REP8_80 0x8080808080808080
 
 /* Parameters and result.  */
 #define src1		x0
@@ -39,146 +39,162 @@ 
 #define data2w		w3
 #define has_nul		x4
 #define diff		x5
+#define off1		x5
 #define syndrome	x6
-#define tmp1		x7
-#define tmp2		x8
-#define tmp3		x9
-#define zeroones	x10
-#define pos		x11
+#define tmp		x6
+#define data3		x7
+#define zeroones	x8
+#define shift		x9
+#define off2		x10
+
+/* On big-endian early bytes are at MSB and on little-endian LSB.
+   LS_FW means shifting towards early bytes.  */
+#ifdef __AARCH64EB__
+# define LS_FW lsl
+#else
+# define LS_FW lsr
+#endif
 
-	/* Start of performance-critical section  -- one 64B cache line.  */
-ENTRY_ALIGN(strcmp, 6)
+/* NUL detection works on the principle that (X - 1) & (~X) & 0x80
+   (=> (X - 1) & ~(X | 0x7f)) is non-zero iff a byte is zero, and
+   can be done in parallel across the entire word.
+   Since carry propagation makes 0x1 bytes before a NUL byte appear
+   NUL too in big-endian, byte-reverse the data before the NUL check.  */
 
+ENTRY(strcmp)
 	DELOUSE (0)
 	DELOUSE (1)
-	eor	tmp1, src1, src2
-	mov	zeroones, #REP8_01
-	tst	tmp1, #7
+	sub	off2, src2, src1
+	mov	zeroones, REP8_01
+	and	tmp, src1, 7
+	tst	off2, 7
 	b.ne	L(misaligned8)
-	ands	tmp1, src1, #7
-	b.ne	L(mutual_align)
-	/* NUL detection works on the principle that (X - 1) & (~X) & 0x80
-	   (=> (X - 1) & ~(X | 0x7f)) is non-zero iff a byte is zero, and
-	   can be done in parallel across the entire word.  */
+	cbnz	tmp, L(mutual_align)
+
+	.p2align 4
+
 L(loop_aligned):
-	ldr	data1, [src1], #8
-	ldr	data2, [src2], #8
+	ldr	data2, [src1, off2]
+	ldr	data1, [src1], 8
 L(start_realigned):
-	sub	tmp1, data1, zeroones
-	orr	tmp2, data1, #REP8_7f
-	eor	diff, data1, data2	/* Non-zero if differences found.  */
-	bic	has_nul, tmp1, tmp2	/* Non-zero if NUL terminator.  */
+#ifdef __AARCH64EB__
+	rev	tmp, data1
+	sub	has_nul, tmp, zeroones
+	orr	tmp, tmp, REP8_7f
+#else
+	sub	has_nul, data1, zeroones
+	orr	tmp, data1, REP8_7f
+#endif
+	bics	has_nul, has_nul, tmp	/* Non-zero if NUL terminator.  */
+	ccmp	data1, data2, 0, eq
+	b.eq	L(loop_aligned)
+#ifdef __AARCH64EB__
+	rev	has_nul, has_nul
+#endif
+	eor	diff, data1, data2
 	orr	syndrome, diff, has_nul
-	cbz	syndrome, L(loop_aligned)
-	/* End of performance-critical section  -- one 64B cache line.  */
-
 L(end):
-#ifndef	__AARCH64EB__
+#ifndef __AARCH64EB__
 	rev	syndrome, syndrome
 	rev	data1, data1
-	/* The MS-non-zero bit of the syndrome marks either the first bit
-	   that is different, or the top bit of the first zero byte.
-	   Shifting left now will bring the critical information into the
-	   top bits.  */
-	clz	pos, syndrome
 	rev	data2, data2
-	lsl	data1, data1, pos
-	lsl	data2, data2, pos
-	/* But we need to zero-extend (char is unsigned) the value and then
-	   perform a signed 32-bit subtraction.  */
-	lsr	data1, data1, #56
-	sub	result, data1, data2, lsr #56
-	RET
-#else
-	/* For big-endian we cannot use the trick with the syndrome value
-	   as carry-propagation can corrupt the upper bits if the trailing
-	   bytes in the string contain 0x01.  */
-	/* However, if there is no NUL byte in the dword, we can generate
-	   the result directly.  We can't just subtract the bytes as the
-	   MSB might be significant.  */
-	cbnz	has_nul, 1f
-	cmp	data1, data2
-	cset	result, ne
-	cneg	result, result, lo
-	RET
-1:
-	/* Re-compute the NUL-byte detection, using a byte-reversed value.  */
-	rev	tmp3, data1
-	sub	tmp1, tmp3, zeroones
-	orr	tmp2, tmp3, #REP8_7f
-	bic	has_nul, tmp1, tmp2
-	rev	has_nul, has_nul
-	orr	syndrome, diff, has_nul
-	clz	pos, syndrome
-	/* The MS-non-zero bit of the syndrome marks either the first bit
-	   that is different, or the top bit of the first zero byte.
+#endif
+	clz	shift, syndrome
+	/* The most-significant-non-zero bit of the syndrome marks either the
+	   first bit that is different, or the top bit of the first zero byte.
 	   Shifting left now will bring the critical information into the
 	   top bits.  */
-	lsl	data1, data1, pos
-	lsl	data2, data2, pos
+	lsl	data1, data1, shift
+	lsl	data2, data2, shift
 	/* But we need to zero-extend (char is unsigned) the value and then
 	   perform a signed 32-bit subtraction.  */
-	lsr	data1, data1, #56
-	sub	result, data1, data2, lsr #56
-	RET
-#endif
+	lsr	data1, data1, 56
+	sub	result, data1, data2, lsr 56
+	ret
+
+	.p2align 4
 
 L(mutual_align):
 	/* Sources are mutually aligned, but are not currently at an
 	   alignment boundary.  Round down the addresses and then mask off
-	   the bytes that preceed the start point.  */
-	bic	src1, src1, #7
-	bic	src2, src2, #7
-	lsl	tmp1, tmp1, #3		/* Bytes beyond alignment -> bits.  */
-	ldr	data1, [src1], #8
-	neg	tmp1, tmp1		/* Bits to alignment -64.  */
-	ldr	data2, [src2], #8
-	mov	tmp2, #~0
-#ifdef __AARCH64EB__
-	/* Big-endian.  Early bytes are at MSB.  */
-	lsl	tmp2, tmp2, tmp1	/* Shift (tmp1 & 63).  */
-#else
-	/* Little-endian.  Early bytes are at LSB.  */
-	lsr	tmp2, tmp2, tmp1	/* Shift (tmp1 & 63).  */
-#endif
-	orr	data1, data1, tmp2
-	orr	data2, data2, tmp2
+	   the bytes that precede the start point.  */
+	bic	src1, src1, 7
+	ldr	data2, [src1, off2]
+	ldr	data1, [src1], 8
+	neg	shift, src2, lsl 3	/* Bits to alignment -64.  */
+	mov	tmp, -1
+	LS_FW	tmp, tmp, shift
+	orr	data1, data1, tmp
+	orr	data2, data2, tmp
 	b	L(start_realigned)
 
 L(misaligned8):
 	/* Align SRC1 to 8 bytes and then compare 8 bytes at a time, always
-	   checking to make sure that we don't access beyond page boundary in
-	   SRC2.  */
-	tst	src1, #7
-	b.eq	L(loop_misaligned)
+	   checking to make sure that we don't access beyond the end of SRC2.  */
+	cbz	tmp, L(src1_aligned)
 L(do_misaligned):
-	ldrb	data1w, [src1], #1
-	ldrb	data2w, [src2], #1
-	cmp	data1w, #1
-	ccmp	data1w, data2w, #0, cs	/* NZCV = 0b0000.  */
+	ldrb	data1w, [src1], 1
+	ldrb	data2w, [src2], 1
+	cmp	data1w, 0
+	ccmp	data1w, data2w, 0, ne	/* NZCV = 0b0000.  */
 	b.ne	L(done)
-	tst	src1, #7
+	tst	src1, 7
 	b.ne	L(do_misaligned)
 
-L(loop_misaligned):
-	/* Test if we are within the last dword of the end of a 4K page.  If
-	   yes then jump back to the misaligned loop to copy a byte at a time.  */
-	and	tmp1, src2, #0xff8
-	eor	tmp1, tmp1, #0xff8
-	cbz	tmp1, L(do_misaligned)
-	ldr	data1, [src1], #8
-	ldr	data2, [src2], #8
-
-	sub	tmp1, data1, zeroones
-	orr	tmp2, data1, #REP8_7f
-	eor	diff, data1, data2	/* Non-zero if differences found.  */
-	bic	has_nul, tmp1, tmp2	/* Non-zero if NUL terminator.  */
+L(src1_aligned):
+	neg	shift, src2, lsl 3
+	bic	src2, src2, 7
+	ldr	data3, [src2], 8
+#ifdef __AARCH64EB__
+	rev	data3, data3
+#endif
+	lsr	tmp, zeroones, shift
+	orr	data3, data3, tmp
+	sub	has_nul, data3, zeroones
+	orr	tmp, data3, REP8_7f
+	bics	has_nul, has_nul, tmp
+	b.ne	L(tail)
+
+	sub	off1, src2, src1
+
+	.p2align 4
+
+L(loop_unaligned):
+	ldr	data3, [src1, off1]
+	ldr	data2, [src1, off2]
+#ifdef __AARCH64EB__
+	rev	data3, data3
+#endif
+	sub	has_nul, data3, zeroones
+	orr	tmp, data3, REP8_7f
+	ldr	data1, [src1], 8
+	bics	has_nul, has_nul, tmp
+	ccmp	data1, data2, 0, eq
+	b.eq	L(loop_unaligned)
+
+	lsl	tmp, has_nul, shift
+#ifdef __AARCH64EB__
+	rev	tmp, tmp
+#endif
+	eor	diff, data1, data2
+	orr	syndrome, diff, tmp
+	cbnz	syndrome, L(end)
+L(tail):
+	ldr	data1, [src1]
+	neg	shift, shift
+	lsr	data2, data3, shift
+	lsr	has_nul, has_nul, shift
+#ifdef __AARCH64EB__
+	rev     data2, data2
+	rev	has_nul, has_nul
+#endif
+	eor	diff, data1, data2
 	orr	syndrome, diff, has_nul
-	cbz	syndrome, L(loop_misaligned)
 	b	L(end)
 
 L(done):
 	sub	result, data1, data2
-	RET
+	ret
+
 END(strcmp)
 libc_hidden_builtin_def (strcmp)
-- 
2.7.4