C Subroutine called INEXT for reversing the order of bytes in a
C bitstring with entry point NEXT for returning the integer
C value of the next N bits where N may be from 1 to 32.
C
C Programmer: Sue Volkmann
C September 1982
C VAX-11 FORTRAN
C
SUBROUTINE INEXT(NUMBYTES,BYTES)
BYTE BYTES(NUMBYTES)
C Reverse bytes internally
DO I=1,NUMBYTES/2
TEMP=BYTES(I)
BYTES(I)=BYTES(NUMBYTES-I+1)
BYTES(NUMBYTES-I+1)=TEMP
END DO
C Establish starting point in reversed array for calls to NEXT
C Divide large arrays into 4000-byte segments
C K = Starting address (byte #) of last segment
C L = The number of bits in last segment or partial segment
L=MOD(NUMBYTES*8,32000)
K=(NUMBYTES/4000)*4000+1
RETURN
C
ENTRY NEXT(N,N1,IR,IND)
C
C IND is used in call to NEXT to signal whether extraction is to
C be zero-filled or sign-filled. IND<0 --> sign-filled
C IND >= 0 --> zero-filled
C The information is used to set MODE and then IND is reset to 1
C
C Read specified number of bits from position of pointer
C and subtract bits from total left to be read
C
C On each call L is checked to see if any bits remain in segment
C If L.LE.0 then K is checked to see if any segments remain
C If K.EQ.1 , buffer has been used up - no operation
C If K.GT.1 , L is set to L+32000 and K is set to K-4000
C
C If buffer still has enough bits (L.GE.N .OR. L.GT.0.AND.K.GT.1)
C then N bits are picked up and L will be set to L-N.
C If more bits requested than are left in buffer - remaining bits
C will be picked up and L will be set to 0.
C
mode=0
if (ind.lt.0)mode=1
IR=0
IND=1
N1=N
IF (N.LE.0 .OR. N.GT.32)THEN !check # bits
IND=4 !illegal bits request
N1=0
RETURN
ELSE IF(L.EQ.0.AND.K.EQ.1)THEN
IND=0 !end of buffer condition
N1=0
RETURN
ELSE IF(L.LE.0)THEN !reposition pointer if beyond 32000 bits
L=32000+L
K=K-4000 !reset starting array element
IF(K.LT.1)THEN
IND=2 !buffer overflow condition
N1=0
RETURN
END IF
ELSE IF (K.EQ.1.AND.L-N.LT.0)THEN
IND=3 !partial overflow of buffer
N1=L !reset n to allow extraction to end
END IF
C All conditions normal - complete bit extraction and return to
C calling program.
L=L-N1
if(mode.eq.0)IR=LIB$EXTZV(L,N1,BYTES(K))
if(mode.eq.1)IR=LIB$EXTV(L,N1,BYTES(K))
RETURN
END