#include <v_lib.h>

/*
 *	cl (ver. 6.0): no optimization. PII-233. 32 channels: 604 samples per millisecond.
 *	cl (ver. 6.0): -O2 -Ot -Gs -G6. PII-233. 32 channels: 1877 samples per millisecond.
 */
void	waveMix8Bit(u8_t *out,u8_t **in_buffers,u32_t num_buffers,u32_t buffer_len)
{
	u32_t pos, offset;
	register u32_t value, buf;
	offset=0x80 * num_buffers;
	for(pos=0;pos<buffer_len;pos++)
	{
		value=in_buffers[0][pos];
		for(buf=1;buf<num_buffers;buf++)
			value+=in_buffers[buf][pos];
		value=0x80+value+offset;
		if(value > 0xFF)
			out[pos]=0xFF;
		else
			out[pos]=(u8_t)value;
	}
}

#define	LEN	1024
#define	NCH	32

int main()
{
	u8_t out_buf[LEN];
	u8_t *in_buf[NCH];
	u32_t i,tim;
	for(i=0;i<NCH;i++)
	{
		in_buf[i]=(u8_t*)memAlloc(LEN);
		memFill(in_buf[i],0x80,LEN);
	}
	tim=timeTickCount();
	for(i=0;i<1000;i++)
		waveMix8Bit(out_buf,in_buf,NCH,LEN);
	tim=timeTickCount()-tim;
	printf("%u repeats: %u channels,%u buffer len; %u milliseconds, %u samples/ms.\n",
		i,NCH,LEN,tim,i*LEN/tim);
	for(i=0;i<NCH;i++)
		memFree(in_buf[i]);
	return 0;
}
