1/*
2 *  (c) Copyright 1986 HEWLETT-PACKARD COMPANY
3 *
4 *  To anyone who acknowledges that this file is provided "AS IS"
5 *  without any express or implied warranty:
6 *      permission to use, copy, modify, and distribute this file
7 *  for any purpose is hereby granted without fee, provided that
8 *  the above copyright notice and this notice appears in all
9 *  copies, and that the name of Hewlett-Packard Company not be
10 *  used in advertising or publicity pertaining to distribution
11 *  of the software without specific, written prior permission.
12 *  Hewlett-Packard Company makes no representations about the
13 *  suitability of this software for any purpose.
14 */
15
16/* HPUX_ID:	@(#) $Revision$	*/
17/*
18 * strncpy(s1, s2, n)
19 *
20 * Copy s2 to s1, truncating or null-padding to always copy n bytes
21 * return s1
22 */
23
24#include <picolibc.h>
25
26#include "DEFS.h"
27
28#define	d_addr  r26
29#define	s_addr  r25
30#define	count   r24
31#define	tmp1    r19
32#define	tmp2    r20
33#define	tmp3    r21
34#define	tmp4    r22
35#define	tmp5	arg3
36#define	save	r1
37
38
39ENTRY(strncpy)
40
41	combt,=   s_addr,r0,pad_null_bytes1	/* if s2==NULL then pad nulls and exit */
42        copy      d_addr,ret0          /* The return value is defined to be the value of d_addr. DELAY SLOT*/
43        addibt,<,n  -4,count,byteloop     /* If count is <= 4 don't get fancy.*/
44
45        extru       s_addr,31,2,tmp1   /* Extract the low two bits of the source address.*/
46        extru       d_addr,31,2,tmp5   /* Extract the low two bits of the destination address.*/
47        add         count,tmp5,count   /* pre increment the count by the byte address so that the count is*/
48        comb,<>       tmp5,tmp1,not_aligned /* branch if tmp5<>tmp1. */
49        dep         0,31,2,s_addr      /* Compute the word address of the source.  DELAY SLOT.*/
50/* aligned*/
51	combt,=		tmp5,r0,skip_mask
52        ldwm        4(0,s_addr),tmp1   /* tmp1 = *s_addr   s_addr += 4 (DELAY SLOT)*/
53	sh3add		tmp5,r0,save	/* compute mask in save*/
54	mtctl		save,11
55	zvdepi		-2,32,save
56	b		skip_mask	/* don't reload tmp1*/
57	or		save,tmp1,tmp1  /* or mask with data*/
58
59chunks:
60	ldwm		4(0,s_addr),tmp1 /* get a word*/
61
62skip_mask:
63	uxor,nbz	tmp1,r0,save 	/* check for null*/
64	b,n		null1
65	addibf,<	-4,count,chunks
66        stbys,b,m   tmp1,4(0,d_addr)   /* store word (delay slot)*/
67
68/* back_porch				   last word to store*/
69         addibt,=,n  4,count,done       /* if count = 0 we're, of course, done !*/
70         ldws        0(s_addr),tmp1     /* load up the back_porch*/
71         add         d_addr,count,d_addr/* final store address  is +1 too high !*/
72	 sh3add		count,r0, save	/* setup right mask based on count*/
73	 mtctl		save,r11
74	 zvdepi		-2,32,save	/*save now has left-hand mask*/
75	 uaddcm		r0,save,save	/*form right hand mask */
76	 or		tmp1,save,tmp1	/*and insert data*/
77	 uxor,nbz	tmp1,r0,save	/* check for null*/
78	 b,n 		null2
79	 bv		0(r2)
80	 stbys,e	tmp1,0(d_addr)	/* done */
81
82/* Begin non_aligned code. */
83not_aligned:
84        sub,>=       tmp5,tmp1,tmp3     /* compute the shift amt.and skip load if tmp5 > tmp1.*/
85        ldwm         4(0,s_addr),tmp1   /* load up the first word from the source. tmp1 = *s_addr++*/
86        zdep         tmp3,28,29,tmp4    /* compute the number of bits to shift */
87        mtctl        tmp4,11            /* load the shift count into cr11 = shift count register.*/
88        addibt,<,n   -4,count,chkchnk2 /* first step in pre adjustment of count for looping.*/
89
90        ldwm        4(0,s_addr),tmp2    /* get either first or second word from source. */
91	combt,=		tmp5,r0,skip_mask2 /* don't mask if whole word is valid*/
92        vshd        tmp1,tmp2,tmp3      /* position data !  (delay slot)*/
93	sh3add		tmp5,r0,save	/* setup r1*/
94	mtctl		save,r11	/* setup mask in save*/
95	zvdepi		-2,32,save
96	or		save, tmp3, tmp3
97        mtctl           tmp4,11            /* re-load the shift count into cr11 */
98	b		skip_mask2
99	copy 		r0, tmp5	/* zero out tmp5 so we don't try to mask again*/
100
101chunk2:
102        ldwm        4(0,s_addr),tmp2
103        vshd        tmp1,tmp2,tmp3
104
105skip_mask2:
106	uxor,nbz	tmp3, r0, save
107	b,n		null3
108        stbys,b,m   tmp3,4(0,d_addr)    /* store ! */
109
110        ldwm        4(0,s_addr),tmp1    /* get 2nd word ! */
111        vshd        tmp2,tmp1,tmp3      /* position data ! */
112	uxor,nbz	tmp3, r0, save
113	b,n		null4
114
115        addibf,<    -8,count,chunk2    /* If count is still >= 8 do another loop.*/
116        stbys,b,m   tmp3,4(0,d_addr)    /* store !*/
117
118chkchnk2:
119        addibt,<,n  4,count,bp_0       /* if we don't have 4 bytes left then do the back porch (bp_0)*/
120
121subchnk2: /* we have less than 8 chars to copy*/
122
123        ldwm        4(0,s_addr),tmp2    /* get next word !*/
124	combt,=		tmp5,r0,skip_mask3
125        vshd        tmp1,tmp2,tmp3      /* position data !*/
126	sh3add		tmp5,r0,save	/* setup r1*/
127	mtctl		save,r11	/* setup mask in save*/
128	zvdepi		-2,32,save
129	or		save, tmp3, tmp3
130	mtctl		tmp4,11		/* restore shift value again */
131	copy 		r0, tmp5	/* zero out tmp5 so we don't try to mask again*/
132skip_mask3:
133	uxor,nbz	tmp3,r0,save
134	b,n		null4
135	b		bp_1 /* we now have less than 4 bytes to move*/
136        stbys,b,m   tmp3,4(0,d_addr)    /* store !*/
137
138bp_0:
139	copy        tmp1,tmp2           /* switch registers used in the shift process.*/
140	addibt,<=,n  4,count,done        /* if count = -4 this implies that count = 0 -> done */
141
142bp_1:
143        ldwm        4(0,s_addr),tmp1    /* get final word !        */
144        vshd        tmp2,tmp1,tmp3      /* position data !*/
145	uxor,sbz	tmp3,r0,save	/* if some-byte-zero */
146	b		no_null		/* don't goto no_null-find which null instead */
147	add		d_addr,count,d_addr	/* get d_addr ready for stbys,e */
148	extru,<>	save,7,8,r0
149	b		found_null5
150	copy		r0, tmp5
151	extru,<>	save,15,8,r0
152	b		found_null5
153	ldil		0x1FE000,tmp5		/* setup mask (FF000000)*/
154	extru,<> 	save,23,8,r0
155	b		found_null5
156	ldil		0x1FFFE0,tmp5		/* setup mask (FFFF0000)*/
157	ldo		-1(r0),tmp5		/* setup mask (FFFFFFFF)*/
158found_null5:
159	and		tmp3,tmp5,tmp3  /* zero out tmp5 based on mask in tmp5*/
160no_null:
161	bv		0(r2)		/* were done*/
162        stbys,e     tmp3,0(0,d_addr)    /* store the data !*/
163
164/* here we do ye old byte-at-a-time moves.*/
165byteloop:
166	addibt,=,n     4,count,done
167        comb,=    0,s_addr,done
168	stbs	  r0,0(d_addr)		/* store null in case s_addr == NULL */
169        ldbs,ma     1(s_addr),tmp1
170encore:
171	combt,=,n	tmp1,r0, pad_null_bytes1
172        stbs,ma     tmp1,1(d_addr)
173        addibf,=,n  -1,count,encore
174        ldbs,ma     1(s_addr),tmp1
175	b,n		done
176
177pnb_1:
178	addibt,=,n	4,count,done /* if count was already 0 then we're done*/
179
180pad_null_bytes1:
181	combt,=,n   	count,r0,done	/* if count==0 then exit */
182pad_null_bytes2:
183	addibf,=	-1,count,pad_null_bytes2
184	stbs,ma		r0,1(d_addr)
185	b,n		done
186
187pad_nulls:
188	addibf,<=,n	-4,count,pad_nulls
189	stwm		r0,4(d_addr)
190	b,n		pnb_1
191
192
193null1:
194	extru,<>	save,7,8,r0
195	b		found_null1
196	copy		r0, tmp5
197	extru,<>	save,15,8,r0
198	b		found_null1
199	ldil		0x1FE000,tmp5		/* setup mask (FF000000)*/
200	extru,<> 	save,23,8,r0
201	b		found_null1
202	ldil		0x1FFFE0,tmp5		/* setup mask (FFFF0000)*/
203	ldo		-1(r0),tmp5		/* setup mask (FFFFFFFF)*/
204found_null1:
205	and		tmp1,tmp5,tmp1		/*zero out tmp1 according to mask*/
206	b		pad_nulls		/* nullify remaining count bytes*/
207	stbys,b,m	tmp1,4(0,d_addr)	/* first word (account for alignment)*/
208
209
210null2:	/* back porch case. We have less than 4 bytes to go.*/
211	extru,<>	save,7,8,r0 	/* is null in 1st byte? */
212	b		found_null2
213	copy		r0, tmp5
214	extru,<>	save,15,8,r0	/* is null in 2nd byte? */
215	b		found_null2
216	ldil		0x1FE000,tmp5		/* setup mask (FF000000)*/
217	b		found_null2	/* null must be in 3rd byte */
218	ldil		0x1FFFE0,tmp5		/* setup mask (FFFF0000)*/
219found_null2:
220	and		tmp1,tmp5,tmp1		/*zero out tmp1 according to mask*/
221	bv		0(r2)			/* we're done*/
222	stbys,e		tmp1,0(0,d_addr)	/* last word (back porch)*/
223
224null3:	/* not_aligned case where null is found in first of two words--adjust count*/
225	extru,<>	save,7,8,r0
226	b		found_null3
227	copy		r0, tmp5
228	extru,<>	save,15,8,r0
229	b		found_null3
230	ldil		0x1FE000,tmp5		/* setup mask (FF000000)*/
231	extru,<> 	save,23,8,r0
232	b		found_null3
233	ldil		0x1FFFE0,tmp5		/* setup mask (FFFF0000)*/
234	ldo		-1(r0),tmp5		/* setup mask (FFFFFFFF)*/
235found_null3:
236	addi		4,count,count 		/* fix count since null is in first of two words*/
237	and		tmp3,tmp5,tmp3		/*zero out tmp3 according to mask*/
238	b		pad_nulls		/* nullify remaining count bytes*/
239        stbys,b,m       tmp3,4(d_addr)
240
241null4:	/* not_aligned case where null is found in second of two words*/
242	extru,<>	save,7,8,r0
243	b		found_null4
244	copy		r0, tmp5
245	extru,<>	save,15,8,r0
246	b		found_null4
247	ldil		0x1FE000,tmp5		/* setup mask (FF000000)*/
248	extru,<> 	save,23,8,r0
249	b		found_null4
250	ldil		0x1FFFE0,tmp5		/* setup mask (FFFF0000)*/
251	ldo		-1(r0),tmp5		/* setup mask (FFFFFFFF)*/
252found_null4:
253	and		tmp3,tmp5,tmp3		/*zero out tmp4 according to mask*/
254	b		pad_nulls		/* nullify remaining count bytes*/
255        stbys,b,m       tmp3,4(d_addr)
256
257done:
258EXIT(strncpy)
259