blob: ef38b2a372740101abd59bfb6b0176069b0e72a0 [file] [log] [blame]
Raghu Gandham405b8022012-07-25 18:16:42 -07001/* $NetBSD: ffs.S,v 1.5 2003/04/05 23:08:52 bjh21 Exp $ */
2/*
3 * Copyright (c) 2001 Christopher Gilbert
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the company nor the name of the author may be used to
15 * endorse or promote products derived from this software without specific
16 * prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
22 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31/*
32 * ffs - find first set bit, this algorithm isolates the first set
33 * bit, then multiplies the number by 0x0450fbaf which leaves the top
34 * 6 bits as an index into the table. This algorithm should be a win
35 * over the checking each bit in turn as per the C compiled version.
36 *
37 * This is the ffs algorithm devised by d.seal and posted to comp.sys.arm on
38 * 16 Feb 1994.
39 */
40
41 .text
42
43 .ent ffs
44 .type ffs, @function
45 .global ffs
46 .align 4
47 .set noreorder
48ffs:
49#if (__mips==32)
50 subu $t0,$0,$a0
51 and $a0,$t0
52 clz $t0,$a0
53 li $v0,32
54 j $ra
55 subu $v0,$t0
56#else
57/* Size of the ffs routine in bytes
58 * This is used to index .L_ffs_table from $t9
59 * FIXME: there must be a better way to do this
60 */
61#define FFSSIZE 12*4
62 subu $t0,$0,$a0
63 and $a0,$t0
64 /*
65 * now a0 has at most one set bit, call this X
66 * if X = 0, all further instructions are skipped
67 */
68 sll $t0,$a0,4 /* t0 = X * 0x00000010 */
69 or $a0,$t0 /* a0 = X * 0x00000011 */
70 sll $t0,$a0,6 /* t0 = X * 0x00000440 */
71 or $a0,$t0 /* a0 = X * 0x00000451 */
72 sll $t0,$a0,16 /* t0 = X * 0x04510000 */
73 subu $a0,$t0,$a0 /* a0 = X * 0x0450fbaf */
74
75 /* now lookup in table indexed on top 6 bits of a0 */
76 srl $a0,25
77 addu $t9,$a0
78 j $ra
79 lbu $v0,FFSSIZE($t9)
80.L_ffs_table:
81/* 0 1 2 3 4 5 6 7 */
82 .byte 0, 1, 2, 13, 3, 7, 0, 14 /* 0- 7 */
83 .byte 4, 0, 8, 0, 0, 0, 0, 15 /* 8-15 */
84 .byte 11, 5, 0, 0, 9, 0, 0, 26 /* 16-23 */
85 .byte 0, 0, 0, 0, 0, 22, 28, 16 /* 24-31 */
86 .byte 32, 12, 6, 0, 0, 0, 0, 0 /* 32-39 */
87 .byte 10, 0, 0, 25, 0, 0, 21, 27 /* 40-47 */
88 .byte 31, 0, 0, 0, 0, 24, 0, 20 /* 48-55 */
89 .byte 30, 0, 23, 19, 29, 18, 17, 0 /* 56-63 */
90
91#endif
92 .end ffs