      SUBROUTINE QDIV(A,B,C)
C
C     This routine divides a quadword by a longword and
C        returns a quadword result.  It makes use of
C        the routine EDIV which divides a quadword by
C        a longword and gives a longword result.
C
      INTEGER*4 A(2),B,C(2),NUM(2),DENOM,DENOM2,QUOHI,ZERO(2)
      LOGICAL NEGATIVE
      DATA ZERO/0,0/
      NEGATIVE = .FALSE.
C
C     make internal copy of args we may need to modify
C
      NUM(1) = A(1)
      NUM(2) = A(2)
      DENOM = B
C
C     check sign of numerator (high order)
C
      IF(NUM(2).LT.0)THEN
         CALL SUBQUAD(ZERO,NUM,NUM)
         NEGATIVE = .NOT.NEGATIVE
      END IF
C
C     check sign of divisor
C
      IF(DENOM.LT.0)THEN
         DENOM = -DENOM
         NEGATIVE = .NOT.NEGATIVE
      END IF
C
C     All the complications arise because the high bit of the
C        result from EDIV must be cleared.  Therefore, we divide
C        the dividend by 2 first (more or less).  However, the
C        dividend had better be greater than 1.
C
      IF(DENOM.EQ.1)THEN
         C(1) = NUM(1)
         C(2) = NUM(2)
      ELSE
         DENOM2 = DENOM/2
C        quohi is the high order result * 2
         QUOHI = NUM(2) / DENOM2
C        leave only the remainder in the high order numerator
C           (this guarantees no problems with EDIV)
         NUM(2) = NUM(2) - (QUOHI * DENOM2)
C        get low order result
         CALL EDIV(NUM,DENOM,C)
C        stick in the highest bit if needed
         IF(MOD(QUOHI,2).EQ.1)C(1) = C(1) .OR. '80000000'X
C        store high order result
         C(2) = ISHFT(QUOHI,-1)
      END IF
C
C     now check for negative result needed
C
      IF(NEGATIVE)CALL SUBQUAD(ZERO,C,C)
      RETURN
      END
