ffs.S revision 256281
1181834Sroberto/*	$NetBSD: ffs.S,v 1.5 2003/04/05 23:08:52 bjh21 Exp $	*/
2181834Sroberto/*
3181834Sroberto * Copyright (c) 2001 Christopher Gilbert
4181834Sroberto * All rights reserved.
5181834Sroberto *
6181834Sroberto * Redistribution and use in source and binary forms, with or without
7181834Sroberto * modification, are permitted provided that the following conditions
8181834Sroberto * are met:
9181834Sroberto * 1. Redistributions of source code must retain the above copyright
10181834Sroberto *    notice, this list of conditions and the following disclaimer.
11181834Sroberto * 2. Redistributions in binary form must reproduce the above copyright
12181834Sroberto *    notice, this list of conditions and the following disclaimer in the
13181834Sroberto *    documentation and/or other materials provided with the distribution.
14181834Sroberto * 3. The name of the company nor the name of the author may be used to
15181834Sroberto *    endorse or promote products derived from this software without specific
16181834Sroberto *    prior written permission.
17181834Sroberto *
18181834Sroberto * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19181834Sroberto * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20181834Sroberto * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21181834Sroberto * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
22181834Sroberto * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23181834Sroberto * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24181834Sroberto * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25181834Sroberto * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26181834Sroberto * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27181834Sroberto * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28181834Sroberto * SUCH DAMAGE.
29181834Sroberto */
30181834Sroberto
31181834Sroberto#include <machine/asm.h>
32181834Sroberto
33181834Sroberto__FBSDID("$FreeBSD: stable/10/lib/libc/arm/string/ffs.S 172616 2007-10-13 12:06:31Z cognet $");
34181834Sroberto
35181834Sroberto/*
36181834Sroberto * ffs - find first set bit, this algorithm isolates the first set
37181834Sroberto * bit, then multiplies the number by 0x0450fbaf which leaves the top
38181834Sroberto * 6 bits as an index into the table.  This algorithm should be a win
39181834Sroberto * over the checking each bit in turn as per the C compiled version.
40181834Sroberto *
41181834Sroberto * under ARMv5 there's an instruction called CLZ (count leading Zero's) that
42181834Sroberto * could be used
43181834Sroberto *
44181834Sroberto * This is the ffs algorithm devised by d.seal and posted to comp.sys.arm on
45181834Sroberto * 16 Feb 1994.
46181834Sroberto */
47181834Sroberto
48181834SrobertoENTRY(ffs)
49181834Sroberto	/* Standard trick to isolate bottom bit in r0 or 0 if r0 = 0 on entry */
50181834Sroberto 	rsb     r1, r0, #0
51181834Sroberto 	ands    r0, r0, r1
52181834Sroberto#ifndef _ARM_ARCH_5
53181834Sroberto	/*
54181834Sroberto	 * now r0 has at most one set bit, call this X
55181834Sroberto	 * if X = 0, all further instructions are skipped
56181834Sroberto	 */
57181834Sroberto	adrne   r2, .L_ffs_table
58181834Sroberto	orrne   r0, r0, r0, lsl #4  /* r0 = X * 0x11 */
59181834Sroberto	orrne   r0, r0, r0, lsl #6  /* r0 = X * 0x451 */
60181834Sroberto	rsbne   r0, r0, r0, lsl #16 /* r0 = X * 0x0450fbaf */
61181834Sroberto
62181834Sroberto	/* now lookup in table indexed on top 6 bits of r0 */
63181834Sroberto	ldrneb  r0, [ r2, r0, lsr #26 ]
64181834Sroberto
65181834Sroberto        RET
66181834Sroberto.text;
67181834Sroberto.type .L_ffs_table, _ASM_TYPE_OBJECT;
68181834Sroberto.L_ffs_table:
69181834Sroberto/*               0   1   2   3   4   5   6   7           */
70181834Sroberto	.byte	 0,  1,  2, 13,  3,  7,  0, 14  /*  0- 7 */
71181834Sroberto	.byte	 4,  0,  8,  0,  0,  0,  0, 15  /*  8-15 */
72181834Sroberto	.byte	11,  5,  0,  0,  9,  0,  0, 26  /* 16-23 */
73181834Sroberto	.byte	 0,  0,  0,  0,  0, 22, 28, 16  /* 24-31 */
74181834Sroberto	.byte	32, 12,  6,  0,  0,  0,  0,  0	/* 32-39 */
75181834Sroberto	.byte	10,  0,  0, 25,  0,  0, 21, 27  /* 40-47 */
76181834Sroberto	.byte	31,  0,  0,  0,  0, 24,  0, 20  /* 48-55 */
77181834Sroberto	.byte   30,  0, 23, 19, 29, 18, 17,  0  /* 56-63 */
78181834Sroberto#else
79181834Sroberto	clzne	r0, r0
80181834Sroberto	rsbne	r0, r0, #32
81181834Sroberto	RET
82181834Sroberto#endif
83181834Sroberto