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