Blame src/pcm/interval.c

Packit 4a16fb
/*
Packit 4a16fb
 *  Interval functions
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
#define SND_INTERVAL_C
Packit 4a16fb
#define SND_INTERVAL_INLINE
Packit 4a16fb
Packit 4a16fb
#include <sys/types.h>
Packit 4a16fb
#include <limits.h>
Packit 4a16fb
#include "pcm_local.h"
Packit 4a16fb
Packit 4a16fb
static inline void div64_32(uint64_t *n, uint32_t d, uint32_t *rem)
Packit 4a16fb
{
Packit 4a16fb
	*rem = *n % d;
Packit 4a16fb
	*n /= d;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static inline unsigned int div32(unsigned int a, unsigned int b, 
Packit 4a16fb
				 unsigned int *r)
Packit 4a16fb
{
Packit 4a16fb
	if (b == 0) {
Packit 4a16fb
		*r = 0;
Packit 4a16fb
		return UINT_MAX;
Packit 4a16fb
	}
Packit 4a16fb
	*r = a % b;
Packit 4a16fb
	return a / b;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static inline unsigned int div_down(unsigned int a, unsigned int b)
Packit 4a16fb
{
Packit 4a16fb
	if (b == 0)
Packit 4a16fb
		return UINT_MAX;
Packit 4a16fb
	return a / b;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static inline unsigned int div_up(unsigned int a, unsigned int b)
Packit 4a16fb
{
Packit 4a16fb
	unsigned int r;
Packit 4a16fb
	unsigned int q;
Packit 4a16fb
	if (b == 0)
Packit 4a16fb
		return UINT_MAX;
Packit 4a16fb
	q = div32(a, b, &r);
Packit 4a16fb
	if (r)
Packit 4a16fb
		++q;
Packit 4a16fb
	return q;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static inline unsigned int mul(unsigned int a, unsigned int b)
Packit 4a16fb
{
Packit 4a16fb
	if (a == 0)
Packit 4a16fb
		return 0;
Packit 4a16fb
	if (div_down(UINT_MAX, a) < b)
Packit 4a16fb
		return UINT_MAX;
Packit 4a16fb
	return a * b;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static inline unsigned int add(unsigned int a, unsigned int b)
Packit 4a16fb
{
Packit 4a16fb
	if (a >= UINT_MAX - b)
Packit 4a16fb
		return UINT_MAX;
Packit 4a16fb
	return a + b;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static inline unsigned int sub(unsigned int a, unsigned int b)
Packit 4a16fb
{
Packit 4a16fb
	if (a > b)
Packit 4a16fb
		return a - b;
Packit 4a16fb
	return 0;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static inline unsigned int muldiv32(unsigned int a, unsigned int b,
Packit 4a16fb
				    unsigned int c, unsigned int *r)
Packit 4a16fb
{
Packit 4a16fb
	uint64_t n = (uint64_t) a * b;
Packit 4a16fb
	if (c == 0) {
Packit 4a16fb
		assert(n > 0);
Packit 4a16fb
		*r = 0;
Packit 4a16fb
		return UINT_MAX;
Packit 4a16fb
	}
Packit 4a16fb
	div64_32(&n, c, r);
Packit 4a16fb
	if (n >= UINT_MAX) {
Packit 4a16fb
		*r = 0;
Packit 4a16fb
		return UINT_MAX;
Packit 4a16fb
	}
Packit 4a16fb
	return n;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
int snd_interval_refine_min(snd_interval_t *i, unsigned int min, int openmin)
Packit 4a16fb
{
Packit 4a16fb
	int changed = 0;
Packit 4a16fb
	if (snd_interval_empty(i))
Packit 4a16fb
		return -ENOENT;
Packit 4a16fb
	if (i->min < min) {
Packit 4a16fb
		i->min = min;
Packit 4a16fb
		i->openmin = openmin;
Packit 4a16fb
		changed = 1;
Packit 4a16fb
	} else if (i->min == min && !i->openmin && openmin) {
Packit 4a16fb
		i->openmin = 1;
Packit 4a16fb
		changed = 1;
Packit 4a16fb
	}
Packit 4a16fb
	if (i->integer) {
Packit 4a16fb
		if (i->openmin) {
Packit 4a16fb
			i->min++;
Packit 4a16fb
			i->openmin = 0;
Packit 4a16fb
		}
Packit 4a16fb
	}
Packit 4a16fb
	if (snd_interval_checkempty(i)) {
Packit 4a16fb
		snd_interval_none(i);
Packit 4a16fb
		return -EINVAL;
Packit 4a16fb
	}
Packit 4a16fb
	return changed;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
int snd_interval_refine_max(snd_interval_t *i, unsigned int max, int openmax)
Packit 4a16fb
{
Packit 4a16fb
	int changed = 0;
Packit 4a16fb
	if (snd_interval_empty(i))
Packit 4a16fb
		return -ENOENT;
Packit 4a16fb
	if (i->max > max) {
Packit 4a16fb
		i->max = max;
Packit 4a16fb
		i->openmax = openmax;
Packit 4a16fb
		changed = 1;
Packit 4a16fb
	} else if (i->max == max && !i->openmax && openmax) {
Packit 4a16fb
		i->openmax = 1;
Packit 4a16fb
		changed = 1;
Packit 4a16fb
	}
Packit 4a16fb
	if (i->integer) {
Packit 4a16fb
		if (i->openmax) {
Packit 4a16fb
			i->max--;
Packit 4a16fb
			i->openmax = 0;
Packit 4a16fb
		}
Packit 4a16fb
	}
Packit 4a16fb
	if (snd_interval_checkempty(i)) {
Packit 4a16fb
		snd_interval_none(i);
Packit 4a16fb
		return -EINVAL;
Packit 4a16fb
	}
Packit 4a16fb
	return changed;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/* r <- v */
