1/* SPDX-License-Identifier: GPL-2.0-or-later */
2/*
3 * String handling functions for PowerPC.
4 *
5 * Copyright (C) 1996 Paul Mackerras.
6 */
7#include <asm/ppc_asm.h>
8#include <asm/export.h>
9#include <asm/cache.h>
10
11	.text
12
13/* This clears out any unused part of the destination buffer,
14   just as the libc version does.  -- paulus */
15_GLOBAL(strncpy)
16	PPC_LCMPI 0,r5,0
17	beqlr
18	mtctr	r5
19	addi	r6,r3,-1
20	addi	r4,r4,-1
21	.balign IFETCH_ALIGN_BYTES
221:	lbzu	r0,1(r4)
23	cmpwi	0,r0,0
24	stbu	r0,1(r6)
25	bdnzf	2,1b		/* dec ctr, branch if ctr != 0 && !cr0.eq */
26	bnelr			/* if we didn't hit a null char, we're done */
27	mfctr	r5
28	PPC_LCMPI 0,r5,0	/* any space left in destination buffer? */
29	beqlr			/* we know r0 == 0 here */
302:	stbu	r0,1(r6)	/* clear it out if so */
31	bdnz	2b
32	blr
33EXPORT_SYMBOL(strncpy)
34
35_GLOBAL(strncmp)
36	PPC_LCMPI 0,r5,0
37	beq-	2f
38	mtctr	r5
39	addi	r5,r3,-1
40	addi	r4,r4,-1
41	.balign IFETCH_ALIGN_BYTES
421:	lbzu	r3,1(r5)
43	cmpwi	1,r3,0
44	lbzu	r0,1(r4)
45	subf.	r3,r0,r3
46	beqlr	1
47	bdnzt	eq,1b
48	blr
492:	li	r3,0
50	blr
51EXPORT_SYMBOL(strncmp)
52
53_GLOBAL(memchr)
54	PPC_LCMPI 0,r5,0
55	beq-	2f
56	mtctr	r5
57	addi	r3,r3,-1
58	.balign IFETCH_ALIGN_BYTES
591:	lbzu	r0,1(r3)
60	cmpw	0,r0,r4
61	bdnzf	2,1b
62	beqlr
632:	li	r3,0
64	blr
65EXPORT_SYMBOL(memchr)
66