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#if !defined(__BIG_ENDIAN__)
30#error "Optimized SRTCPY is only supported by big-endian architecture!"
31#endif
32
33#include <machine/asm.h>
34__FBSDID("$FreeBSD$");
35
36#if 0
37	RCSID("$NetBSD: strcpy.S,v 1.0 2018/05/08 13:00:49 lbianc Exp $")
38#endif
39
40ENTRY(__strcpy_arch_2_05)
41	mr	%r8, %r3
42
43/*
44 * Aligning the reading address, even if it is already aligned to avoid
45 * performance degradation with strings with 8 bytes or less.
46 */
47.Lalignment:
48	lbz	%r0,0(%r4)
49	cmpdi	cr7,%r0,0
50	stb	%r0,0(%r8)
51	beq	cr7,.Lexit
52	addi	%r4,%r4,1
53	addi	%r8,%r8,1
54	andi.	%r0,%r4,0x7
55	bne	.Lalignment
56
57/* Copy by double word with aligned address. */
58.Lcopy_dw:
59	ld	%r0,0(%r4)
60	xor	%r6,%r6,%r6
61	cmpb	%r5,%r0,%r6
62	cmpdi	cr7,%r5,0
63	bne	cr7,.Lcheck_zero
64	/* Backward r8 to use stdu instruction in Lcopy_dw_loop */
65	addi	%r8,%r8,-8
66.Lcopy_dw_loop:
67	stdu	%r0,8(%r8)
68	ldu	%r0,8(%r4)
69	cmpb	%r5,%r0,%r6
70	cmpdi	cr7,%r5,0
71	beq	cr7,.Lcopy_dw_loop
72
73	addi	%r8,%r8,8   /* Forward r8 to use std instruction. */
74/* Find where the zero is located. */
75.Lcheck_zero:
76	rldicr.	%r5,%r0,0,7
77	beq	.Lfound_on_byte_0
78	rldicr.	%r7,%r0,8,7
79	beq	.Lfound_on_byte_1
80	rldicr.	%r7,%r0,16,7
81	beq	.Lfound_on_byte_2
82	rldicr.	%r7,%r0,24,7
83	beq	.Lfound_on_byte_3
84	andis.	%r7,%r0,0xff00
85	beq	.Lfound_on_byte_4
86	andis.	%r7,%r0,0xff
87	beq	.Lfound_on_byte_5
88	andi.	%r7,%r0,0xff00
89	beq	.Lfound_on_byte_6
90
91/* Copy the last string bytes according to the string end position. */
92.Lfound_on_byte_7:
93	std	%r0,0(%r8)
94	b	.Lexit
95
96.Lfound_on_byte_6:
97	srdi	%r6,%r0,32
98	stw	%r6,0(%r8)
99	srdi	%r6,%r0,16
100	sth	%r6,4(%r8)
101	srdi	%r6,%r0,8
102	stb	%r6,6(%r8)
103	b	.Lexit
104
105.Lfound_on_byte_5:
106	srdi	%r6,%r0,32
107	stw	%r6,0(%r8)
108	srdi	%r6,%r0,16
109	sth	%r6,4(%r8)
110	b	.Lexit
111
112.Lfound_on_byte_4:
113	srdi	%r6,%r0,32
114	stw	%r6,0(%r8)
115	srdi	%r6,%r0,24
116	stb	%r6,4(%r8)
117	b	.Lexit
118
119.Lfound_on_byte_3:
120	srdi	%r6,%r0,32
121	stw	%r6,0(%r8)
122	b	.Lexit
123
124.Lfound_on_byte_2:
125	srdi	%r6,%r0,48
126	sth	%r6,0(%r8)
127	srdi	%r6,%r0,40
128	stb	%r6,2(%r8)
129	b	.Lexit
130
131.Lfound_on_byte_1:
132	srdi	%r6,%r0,48
133	sth	%r6,0(%r8)
134	b	.Lexit
135
136.Lfound_on_byte_0:
137	srdi	%r6,%r0,56
138	stb	%r6,0(%r8)
139.Lexit:
140	blr
141
142END(__strcpy_arch_2_05)
143
144	.section .note.GNU-stack,"",%progbits
145