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