1 /* Vector optimized 32/64 bit S/390 version of memrchr.
2 Copyright (C) 2015-2018 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
19 #if defined HAVE_S390_VX_ASM_SUPPORT && IS_IN (libc)
22 # include "asm-syntax.h"
26 /* void *memrchr (const void *s, int c, size_t n)
27 Scans memory for character c backwards
28 and returns pointer to first c.
45 .machinemode "zarch_nohighgprs"
47 # if !defined __s390x__
49 # endif /* !defined __s390x__ */
50 clgije %r4,0,.Lnot_found
52 vlvgb %v18,%r3,0 /* Generate vector which elements are all c.
53 If c > 255, c will be truncated. */
56 llcr %r3,%r3 /* char c_char = (char) c. */
58 /* check byte n - 1. */
61 clrje %r0,%r3,.Lfound_end
62 jh .Lnot_found /* Return NULL if n is now 0. */
64 larl %r1,.Lpermute_mask /* Load permute mask. */
67 /* check byte n - 2. */
70 clrje %r0,%r3,.Lfound_end
71 jh .Lnot_found /* Return NULL if n is now 0. */
73 clgijhe %r4,64,.Lloop64 /* If n >= 64 -> loop64. */
76 /* Process n < 64 bytes. */
77 clgijl %r4,16,.Llt16 /* Jump away if n < 16. */
93 clgfi %r4,0 /* if remaining bytes == 0, return NULL. */
97 aghi %r4,-1 /* vll needs highest index. */
98 vll %v16,%r4,0(%r2) /* Load remaining bytes. */
100 /* Right-shift of v16 to mask bytes after highest index. */
102 slr %r0,%r4 /* Compute byte count for vector shift right. */
103 sll %r0,3 /* Convert to bit count. */
105 vsrlb %v16,%v16,%v17 /* Vector shift right by byte by number of bytes
106 specified in bits 1-4 of byte 7 in v17. */
116 la %r2,0(%r4,%r2) /* Set pointer to start of v16. */
117 lghi %r4,15 /* Set highest index in v16 to last index. */
119 /* Search for a c in v16 in reversed byte order. v16 contains %r4 + 1
120 bytes. If v16 was not fully loaded, the bytes are already
121 right shifted, so that the bytes in v16 can simply be reversed. */
122 vperm %v16,%v16,%v16,%v20 /* Permute v16 to reversed order. */
123 vfeeb %v16,%v16,%v18 /* Find c in reversed v16. */
124 vlgvb %r1,%v16,7 /* Index of c or 16 if not found. */
126 /* Return NULL if there is no c in loaded bytes. */
127 clrjh %r1,%r4,.Lnot_found
131 la %r2,0(%r4,%r2) /* Return pointer to c. */
139 .byte 0x0F,0x0E,0x0D,0x0C,0x0B,0x0A,0x09,0x08
140 .byte 0x07,0x06,0x05,0x04,0x03,0x02,0x01,0x00
144 vl %v16,48(%r4,%r2) /* Load 16bytes of memory area. */
145 vfeebs %v17,%v16,%v18 /* Find c. */
146 jno .Lfound48 /* Jump away if c was found. */
148 vfeebs %v17,%v16,%v18
151 vfeebs %v17,%v16,%v18
154 vfeebs %v17,%v16,%v18
157 clgijhe %r4,64,.Lloop64 /* If n >= 64 -> loop64. */
160 #endif /* HAVE_S390_VX_ASM_SUPPORT && IS_IN (libc) */