1/* Optimized version of the memccpy() function.
2   This file is part of the GNU C Library.
3   Copyright (C) 2000, 2001, 2003 Free Software Foundation, Inc.
4   Contributed by Dan Pop <Dan.Pop@cern.ch>.
5
6   The GNU C Library is free software; you can redistribute it and/or
7   modify it under the terms of the GNU Lesser General Public
8   License as published by the Free Software Foundation; either
9   version 2.1 of the License, or (at your option) any later version.
10
11   The GNU C Library is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   Lesser General Public License for more details.
15
16   You should have received a copy of the GNU Lesser General Public
17   License along with the GNU C Library; if not, see
18   <http://www.gnu.org/licenses/>.  */
19
20/* Return: a pointer to the next byte after char in dest or NULL
21
22   Inputs:
23        in0:    dest
24        in1:    src
25	in2:	char
26        in3:    byte count
27
28   This implementation assumes little endian mode (UM.be = 0).
29
30   This implementation assumes that it is safe to do read ahead
31   in the src block, without getting beyond its limit.  */
32
33#include <sysdep.h>
34#undef ret
35
36#define OP_T_THRES 	16
37#define OPSIZ 		8
38
39#define saved_pr	r17
40#define saved_lc	r18
41#define dest		r19
42#define src		r20
43#define len		r21
44#define asrc		r22
45#define tmp		r23
46#define char		r24
47#define charx8		r25
48#define saved_ec	r26
49#define sh2		r28
50#define	sh1		r29
51#define loopcnt		r30
52#define	value		r31
53
54#ifdef GAS_ALIGN_BREAKS_UNWIND_INFO
55/* Manually force proper loop-alignment.  Note: be sure to
56   double-check the code-layout after making any changes to
57   this routine! */
58# define ALIGN(n)	{ nop 0 }
59#else
60# define ALIGN(n)	.align n
61#endif
62
63ENTRY(memccpy)
64	.prologue
65	alloc 	r2 = ar.pfs, 4, 40 - 4, 0, 40
66
67#include "softpipe.h"
68	.rotr	r[MEMLAT + 7], tmp1[4], tmp2[4], val[4], tmp3[2], pos0[2]
69	.rotp	p[MEMLAT + 6 + 1]
70
71	mov	ret0 = r0		/* return NULL if no match */
72	.save pr, saved_pr
73	mov	saved_pr = pr		/* save the predicate registers */
74	mov 	dest = in0		/* dest */
75	.save ar.lc, saved_lc
76        mov 	saved_lc = ar.lc	/* save the loop counter */
77        mov 	saved_ec = ar.ec	/* save the loop counter */
78	.body
79	mov 	src = in1		/* src */
80	extr.u	char = in2, 0, 8	/* char */
81	mov	len = in3		/* len */
82	sub	tmp = r0, in0		/* tmp = -dest */
83	cmp.ne	p7, p0 = r0, r0		/* clear p7 */
84	;;
85	and	loopcnt = 7, tmp	/* loopcnt = -dest % 8 */
86	cmp.ge	p6, p0 = OP_T_THRES, len	/* is len <= OP_T_THRES */
87	mov	ar.ec = 0		/* ec not guaranteed zero on entry */
88(p6)	br.cond.spnt	.cpyfew		/* copy byte by byte */
89	;;
90	cmp.eq	p6, p0 = loopcnt, r0
91	mux1	charx8 = char, @brcst
92(p6)	br.cond.sptk .dest_aligned
93	sub	len = len, loopcnt	/* len -= -dest % 8 */
94	adds	loopcnt = -1, loopcnt	/* --loopcnt */
95	;;
96	mov	ar.lc = loopcnt
97.l1:					/* copy -dest % 8 bytes */
98	ld1	value = [src], 1	/* value = *src++ */
99	;;
100	st1	[dest] = value, 1	/* *dest++ = value */
101	cmp.eq	p6, p0 = value, char
102(p6)	br.cond.spnt .foundit
103	br.cloop.dptk .l1
104.dest_aligned:
105	and	sh1 = 7, src 		/* sh1 = src % 8 */
106	and	tmp = -8, len   	/* tmp = len & -OPSIZ */
107	and	asrc = -8, src		/* asrc = src & -OPSIZ  -- align src */
108	shr.u	loopcnt = len, 3	/* loopcnt = len / 8 */
109	and	len = 7, len ;;		/* len = len % 8 */
110	shl	sh1 = sh1, 3		/* sh1 = 8 * (src % 8) */
111	adds	loopcnt = -1, loopcnt	/* --loopcnt */
112	mov     pr.rot = 1 << 16 ;;	/* set rotating predicates */
113	sub	sh2 = 64, sh1		/* sh2 = 64 - sh1 */
114	mov	ar.lc = loopcnt		/* set LC */
115	cmp.eq  p6, p0 = sh1, r0 	/* is the src aligned? */
116(p6)    br.cond.sptk .src_aligned ;;
117	add	src = src, tmp		/* src += len & -OPSIZ */
118	mov	ar.ec = MEMLAT + 6 + 1 	/* six more passes needed */
119	ld8	r[1] = [asrc], 8 	/* r[1] = w0 */
120	cmp.ne	p6, p0 = r0, r0	;;	/* clear p6 */
121	ALIGN(32)
122.l2:
123(p[0])		ld8.s	r[0] = [asrc], 8		/* r[0] = w1 */
124(p[MEMLAT])	shr.u	tmp1[0] = r[1 + MEMLAT], sh1	/* tmp1 = w0 >> sh1 */
125(p[MEMLAT])	shl	tmp2[0] = r[0 + MEMLAT], sh2  	/* tmp2 = w1 << sh2 */
126(p[MEMLAT+4])	xor	tmp3[0] = val[1], charx8
127(p[MEMLAT+5])	czx1.r	pos0[0] = tmp3[1]
128(p[MEMLAT+6])	chk.s	r[6 + MEMLAT], .recovery1	/* our data isn't */
129							/* valid - rollback! */
130(p[MEMLAT+6])	cmp.ne	p6, p0 = 8, pos0[1]
131(p6)		br.cond.spnt	.gotit
132(p[MEMLAT+6])	st8	[dest] = val[3], 8		/* store val to dest */
133(p[MEMLAT+3])	or	val[0] = tmp1[3], tmp2[3] 	/* val = tmp1 | tmp2 */
134		br.ctop.sptk    .l2
135		br.cond.sptk .cpyfew
136
137.src_aligned:
138		cmp.ne  p6, p0 = r0, r0			/* clear p6 */
139		mov     ar.ec = MEMLAT + 2 + 1 ;;	/* set EC */
140.l3:
141(p[0])		ld8.s	r[0] = [src], 8
142(p[MEMLAT])	xor	tmp3[0] = r[MEMLAT], charx8
143(p[MEMLAT+1])	czx1.r	pos0[0] = tmp3[1]
144(p[MEMLAT+2])	cmp.ne	p7, p0 = 8, pos0[1]
145(p[MEMLAT+2])	chk.s	r[MEMLAT+2], .recovery2
146(p7)		br.cond.spnt	.gotit
147.back2:
148(p[MEMLAT+2])	st8	[dest] = r[MEMLAT+2], 8
149		br.ctop.dptk .l3
150.cpyfew:
151	cmp.eq	p6, p0 = len, r0	/* is len == 0 ? */
152	adds	len = -1, len		/* --len; */
153(p6)	br.cond.spnt	.restore_and_exit ;;
154	mov	ar.lc = len
155.l4:
156	ld1	value = [src], 1
157	;;
158	st1	[dest] = value, 1
159	cmp.eq	p6, p0 = value, char
160(p6)	br.cond.spnt .foundit
161	br.cloop.dptk	.l4 ;;
162.foundit:
163(p6)	mov	ret0 = dest
164.restore_and_exit:
165	mov     pr = saved_pr, -1    	/* restore the predicate registers */
166	mov 	ar.lc = saved_lc	/* restore the loop counter */
167	mov 	ar.ec = saved_ec ;;	/* restore the epilog counter */
168	br.ret.sptk.many b0
169.gotit:
170	.pred.rel "mutex" p6, p7
171(p6)	mov	value = val[3]		/* if coming from l2 */
172(p7)	mov	value = r[MEMLAT+2]	/* if coming from l3 */
173	mov	ar.lc = pos0[1] ;;
174.l5:
175	extr.u	tmp = value, 0, 8 ;;
176	st1	[dest] = tmp, 1
177	shr.u	value = value, 8
178	br.cloop.sptk .l5 ;;
179	mov 	ret0 = dest
180	mov	pr = saved_pr, -1
181	mov	ar.lc = saved_lc
182	br.ret.sptk.many b0
183
184.recovery1:
185	adds	src = -(MEMLAT + 6 + 1) * 8, asrc
186	mov	loopcnt = ar.lc
187	mov	tmp = ar.ec ;;
188	sub	sh1 = (MEMLAT + 6 + 1), tmp
189	shr.u	sh2 = sh2, 3
190	;;
191	shl	loopcnt = loopcnt, 3
192	sub	src = src, sh2
193	shl	sh1 = sh1, 3
194	shl	tmp = tmp, 3
195	;;
196	add	len = len, loopcnt
197	add	src = sh1, src ;;
198	add	len = tmp, len
199.back1:
200	br.cond.sptk .cpyfew
201
202.recovery2:
203	add	tmp = -(MEMLAT + 3) * 8, src
204(p7)	br.cond.spnt .gotit
205	;;
206	ld8	r[MEMLAT+2] = [tmp] ;;
207	xor	pos0[1] = r[MEMLAT+2], charx8 ;;
208	czx1.r	pos0[1] = pos0[1] ;;
209	cmp.ne	p7, p6 = 8, pos0[1]
210(p7)	br.cond.spnt .gotit
211	br.cond.sptk .back2
212END(memccpy)
213