From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1895) id 00FB93858413; Tue, 17 Jan 2023 16:36:06 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 00FB93858413 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1673973367; bh=DReNILPX1JoKYfFjV95mEDHANpW9S2EJ4RVFXmEG/fc=; h=From:To:Subject:Date:From; b=IUPu6DiHpDzrcrLML/QCXzMVvjjsh5GeP+uLxWlVzKBKhepQp95aE0o8YV/4FcRor seFWlNLX426/JqW8q8Ttp2AWoH3TOl/aRLqx+NzJFSa2Z2SJa73JOARkdoGTSe3SKb sdeZ/zj2ZSXQ8FlulrhpW87Hxd9WV8CQo0IoEnSg= Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: Wilco Dijkstra To: glibc-cvs@sourceware.org Subject: [glibc] AArch64: Optimize strlen X-Act-Checkin: glibc X-Git-Author: Wilco Dijkstra X-Git-Refname: refs/heads/master X-Git-Oldrev: 349e48c01e85bd96006860084e76d322e6ca02f1 X-Git-Newrev: 03c8ce5000198947a4dd7b2c14e5131738fda62b Message-Id: <20230117163607.00FB93858413@sourceware.org> Date: Tue, 17 Jan 2023 16:36:06 +0000 (GMT) List-Id: https://sourceware.org/git/gitweb.cgi?p=glibc.git;h=03c8ce5000198947a4dd7b2c14e5131738fda62b commit 03c8ce5000198947a4dd7b2c14e5131738fda62b Author: Wilco Dijkstra Date: Wed Jan 11 13:52:53 2023 +0000 AArch64: Optimize strlen Optimize strlen by unrolling the main loop. Large strings are 64% faster on modern CPUs. Reviewed-by: Szabolcs Nagy Diff: --- sysdeps/aarch64/strlen.S | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/sysdeps/aarch64/strlen.S b/sysdeps/aarch64/strlen.S index b3c92d9dc9..133ef93342 100644 --- a/sysdeps/aarch64/strlen.S +++ b/sysdeps/aarch64/strlen.S @@ -43,12 +43,9 @@ #define dend d2 /* Core algorithm: - - For each 16-byte chunk we calculate a 64-bit nibble mask value with four bits - per byte. We take 4 bits of every comparison byte with shift right and narrow - by 4 instruction. Since the bits in the nibble mask reflect the order in - which things occur in the original string, counting trailing zeros identifies - exactly which byte matched. */ + Process the string in 16-byte aligned chunks. Compute a 64-bit mask with + four bits per byte using the shrn instruction. A count trailing zeros then + identifies the first zero byte. */ ENTRY (STRLEN) PTR_ARG (0) @@ -68,18 +65,25 @@ ENTRY (STRLEN) .p2align 5 L(loop): - ldr data, [src, 16]! + ldr data, [src, 16] + cmeq vhas_nul.16b, vdata.16b, 0 + umaxp vend.16b, vhas_nul.16b, vhas_nul.16b + fmov synd, dend + cbnz synd, L(loop_end) + ldr data, [src, 32]! cmeq vhas_nul.16b, vdata.16b, 0 umaxp vend.16b, vhas_nul.16b, vhas_nul.16b fmov synd, dend cbz synd, L(loop) - + sub src, src, 16 +L(loop_end): shrn vend.8b, vhas_nul.8h, 4 /* 128->64 */ sub result, src, srcin fmov synd, dend #ifndef __AARCH64EB__ rbit synd, synd #endif + add result, result, 16 clz tmp, synd add result, result, tmp, lsr 2 ret