update from main archive 961105
[glibc.git] / sysdeps / alpha / ffs.S
blob959d1046d402578620991176cfbe18e035360be6
1 /* Copyright (C) 1996 Free Software Foundation, Inc.
2    Contributed by David Mosberger (davidm@cs.arizona.edu).
4 This file is part of the GNU C Library.
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.
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.
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.  */
21 /* Finds the first bit set in an integer.  Optimized for the Alpha
22 architecture.  */
24 #include <sysdep.h>
26         .set noreorder
27         .set noat
29 ENTRY(ffs)
30 #ifdef PROF
31         ldgp    gp, 0(pv)
32         lda     AT, _mcount
33         jsr     AT, (AT), _mcount
34         .prologue 1
35 #else
36         .prologue 0
37 #endif
39         ldq_u   zero, 0(sp)     # on the 21064, this helps dual-issuing
40         addl    a0, zero, a0    # the last insn and reduces the stall
41         negq    a0, t0          # due to the srl instruction
42         and     a0, t0, t0
43         clr     v0
44         beq     a0, $done
46         # now do binary search for first non-zero bit
48         zapnot  t0, 0x03, t2
49         addq    v0, 16, t3
50         cmoveq  t2, t3, v0
52         zapnot  t0, 0x05, t2
53         addq    v0, 8, t3
54         cmoveq  t2, t3, v0
56         srl     t0, v0, t0
57         addq    v0, 1, v0
59         and     t0, 0x0f, t2
60         addq    v0, 4, t3
61         cmoveq  t2, t3, v0
63         and     t0, 0x33, t2
64         addq    v0, 2, t3
65         cmoveq  t2, t3, v0
67         and     t0, 0x55, t2
68         addq    v0, 1, t3
69         cmoveq  t2, t3, v0
71 $done:  ret
73         END(ffs)