ffs.S revision 1.3 1 1.3 matt /* $NetBSD: ffs.S,v 1.3 2011/01/23 06:47:14 matt Exp $ */
2 1.1 christos
3 1.1 christos /*-
4 1.3 matt * Copyright (c) 2010 The NetBSD Foundation, Inc.
5 1.3 matt * All rights reserved.
6 1.1 christos *
7 1.3 matt * This code is derived from software contributed to The NetBSD Foundation
8 1.3 matt * by Matt Thomas of 3am Software Foundry.
9 1.1 christos *
10 1.1 christos * Redistribution and use in source and binary forms, with or without
11 1.1 christos * modification, are permitted provided that the following conditions
12 1.1 christos * are met:
13 1.1 christos * 1. Redistributions of source code must retain the above copyright
14 1.1 christos * notice, this list of conditions and the following disclaimer.
15 1.1 christos * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 christos * notice, this list of conditions and the following disclaimer in the
17 1.1 christos * documentation and/or other materials provided with the distribution.
18 1.1 christos *
19 1.3 matt * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.3 matt * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.3 matt * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.3 matt * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.3 matt * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.3 matt * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.3 matt * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.3 matt * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.3 matt * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.3 matt * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.3 matt * POSSIBILITY OF SUCH DAMAGE.
30 1.1 christos */
31 1.1 christos
32 1.1 christos #include <mips/asm.h>
33 1.1 christos
34 1.3 matt RCSID("$NetBSD: ffs.S,v 1.3 2011/01/23 06:47:14 matt Exp $")
35 1.1 christos
36 1.1 christos /* bit = ffs(value) */
37 1.1 christos
38 1.3 matt .text
39 1.3 matt .set noreorder
40 1.3 matt
41 1.3 matt #if __mips == 64 || __mips == 32
42 1.3 matt LEAF(ffs)
43 1.3 matt #ifndef _LP64
44 1.3 matt XLEAF(ffsl)
45 1.3 matt #endif
46 1.3 matt .set push
47 1.3 matt .set mips32
48 1.3 matt li v1, 32
49 1.3 matt #if __mips == 64
50 1.3 matt sll a0, a0, 0
51 1.3 matt #endif
52 1.3 matt negu a1, a0
53 1.3 matt and a0, a1
54 1.3 matt clz v0, a0
55 1.3 matt j ra
56 1.3 matt subu v0, v1, v0
57 1.3 matt .set pop
58 1.3 matt END(ffs)
59 1.3 matt #if defined(_LP64) && __mips == 64
60 1.3 matt LEAF(ffsl)
61 1.3 matt li v1, 64
62 1.3 matt negu a1, a0
63 1.3 matt and a0, a1
64 1.3 matt dclz v0, a0
65 1.3 matt j ra
66 1.3 matt subu v0, v1, v0
67 1.3 matt END(ffsl)
68 1.3 matt #endif
69 1.3 matt #else /* __mips != 64 && __mips != 32 */
70 1.3 matt
71 1.3 matt #ifdef _LP64
72 1.3 matt XLEAF(ffsl)
73 1.3 matt beqz a0, 6f # fast escape if 0
74 1.3 matt li v0, 0
75 1.3 matt
76 1.3 matt li v0, 1
77 1.3 matt li a3, 0xffffffff # initial mask
78 1.3 matt b 1f
79 1.3 matt li a2, 32 # bit count of mask
80 1.3 matt #endif /* _LP64 */
81 1.1 christos LEAF(ffs)
82 1.3 matt #ifndef _LP64
83 1.3 matt XLEAF(ffsl)
84 1.3 matt #endif /* !_LP64 */
85 1.3 matt beqz a0, 6f
86 1.3 matt li v0, 0
87 1.3 matt
88 1.3 matt li v0, 1
89 1.3 matt li a3, 0xffff # initial mask
90 1.3 matt li a2, 16 # bit count of mask
91 1.1 christos 1:
92 1.3 matt and v1, a0, a3 # focus no lower half of bits left
93 1.3 matt bnez v1, 2f # any of the lower half set?
94 1.3 matt nop
95 1.3 matt addu v0, a2 # nope, then bit is in the upper half
96 1.3 matt #ifdef _LP64
97 1.3 matt dsrlv a0, a0, a2 # discard low bits
98 1.3 matt #else
99 1.3 matt srlv a0, a0, a2 # discard low bits
100 1.3 matt #endif
101 1.3 matt 2:
102 1.3 matt srl a2, 1 # divide bit count by 2
103 1.3 matt bnez a2, 1b # still bits left to text?
104 1.3 matt srlv a3, a3, a2 # shrink mask in half
105 1.3 matt 6:
106 1.1 christos j ra
107 1.3 matt nop
108 1.1 christos END(ffs)
109 1.3 matt #endif /* __mips == 64 || __mips == 32 */
110