1/* Optimized strlen implementation for PowerPC64/POWER7 using cmpb insn.
2   Copyright (C) 2010-2022 Free Software Foundation, Inc.
3   This file is part of the GNU C Library.
4
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.
9
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.
14
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   <https://www.gnu.org/licenses/>.  */
18
19#include <sysdep.h>
20
21/* int [r3] strlen (char *s [r3])  */
22
23#ifndef STRLEN
24# define STRLEN strlen
25#endif
26	.machine  power7
27ENTRY_TOCLESS (STRLEN)
28	CALL_MCOUNT 1
29	dcbt	0,r3
30	clrrdi	r4,r3,3	      /* Align the address to doubleword boundary.  */
31	rlwinm	r6,r3,3,26,28 /* Calculate padding.  */
32	li	r0,0	      /* Doubleword with null chars to use
33				 with cmpb.  */
34	li	r5,-1	      /* MASK = 0xffffffffffffffff.  */
35	ld	r12,0(r4)     /* Load doubleword from memory.  */
36#ifdef __LITTLE_ENDIAN__
37	sld	r5,r5,r6
38#else
39	srd	r5,r5,r6      /* MASK = MASK >> padding.  */
40#endif
41	orc	r9,r12,r5     /* Mask bits that are not part of the string.  */
42	cmpb	r10,r9,r0     /* Check for null bytes in DWORD1.  */
43	cmpdi	cr7,r10,0     /* If r10 == 0, no null's have been found.  */
44	bne	cr7,L(done)
45
46	mtcrf   0x01,r4
47
48	/* Are we now aligned to a quadword boundary?  If so, skip to
49	   the main loop.  Otherwise, go through the alignment code.  */
50
51	bt	28,L(loop)
52
53	/* Handle DWORD2 of pair.  */
54	ldu	r12,8(r4)
55	cmpb	r10,r12,r0
56	cmpdi	cr7,r10,0
57	bne	cr7,L(done)
58
59	/* Main loop to look for the end of the string.  Since it's a
60	   small loop (< 8 instructions), align it to 32-bytes.  */
61	.p2align  5
62L(loop):
63	/* Load two doublewords, compare and merge in a
64	   single register for speed.  This is an attempt
65	   to speed up the null-checking process for bigger strings.  */
66
67	ld	r12, 8(r4)
68	ldu	r11, 16(r4)
69	cmpb	r10,r12,r0
70	cmpb	r9,r11,r0
71	or	r8,r9,r10     /* Merge everything in one doubleword.  */
72	cmpdi	cr7,r8,0
73	beq	cr7,L(loop)
74
75	/* OK, one (or both) of the doublewords contains a null byte.  Check
76	   the first doubleword and decrement the address in case the first
77	   doubleword really contains a null byte.  */
78
79	cmpdi	cr6,r10,0
80	addi	r4,r4,-8
81	bne	cr6,L(done)
82
83	/* The null byte must be in the second doubleword.  Adjust the address
84	   again and move the result of cmpb to r10 so we can calculate the
85	   length.  */
86
87	mr	r10,r9
88	addi	r4,r4,8
89
90	/* r10 has the output of the cmpb instruction, that is, it contains
91	   0xff in the same position as the null byte in the original
92	   doubleword from the string.  Use that to calculate the length.  */
93L(done):
94#ifdef __LITTLE_ENDIAN__
95	addi	r9, r10, -1   /* Form a mask from trailing zeros.  */
96	andc	r9, r9, r10
97	popcntd r0, r9	      /* Count the bits in the mask.  */
98#else
99	cntlzd	r0,r10	      /* Count leading zeros before the match.  */
100#endif
101	subf	r5,r3,r4
102	srdi	r0,r0,3	      /* Convert leading/trailing zeros to bytes.  */
103	add	r3,r5,r0      /* Compute final length.  */
104	blr
105END (STRLEN)
106libc_hidden_builtin_def (strlen)
107