1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2006 by Michael Sevakis
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.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
24 int round_value_to_list32(unsigned long value
,
25 const unsigned long list
[],
29 unsigned long dmin
= ULONG_MAX
;
32 for (i
= 0; i
< count
; i
++)
42 if (signd
? ((long)list
[i
] < (long)value
) : (list
[i
] < value
))
43 diff
= value
- list
[i
];
45 diff
= list
[i
] - value
;
55 } /* round_value_to_list32 */
57 /* Number of bits set in src_mask should equal src_list length */
58 int make_list_from_caps32(unsigned long src_mask
,
59 const unsigned long *src_list
,
60 unsigned long caps_mask
,
61 unsigned long *caps_list
)
66 for (mask
= src_mask
, count
= 0, i
= 0;
70 unsigned long test_bit
;
71 mask
&= mask
- 1; /* Zero lowest bit set */
72 test_bit
= mask
^ src_mask
; /* Isolate the bit */
73 if (test_bit
& caps_mask
) /* Add item if caps has test bit set */
74 caps_list
[count
++] = src_list
? src_list
[i
] : (unsigned long)i
;
78 } /* make_list_from_caps32 */
80 /* Align a buffer and size to a size boundary while remaining within
81 * the original boundaries */
82 size_t align_buffer(void **start
, size_t size
, size_t align
)
84 void *newstart
= *start
;
85 void *newend
= newstart
+ size
;
87 /* Align the end down and the start up */
88 newend
= (void *)ALIGN_DOWN((intptr_t)newend
, align
);
89 newstart
= (void *)ALIGN_UP((intptr_t)newstart
, align
);
91 /* Hmmm - too small for this */
92 if (newend
<= newstart
)
95 /* Return adjusted pointer and size */
97 return newend
- newstart
;