xref: /csrg-svn/lib/libc/vax/string/rindex.s (revision 17334)
1/*	rindex.s	4.3	84/11/04	*/
2
3/*
4 * Find the last occurence of c in the string cp.
5 * Return pointer to match or null pointer.
6 *
7 * char *
8 * rindex(cp, c)
9 *	char *cp, c;
10 */
11#include "DEFS.h"
12
13ENTRY(rindex, 0)
14	movq	4(ap),r1	# r1 = cp; r2 = c
15	tstl	r2		# check for special case c == '\0'
16	bneq	2f
171:
18	locc	$0,$65535,(r1)	# just find end of string
19	beql	1b		# still looking
20	movl	r1,r0		# found it
21	ret
222:
23	moval	tbl,r3		# r3 = address of table
24	bbss	$0,(r3),5f	# insure not reentering
25	movab	(r3)[r2],r5	# table entry for c
26	incb	(r5)
27	clrl	r4		# last found
283:
29	scanc	$65535,(r1),(r3),$1	# look for c or '\0'
30	beql	3b		# keep looking
31	tstb	(r1)		# if have found '\0'
32	beql	4f		#    we are done
33	movl	r1,r4		# save most recently found
34	incl	r1		# skip over character
35	jbr	3b		# keep looking
364:
37	movl	r4,r0		# return last found (if any)
38	clrb	(r5)		# clean up table
39	clrb	(r3)
40	ret
41
42	.data
43tbl:	.space	256
44	.text
45
46/*
47 * Reentrant, but slower version of rindex
48 */
495:
50	movl	r1,r3
51	clrl	r4		# r4 = pointer to last match
526:
53	locc	$0,$65535,(r3)	# look for '\0'
54	bneq	8f
55	decw	r0		# r0 = 65535
561:
57	locc	r2,r0,(r3)	# look for c
58	bneq	7f
59	movl	r1,r3		# reset pointer and ...
60	jbr	6b		# ... try again
617:
62	movl	r1,r4		# stash pointer ...
63	addl3	$1,r1,r3	# ... skip over match and ...
64	decl	r0		# ... decrement count
65	jbr	6b		# ... try again
668:
67	subl3	r3,r1,r0	# length of short block
68	incl	r0		# +1 for '\0'
699:
70	locc	r2,r0,(r3)	# look for c
71	beql	0f
72	movl	r1,r4		# stash pointer ...
73	addl3	$1,r1,r3	# ... skip over match ...
74	decl	r0		# ... adjust count and ...
75	jbr	9b		# ... try again
760:
77	movl	r4,r0		# return stashed pointer
78	ret
79