xref: /linux-6.15/arch/powerpc/lib/string.S (revision 3ece1663)
1/*
2 * String handling functions for PowerPC.
3 *
4 * Copyright (C) 1996 Paul Mackerras.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 */
11#include <asm/processor.h>
12#include <asm/errno.h>
13#include <asm/ppc_asm.h>
14
15	.section __ex_table,"a"
16	PPC_LONG_ALIGN
17	.text
18
19/* This clears out any unused part of the destination buffer,
20   just as the libc version does.  -- paulus */
21_GLOBAL(strncpy)
22	PPC_LCMPI 0,r5,0
23	beqlr
24	mtctr	r5
25	addi	r6,r3,-1
26	addi	r4,r4,-1
271:	lbzu	r0,1(r4)
28	cmpwi	0,r0,0
29	stbu	r0,1(r6)
30	bdnzf	2,1b		/* dec ctr, branch if ctr != 0 && !cr0.eq */
31	bnelr			/* if we didn't hit a null char, we're done */
32	mfctr	r5
33	PPC_LCMPI 0,r5,0	/* any space left in destination buffer? */
34	beqlr			/* we know r0 == 0 here */
352:	stbu	r0,1(r6)	/* clear it out if so */
36	bdnz	2b
37	blr
38
39_GLOBAL(strncmp)
40	PPC_LCMPI 0,r5,0
41	beq-	2f
42	mtctr	r5
43	addi	r5,r3,-1
44	addi	r4,r4,-1
451:	lbzu	r3,1(r5)
46	cmpwi	1,r3,0
47	lbzu	r0,1(r4)
48	subf.	r3,r0,r3
49	beqlr	1
50	bdnzt	eq,1b
51	blr
522:	li	r3,0
53	blr
54
55#ifdef CONFIG_PPC32
56_GLOBAL(memcmp)
57	PPC_LCMPI 0,r5,0
58	beq-	2f
59	mtctr	r5
60	addi	r6,r3,-1
61	addi	r4,r4,-1
621:	lbzu	r3,1(r6)
63	lbzu	r0,1(r4)
64	subf.	r3,r0,r3
65	bdnzt	2,1b
66	blr
672:	li	r3,0
68	blr
69#endif
70
71_GLOBAL(memchr)
72	PPC_LCMPI 0,r5,0
73	beq-	2f
74	mtctr	r5
75	addi	r3,r3,-1
761:	lbzu	r0,1(r3)
77	cmpw	0,r0,r4
78	bdnzf	2,1b
79	beqlr
802:	li	r3,0
81	blr
82
83#ifdef CONFIG_PPC32
84_GLOBAL(__clear_user)
85	addi	r6,r3,-4
86	li	r3,0
87	li	r5,0
88	cmplwi	0,r4,4
89	blt	7f
90	/* clear a single word */
9111:	stwu	r5,4(r6)
92	beqlr
93	/* clear word sized chunks */
94	andi.	r0,r6,3
95	add	r4,r0,r4
96	subf	r6,r0,r6
97	srwi	r0,r4,2
98	andi.	r4,r4,3
99	mtctr	r0
100	bdz	7f
1011:	stwu	r5,4(r6)
102	bdnz	1b
103	/* clear byte sized chunks */
1047:	cmpwi	0,r4,0
105	beqlr
106	mtctr	r4
107	addi	r6,r6,3
1088:	stbu	r5,1(r6)
109	bdnz	8b
110	blr
11190:	mr	r3,r4
112	blr
11391:	mfctr	r3
114	slwi	r3,r3,2
115	add	r3,r3,r4
116	blr
11792:	mfctr	r3
118	blr
119
120	.section __ex_table,"a"
121	PPC_LONG	11b,90b
122	PPC_LONG	1b,91b
123	PPC_LONG	8b,92b
124	.text
125#endif
126