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