Home | History | Annotate | Line # | Download | only in string
ffs.S revision 1.2.8.1
      1  1.2.8.1       tls /*	$NetBSD: ffs.S,v 1.2.8.1 2014/08/19 23:45:12 tls Exp $	*/
      2      1.1  christos /*
      3      1.1  christos  * Copyright (c) 2001 Christopher Gilbert
      4      1.1  christos  * All rights reserved.
      5      1.1  christos  *
      6      1.1  christos  * Redistribution and use in source and binary forms, with or without
      7      1.1  christos  * modification, are permitted provided that the following conditions
      8      1.1  christos  * are met:
      9      1.1  christos  * 1. Redistributions of source code must retain the above copyright
     10      1.1  christos  *    notice, this list of conditions and the following disclaimer.
     11      1.1  christos  * 2. Redistributions in binary form must reproduce the above copyright
     12      1.1  christos  *    notice, this list of conditions and the following disclaimer in the
     13      1.1  christos  *    documentation and/or other materials provided with the distribution.
     14      1.1  christos  * 3. The name of the company nor the name of the author may be used to
     15      1.1  christos  *    endorse or promote products derived from this software without specific
     16      1.1  christos  *    prior written permission.
     17      1.1  christos  *
     18      1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     19      1.1  christos  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     20      1.1  christos  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     21      1.1  christos  * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
     22      1.1  christos  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     23      1.1  christos  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     24      1.1  christos  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     25      1.1  christos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     26      1.1  christos  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     27      1.1  christos  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     28      1.1  christos  * SUCH DAMAGE.
     29      1.1  christos  */
     30      1.1  christos 
     31      1.1  christos #include <machine/asm.h>
     32      1.1  christos 
     33  1.2.8.1       tls RCSID("$NetBSD: ffs.S,v 1.2.8.1 2014/08/19 23:45:12 tls Exp $")
     34      1.1  christos 
     35      1.1  christos /*
     36      1.1  christos  * ffs - find first set bit, this algorithm isolates the first set
     37      1.1  christos  * bit, then multiplies the number by 0x0450fbaf which leaves the top
     38      1.1  christos  * 6 bits as an index into the table.  This algorithm should be a win
     39      1.1  christos  * over the checking each bit in turn as per the C compiled version.
     40      1.1  christos  *
     41      1.1  christos  * On ARMv5 we use CLZ (count leading Zero's) and then subtract the result
     42      1.1  christos  * from 32.
     43      1.1  christos  *
     44      1.1  christos  * This is the ffs algorithm devised by d.seal and posted to comp.sys.arm on
     45      1.1  christos  * 16 Feb 1994.
     46      1.1  christos  */
     47  1.2.8.1       tls STRONG_ALIAS(__ffssi2,ffs)
     48  1.2.8.1       tls #if (defined(_ARM_ARCH_5) && !defined(__thumb__)) || defined(_ARM_ARCH_T2)
     49  1.2.8.1       tls #if defined(_ARM_ARCH_T2)
     50      1.1  christos ENTRY(ffs)
     51  1.2.8.1       tls #else
     52  1.2.8.1       tls ARM_ENTRY(ffs)
     53  1.2.8.1       tls #endif
     54      1.1  christos 	/* (X & -X) gives LSB or zero.  */
     55  1.2.8.1       tls  	neg	r1, r0
     56      1.1  christos  	and	r0, r0, r1
     57      1.1  christos 	clz	r0, r0
     58      1.1  christos 	rsb	r0, r0, #32
     59      1.1  christos 	RET
     60  1.2.8.1       tls END(ffs)
     61      1.1  christos #else
     62  1.2.8.1       tls ARM_ENTRY(ffs)
     63      1.1  christos 	/* Standard trick to isolate bottom bit in r0 or 0 if r0 = 0 on entry */
     64  1.2.8.1       tls  	neg     r1, r0
     65      1.1  christos  	ands    r0, r0, r1
     66      1.1  christos 	/*
     67      1.1  christos 	 * now r0 has at most one set bit, call this X
     68      1.1  christos 	 * if X = 0, all further instructions are skipped
     69      1.1  christos 	 */
     70      1.1  christos 	adrne   r2, .L_ffs_table
     71      1.1  christos 	orrne   r0, r0, r0, lsl #4  /* r0 = X * 0x11 */
     72      1.1  christos 	orrne   r0, r0, r0, lsl #6  /* r0 = X * 0x451 */
     73      1.1  christos 	rsbne   r0, r0, r0, lsl #16 /* r0 = X * 0x0450fbaf */
     74      1.1  christos 
     75      1.1  christos 	/* now lookup in table indexed on top 6 bits of r0 */
     76  1.2.8.1       tls 	ldrbne  r0, [r2, r0, lsr #26]
     77      1.1  christos 
     78      1.1  christos         RET
     79      1.1  christos .text;
     80      1.1  christos .type .L_ffs_table, _ASM_TYPE_OBJECT;
     81      1.1  christos .L_ffs_table:
     82      1.1  christos /*               0   1   2   3   4   5   6   7           */
     83      1.1  christos 	.byte	 0,  1,  2, 13,  3,  7,  0, 14  /*  0- 7 */
     84      1.1  christos 	.byte	 4,  0,  8,  0,  0,  0,  0, 15  /*  8-15 */
     85      1.1  christos 	.byte	11,  5,  0,  0,  9,  0,  0, 26  /* 16-23 */
     86      1.1  christos 	.byte	 0,  0,  0,  0,  0, 22, 28, 16  /* 24-31 */
     87      1.1  christos 	.byte	32, 12,  6,  0,  0,  0,  0,  0	/* 32-39 */
     88      1.1  christos 	.byte	10,  0,  0, 25,  0,  0, 21, 27  /* 40-47 */
     89      1.1  christos 	.byte	31,  0,  0,  0,  0, 24,  0, 20  /* 48-55 */
     90      1.1  christos 	.byte   30,  0, 23, 19, 29, 18, 17,  0  /* 56-63 */
     91  1.2.8.1       tls END(ffs)
     92      1.1  christos #endif
     93