1/* SPDX-License-Identifier: GPL-2.0 */
2/*
3 * arch/alpha/lib/strncat.S
4 * Contributed by Richard Henderson (rth@tamu.edu)
5 *
6 * Append no more than COUNT characters from the null-terminated string SRC
7 * to the null-terminated string DST.  Always null-terminate the new DST.
8 *
9 * This differs slightly from the semantics in libc in that we never write
10 * past count, whereas libc may write to count+1.  This follows the generic
11 * implementation in lib/string.c and is, IMHO, more sensible.
12 */
13#include <linux/export.h>
14	.text
15
16	.align 3
17	.globl strncat
18	.ent strncat
19strncat:
20	.frame $30, 0, $26
21	.prologue 0
22
23	mov	$16, $0		# set up return value
24	beq	$18, $zerocount
25
26	/* Find the end of the string.  */
27
28	ldq_u   $1, 0($16)	# load first quadword ($16 may be misaligned)
29	lda     $2, -1($31)
30	insqh   $2, $16, $2
31	andnot  $16, 7, $16
32	or      $2, $1, $1
33	cmpbge  $31, $1, $2	# bits set iff byte == 0
34	bne     $2, $found
35
36$loop:	ldq     $1, 8($16)
37	addq    $16, 8, $16
38	cmpbge  $31, $1, $2
39	beq     $2, $loop
40
41$found:	negq    $2, $3		# clear all but least set bit
42	and     $2, $3, $2
43
44	and     $2, 0xf0, $3	# binary search for that set bit
45	and	$2, 0xcc, $4
46	and	$2, 0xaa, $5
47	cmovne	$3, 4, $3
48	cmovne	$4, 2, $4
49	cmovne	$5, 1, $5
50	addq	$3, $4, $3
51	addq	$16, $5, $16
52	addq	$16, $3, $16
53
54	/* Now do the append.  */
55
56	bsr	$23, __stxncpy
57
58	/* Worry about the null termination.  */
59
60	zapnot	$1, $27, $2	# was last byte a null?
61	bne	$2, 0f
62	ret
63
640:	cmplt	$27, $24, $2	# did we fill the buffer completely?
65	or	$2, $18, $2
66	bne	$2, 2f
67
68	and	$24, 0x80, $2	# no zero next byte
69	bne	$2, 1f
70
71	/* Here there are bytes left in the current word.  Clear one.  */
72	addq	$24, $24, $24	# end-of-count bit <<= 1
732:	zap	$1, $24, $1
74	stq_u	$1, 0($16)
75	ret
76
771:	/* Here we must read the next DST word and clear the first byte.  */
78	ldq_u	$1, 8($16)
79	zap	$1, 1, $1
80	stq_u	$1, 8($16)
81
82$zerocount:
83	ret
84
85	.end strncat
86	EXPORT_SYMBOL(strncat)
87