1/* $NetBSD: ffs.S,v 1.1 2005/12/20 19:28:49 christos 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 for the
18 *          NetBSD Project.  See http://www.NetBSD.org/ for
19 *          information about NetBSD.
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 * <<Id: LICENSE,v 1.2 2000/06/14 15:57:33 cgd Exp>>
35 */
36
37#include <machine/asm.h>
38
39LEAF(ffs, 1)
40          addl      a0, 0, t0
41          beq       t0, Lallzero
42
43          /*
44           * Initialize return value (v0), and set up t1 so that it
45           * contains the mask with only the lowest bit set.
46           */
47          subl      zero, t0, t1
48          ldil      v0, 1
49          and       t0, t1, t1
50
51          and       t1, 0xff, t2
52          bne       t2, Ldo8
53
54          /*
55           * If lower 16 bits empty, add 16 to result and use upper 16.
56           */
57          zapnot    t1, 0x03, t3
58          bne       t3, Ldo16
59          sra       t1, 16, t1
60          addl      v0, 16, v0
61
62Ldo16:
63          /*
64           * If lower 8 bits empty, add 8 to result and use upper 8.
65           */
66          and       t1, 0xff, t4
67          bne       t4, Ldo8
68          sra       t1, 8, t1
69          addl      v0, 8, v0
70
71Ldo8:
72          and       t1, 0x0f, t5                  /* lower 4 of 8 empty? */
73          and       t1, 0x33, t6                  /* lower 2 of each 4 empty? */
74          and       t1, 0x55, t7                  /* lower 1 of each 2 empty? */
75
76          /* If lower 4 bits empty, add 4 to result. */
77          bne       t5, Ldo4
78          addl      v0, 4, v0
79
80Ldo4:     /* If lower 2 bits of each 4 empty, add 2 to result. */
81          bne       t6, Ldo2
82          addl      v0, 2, v0
83
84Ldo2:     /* If lower bit of each 2 empty, add 1 to result. */
85          bne       t7, Ldone
86          addl      v0, 1, v0
87
88Ldone:
89          RET
90
91Lallzero:
92          bis       zero, zero, v0
93          RET
94END(ffs)
95