--
--	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 = 32)

--{{{  #options and #comments
#OPTION "E V"  -- no compiler libs, no vector space
#COMMENT "Support routines library, V1.2, 10/03/90"
#COMMENT "(c) Copyright INMOS Limited 1988"
--}}}

#PRAGMA TRANSLATE RealIDiv "RealIDiv%CHK"

--{{{  RealIDiv
--* Perform an unsigned part divide operation.
--
-- @return the quotient
-- @return the most significant word of the remainder
-- @return the least significant word of the remainder
-- @param Hi the most significant word of the dividend
-- @param Lo the middle word of the dividend
-- @param Guard the least significant word of the dividend
-- @param Yfrac the two words of the divisor
INT, INT, INT FUNCTION RealIDiv (VAL INT Hi, Lo, Guard, VAL [2]INT Yfrac)
  --{{{  history
  -- V1.1, 21-Feb-89
  -- Amalgamated the T2 and TB/T8 code; V1.2, 10-Mar-90; SRH
  --}}}

  #INCLUDE "endian.inc" -- 4-Sep-07

  INT Quot, Rem.Hi, Rem.Lo :

  VALOF
    INT Carry, Borrow :
    [3]INT W :
    PLACE W IN WORKSPACE :
    --{{{  
    SEQ
      IF
        Yfrac[hi] = Hi
          Quot := (~0)
        TRUE
          Quot,  Carry := LONGDIV ( Hi,  Lo,  Yfrac[hi] )
      IF
        Quot <> 0   -- ensure not to big
          SEQ
            W[1],  W[0] := LONGPROD ( Quot,  Yfrac[lo],  0 )
            W[2],  W[1] := LONGPROD ( Quot,  Yfrac[hi],  W[1] )
            Carry,  Rem.Lo := LONGDIFF ( Guard,  W[0],  0 )
            Carry,  Rem.Hi := LONGDIFF ( Lo,  W[1],  Carry )
            Carry,  Borrow := LONGDIFF ( Hi,  W[2],  Carry )
            WHILE ( Borrow /\ (MOSTNEG INT) ) <> 0   -- too big, add back
              SEQ
                Quot := Quot MINUS 1
                Carry,  Rem.Lo := LONGSUM ( Rem.Lo,  Yfrac[lo],  0 )
                Carry,  Rem.Hi := LONGSUM ( Rem.Hi,  Yfrac[hi],  Carry )
                Carry,  Borrow := LONGSUM ( Borrow,  0,  Carry )
        TRUE
          SEQ
            Rem.Hi := Lo
            Rem.Lo := Guard
    --}}}
    RESULT Quot, Rem.Hi, Rem.Lo
:
--}}}

#ENDIF
