Michael Sevakis | 27cf677 | 2008-03-25 02:34:12 +0000 | [diff] [blame] | 1 | /*************************************************************************** |
| 2 | * __________ __ ___. |
| 3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ |
| 4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / |
| 5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < |
| 6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ |
| 7 | * \/ \/ \/ \/ \/ |
| 8 | * $Id$ |
| 9 | * |
| 10 | * Copyright (C) 2008 by Michael Sevakis |
| 11 | * |
Daniel Stenberg | 2acc0ac | 2008-06-28 18:10:04 +0000 | [diff] [blame^] | 12 | * This program is free software; you can redistribute it and/or |
| 13 | * modify it under the terms of the GNU General Public License |
| 14 | * as published by the Free Software Foundation; either version 2 |
| 15 | * of the License, or (at your option) any later version. |
Michael Sevakis | 27cf677 | 2008-03-25 02:34:12 +0000 | [diff] [blame] | 16 | * |
| 17 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY |
| 18 | * KIND, either express or implied. |
| 19 | * |
| 20 | ****************************************************************************/ |
| 21 | #include "config.h" |
| 22 | |
| 23 | /**************************************************************************** |
| 24 | * int find_first_set_bit(uint32_t val); |
| 25 | * |
| 26 | * Find the index of the least significant set bit in the 32-bit word. |
| 27 | * |
| 28 | * return values: |
| 29 | * 0 - bit 0 is set |
| 30 | * 1 - bit 1 is set |
| 31 | * ... |
| 32 | * 31 - bit 31 is set |
| 33 | * 32 - no bits set |
| 34 | ****************************************************************************/ |
| 35 | .text |
| 36 | .align 2 |
| 37 | .global find_first_set_bit |
| 38 | .type find_first_set_bit,@function |
| 39 | find_first_set_bit: |
| 40 | | this is a coldfire version of the ffs algorithm devised by D.Seal |
| 41 | | and posted to comp.sys.arm on 16 Feb 1994. |
| 42 | | |
| 43 | | Output modified to suit rockbox purposes. |
| 44 | |
| 45 | | Standard trick to isolate bottom bit in r0 or 0 if r0 = 0 on entry |
| 46 | move.l 4(%sp), %d1 | %d1 = %d1 & -%d1 |
| 47 | lea.l L_ffs_table, %a0 | %a0 = table address |
| 48 | move.l %d1, %d0 | |
| 49 | neg.l %d1 | |
| 50 | and.l %d0, %d1 | |
| 51 | |
| 52 | | now %d1 has at most one set bit, call this X |
| 53 | |
| 54 | move.l #0x0450fbaf, %d0 | %d0 = multiplier |
| 55 | mulu.l %d0, %d1 | %d1 = X * 0x0450fbaf |
| 56 | |
| 57 | | now lookup in table indexed on top 6 bits of %d0 |
| 58 | moveq.l #26, %d0 | %d0 = final shift count |
| 59 | lsr.l %d0, %d1 | |
| 60 | clr.l %d0 | |
| 61 | move.b (%a0, %d1.l), %d0 | |
| 62 | rts | |
| 63 | |
| 64 | .size find_first_set_bit, .-find_first_set_bit |