--
--      Inmos Libraries
--      Copyright (C) 1988, 1989, 1990, 1991, 1993  Inmos Limited
--
--      This library is free software; you can redistribute it and/or
--      modify it under the terms of the GNU Lesser General Public
--      License as published by the Free Software Foundation; either
--      version 2 of the License, or (at your option) any later version.
--
--      This library is distributed in the hope that it will be useful,
--      but WITHOUT ANY WARRANTY; without even the implied warranty of
--      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
--      Lesser General Public License for more details.
--
--      You should have received a copy of the GNU Lesser General Public
--      License along with this library; if not, write to the Free Software
--      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
--

--** @module forall

#IF (TARGET.BITS.PER.WORD = 16)

--{{{  #options and #comments
#OPTION "E V"  -- no compiler libs, no vector space
#COMMENT "Support routines library, V1.1 21/02/89"
#COMMENT "(c) Copyright INMOS Limited 1988"
--}}}

#PRAGMA TRANSLATE QRealIDiv "QRealIDiv%CHK"

#INCLUDE "realvals.inc"

--{{{  QRealIDiv -- T2
INT, INT, INT, INT, INT FUNCTION QRealIDiv (VAL INT Dv3.in, Dv2.in, Dv1.in,
                                            Dv0.in, Guard.in, VAL [4]INT Yfrac)

  -- Function version (10 Jun 87)
  -- Fix for Q=0 case 16-sep-87
  --
  -- unsigned part divide operation
  -- Divides Dv3..Dv0 by Yfrac[0..3] to produce quotient Quot and
  -- remainder Dv2..Dv0

  INT Q, Dv2, Dv1, Dv0, Guard :
  VALOF
    INT Carry, Dv3 :
    [5] INT W :
    PLACE W IN WORKSPACE :
    SEQ
      IF
        Yfrac[3] = Dv3.in
          Q := MaxInt
        TRUE
          Q,  Carry := LONGDIV ( Dv3.in,  Dv2.in,  Yfrac[3])
      IF
        Q <> 0                --ensure not too big
          SEQ
            W[1],  W[0] := LONGPROD ( Q,  Yfrac[0],  0)
            W[2],  W[1] := LONGPROD ( Q,  Yfrac[1],  W[1])
            W[3],  W[2] := LONGPROD ( Q,  Yfrac[2],  W[2])
            W[4],  W[3] := LONGPROD ( Q,  Yfrac[3],  W[3])
            Carry,Guard := LONGDIFF ( Guard.in,  W[0],  0)
            Carry,  Dv0 := LONGDIFF ( Dv0.in,  W[1],  Carry)
            Carry,  Dv1 := LONGDIFF ( Dv1.in,  W[2],  Carry)
            Carry,  Dv2 := LONGDIFF ( Dv2.in,  W[3],  Carry)
            Carry,  Dv3 := LONGDIFF ( Dv3.in,  W[4],  Carry)
            WHILE (Dv3 /\ SignBit) <> 0   --too big, add back
              SEQ
                Carry,  Q  := LONGDIFF ( Q,  1,  0)
                Carry,Guard := LONGSUM ( Guard,  Yfrac[0],  0)
                Carry,  Dv0 := LONGSUM ( Dv0,  Yfrac[1],  Carry)
                Carry,  Dv1 := LONGSUM ( Dv1,  Yfrac[2],  Carry)
                Carry,  Dv2 := LONGSUM ( Dv2,  Yfrac[3],  Carry)
                Carry,  Dv3 := LONGSUM ( Dv3,  0,  Carry)
        TRUE
          SEQ
            Dv2 := Dv2.in
            Dv1 := Dv1.in
            Dv0 := Dv0.in
            Guard := Guard.in -- fix added 16-sep-87
    RESULT Q, Dv2, Dv1, Dv0, Guard
:
--}}}

#ENDIF
