NetBSD/sys/dev/auvolconv.c
riastradh d1579b2d70 Rename min/max -> uimin/uimax for better honesty.
These functions are defined on unsigned int.  The generic name
min/max should not silently truncate to 32 bits on 64-bit systems.
This is purely a name change -- no functional change intended.

HOWEVER!  Some subsystems have

	#define min(a, b)	((a) < (b) ? (a) : (b))
	#define max(a, b)	((a) > (b) ? (a) : (b))

even though our standard name for that is MIN/MAX.  Although these
may invite multiple evaluation bugs, these do _not_ cause integer
truncation.

To avoid `fixing' these cases, I first changed the name in libkern,
and then compile-tested every file where min/max occurred in order to
confirm that it failed -- and thus confirm that nothing shadowed
min/max -- before changing it.

I have left a handful of bootloaders that are too annoying to
compile-test, and some dead code:

cobalt ews4800mips hp300 hppa ia64 luna68k vax
acorn32/if_ie.c (not included in any kernels)
macppc/if_gm.c (superseded by gem(4))

It should be easy to fix the fallout once identified -- this way of
doing things fails safe, and the goal here, after all, is to _avoid_
silent integer truncations, not introduce them.

Maybe one day we can reintroduce min/max as type-generic things that
never silently truncate.  But we should avoid doing that for a while,
so that existing code has a chance to be detected by the compiler for
conversion to uimin/uimax without changing the semantics until we can
properly audit it all.  (Who knows, maybe in some cases integer
truncation is actually intended!)
2018-09-03 16:29:22 +00:00

99 lines
3.1 KiB
C

/* $NetBSD: auvolconv.c,v 1.4 2018/09/03 16:29:30 riastradh Exp $ */
/*-
* Copyright (c) 2007 Jared D. McNeill <jmcneill@invisible.ca>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: auvolconv.c,v 1.4 2018/09/03 16:29:30 riastradh Exp $");
#include <sys/types.h>
#include <sys/param.h>
#include <sys/select.h>
#include <sys/condvar.h>
#include <sys/kmem.h>
#include <sys/device.h>
#include <sys/endian.h>
#include <dev/audiovar.h>
#include <dev/auconv.h>
#include <dev/auvolconv.h>
int
auvolconv_slinear16_le_fetch_to(struct audio_softc *asc,
stream_fetcher_t *self, audio_stream_t *dst, int max_used)
{
auvolconv_filter_t *pf;
stream_filter_t *this;
int16_t j, *wp;
int m, err;
u_int vol;
pf = (auvolconv_filter_t *)self;
this = &pf->base;
max_used = (max_used + 1) & ~1;
vol = *pf->vol;
if ((err = this->prev->fetch_to(asc, this->prev, this->src, max_used)))
return err;
m = (dst->end - dst->start) & ~1;
m = uimin(m, max_used);
FILTER_LOOP_PROLOGUE(this->src, 2, dst, 2, m) {
j = le16dec(s);
wp = (int16_t *)d;
le16enc(wp, (int32_t)(j * vol) / 255);
} FILTER_LOOP_EPILOGUE(this->src, dst);
return 0;
}
int
auvolconv_slinear16_be_fetch_to(struct audio_softc *asc,
stream_fetcher_t *self, audio_stream_t *dst, int max_used)
{
auvolconv_filter_t *pf;
stream_filter_t *this;
int16_t j, *wp;
int m, err;
u_int vol;
pf = (auvolconv_filter_t *)self;
this = &pf->base;
max_used = (max_used + 1) & ~1;
vol = *pf->vol;
if ((err = this->prev->fetch_to(asc, this->prev, this->src, max_used)))
return err;
m = (dst->end - dst->start) & ~1;
m = uimin(m, max_used);
FILTER_LOOP_PROLOGUE(this->src, 2, dst, 2, m) {
j = be16dec(s);
wp = (int16_t *)d;
be16enc(wp, (int32_t)(j * vol) / 255);
} FILTER_LOOP_EPILOGUE(this->src, dst);
return 0;
}