1/* Copyright (C) 1996-2022 Free Software Foundation, Inc.
2   This file is part of the GNU C Library.
3
4   The GNU C Library is free software; you can redistribute it and/or
5   modify it under the terms of the GNU Lesser General Public
6   License as published by the Free Software Foundation; either
7   version 2.1 of the License, or (at your option) any later version.
8
9   The GNU C Library is distributed in the hope that it will be useful,
10   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12   Lesser General Public License for more details.
13
14   You should have received a copy of the GNU Lesser General Public
15   License along with the GNU C Library.  If not, see
16   <https://www.gnu.org/licenses/>.  */
17
18/* Copy no more than COUNT bytes of the null-terminated string from
19   SRC to DST.  If SRC does not cover all of COUNT, the balance is
20   zeroed.  */
21
22#include <sysdep.h>
23
24	.set noat
25	.set noreorder
26
27	.text
28
29ENTRY(strncpy)
30	ldgp	gp, 0(pv)
31#ifdef PROF
32	lda	AT, _mcount
33	jsr	AT, (AT), _mcount
34#endif
35	.prologue 1
36
37	mov	a0, v0		# set return value now
38	beq	a2, $zerocount
39	jsr	t9, __stxncpy	# do the work of the copy
40
41	bne	a2, $multiword	# do we have full words left?
42
43	.align 3
44	subq	t8, 1, t2	# e0    : guess not
45	subq	t10, 1, t3	# .. e1 :
46	or	t2, t8, t2	# e0    : clear the bits between the last
47	or	t3, t10, t3	# .. e1 : written byte and the last byte in
48	andnot	t3, t2, t3	# e0    : COUNT
49	zap	t0, t3, t0	# e1    :
50	stq_u	t0, 0(a0)	# e0    :
51	ret			# .. e1 :
52
53$multiword:
54	subq	t8, 1, t7	# e0    : clear the final bits in the prev
55	or	t7, t8, t7	# e1    : word
56	zapnot	t0, t7, t0	# e0    :
57	subq	a2, 1, a2	# .. e1 :
58	stq_u	t0, 0(a0)	# e0    :
59	addq	a0, 8, a0	# .. e1 :
60
61	beq	a2, 1f		# e1    :
62	blbc	a2, 0f		# e1    :
63
64	stq_u	zero, 0(a0)	# e0    : zero one word
65	subq	a2, 1, a2	# .. e1 :
66	addq	a0, 8, a0	# e0    :
67	beq	a2, 1f		# .. e1 :
68
690:	stq_u	zero, 0(a0)	# e0    : zero two words
70	subq	a2, 2, a2	# .. e1 :
71	stq_u	zero, 8(a0)	# e0    :
72	addq	a0, 16, a0	# .. e1 :
73	bne	a2, 0b		# e1    :
74	unop
75
761:	ldq_u	t0, 0(a0)	# e0    : clear the leading bits in the final
77	subq	t10, 1, t7	# .. e1 : word
78	or	t7, t10, t7	# e0    :
79	zap	t0, t7, t0	# e1 (stall)
80	stq_u	t0, 0(a0)	# e0    :
81
82$zerocount:
83	ret			# .. e1 :
84
85	END(strncpy)
86libc_hidden_builtin_def (strncpy)
87