xref: /freebsd/lib/libc/powerpc64/string/strncpy_arch_2_05.S (revision 8bcb0991864975618c09697b1aca10683346d9f0)
1/*-
2 * Copyright (c) 2018 Instituto de Pesquisas Eldorado
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the author nor the names of its contributors may
14 *    be used to endorse or promote products derived from this software
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29#include <machine/asm.h>
30__FBSDID("$FreeBSD$");
31
32ENTRY(__strncpy_arch_2_05)
33	stdu	%r1,-40(%r1)
34	mflr	%r0
35	std	%r0,16(%r1)
36	std	%r3,32(%r1)
37
38	xor	%r6,%r6,%r6	/* fixed 0 reg */
39
40/* align loop */
41	addi	%r3,%r3,-1
42.Lalign_loop:
43	/* len? */
44	cmpdi	%r5,0
45	beq	.Lexit
46	/* aligned? */
47	andi.	%r0,%r4,7
48	beq	.Ldw_copy
49	/* copy */
50	lbz	%r7,0(%r4)
51	stbu	%r7,1(%r3)
52	addi	%r4,%r4,1
53	addi	%r5,%r5,-1
54	/* zero? */
55	cmpdi	%r7,0
56	beq	.Lzero
57	b	.Lalign_loop
58
59/* dword copy loop */
60.Ldw_copy:
61	/* prepare src and dst to use load/store and update */
62	addi	%r3,%r3,-7
63	addi	%r4,%r4,-8
64.Ldw_copy_loop:
65	cmpdi	%r5,8
66	blt	.Lbyte_copy
67
68	ldu	%r0,8(%r4)
69	/* check for 0 */
70	cmpb	%r7,%r0,%r6
71	cmpdi	%r7,0
72	bne	.Lbyte_copy_and_zero
73	/* copy to dst */
74	stdu	%r0,8(%r3)
75	addi	%r5,%r5,-8
76	b	.Ldw_copy_loop
77
78/* Copy remaining src bytes, zero-out buffer
79 * Note: r5 will be >= 8
80 */
81.Lbyte_copy_and_zero:
82	addi	%r3,%r3,7
83	addi	%r4,%r4,-1
84.Lbyte_copy_and_zero_loop:
85	lbzu	%r7,1(%r4)
86	stbu	%r7,1(%r3)
87	addi	%r5,%r5,-1
88	cmpdi	%r7,0
89	beq	.Lzero
90	b	.Lbyte_copy_and_zero_loop
91
92/* zero-out remaining dst bytes */
93.Lzero:
94	addi	%r3,%r3,1
95	li	%r4,0
96	/* r5 has len already */
97	bl	memset
98	nop
99	b	.Lexit
100
101/* copy remaining (< 8) bytes */
102.Lbyte_copy:
103	cmpdi	%r5,0
104	beq	.Lexit
105	addi	%r3,%r3,7
106	addi	%r4,%r4,7
107	mtctr	%r5
108.Lbyte_copy_loop:
109	lbzu	%r7,1(%r4)
110	stbu	%r7,1(%r3)
111	cmpdi	%r7,0
112	/* 0 found: zero out remaining bytes */
113	beq	.Lbyte_copy_zero
114	bdnz	.Lbyte_copy_loop
115	b	.Lexit
116.Lbyte_copy_zero_loop:
117	stbu	%r6,1(%r3)
118.Lbyte_copy_zero:
119	bdnz	.Lbyte_copy_zero_loop
120
121.Lexit:
122	/* epilogue */
123	ld	%r3,32(%r1)
124	ld	%r0,16(%r1)
125	mtlr	%r0
126	addi	%r1,%r1,40
127	blr
128
129END(__strncpy_arch_2_05)
130
131	.section .note.GNU-stack,"",%progbits
132