ffs.S revision 1.1
1/*	$NetBSD: ffs.S,v 1.3 1996/10/17 03:08:13 cgd Exp $	*/
2
3/*
4 * Copyright (c) 1995 Christopher G. Demetriou
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 *    must display the following acknowledgement:
17 *      This product includes software developed by Christopher G. Demetriou
18 *	for the NetBSD Project.
19 * 4. The name of the author may not be used to endorse or promote products
20 *    derived from this software without specific prior written permission
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34#include <machine/asm.h>
35
36LEAF(ffs, 1)
37	addl	a0, 0, t0
38	beq	t0, Lallzero
39
40	/*
41	 * Initialize return value (v0), and set up t1 so that it
42	 * contains the mask with only the lowest bit set.
43	 */
44	subl	zero, t0, t1
45	ldil	v0, 1
46	and	t0, t1, t1
47
48	and	t1, 0xff, t2
49	bne	t2, Ldo8
50
51	/*
52	 * If lower 16 bits empty, add 16 to result and use upper 16.
53	 */
54	zapnot	t1, 0x03, t3
55	bne	t3, Ldo16
56	sra	t1, 16, t1
57	addl	v0, 16, v0
58
59Ldo16:
60	/*
61	 * If lower 8 bits empty, add 8 to result and use upper 8.
62	 */
63	and	t1, 0xff, t4
64	bne	t4, Ldo8
65	sra	t1, 8, t1
66	addl	v0, 8, v0
67
68Ldo8:
69	and	t1, 0x0f, t5		/* lower 4 of 8 empty? */
70	and	t1, 0x33, t6		/* lower 2 of each 4 empty? */
71	and	t1, 0x55, t7		/* lower 1 of each 2 empty? */
72
73	/* If lower 4 bits empty, add 4 to result. */
74	bne	t5, Ldo4
75	addl	v0, 4, v0
76
77Ldo4:	/* If lower 2 bits of each 4 empty, add 2 to result. */
78	bne	t6, Ldo2
79	addl	v0, 2, v0
80
81Ldo2:	/* If lower bit of each 2 empty, add 1 to result. */
82	bne	t7, Ldone
83	addl	v0, 1, v0
84
85Ldone:
86	RET
87
88Lallzero:
89	bis	zero, zero, v0
90	RET
91END(ffs)
92