1/*******************************************************************************
2 *
3 * Copyright (c) 1993 Intel Corporation
4 *
5 * Intel hereby grants you permission to copy, modify, and distribute this
6 * software and its documentation.  Intel grants this permission provided
7 * that the above copyright notice appears in all copies and that both the
8 * copyright notice and this permission notice appear in supporting
9 * documentation.  In addition, Intel grants this permission provided that
10 * you prominently mark as "not part of the original" any modifications
11 * made to this software or documentation, and that the name of Intel
12 * Corporation not be used in advertising or publicity pertaining to
13 * distribution of the software or the documentation without specific,
14 * written prior permission.
15 *
16 * Intel Corporation provides this AS IS, WITHOUT ANY WARRANTY, EXPRESS OR
17 * IMPLIED, INCLUDING, WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY
18 * OR FITNESS FOR A PARTICULAR PURPOSE.  Intel makes no guarantee or
19 * representations regarding the use of, or the results of the use of,
20 * the software and documentation in terms of correctness, accuracy,
21 * reliability, currentness, or otherwise; and you rely on the software,
22 * documentation and results solely at your own risk.
23 *
24 * IN NO EVENT SHALL INTEL BE LIABLE FOR ANY LOSS OF USE, LOSS OF BUSINESS,
25 * LOSS OF PROFITS, INDIRECT, INCIDENTAL, SPECIAL OR CONSEQUENTIAL DAMAGES
26 * OF ANY KIND.  IN NO EVENT SHALL INTEL'S TOTAL LIABILITY EXCEED THE SUM
27 * PAID TO INTEL FOR THE PRODUCT LICENSED HEREUNDER.
28 *
29 ******************************************************************************/
30
31	.file "strle_ca.s"
32#ifdef	__PIC
33	.pic
34#endif
35#ifdef	__PID
36	.pid
37#endif
38/*
39 * (c) copyright 1988,1993 Intel Corp., all rights reserved
40 */
41
42/*
43	procedure strlen  (optimized assembler version for the CA)
44
45	src_addr = strlen (src_addr)
46
47	return the number of bytes that precede the null byte in the
48	string pointed to by src_addr.
49
50	Undefined behavior will occur if the end of the source string (i.e.
51	the terminating null byte) is in the last four words of the program's
52	allocated memory space.  This is so because, in several cases, strlen
53	will fetch ahead several words.  Disallowing the fetch ahead would
54	impose a severe performance penalty.
55
56	This program handles two cases:
57
58	1) the argument starts on a word boundary
59	2) the argument doesn't start on a word boundary
60
61	At the time of this writing, only g0 thru g7 and g13 are available
62	for use in this leafproc;  other registers would have to be saved and
63	restored.  These nine registers, plus tricky use of g14 are sufficient
64	to implement the routine.  The registers are used as follows:
65
66	g0  original src ptr;  upon return it is the byte count.
67	g1
68	g2  src ptr
69	g3  mask
70	g4  even word of the source string
71	g5  odd word of the source string
72	g6  copy of even word, shift count
73	g7  copy of odd word
74	g13 return address
75	g14 byte extracted.
76*/
77
78	.globl	_strlen
79	.globl	__strlen
80	.leafproc	_strlen, __strlen
81	.align	2
82_strlen:
83#ifndef __PIC
84	lda 	Lrett,g14
85#else
86	lda 	Lrett-(.+8)(ip),g14
87#endif
88__strlen:
89
90	notand	g0,3,g2		# extract word addr of start of src
91	lda	(g14),g13	# preserve return address
92	and	g0,3,g7		# extract byte offset of src
93	ld	(g2),g5		# fetch word containing at least first byte
94	shlo	3,g7,g7		# get shift count for making mask for first word
95	lda	4(g2),g2	# post-increment src word pointer
96	subi	1,0,g3		# mask initially all ones
97	chkbit	2,g2		# are we on an even word boundary or an odd one?
98#if __i960_BIG_ENDIAN__
99	shro	g7,g3,g3	# get mask for bytes needed from first word
100	notor	g5,g3,g7	# set unneeded bytes to all ones
101	lda	0xff000000,g3	# byte extraction mask
102#else
103	shlo	g7,g3,g3	# get mask for bytes needed from first word
104	notor	g5,g3,g7	# set unneeded bytes to all ones
105	lda	0xff,g3		# byte extraction mask
106#endif
107	bno.f	Lodd_word	# branch if first word is odd
108	mov	g7,g4		# move first word to copy thereof
109	ld	(g2),g5		# load odd word
110	lda	4(g2),g2	# post-increment src word pointer
111
112
113Leven_word:
114	scanbyte 0,g4		# check for null byte
115	movl	g4,g6		# copy both words
116Lodd_word:			# trickery!  if we branch here, following branch
117				/* instruction will fall thru, as we want,  */
118				/* effecting the load of g4 and g5 only. */
119	ldl	(g2),g4		# fetch next pair of word of src
120	bo.f	Lsearch_for_null	# branch if null found
121	scanbyte 0,g7		# check for null byte
122	lda	8(g2),g2	# post-increment src word pointer
123	bno.t	Leven_word	# branch if null not found yet
124
125	subo	4,g2,g2		# back up the byte pointer
126	lda	(g7),g6		# move odd word to search word
127Lsearch_for_null:
128	subo	9,g2,g2		# back up the byte pointer
129Lsearch_for_null.a:
130	and	g6,g3,g14	# extract byte
131	cmpo	0,g14		# is it null?
132	lda	1(g2),g2	# bump src byte ptr
133#if __i960_BIG_ENDIAN__
134	shlo	8,g6,g6		# shift word to position next byte
135#else
136	shro	8,g6,g6		# shift word to position next byte
137#endif
138	bne.t	Lsearch_for_null.a
139
140Lexit_code:
141	subo	g0,g2,g0	# calculate string length
142	bx	(g13)		# g0 = addr of src;  g14 = 0
143Lrett:
144	ret
145
146/* end of strlen */
147