7cf6281682cf0869297669c5a5faf8e43f08ee6c
[platform/upstream/glibc.git] / sysdeps / alpha / ffs.S
1 /* Copyright (C) 1996 Free Software Foundation, Inc.
2    Contributed by David Mosberger (davidm@cs.arizona.edu).
3
4 This file is part of the GNU C Library.
5
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public License as
8 published by the Free Software Foundation; either version 2 of the
9 License, or (at your option) any later version.
10
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 Library General Public License for more details.
15
16 You should have received a copy of the GNU Library General Public
17 License along with the GNU C Library; see the file COPYING.LIB.  If
18 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
19 Cambridge, MA 02139, USA.  */
20
21 /* Finds the first bit set in an integer.  Optimized for the Alpha
22 architecture.  */
23
24 #include <sysdep.h>
25
26         .set noreorder
27         .set noat
28
29 ENTRY(ffs)
30         .prologue 0
31
32         ldq_u   zero, 0(sp)     # on the 21064, this helps dual-issuing
33         addl    a0, zero, a0    # the last insn and reduces the stall
34         negq    a0, t0          # due to the srl instruction
35         and     a0, t0, t0
36         clr     v0
37         beq     a0, done
38
39         # now do binary search for first non-zero bit
40
41         zapnot  t0, 0x03, t2
42         addq    v0, 16, t3
43         cmoveq  t2, t3, v0
44
45         zapnot  t0, 0x05, t2
46         addq    v0, 8, t3
47         cmoveq  t2, t3, v0
48
49         srl     t0, v0, t0
50         addq    v0, 1, v0
51
52         and     t0, 0x0f, t2
53         addq    v0, 4, t3
54         cmoveq  t2, t3, v0
55
56         and     t0, 0x33, t2
57         addq    v0, 2, t3
58         cmoveq  t2, t3, v0
59
60         and     t0, 0x55, t2
61         addq    v0, 1, t3
62         cmoveq  t2, t3, v0
63
64 done:   ret
65
66         .end    ffs