Path: blob/main/contrib/arm-optimized-routines/string/aarch64/strnlen.S
39491 views
/*1* strnlen - calculate the length of a string with limit.2*3* Copyright (c) 2020-2022, Arm Limited.4* SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception5*/67/* Assumptions:8*9* ARMv8-a, AArch64, Advanced SIMD.10* MTE compatible.11*/1213#include "asmdefs.h"1415#define srcin x016#define cntin x117#define result x01819#define src x220#define synd x321#define shift x422#define tmp x423#define cntrem x52425#define qdata q026#define vdata v027#define vhas_chr v128#define vend v229#define dend d23031/*32Core algorithm:33Process the string in 16-byte aligned chunks. Compute a 64-bit mask with34four bits per byte using the shrn instruction. A count trailing zeros then35identifies the first zero byte. */3637ENTRY (__strnlen_aarch64)38bic src, srcin, 1539cbz cntin, L(nomatch)40ld1 {vdata.16b}, [src]41cmeq vhas_chr.16b, vdata.16b, 042lsl shift, srcin, 243shrn vend.8b, vhas_chr.8h, 4 /* 128->64 */44fmov synd, dend45lsr synd, synd, shift46cbz synd, L(start_loop)47L(finish):48rbit synd, synd49clz synd, synd50lsr result, synd, 251cmp cntin, result52csel result, cntin, result, ls53ret5455L(nomatch):56mov result, cntin57ret5859L(start_loop):60sub tmp, src, srcin61add tmp, tmp, 1762subs cntrem, cntin, tmp63b.lo L(nomatch)6465/* Make sure that it won't overread by a 16-byte chunk */66tbz cntrem, 4, L(loop32_2)67sub src, src, 1668.p2align 569L(loop32):70ldr qdata, [src, 32]!71cmeq vhas_chr.16b, vdata.16b, 072umaxp vend.16b, vhas_chr.16b, vhas_chr.16b /* 128->64 */73fmov synd, dend74cbnz synd, L(end)75L(loop32_2):76ldr qdata, [src, 16]77subs cntrem, cntrem, 3278cmeq vhas_chr.16b, vdata.16b, 079b.lo L(end_2)80umaxp vend.16b, vhas_chr.16b, vhas_chr.16b /* 128->64 */81fmov synd, dend82cbz synd, L(loop32)83L(end_2):84add src, src, 1685L(end):86shrn vend.8b, vhas_chr.8h, 4 /* 128->64 */87sub result, src, srcin88fmov synd, dend89#ifndef __AARCH64EB__90rbit synd, synd91#endif92clz synd, synd93add result, result, synd, lsr 294cmp cntin, result95csel result, cntin, result, ls96ret9798END (__strnlen_aarch64)99100101102