C+
C NAME:
C	IPEELB
C PURPOSE:
C	Extracts a specified number of bits, starting at a specified location,
c	 from the data area defined by
C	array INB. By default bit extraction continues after the last bit 
c	 extracted during the previous call
C	to IPEELB. The starting point can be specified explicitly by setting 
c	 the variables IB and IS in the
C	calling program.
C CALLING SEQUENCE:
C	I = IPEELB(INB)
C INPUTS:
C	INB	integer*2; array from which bits are to be peeled
C	IB	integer*2; number of bytes already peeled off
C	IS	integer*2; number of bits already peeled off in byte IB+1
C	IP	integer*2; number of bits to be peeled off
C OUTPUTS:
C	IPEELB	integer*2; destination of extracted bits
C	IB	integer*2; same as input; updated value
C	IS	integer*2; same as input; updated value
C COMMON BLOCKS:
C	common /PEELB/ IB,IS,IP
C RESTRICTIONS:
C	The common block /PEELB/ must be defined in the calling program
C	Never extract more than 16 bits (one word) at a time (i.e. make sure that IP .le. 16)
C PROCEDURE:
C	The extraction of bits is done according to the following rules 
c	 (consider the integer*2 array INB as 
C	sequence of bytes (number of bytes is twice the array dimension):
C	1)  Bytes are used sequentially as the source for the bit extraction;
c	    the least significant byte of
C	    any (2-byte) INB element is used first.
C	2)  Inside each byte extraction starts at the most significant byte.
C	3)  For bits extracted from one byte the order of significance is
c	     maintained in the destination.
C	4)  If bits are extracted from two subsequent bytes than the set of 
c	    bits from the first byte is
C	    treated as more significant than the second set of bits.
C MODIFICATION HISTORY:
C	Created NOV-1990 by PaulH.
C	This subroutine is the FORTRAN version of the IPEELB entry in the 
c	machine language file HAXIMASS.MAR.
C-

	integer*2 function IPEELB(INB)
	implicit integer*2 (I-N)
	integer*2 INB(*),IB/0/,IS/0/
	common /PEELB/ IB,IS,IP		! Initialized in DECIMH
					! Bit IS of IN(IB) is the next bit to 
					! be peeled (updated on return)
C	WRITE (*,*) '------IN IPEELB'

	IPP = IP			! IP<=0: number of bits to be peeled 
					! (unchanged on return)
	IPEELB = 0			! Clear all bits in destination

	do while (IPP .ne. 0)

		IBW = IB/2		! Number of words (integer*2) already peeled
		IEX = min(8-IS,IPP)	! Number of bits to be peeled (never more than 8 bits at a time)
		ISW = (IB-IBW*2+1)*8-IS-IEX	! Start peeling at bit nr ISW	
		IBW = IBW+1			! Array index of last unfinished word
		IPOS = IPP-IEX		! Dump extracted bits starting at bit IPOS in IPEELB
		call MVBITS(INB(IBW),ISW,IEX,IPEELB,IPOS)

		IPP = IPP-IEX		! Number of bits left to be peeled
		IS = IS+IEX
		IB = IB+IS/8		! Number of bytes already peeled
		IS = mod(IS,8)

	end do

C	WRITE (*,*) '------OUT IPEELB'

	return
	end
