131914882SAlex Richardson/* 231914882SAlex Richardson * strrchr - find the last of a character in a string 331914882SAlex Richardson * 4*072a4ba8SAndrew Turner * Copyright (c) 2019-2022, Arm Limited. 5*072a4ba8SAndrew Turner * SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception 631914882SAlex Richardson */ 731914882SAlex Richardson 8*072a4ba8SAndrew Turner#include "asmdefs.h" 931914882SAlex Richardson 1031914882SAlex Richardson#if __ARM_FEATURE_SVE 1131914882SAlex Richardson/* Assumptions: 1231914882SAlex Richardson * 1331914882SAlex Richardson * ARMv8-a, AArch64 1431914882SAlex Richardson * SVE Available. 1531914882SAlex Richardson */ 1631914882SAlex Richardson 1731914882SAlex RichardsonENTRY (__strrchr_aarch64_sve) 1831914882SAlex Richardson PTR_ARG (0) 1931914882SAlex Richardson dup z1.b, w1 /* replicate byte across vector */ 2031914882SAlex Richardson setffr /* initialize FFR */ 2131914882SAlex Richardson ptrue p1.b /* all ones; loop invariant */ 2231914882SAlex Richardson mov x2, 0 /* no match found so far */ 2331914882SAlex Richardson pfalse p2.b 2431914882SAlex Richardson 2531914882SAlex Richardson .p2align 4 2631914882SAlex Richardson /* Read a vector's worth of bytes, stopping on first fault. */ 2731914882SAlex Richardson0: ldff1b z0.b, p1/z, [x0, xzr] 2831914882SAlex Richardson rdffrs p0.b, p1/z 2931914882SAlex Richardson b.nlast 1f 3031914882SAlex Richardson 3131914882SAlex Richardson /* First fault did not fail: the whole vector is valid. 3231914882SAlex Richardson Avoid depending on the contents of FFR beyond the branch. */ 3331914882SAlex Richardson incb x0, all /* skip bytes this round */ 3431914882SAlex Richardson cmpeq p3.b, p1/z, z0.b, 0 /* search for 0 */ 3531914882SAlex Richardson b.any 3f 3631914882SAlex Richardson 3731914882SAlex Richardson cmpeq p3.b, p1/z, z0.b, z1.b /* search for c; no eos */ 3831914882SAlex Richardson b.none 0b 3931914882SAlex Richardson 4031914882SAlex Richardson mov x2, x0 /* save advanced base */ 4131914882SAlex Richardson mov p2.b, p3.b /* save current search */ 4231914882SAlex Richardson b 0b 4331914882SAlex Richardson 4431914882SAlex Richardson /* First fault failed: only some of the vector is valid. 4531914882SAlex Richardson Perform the comparisions only on the valid bytes. */ 4631914882SAlex Richardson1: cmpeq p3.b, p0/z, z0.b, 0 /* search for 0 */ 4731914882SAlex Richardson b.any 2f 4831914882SAlex Richardson 4931914882SAlex Richardson cmpeq p3.b, p0/z, z0.b, z1.b /* search for c; no eos */ 5031914882SAlex Richardson mov x3, x0 5131914882SAlex Richardson incp x0, p0.b /* skip bytes this round */ 5231914882SAlex Richardson setffr /* re-init FFR */ 5331914882SAlex Richardson b.none 0b 5431914882SAlex Richardson 5531914882SAlex Richardson addvl x2, x3, 1 /* save advanced base */ 5631914882SAlex Richardson mov p2.b, p3.b /* save current search */ 5731914882SAlex Richardson b 0b 5831914882SAlex Richardson 5931914882SAlex Richardson /* Found end-of-string. */ 6031914882SAlex Richardson2: incb x0, all /* advance base */ 6131914882SAlex Richardson3: brka p3.b, p1/z, p3.b /* mask after first 0 */ 6231914882SAlex Richardson cmpeq p3.b, p3/z, z0.b, z1.b /* search for c not after eos */ 6331914882SAlex Richardson b.any 4f 6431914882SAlex Richardson 6531914882SAlex Richardson /* No C within last vector. Did we have one before? */ 6631914882SAlex Richardson cbz x2, 5f 6731914882SAlex Richardson mov x0, x2 /* restore advanced base */ 6831914882SAlex Richardson mov p3.b, p2.b /* restore saved search */ 6931914882SAlex Richardson 7031914882SAlex Richardson /* Find the *last* match in the predicate. This is slightly 7131914882SAlex Richardson more complicated than finding the first match. */ 7231914882SAlex Richardson4: rev p3.b, p3.b /* reverse the bits */ 7331914882SAlex Richardson brka p3.b, p1/z, p3.b /* find position of last match */ 7431914882SAlex Richardson decp x0, p3.b /* retard pointer to last match */ 7531914882SAlex Richardson ret 7631914882SAlex Richardson 7731914882SAlex Richardson /* No C whatsoever. Return NULL. */ 7831914882SAlex Richardson5: mov x0, 0 7931914882SAlex Richardson ret 8031914882SAlex Richardson 8131914882SAlex RichardsonEND (__strrchr_aarch64_sve) 8231914882SAlex Richardson 8331914882SAlex Richardson#endif 8431914882SAlex Richardson 85