Commit FS#9607 by Adam Hogan: fix plugin keymaps for Creative Zen Vision:M
[kugel-rb.git] / firmware / general.c
blob1c2abe1256298a1601dcf88c3815406bc3377c25
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2006 by Michael Sevakis
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.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
21 #include <limits.h>
22 #include "system.h"
23 #include "config.h"
24 #include "general.h"
26 int round_value_to_list32(unsigned long value,
27 const unsigned long list[],
28 int count,
29 bool signd)
31 unsigned long dmin = ULONG_MAX;
32 int idmin = -1, i;
34 for (i = 0; i < count; i++)
36 unsigned long diff;
38 if (list[i] == value)
40 idmin = i;
41 break;
44 if (signd ? ((long)list[i] < (long)value) : (list[i] < value))
45 diff = value - list[i];
46 else
47 diff = list[i] - value;
49 if (diff < dmin)
51 dmin = diff;
52 idmin = i;
56 return idmin;
57 } /* round_value_to_list32 */
59 /* Number of bits set in src_mask should equal src_list length */
60 int make_list_from_caps32(unsigned long src_mask,
61 const unsigned long *src_list,
62 unsigned long caps_mask,
63 unsigned long *caps_list)
65 int i, count;
66 unsigned long mask;
68 for (mask = src_mask, count = 0, i = 0;
69 mask != 0;
70 src_mask = mask, i++)
72 unsigned long test_bit;
73 mask &= mask - 1; /* Zero lowest bit set */
74 test_bit = mask ^ src_mask; /* Isolate the bit */
75 if (test_bit & caps_mask) /* Add item if caps has test bit set */
76 caps_list[count++] = src_list ? src_list[i] : (unsigned long)i;
79 return count;
80 } /* make_list_from_caps32 */
82 /* Align a buffer and size to a size boundary while remaining within
83 * the original boundaries */
84 size_t align_buffer(void **start, size_t size, size_t align)
86 void *newstart = *start;
87 void *newend = newstart + size;
89 /* Align the end down and the start up */
90 newend = (void *)ALIGN_DOWN((intptr_t)newend, align);
91 newstart = (void *)ALIGN_UP((intptr_t)newstart, align);
93 /* Hmmm - too small for this */
94 if (newend <= newstart)
95 return 0;
97 /* Return adjusted pointer and size */
98 *start = newstart;
99 return newend - newstart;