Blame src/pcm/mask_inline.h

Packit 4a16fb
/*
Packit 4a16fb
 *  Mask inlines
Packit 4a16fb
 *  Copyright (c) 2000 by Abramo Bagnara <abramo@alsa-project.org>
Packit 4a16fb
 *
Packit 4a16fb
 *
Packit 4a16fb
 *   This library is free software; you can redistribute it and/or modify
Packit 4a16fb
 *   it under the terms of the GNU Lesser General Public License as
Packit 4a16fb
 *   published by the Free Software Foundation; either version 2.1 of
Packit 4a16fb
 *   the License, or (at your option) any later version.
Packit 4a16fb
 *
Packit 4a16fb
 *   This program is distributed in the hope that it will be useful,
Packit 4a16fb
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 4a16fb
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 4a16fb
 *   GNU Lesser General Public License for more details.
Packit 4a16fb
 *
Packit 4a16fb
 *   You should have received a copy of the GNU Lesser General Public
Packit 4a16fb
 *   License along with this library; if not, write to the Free Software
Packit 4a16fb
 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
Packit 4a16fb
 *
Packit 4a16fb
 */
Packit 4a16fb
  
Packit 4a16fb
#include <strings.h>
Packit 4a16fb
#include <sys/types.h>
Packit 4a16fb
Packit 4a16fb
#define MASK_INLINE static inline
Packit 4a16fb
Packit 4a16fb
#define MASK_MAX SND_MASK_MAX
Packit 4a16fb
#define MASK_SIZE (MASK_MAX / 32)
Packit 4a16fb
Packit 4a16fb
#define MASK_OFS(i)	((i) >> 5)
Packit 4a16fb
#define MASK_BIT(i)	(1U << ((i) & 31))
Packit 4a16fb
Packit 4a16fb
MASK_INLINE unsigned int ld2(uint32_t v)
Packit 4a16fb
{
Packit 4a16fb
        unsigned r = 0;
Packit 4a16fb
Packit 4a16fb
        if (v >= 0x10000) {
Packit 4a16fb
                v >>= 16;
Packit 4a16fb
                r += 16;
Packit 4a16fb
        }
Packit 4a16fb
        if (v >= 0x100) {
Packit 4a16fb
                v >>= 8;
Packit 4a16fb
                r += 8;
Packit 4a16fb
        }
Packit 4a16fb
        if (v >= 0x10) {
Packit 4a16fb
                v >>= 4;
Packit 4a16fb
                r += 4;
Packit 4a16fb
        }
Packit 4a16fb
        if (v >= 4) {
Packit 4a16fb
                v >>= 2;
Packit 4a16fb
                r += 2;
Packit 4a16fb
        }
Packit 4a16fb
        if (v >= 2)
Packit 4a16fb
                r++;
Packit 4a16fb
        return r;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
MASK_INLINE unsigned int hweight32(uint32_t v)
Packit 4a16fb
{
Packit 4a16fb
        v = (v & 0x55555555) + ((v >> 1) & 0x55555555);
Packit 4a16fb
        v = (v & 0x33333333) + ((v >> 2) & 0x33333333);
Packit 4a16fb
        v = (v & 0x0F0F0F0F) + ((v >> 4) & 0x0F0F0F0F);
Packit 4a16fb
        v = (v & 0x00FF00FF) + ((v >> 8) & 0x00FF00FF);
Packit 4a16fb
        return (v & 0x0000FFFF) + ((v >> 16) & 0x0000FFFF);
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
MASK_INLINE size_t snd_mask_sizeof(void)
Packit 4a16fb
{
Packit 4a16fb
	return sizeof(snd_mask_t);
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
MASK_INLINE void snd_mask_none(snd_mask_t *mask)
Packit 4a16fb
{
Packit 4a16fb
	memset(mask, 0, sizeof(*mask));
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
MASK_INLINE void snd_mask_any(snd_mask_t *mask)
Packit 4a16fb
{
Packit 4a16fb
	memset(mask, 0xff, MASK_SIZE * sizeof(uint32_t));
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
MASK_INLINE int snd_mask_empty(const snd_mask_t *mask)
Packit 4a16fb
{
Packit 4a16fb
	int i;
Packit 4a16fb
	for (i = 0; i < MASK_SIZE; i++)
Packit 4a16fb
		if (mask->bits[i])
Packit 4a16fb
			return 0;
Packit 4a16fb
	return 1;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
MASK_INLINE int snd_mask_full(const snd_mask_t *mask)
Packit 4a16fb
{
Packit 4a16fb
	int i;
Packit 4a16fb
	for (i = 0; i < MASK_SIZE; i++)
Packit 4a16fb
		if (mask->bits[i] != 0xffffffff)
Packit 4a16fb
			return 0;
Packit 4a16fb
	return 1;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
MASK_INLINE unsigned int snd_mask_count(const snd_mask_t *mask)
Packit 4a16fb
{
Packit 4a16fb
	int i, w = 0;
Packit 4a16fb
	for (i = 0; i < MASK_SIZE; i++)
Packit 4a16fb
		w += hweight32(mask->bits[i]);
Packit 4a16fb
	return w;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
MASK_INLINE unsigned int snd_mask_min(const snd_mask_t *mask)
Packit 4a16fb
{
Packit 4a16fb
	int i;
Packit 4a16fb
	assert(!snd_mask_empty(mask));
Packit 4a16fb
	for (i = 0; i < MASK_SIZE; i++) {
Packit 4a16fb
		if (mask->bits[i])
Packit 4a16fb
			return ffs(mask->bits[i]) - 1 + (i << 5);
Packit 4a16fb
	}
Packit 4a16fb
	return 0;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
MASK_INLINE unsigned int snd_mask_max(const snd_mask_t *mask)
Packit 4a16fb
{
Packit 4a16fb
	int i;
Packit 4a16fb
	assert(!snd_mask_empty(mask));
Packit 4a16fb
	for (i = MASK_SIZE - 1; i >= 0; i--) {
Packit 4a16fb
		if (mask->bits[i])
Packit 4a16fb
			return ld2(mask->bits[i]) + (i << 5);
Packit 4a16fb
	}
Packit 4a16fb
	return 0;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
MASK_INLINE void snd_mask_set(snd_mask_t *mask, unsigned int val)
Packit 4a16fb
{
Packit 4a16fb
	assert(val <= SND_MASK_MAX);
Packit 4a16fb
	mask->bits[MASK_OFS(val)] |= MASK_BIT(val);
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
MASK_INLINE void snd_mask_reset(snd_mask_t *mask, unsigned int val)
Packit 4a16fb
{
Packit 4a16fb
	assert(val <= SND_MASK_MAX);
Packit 4a16fb
	mask->bits[MASK_OFS(val)] &= ~MASK_BIT(val);
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
MASK_INLINE void snd_mask_set_range(snd_mask_t *mask, unsigned int from, unsigned int to)
Packit 4a16fb
{
Packit 4a16fb
	unsigned int i;
Packit 4a16fb
	assert(to <= SND_MASK_MAX && from <= to);
Packit 4a16fb
	for (i = from; i <= to; i++)
Packit 4a16fb
		mask->bits[MASK_OFS(i)] |= MASK_BIT(i);
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
MASK_INLINE void snd_mask_reset_range(snd_mask_t *mask, unsigned int from, unsigned int to)
Packit 4a16fb
{
Packit 4a16fb
	unsigned int i;
Packit 4a16fb
	assert(to <= SND_MASK_MAX && from <= to);
Packit 4a16fb
	for (i = from; i <= to; i++)
Packit 4a16fb
		mask->bits[MASK_OFS(i)] &= ~MASK_BIT(i);
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
MASK_INLINE void snd_mask_leave(snd_mask_t *mask, unsigned int val)
Packit 4a16fb
{
Packit 4a16fb
	unsigned int v;
Packit 4a16fb
	assert(val <= SND_MASK_MAX);
Packit 4a16fb
	v = mask->bits[MASK_OFS(val)] & MASK_BIT(val);
Packit 4a16fb
	snd_mask_none(mask);
Packit 4a16fb
	mask->bits[MASK_OFS(val)] = v;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
MASK_INLINE void snd_mask_intersect(snd_mask_t *mask, const snd_mask_t *v)
Packit 4a16fb
{
Packit 4a16fb
	int i;
Packit 4a16fb
	for (i = 0; i < MASK_SIZE; i++)
Packit 4a16fb
		mask->bits[i] &= v->bits[i];
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
MASK_INLINE void snd_mask_union(snd_mask_t *mask, const snd_mask_t *v)
Packit 4a16fb
{
Packit 4a16fb
	int i;
Packit 4a16fb
	for (i = 0; i < MASK_SIZE; i++)
Packit 4a16fb
		mask->bits[i] |= v->bits[i];
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
MASK_INLINE int snd_mask_eq(const snd_mask_t *mask, const snd_mask_t *v)
Packit 4a16fb
{
Packit 4a16fb
	return ! memcmp(mask, v, MASK_SIZE * 4);
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
MASK_INLINE void snd_mask_copy(snd_mask_t *mask, const snd_mask_t *v)
Packit 4a16fb
{
Packit 4a16fb
	*mask = *v;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
MASK_INLINE int snd_mask_test(const snd_mask_t *mask, unsigned int val)
Packit 4a16fb
{
Packit 4a16fb
	assert(val <= SND_MASK_MAX);
Packit 4a16fb
	return mask->bits[MASK_OFS(val)] & MASK_BIT(val);
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
MASK_INLINE int snd_mask_single(const snd_mask_t *mask)
Packit 4a16fb
{
Packit 4a16fb
	int i, c = 0;
Packit 4a16fb
	assert(!snd_mask_empty(mask));
Packit 4a16fb
	for (i = 0; i < MASK_SIZE; i++) {
Packit 4a16fb
		if (! mask->bits[i])
Packit 4a16fb
			continue;
Packit 4a16fb
		if (mask->bits[i] & (mask->bits[i] - 1))
Packit 4a16fb
			return 0;
Packit 4a16fb
		if (c)
Packit 4a16fb
			return 0;
Packit 4a16fb
		c++;
Packit 4a16fb
	}
Packit 4a16fb
	return 1;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
MASK_INLINE int snd_mask_refine(snd_mask_t *mask, const snd_mask_t *v)
Packit 4a16fb
{
Packit 4a16fb
	snd_mask_t old;
Packit 4a16fb
	if (snd_mask_empty(mask))
Packit 4a16fb
		return -ENOENT;
Packit 4a16fb
	snd_mask_copy(&old, mask);
Packit 4a16fb
	snd_mask_intersect(mask, v);
Packit 4a16fb
	if (snd_mask_empty(mask))
Packit 4a16fb
		return -EINVAL;
Packit 4a16fb
	return !snd_mask_eq(mask, &old;;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
MASK_INLINE int snd_mask_refine_first(snd_mask_t *mask)
Packit 4a16fb
{
Packit 4a16fb
	if (snd_mask_empty(mask))
Packit 4a16fb
		return -ENOENT;
Packit 4a16fb
	if (snd_mask_single(mask))
Packit 4a16fb
		return 0;
Packit 4a16fb
	snd_mask_leave(mask, snd_mask_min(mask));
Packit 4a16fb
	return 1;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
MASK_INLINE int snd_mask_refine_last(snd_mask_t *mask)
Packit 4a16fb
{
Packit 4a16fb
	if (snd_mask_empty(mask))
Packit 4a16fb
		return -ENOENT;
Packit 4a16fb
	if (snd_mask_single(mask))
Packit 4a16fb
		return 0;
Packit 4a16fb
	snd_mask_leave(mask, snd_mask_max(mask));
Packit 4a16fb
	return 1;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
MASK_INLINE int snd_mask_refine_min(snd_mask_t *mask, unsigned int val)
Packit 4a16fb
{
Packit 4a16fb
	if (snd_mask_empty(mask))
Packit 4a16fb
		return -ENOENT;
Packit 4a16fb
	if (snd_mask_min(mask) >= val)
Packit 4a16fb
		return 0;
Packit 4a16fb
	snd_mask_reset_range(mask, 0, val - 1);
Packit 4a16fb
	if (snd_mask_empty(mask))
Packit 4a16fb
		return -EINVAL;
Packit 4a16fb
	return 1;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
MASK_INLINE int snd_mask_refine_max(snd_mask_t *mask, unsigned int val)
Packit 4a16fb
{
Packit 4a16fb
	if (snd_mask_empty(mask))
Packit 4a16fb
		return -ENOENT;
Packit 4a16fb
	if (snd_mask_max(mask) <= val)
Packit 4a16fb
		return 0;
Packit 4a16fb
	snd_mask_reset_range(mask, val + 1, SND_MASK_MAX);
Packit 4a16fb
	if (snd_mask_empty(mask))
Packit 4a16fb
		return -EINVAL;
Packit 4a16fb
	return 1;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
MASK_INLINE int snd_mask_refine_set(snd_mask_t *mask, unsigned int val)
Packit 4a16fb
{
Packit 4a16fb
	int changed;
Packit 4a16fb
	if (snd_mask_empty(mask))
Packit 4a16fb
		return -ENOENT;
Packit 4a16fb
	changed = !snd_mask_single(mask);
Packit 4a16fb
	snd_mask_leave(mask, val);
Packit 4a16fb
	if (snd_mask_empty(mask))
Packit 4a16fb
		return -EINVAL;
Packit 4a16fb
	return changed;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
MASK_INLINE int snd_mask_value(const snd_mask_t *mask)
Packit 4a16fb
{
Packit 4a16fb
	assert(!snd_mask_empty(mask));
Packit 4a16fb
	return snd_mask_min(mask);
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
MASK_INLINE int snd_mask_always_eq(const snd_mask_t *m1, const snd_mask_t *m2)
Packit 4a16fb
{
Packit 4a16fb
	return snd_mask_single(m1) && snd_mask_single(m2) &&
Packit 4a16fb
		snd_mask_value(m1) == snd_mask_value(m2);
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
MASK_INLINE int snd_mask_never_eq(const snd_mask_t *m1, const snd_mask_t *m2)
Packit 4a16fb
{
Packit 4a16fb
	int i;
Packit 4a16fb
	for (i = 0; i < MASK_SIZE; i++)
Packit 4a16fb
		if (m1->bits[i] & m2->bits[i])
Packit 4a16fb
			return 0;
Packit 4a16fb
	return 1;
Packit 4a16fb
}