/**************************************************************************/
/**************************************************************************/
/*
aperr.c: This file contains the analysis code for doing the CSX proprietary
	APCM scaling and error adjustments on the Pre-emph data for CSXC.
*/
/**************************************************************************/
/*
Copyright, 1995 Atari Corporation.
Written for Atari by Highlight Software Solutions.
*/
/**************************************************************************/

#include "hdef.h"	/* this file contains the data types and constants. */


/**************************************************************************/
/*
Function: aperr

definition:
	This function implements a scaling multiply with error adjusting
	on a block of data. The calling parameters contain the number
	of word elements to be processed from an input buffer, the
	scale factor, an initial error value if needed, and a destination
	buffer.
arguments:
	num: contains the number of word elements to scale.
	sf: contains the scale factor as a 16 bit unsigned integer.
	eval: contains a pointer to a starting error value. The
		function returns the ending error value to this location.
	from: the source buffer.
	to: the destination buffer.
-------------------------------------------------------
returns:
	an HINT that is currently not meaningful
-------------------------------------------------------
example:
aperr(256, 500, &errval, datain, dataout);
calls this function to scale each of the 256 elements at datain by a
factor of 500/32767 with the errval passed in *errval and places the
results in dataout.

*/
/**************************************************************************/

HINT aperr(HINT num, HWORD sf,LPHLONG eval, LPHINT from, LPHINT to)
{
HINT	i;
HLONG	errsav;
HLONG   p, acc;
HLONG	te;

	errsav = *eval;
	for (i = 0; i < num; i++) {
		p = ((HLONG) from[i]) * ((HLONG) sf);
		acc = p + errsav;
		te = acc - ((HLONG) acc - (HLONG) (acc & ((HLONG)0x0000ffff)));
        	errsav = te;
                to[i] = (HINT) ((acc & 0xffff0000) >> 16);
	}
	*eval = errsav;
return(0);
}
