diff options
Diffstat (limited to 'firmware/common')
-rw-r--r-- | firmware/common/ffs.c | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/firmware/common/ffs.c b/firmware/common/ffs.c new file mode 100644 index 0000000000..e3dc9b0dc5 --- /dev/null +++ b/firmware/common/ffs.c | |||
@@ -0,0 +1,54 @@ | |||
1 | /*************************************************************************** | ||
2 | * __________ __ ___. | ||
3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ | ||
4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / | ||
5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < | ||
6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ | ||
7 | * \/ \/ \/ \/ \/ | ||
8 | * $Id$ | ||
9 | * | ||
10 | * Copyright (C) 2008 by Michael Sevakis | ||
11 | * | ||
12 | * All files in this archive are subject to the GNU General Public License. | ||
13 | * See the file COPYING in the source tree root for full license agreement. | ||
14 | * | ||
15 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY | ||
16 | * KIND, either express or implied. | ||
17 | * | ||
18 | ****************************************************************************/ | ||
19 | #include "config.h" | ||
20 | #include <inttypes.h> | ||
21 | |||
22 | /* find_first_set_bit() - this is a C version of the ffs algorithm devised | ||
23 | * by D.Seal and posted to comp.sys.arm on 16 Feb 1994. | ||
24 | * | ||
25 | * Find the index of the least significant set bit in the word. | ||
26 | * return values: | ||
27 | * 0 - bit 0 is set | ||
28 | * 1 - bit 1 is set | ||
29 | * ... | ||
30 | * 31 - bit 31 is set | ||
31 | * 32 - no bits set | ||
32 | */ | ||
33 | |||
34 | /* Table shared with assembly code */ | ||
35 | const uint8_t L_ffs_table[64] ICONST_ATTR = | ||
36 | { | ||
37 | /* 0 1 2 3 4 5 6 7 */ | ||
38 | /* ----------------------------------------- */ | ||
39 | 32, 0, 1, 12, 2, 6, 0, 13, /* 0- 7 */ | ||
40 | 3, 0, 7, 0, 0, 0, 0, 14, /* 8-15 */ | ||
41 | 10, 4, 0, 0, 8, 0, 0, 25, /* 16-23 */ | ||
42 | 0, 0, 0, 0, 0, 21, 27, 15, /* 24-31 */ | ||
43 | 31, 11, 5, 0, 0, 0, 0, 0, /* 32-39 */ | ||
44 | 9, 0, 0, 24, 0, 0, 20, 26, /* 40-47 */ | ||
45 | 30, 0, 0, 0, 0, 23, 0, 19, /* 48-55 */ | ||
46 | 29, 0, 22, 18, 28, 17, 16, 0, /* 56-63 */ | ||
47 | }; | ||
48 | |||
49 | #if !defined(CPU_COLDFIRE) | ||
50 | int find_first_set_bit(uint32_t val) | ||
51 | { | ||
52 | return L_ffs_table[((val & -val)*0x0450fbaf) >> 26]; | ||
53 | } | ||
54 | #endif | ||