summaryrefslogtreecommitdiff
path: root/firmware/general.c
diff options
context:
space:
mode:
Diffstat (limited to 'firmware/general.c')
-rw-r--r--firmware/general.c77
1 files changed, 77 insertions, 0 deletions
diff --git a/firmware/general.c b/firmware/general.c
new file mode 100644
index 0000000000..7f4348046c
--- /dev/null
+++ b/firmware/general.c
@@ -0,0 +1,77 @@
1/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2006 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 <limits.h>
20#include "config.h"
21#include "general.h"
22
23int round_value_to_list32(unsigned long value,
24 const unsigned long list[],
25 int count,
26 bool signd)
27{
28 unsigned long dmin = ULONG_MAX;
29 int idmin = -1, i;
30
31 for (i = 0; i < count; i++)
32 {
33 unsigned long diff;
34
35 if (list[i] == value)
36 {
37 idmin = i;
38 break;
39 }
40
41 if (signd ? ((long)list[i] < (long)value) : (list[i] < value))
42 diff = value - list[i];
43 else
44 diff = list[i] - value;
45
46 if (diff < dmin)
47 {
48 dmin = diff;
49 idmin = i;
50 }
51 }
52
53 return idmin;
54} /* round_value_to_list32 */
55
56/* Number of bits set in src_mask should equal src_list length */
57int make_list_from_caps32(unsigned long src_mask,
58 const unsigned long *src_list,
59 unsigned long caps_mask,
60 unsigned long *caps_list)
61{
62 int i, count;
63 unsigned long mask;
64
65 for (mask = src_mask, count = 0, i = 0;
66 mask != 0;
67 src_mask = mask, i++)
68 {
69 unsigned long test_bit;
70 mask &= mask - 1; /* Zero lowest bit set */
71 test_bit = mask ^ src_mask; /* Isolate the bit */
72 if (test_bit & caps_mask) /* Add item if caps has test bit set */
73 caps_list[count++] = src_list ? src_list[i] : (unsigned long)i;
74 }
75
76 return count;
77} /* make_list_from_caps32 */