/**************************************************************************/
/**************************************************************************/
/*
maxvalue.c: This file contains a function to calculate the maximum value
used by an array of elements so that we can later pack the data into less
bits.
*/
/**************************************************************************/
/*
Copyright, 1995 Atari Corporation.
Written for Atari by Highlight Software Solutions.
*/
/**************************************************************************/

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


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

#define SIGNMASK	0x8000		/* mask to check sign bit */
#define MAXCOUNT 16			/** maximum bits per entry allowed **/
static HINT maskarray[16] = {0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80,0x100,0x200,
		0x400,0x800,0x1000,0x2000,0x4000,0x8000};

/**************************************************************************/
/*
Function: maxvalue

definition:
	This function looks at a chosen number of elements of HWORD's
	at a chosen address and returns the absolute value
	of the largest value in the array. Nothing in the
	initial array is changed by this function.
arguments:
	num: The number of elements to look at.
	inarray: The starting address of the elements.
---------------------------------------------------------------------
returns:
	this function returns a HINT which contains the number of bits
	needed to pack the input data into, preserving sign bits.
---------------------------------------------------------------------
example:
	val = maxvalue(32, add);
	This calls maxbits to see how many packed bits are needed to
	reproduce the next 32 elements of HWORD size starting at address
	add. The absolute value of the largest value is returned
	in val. This number should fall between 0 and 32K.
*/
/**************************************************************************/

HINT maxvalue(HINT num,LPHINT inarray)
{
HINT incnt;		/* Word counters for in array */
HINT bits;		/* maximum bits needed in array */
HINT i;			/* array count */
HINT posval;		/* negative and positive accumulators */

posval = 0x0000;

for (i = 0; i < num; i++) {
	if (abs(inarray[i]) > posval) {
		posval = abs(inarray[i]);
	}
}
return(posval);
} /* end of maxvalue */

