1129202Scognet/*	$NetBSD: ffs.S,v 1.5 2003/04/05 23:08:52 bjh21 Exp $	*/
2129202Scognet/*
3129202Scognet * Copyright (c) 2001 Christopher Gilbert
4129202Scognet * All rights reserved.
5129202Scognet *
6129202Scognet * Redistribution and use in source and binary forms, with or without
7129202Scognet * modification, are permitted provided that the following conditions
8129202Scognet * are met:
9129202Scognet * 1. Redistributions of source code must retain the above copyright
10129202Scognet *    notice, this list of conditions and the following disclaimer.
11129202Scognet * 2. Redistributions in binary form must reproduce the above copyright
12129202Scognet *    notice, this list of conditions and the following disclaimer in the
13129202Scognet *    documentation and/or other materials provided with the distribution.
14129202Scognet * 3. The name of the company nor the name of the author may be used to
15129202Scognet *    endorse or promote products derived from this software without specific
16129202Scognet *    prior written permission.
17129202Scognet *
18129202Scognet * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19129202Scognet * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20129202Scognet * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21129202Scognet * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
22129202Scognet * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23129202Scognet * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24129202Scognet * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25129202Scognet * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26129202Scognet * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27129202Scognet * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28129202Scognet * SUCH DAMAGE.
29129202Scognet */
30129202Scognet
31129202Scognet#include <machine/asm.h>
32129202Scognet
33129202Scognet__FBSDID("$FreeBSD$");
34129202Scognet
35129202Scognet/*
36129202Scognet * ffs - find first set bit, this algorithm isolates the first set
37129202Scognet * bit, then multiplies the number by 0x0450fbaf which leaves the top
38129202Scognet * 6 bits as an index into the table.  This algorithm should be a win
39129202Scognet * over the checking each bit in turn as per the C compiled version.
40129202Scognet *
41129202Scognet * under ARMv5 there's an instruction called CLZ (count leading Zero's) that
42129202Scognet * could be used
43129202Scognet *
44129202Scognet * This is the ffs algorithm devised by d.seal and posted to comp.sys.arm on
45129202Scognet * 16 Feb 1994.
46129202Scognet */
47129202Scognet
48129202ScognetENTRY(ffs)
49129202Scognet	/* Standard trick to isolate bottom bit in r0 or 0 if r0 = 0 on entry */
50129202Scognet 	rsb     r1, r0, #0
51129202Scognet 	ands    r0, r0, r1
52172616Scognet#ifndef _ARM_ARCH_5
53129202Scognet	/*
54129202Scognet	 * now r0 has at most one set bit, call this X
55129202Scognet	 * if X = 0, all further instructions are skipped
56129202Scognet	 */
57129202Scognet	adrne   r2, .L_ffs_table
58129202Scognet	orrne   r0, r0, r0, lsl #4  /* r0 = X * 0x11 */
59129202Scognet	orrne   r0, r0, r0, lsl #6  /* r0 = X * 0x451 */
60129202Scognet	rsbne   r0, r0, r0, lsl #16 /* r0 = X * 0x0450fbaf */
61129202Scognet
62129202Scognet	/* now lookup in table indexed on top 6 bits of r0 */
63129202Scognet	ldrneb  r0, [ r2, r0, lsr #26 ]
64129202Scognet
65137464Scognet        RET
66129202Scognet.text;
67129202Scognet.type .L_ffs_table, _ASM_TYPE_OBJECT;
68129202Scognet.L_ffs_table:
69129202Scognet/*               0   1   2   3   4   5   6   7           */
70129202Scognet	.byte	 0,  1,  2, 13,  3,  7,  0, 14  /*  0- 7 */
71129202Scognet	.byte	 4,  0,  8,  0,  0,  0,  0, 15  /*  8-15 */
72129202Scognet	.byte	11,  5,  0,  0,  9,  0,  0, 26  /* 16-23 */
73129202Scognet	.byte	 0,  0,  0,  0,  0, 22, 28, 16  /* 24-31 */
74129202Scognet	.byte	32, 12,  6,  0,  0,  0,  0,  0	/* 32-39 */
75129202Scognet	.byte	10,  0,  0, 25,  0,  0, 21, 27  /* 40-47 */
76129202Scognet	.byte	31,  0,  0,  0,  0, 24,  0, 20  /* 48-55 */
77129202Scognet	.byte   30,  0, 23, 19, 29, 18, 17,  0  /* 56-63 */
78137343Scognet#else
79137343Scognet	clzne	r0, r0
80137343Scognet	rsbne	r0, r0, #32
81137464Scognet	RET
82137343Scognet#endif
83