--
--	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
--

#IF (TARGET.BITS.PER.WORD = 32) AND (NOT DEFINED(TARGET.HAS.FPU))

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

--{{{  #PRAGMA TRANSLATE
#PRAGMA TRANSLATE INT64TOREAL32 "INT64TOREAL32%CHK"
--}}}

#INCLUDE "realvals.inc"

--{{{  INT64TOREAL32
REAL32 FUNCTION INT64TOREAL32 (VAL INT Mode, VAL INT64 A)

  -- Beta-2 (27feb87)

  REAL32 Result:
  VALOF
    --{{{  constants
    VAL low  IS 0 :
    VAL high IS 1 :
    --}}}
    INT Result RETYPES Result :
    VAL [2]INT A RETYPES A :
    [2] INT B :
    PLACE B IN WORKSPACE :
    INT Xfrac, Xexp, Ans, Carry, Places :
    --{{{  
    SEQ
      IF
        A [high] < 0
          SEQ
            Carry,  B [low] := LONGDIFF ( 0,  A [low],  0)
            Carry,  B [high] := LONGDIFF ( 0,  A [high],  Carry)
            Ans := SignBit
        TRUE
          SEQ
            B [high] := A [high]
            B [low] := A [low]
            Ans := 0 -- positive
      Places,  B [high],  B [low] := NORMALISE ( B [high],  B [low])
      IF
        Places = (2 * BitsPerWord)
          Result := 0
        TRUE
          --{{{  round and pack
          SEQ
            Xexp := (((2 * BitsPerWord) - 1) - Places) + RealXcess
            Places := B [high] /\ RealExp      --get round bits
            Xexp,  Xfrac := SHIFTRIGHT ( Xexp,  B [high] << 1,  RealShift + 1)
            IF
              (Mode = Truncate) OR ((Places /\ RealRBit) = 0)
                SKIP
              ((B [low] \/ (Places /\ RealXcess)) \/ (Xfrac /\ 1)) = 0
                SKIP                         --round down
              TRUE                           --round up/even
                Xfrac := Xfrac + 1
            Result := Ans \/ Xfrac
          --}}}
    --}}}
    RESULT Result
:
--}}}

#ENDIF


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

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

--{{{  #PRAGMA TRANSLATE
#PRAGMA TRANSLATE INT64TOREAL32 "INT64TOREAL32%CHK"
--}}}

#INCLUDE "realvals.inc"

--{{{  INT64TOREAL32
REAL32 FUNCTION INT64TOREAL32 (VAL INT Mode, VAL INT64 A)

  -- Function version (10 Jun 87)

  -- Convert quad length integer to real, round to zero or nearest.
  --   Mode      : Truncate or Nearest (rounding mode)
  --   A         : source integer

  REAL32 X :
  VALOF
    [2]INT X RETYPES X :
    VAL [4]INT N RETYPES A :
    INT Carry, Places, Xexp :
    [2] INT Xfrac :
    PLACE Xfrac IN WORKSPACE :
    [4] INT Xint :
    PLACE Xint IN WORKSPACE :
    INT Xsign :
    IF
      --{{{  Zero
      (((N[3] \/ N[2]) \/ N[1]) \/ N[0]) = 0
        X := [0, 0]
      --}}}
      --{{{  Not zero
      TRUE
        SEQ
          --{{{  Get absolute value and sign of N
          IF
            N[3] < 0
              SEQ
                Xsign := SignBit
                Carry,  Xint[0] := LONGDIFF ( 0,  N[0],  0)
                Carry,  Xint[1] := LONGDIFF ( 0,  N[1],  Carry)
                Carry,  Xint[2] := LONGDIFF ( 0,  N[2],  Carry)
                Carry,  Xint[3] := LONGDIFF ( 0,  N[3],  Carry)
            TRUE
              SEQ
                Xsign := 0
                Xint := N
          --}}}
          --{{{  Float
          IF
            Xint[3] <> 0
              SEQ
                Places,  Xint[3],  Carry := NORMALISE ( Xint[3],  Xint[2])
                Xint[2],  Carry := SHIFTLEFT ( Xint[2],  Xint[1],  Places)
                Xint[1],  Xint[0] := SHIFTLEFT ( Xint[1],  Xint[0],  Places)
            Xint[2] <> 0
              SEQ
                Places,  Xint[3],  Carry := NORMALISE ( Xint[2],  Xint[1])
                Xint[2],  Xint[1] := SHIFTLEFT ( Xint[1],  Xint[0],  Places)
                Xint[0] := 0
                Places := Places + BitsPerWord
            Xint[1] <> 0
              SEQ
                Places,  Xint[3],  Xint[2] := NORMALISE ( Xint[1],  Xint[0])
                Xint[1] := 0
                Xint[0] := 0
                Places := Places + (BitsPerWord * 2)
            TRUE  -- Xint[0] must be non-zero
              SEQ
                Places,  Xint[3],  Carry := NORMALISE ( Xint[0],  0)
                Xint[2] := 0
                Xint[1] := 0
                Xint[0] := 0
                Places := Places + (BitsPerWord * 3)
          Xexp := ((RealXcess - 1) + (4 * BitsPerWord)) - Places
          Xexp,  Xfrac[1] := SHIFTRIGHT ( Xexp,  Xint[3]<<1,  RealShift + 1)
          Xexp,  Xfrac[0] := SHIFTRIGHT ( Xint[3],  Xint[2],  RealShift)
          --}}}
          --{{{  Round if not exact answer
          IF
            Places > ((2 * BitsPerWord) + RealShift) --exact answer
              SKIP
            TRUE                                   --must round
              SEQ
                Carry := Xint[2] /\ RealExp     --get bottom bits
                IF
                  (Mode = Truncate) OR ((Carry /\ RealRBit) = 0)--round to zero
                    SKIP  --round bit clear
                  (((Xint[2] /\ RealXcess) \/ (Xint[1] \/ Xint[0])) \/
                      (Xfrac[0] /\ 1)) = 0--round down
                    SKIP
                  TRUE                    --round up/even
                    SEQ
                      Carry,  Xfrac[0] := LONGSUM ( Xfrac[0],  1,  0)
                      Carry,  Xfrac[1] := LONGSUM ( Xfrac[1],  0,  Carry)
          --}}}
          X[1] := Xsign \/ Xfrac[1]
          X[0] := Xfrac[0]
      --}}}
    RESULT X
:
--}}}

#ENDIF
