xref: /freebsd/lib/libc/powerpc64/string/strncpy_arch_2_05.S (revision 7fdf597e96a02165cfe22ff357b857d5fa15ed8a)
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>
30ENTRY(__strncpy_arch_2_05)
31	stdu	%r1,-40(%r1)
32	mflr	%r0
33	std	%r0,16(%r1)
34	std	%r3,32(%r1)
35
36	xor	%r6,%r6,%r6	/* fixed 0 reg */
37
38/* align loop */
39	addi	%r3,%r3,-1
40.Lalign_loop:
41	/* len? */
42	cmpdi	%r5,0
43	beq	.Lexit
44	/* aligned? */
45	andi.	%r0,%r4,7
46	beq	.Ldw_copy
47	/* copy */
48	lbz	%r7,0(%r4)
49	stbu	%r7,1(%r3)
50	addi	%r4,%r4,1
51	addi	%r5,%r5,-1
52	/* zero? */
53	cmpdi	%r7,0
54	beq	.Lzero
55	b	.Lalign_loop
56
57/* dword copy loop */
58.Ldw_copy:
59	/* prepare src and dst to use load/store and update */
60	addi	%r3,%r3,-7
61	addi	%r4,%r4,-8
62.Ldw_copy_loop:
63	cmpdi	%r5,8
64	blt	.Lbyte_copy
65
66	ldu	%r0,8(%r4)
67	/* check for 0 */
68	cmpb	%r7,%r0,%r6
69	cmpdi	%r7,0
70	bne	.Lbyte_copy_and_zero
71	/* copy to dst */
72	stdu	%r0,8(%r3)
73	addi	%r5,%r5,-8
74	b	.Ldw_copy_loop
75
76/* Copy remaining src bytes, zero-out buffer
77 * Note: r5 will be >= 8
78 */
79.Lbyte_copy_and_zero:
80	addi	%r3,%r3,7
81	addi	%r4,%r4,-1
82.Lbyte_copy_and_zero_loop:
83	lbzu	%r7,1(%r4)
84	stbu	%r7,1(%r3)
85	addi	%r5,%r5,-1
86	cmpdi	%r7,0
87	beq	.Lzero
88	b	.Lbyte_copy_and_zero_loop
89
90/* zero-out remaining dst bytes */
91.Lzero:
92	addi	%r3,%r3,1
93	li	%r4,0
94	/* r5 has len already */
95	bl	memset
96	nop
97	b	.Lexit
98
99/* copy remaining (< 8) bytes */
100.Lbyte_copy:
101	cmpdi	%r5,0
102	beq	.Lexit
103	addi	%r3,%r3,7
104	addi	%r4,%r4,7
105	mtctr	%r5
106.Lbyte_copy_loop:
107	lbzu	%r7,1(%r4)
108	stbu	%r7,1(%r3)
109	cmpdi	%r7,0
110	/* 0 found: zero out remaining bytes */
111	beq	.Lbyte_copy_zero
112	bdnz	.Lbyte_copy_loop
113	b	.Lexit
114.Lbyte_copy_zero_loop:
115	stbu	%r6,1(%r3)
116.Lbyte_copy_zero:
117	bdnz	.Lbyte_copy_zero_loop
118
119.Lexit:
120	/* epilogue */
121	ld	%r3,32(%r1)
122	ld	%r0,16(%r1)
123	mtlr	%r0
124	addi	%r1,%r1,40
125	blr
126
127END(__strncpy_arch_2_05)
128
129	.section .note.GNU-stack,"",%progbits
130