/**************************************************************************/
/**************************************************************************/
/*
pack.c: This file contains a function to pack data from HWORDs into
the minimum number of bits needed to recover the data.
*/
/**************************************************************************/
/*
Copyright, 1995 Atari Corporation.
Written for Atari by Highlight Software Solutions.
*/
/**************************************************************************/

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


/**************************************************************************/
static HINT maskarray[16] = {0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80,0x100,0x200,
		0x400,0x800,0x1000,0x2000,0x4000,0x8000};

/**************************************************************************/
/*
Function: pack

definition:
	This function is called to pack a variable length buffer of
	elements of HWORD's into a number of bits that has been
	previously determined to be sufficient for that data and
	place the packed data into a second HWORD buffer.
	The function returns an HINT with the number of elements of HWORD
	needed to hold the packed data. If the pack data only uses part of
	the last HWORD, then that word is still counted in the return count.
arguments:
	num: The number of bits that the HWORDS can be packed into. This
		should be previously determined as appropriate.
	to: This is the buffer where the packed data is placed.
	from: This is the buffer from which the data is taken to pack.
	entries: This is the number of elements to pack.
---------------------------------------------------------------------
returns:
	this function returns a HINT which contains the number of words
	needed to pack the given data. This can be used to skip over
	the packed data in an output buffer.
---------------------------------------------------------------------
example:
	numwords = pack(10, pbuf, wbuf, 32);
	This function packs the next 32 HWORD's in wbuf into 10 bits each
	and places the packed data into pbuf. The number of HWORD's needed
	to pack this data (in this case 10 * 32 / 16)  is returned. In the
	example it returns 20.
*/
/**************************************************************************/

pack(HINT num,LPHINT to, LPHINT from, HINT entries)
{
HINT incnt, outcnt;	/* Word counters for in array and out array */
HINT inbit, outbit;	/* bit position for in array and out array */
HINT inmax;		/* maximum bit needed from input values */
HINT temp;		/* temporary storage */

/* initialize variables */
incnt = 0;
outcnt = 0;
inmax = num - 1;
inbit = inmax;
outbit = BEGINCOUNT;
while (incnt < entries) {

	/** pack bits in the output array from the input array **/
	while (inbit >= 0) {
		if (outbit == BEGINCOUNT) {
			to[outcnt] = 0;		/* clear the value here */
		} /* if outbit */

		temp = ((from[incnt] >> inbit) & (0x0001));
		to[outcnt] |= ((temp << outbit) & maskarray[outbit]);

		outbit--;
		if (outbit < 0) {
			outbit = BEGINCOUNT;
			outcnt++;
		}

		inbit--;
	}

	/** Increment In Counter **/
	inbit = inmax;
	incnt++;
}
if (outbit < BEGINCOUNT) outcnt++;
return(outcnt);
} /* return from pack */
