1/*	$NetBSD: str.S,v 1.1.20.3 2004/09/21 13:23:40 skrll Exp $ */
2/*
3 * Copyright (c) 1996 Ludd, University of Lule}, Sweden.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 *    must display the following acknowledgement:
16 *      This product includes software developed at Ludd, University of
17 *      Lule}, Sweden and its contributors.
18 * 4. The name of the author may not be used to endorse or promote products
19 *    derived from this software without specific prior written permission
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33/*
34 * Small versions of the most common functions not using any
35 * emulated instructions.
36 */
37
38#include "asm.h"
39
40/*
41 * atoi() used in devopen.
42 */
43ENTRY(atoi, 0)
44	movl	4(%ap),%r1
45	clrl	%r0
46
472:	movzbl	(%r1)+,%r2
48	cmpb	%r2,$48
49	blss	1f
50	cmpb	%r2,$57
51	bgtr	1f
52	subl2	$48,%r2
53	mull2	$10,%r0
54	addl2	%r2,%r0
55	brb	2b
561:	ret
57
58/*
59 * strchr() small and easy.
60 */
61ENTRY(strchr, 0)
62	movq	4(%ap),%r0
631:	cmpb	(%r0), 8(%ap)
64	beql	2f
65	tstb	(%r0)+
66	bneq	1b
67	clrl	%r0
682:	ret
69
70/*
71 * cmpc3 is emulated on MVII.
72 */
73ENTRY(memcmp, 0)
74	brb	10f
75ENTRY(bcmp, 0)
7610:	movl	4(%ap), %r2
77	movl	8(%ap), %r1
78	movl	12(%ap), %r0
792:	cmpb	(%r2)+, (%r1)+
80	bneq	1f
81	sobgtr	%r0, 2b
823:	ret
831:	bgtru	5f
84	movl	$-1, %r0
85	brb	3b
865:	movl    $1, %r0
87	ret
88
89/*
90 * movc can't do length in excess of 64K, so we shall not use them.
91 */
92ENTRY(bzero,0)
93	movl	4(%ap),%r0
94	movl	8(%ap),%r1
951:	clrb	(%r0)+
96	sobgtr	%r1,1b
97	ret
98
99/*
100 * memcpy and bcopy are the same, except for argument order. Silly stuff.
101 */
102ENTRY(memcpy,0)
103	movl	8(%ap),%r0
104	movl	4(%ap),%r1
105	brb	1f
106ENTRY(bcopy,0)
107	movl	4(%ap),%r0
108	movl	8(%ap),%r1
1091:	movl	12(%ap),%r2
110	cmpl	%r0,%r1
111	bgtru	3f
112	addl2	%r2,%r0
113	addl2	%r2,%r1
1142:	movb	-(%r0),-(%r1)
115	sobgtr	%r2,2b
116	ret
1173:	movb	(%r0)+,(%r1)+
118	sobgtr	%r2,3b
119	ret
120
121ENTRY(memset,0)
122	movl	4(%ap),%r0
123	movl	12(%ap),%r1
1241:	movb	8(%ap),(%r0)+
125	sobgtr	%r1,1b
126	ret
127
128ENTRY(strlen, 0)
129	movl	4(%ap), %r0
1301:	tstb	(%r0)+
131	bneq	1b
132	decl	%r0
133	subl2	4(%ap), %r0
134	ret
135
136ENTRY(strncmp, 0)
137	movl	12(%ap), %r3
138	bneq	5f
139	brb	4f
140
141ENTRY(strcmp, 0)
142	movl	$250, %r3	# max string len to compare
1435:	movl	4(%ap), %r2
144	movl	8(%ap), %r1
145	movl	$1, %r0
146
1472:	cmpb	(%r2),(%r1)+
148	bneq	1f		# something differ
149	tstb	(%r2)+
150	beql	4f		# continue, strings unequal
151	decl	%r3		# max string len encountered?
152	bneq	2b
153
1544:	clrl	%r0		# We are done, strings equal.
155	ret
156
1571:	bgtru	3f
158	mnegl	%r0, %r0
1593:	ret
160
161ENTRY(strncpy, 0)
162	movl	4(%ap), %r1
163	movl	8(%ap), %r2
164	movl	12(%ap), %r3
165	bleq	2f
166
1671:	movb	(%r2)+, (%r1)+
168	beql	2f
169	sobgtr	%r3,1b
170	ret
1712:	decl	%r1
1723:	clrb	(%r1)+
173	sobgtr	%r3,3b
174	ret
175
176ENTRY(strcat, 0)
177	pushl	4(%ap)
178	calls	$1,_C_LABEL(strlen)
179	addl2	4(%ap),%r0
180	movl	8(%ap),%r1
1811:	movb	(%r1)+,(%r0)+
182	bneq	1b
183	ret
184
185ENTRY(setjmp, 0)
186	movl	4(%ap), %r0
187	movl	8(%fp), (%r0)
188	movl	12(%fp), 4(%r0)
189	movl	16(%fp), 8(%r0)
190	addl3	%fp,$28,12(%r0)
191	clrl	%r0
192	ret
193
194ENTRY(longjmp, 0)
195	movl	4(%ap), %r1
196	movl	8(%ap), %r0
197	movl	(%r1), %ap
198	movl	4(%r1), %fp
199	movl	12(%r1), %sp
200	jmp	*8(%r1)
201
202