Packit 4a16fb
int snd_interval_refine(snd_interval_t *i, const snd_interval_t *v)
Packit 4a16fb
{
Packit 4a16fb
	int changed = 0;
Packit 4a16fb
	if (snd_interval_empty(i))
Packit 4a16fb
		return -ENOENT;
Packit 4a16fb
	if (i->min < v->min) {
Packit 4a16fb
		i->min = v->min;
Packit 4a16fb
		i->openmin = v->openmin;
Packit 4a16fb
		changed = 1;
Packit 4a16fb
	} else if (i->min == v->min && !i->openmin && v->openmin) {
Packit 4a16fb
		i->openmin = 1;
Packit 4a16fb
		changed = 1;
Packit 4a16fb
	}
Packit 4a16fb
	if (i->max > v->max) {
Packit 4a16fb
		i->max = v->max;
Packit 4a16fb
		i->openmax = v->openmax;
Packit 4a16fb
		changed = 1;
Packit 4a16fb
	} else if (i->max == v->max && !i->openmax && v->openmax) {
Packit 4a16fb
		i->openmax = 1;
Packit 4a16fb
		changed = 1;
Packit 4a16fb
	}
Packit 4a16fb
	if (!i->integer && v->integer) {
Packit 4a16fb
		i->integer = 1;
Packit 4a16fb
		changed = 1;
Packit 4a16fb
	}
Packit 4a16fb
	if (i->integer) {
Packit 4a16fb
		if (i->openmin) {
Packit 4a16fb
			i->min++;
Packit 4a16fb
			i->openmin = 0;
Packit 4a16fb
		}
Packit 4a16fb
		if (i->openmax) {
Packit 4a16fb
			i->max--;
Packit 4a16fb
			i->openmax = 0;
Packit 4a16fb
		}
Packit 4a16fb
	} else if (!i->openmin && !i->openmax && i->min == i->max)
Packit 4a16fb
		i->integer = 1;
Packit 4a16fb
	if (snd_interval_checkempty(i)) {
Packit 4a16fb
		snd_interval_none(i);
Packit 4a16fb
		return -EINVAL;
Packit 4a16fb
	}
Packit 4a16fb
	return changed;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
int snd_interval_refine_first(snd_interval_t *i)
Packit 4a16fb
{
Packit 4a16fb
	const unsigned int last_max = i->max;
Packit 4a16fb
Packit 4a16fb
	if (snd_interval_empty(i))
Packit 4a16fb
		return -ENOENT;
Packit 4a16fb
	if (snd_interval_single(i))
Packit 4a16fb
		return 0;
Packit 4a16fb
	i->max = i->min;
Packit 4a16fb
	if (i->openmin)
Packit 4a16fb
		i->max++;
Packit 4a16fb
	/* only exclude max value if also excluded before refine */
Packit 4a16fb
	i->openmax = (i->openmax && i->max >= last_max);
Packit 4a16fb
	return 1;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
int snd_interval_refine_last(snd_interval_t *i)
Packit 4a16fb
{
Packit 4a16fb
	const unsigned int last_min = i->min;
Packit 4a16fb
Packit 4a16fb
	if (snd_interval_empty(i))
Packit 4a16fb
		return -ENOENT;
Packit 4a16fb
	if (snd_interval_single(i))
Packit 4a16fb
		return 0;
Packit 4a16fb
	i->min = i->max;
Packit 4a16fb
	if (i->openmax)
Packit 4a16fb
		i->min--;
Packit 4a16fb
	/* only exclude min value if also excluded before refine */
Packit 4a16fb
	i->openmin = (i->openmin && i->min <= last_min);
Packit 4a16fb
	return 1;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
int snd_interval_refine_set(snd_interval_t *i, unsigned int val)
Packit 4a16fb
{
Packit 4a16fb
	snd_interval_t t;
Packit 4a16fb
	t.empty = 0;
Packit 4a16fb
	t.min = t.max = val;
Packit 4a16fb
	t.openmin = t.openmax = 0;
Packit 4a16fb
	t.integer = 1;
Packit 4a16fb
	return snd_interval_refine(i, &t);
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
void snd_interval_add(const snd_interval_t *a, const snd_interval_t *b, snd_interval_t *c)
Packit 4a16fb
{
Packit 4a16fb
	if (a->empty || b->empty) {
Packit 4a16fb
		snd_interval_none(c);
Packit 4a16fb
		return;
Packit 4a16fb
	}
Packit 4a16fb
	c->empty = 0;
Packit 4a16fb
	c->min = add(a->min, b->min);
Packit 4a16fb
	c->openmin = (a->openmin || b->openmin);
Packit 4a16fb
	c->max = add(a->max,  b->max);
Packit 4a16fb
	c->openmax = (a->openmax || b->openmax);
Packit 4a16fb
	c->integer = (a->integer && b->integer);
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
void snd_interval_sub(const snd_interval_t *a, const snd_interval_t *b, snd_interval_t *c)
Packit 4a16fb
{
Packit 4a16fb
	if (a->empty || b->empty) {
Packit 4a16fb
		snd_interval_none(c);
Packit 4a16fb
		return;
Packit 4a16fb
	}
Packit 4a16fb
	c->empty = 0;
Packit 4a16fb
	c->min = sub(a->min, b->max);
Packit 4a16fb
	c->openmin = (a->openmin || b->openmax);
Packit 4a16fb
	c->max = add(a->max,  b->min);
Packit 4a16fb
	c->openmax = (a->openmax || b->openmin);
Packit 4a16fb
	c->integer = (a->integer && b->integer);
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
void snd_interval_mul(const snd_interval_t *a, const snd_interval_t *b, snd_interval_t *c)
Packit 4a16fb
{
Packit 4a16fb
	if (a->empty || b->empty) {
Packit 4a16fb
		snd_interval_none(c);
Packit 4a16fb
		return;
Packit 4a16fb
	}
Packit 4a16fb
	c->empty = 0;
Packit 4a16fb
	c->min = mul(a->min, b->min);
Packit 4a16fb
	c->openmin = (a->openmin || b->openmin);
Packit 4a16fb
	c->max = mul(a->max,  b->max);
Packit 4a16fb
	c->openmax = (a->openmax || b->openmax);
Packit 4a16fb
	c->integer = (a->integer && b->integer);
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
void snd_interval_div(const snd_interval_t *a, const snd_interval_t *b, snd_interval_t *c)
Packit 4a16fb
{
Packit 4a16fb
	unsigned int r;
Packit 4a16fb
	if (a->empty || b->empty) {
Packit 4a16fb
		snd_interval_none(c);
Packit 4a16fb
		return;
Packit 4a16fb
	}
Packit 4a16fb
	c->empty = 0;
Packit 4a16fb
	c->min = div32(a->min, b->max, &r);
Packit 4a16fb
	c->openmin = (r || a->openmin || b->openmax);
Packit 4a16fb
	if (b->min > 0) {
Packit 4a16fb
		c->max = div32(a->max, b->min, &r);
Packit 4a16fb
		if (r) {
Packit 4a16fb
			c->max++;
Packit 4a16fb
			c->openmax = 1;
Packit 4a16fb
		} else
Packit 4a16fb
			c->openmax = (a->openmax || b->openmin);
Packit 4a16fb
	} else {
Packit 4a16fb
		c->max = UINT_MAX;
Packit 4a16fb
		c->openmax = 0;
Packit 4a16fb
	}
Packit 4a16fb
	c->integer = 0;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/* a * b / c */
Packit 4a16fb
void snd_interval_muldiv(const snd_interval_t *a, const snd_interval_t *b,
Packit 4a16fb
		     const snd_interval_t *c, snd_interval_t *d)
Packit 4a16fb
{
Packit 4a16fb
	unsigned int r;
Packit 4a16fb
	if (a->empty || b->empty || c->empty) {
Packit 4a16fb
		snd_interval_none(d);
Packit 4a16fb
		return;
Packit 4a16fb
	}
Packit 4a16fb
	d->empty = 0;
Packit 4a16fb
	d->min = muldiv32(a->min, b->min, c->max, &r);
Packit 4a16fb
	d->openmin = (r || a->openmin || b->openmin || c->openmax);
Packit 4a16fb
	d->max = muldiv32(a->max, b->max, c->min, &r);
Packit 4a16fb
	if (r) {
Packit 4a16fb
		d->max++;
Packit 4a16fb
		d->openmax = 1;
Packit 4a16fb
	} else
Packit 4a16fb
		d->openmax = (a->openmax || b->openmax || c->openmin);
Packit 4a16fb
	d->integer = 0;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/* a * b / k */
Packit 4a16fb
void snd_interval_muldivk(const snd_interval_t *a, const snd_interval_t *b,
Packit 4a16fb
		      unsigned int k, snd_interval_t *c)
Packit 4a16fb
{
Packit 4a16fb
	unsigned int r;
Packit 4a16fb
	if (a->empty || b->empty) {
Packit 4a16fb
		snd_interval_none(c);
Packit 4a16fb
		return;
Packit 4a16fb
	}
Packit 4a16fb
	c->empty = 0;
Packit 4a16fb
	c->min = muldiv32(a->min, b->min, k, &r);
Packit 4a16fb
	c->openmin = (r || a->openmin || b->openmin);
Packit 4a16fb
	c->max = muldiv32(a->max, b->max, k, &r);
Packit 4a16fb
	if (r) {
Packit 4a16fb
		c->max++;
Packit 4a16fb
		c->openmax = 1;
Packit 4a16fb
	} else
Packit 4a16fb
		c->openmax = (a->openmax || b->openmax);
Packit 4a16fb
	c->integer = 0;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/* a * k / b */
Packit 4a16fb
void snd_interval_mulkdiv(const snd_interval_t *a, unsigned int k,
Packit 4a16fb
		      const snd_interval_t *b, snd_interval_t *c)
Packit 4a16fb
{
Packit 4a16fb
	unsigned int r;
Packit 4a16fb
	if (a->empty || b->empty) {
Packit 4a16fb
		snd_interval_none(c);
Packit 4a16fb
		return;
Packit 4a16fb
	}
Packit 4a16fb
	c->empty = 0;
Packit 4a16fb
	c->min = muldiv32(a->min, k, b->max, &r);
Packit 4a16fb
	c->openmin = (r || a->openmin || b->openmax);
Packit 4a16fb
	if (b->min > 0) {
Packit 4a16fb
		c->max = muldiv32(a->max, k, b->min, &r);
Packit 4a16fb
		if (r) {
Packit 4a16fb
			c->max++;
Packit 4a16fb
			c->openmax = 1;
Packit 4a16fb
		} else
Packit 4a16fb
			c->openmax = (a->openmax || b->openmin);
Packit 4a16fb
	} else {
Packit 4a16fb
		c->max = UINT_MAX;
Packit 4a16fb
		c->openmax = 0;
Packit 4a16fb
	}
Packit 4a16fb
	c->integer = 0;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
void snd_interval_print(const snd_interval_t *i, snd_output_t *out)
Packit 4a16fb
{
Packit 4a16fb
	if (snd_interval_empty(i))
Packit 4a16fb
		snd_output_printf(out, "NONE");
Packit 4a16fb
	else if (i->min == 0 && i->openmin == 0 && 
Packit 4a16fb
		 i->max == UINT_MAX && i->openmax == 0)
Packit 4a16fb
		snd_output_printf(out, "ALL");
Packit 4a16fb
	else if (snd_interval_single(i) && i->integer)
Packit 4a16fb
		snd_output_printf(out, "%u", snd_interval_value(i));
Packit 4a16fb
	else
Packit 4a16fb
		snd_output_printf(out, "%c%u %u%c",
Packit 4a16fb
				i->openmin ? '(' : '[',
Packit 4a16fb
				i->min, i->max,
Packit 4a16fb
				i->openmax ? ')' : ']');
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
#if 0
Packit 4a16fb
static void boundary_abs(int a, int adir, int *b, int *bdir)
Packit 4a16fb
{
Packit 4a16fb
	if (a < 0 || (a == 0 && adir < 0)) {
Packit 4a16fb
		*b = -a;
Packit 4a16fb
		*bdir = -adir;
Packit 4a16fb
	} else {
Packit 4a16fb
		*b = a;
Packit 4a16fb
		*bdir = adir;
Packit 4a16fb
	}
Packit 4a16fb
}
Packit 4a16fb
#endif
Packit 4a16fb
Packit 4a16fb
void boundary_sub(int a, int adir, int b, int bdir, int *c, int *cdir)
Packit 4a16fb
{
Packit 4a16fb
	adir = adir < 0 ? -1 : (adir > 0 ? 1 : 0);
Packit 4a16fb
	bdir = bdir < 0 ? -1 : (bdir > 0 ? 1 : 0);
Packit 4a16fb
	*c = a - b;
Packit 4a16fb
	*cdir = adir - bdir;
Packit 4a16fb
	if (*cdir == -2) {
Packit 4a16fb
		assert(*c > INT_MIN);
Packit 4a16fb
		(*c)--;
Packit 4a16fb
	} else if (*cdir == 2) {
Packit 4a16fb
		assert(*c < INT_MAX);
Packit 4a16fb
		(*c)++;
Packit 4a16fb
	}
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
int boundary_lt(unsigned int a, int adir, unsigned int b, int bdir)
Packit 4a16fb
{
Packit 4a16fb
	assert(a > 0 || adir >= 0);
Packit 4a16fb
	assert(b > 0 || bdir >= 0);
Packit 4a16fb
	if (adir < 0) {
Packit 4a16fb
		a--;
Packit 4a16fb
		adir = 1;
Packit 4a16fb
	} else if (adir > 0)
Packit 4a16fb
		adir = 1;
Packit 4a16fb
	if (bdir < 0) {
Packit 4a16fb
		b--;
Packit 4a16fb
		bdir = 1;
Packit 4a16fb
	} else if (bdir > 0)
Packit 4a16fb
		bdir = 1;
Packit 4a16fb
	return a < b || (a == b && adir < bdir);
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/* Return 1 if min is nearer to best than max */
Packit 4a16fb
int boundary_nearer(int min, int mindir, int best, int bestdir, int max, int maxdir)
Packit 4a16fb
{
Packit 4a16fb
	int dmin, dmindir;
Packit 4a16fb
	int dmax, dmaxdir;
Packit 4a16fb
	boundary_sub(best, bestdir, min, mindir, &dmin, &dmindir);
Packit 4a16fb
	boundary_sub(max, maxdir, best, bestdir, &dmax, &dmaxdir);
Packit 4a16fb
	return boundary_lt(dmin, dmindir, dmax, dmaxdir);
Packit 4a16fb
}
Packit 4a16fb