--
--	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 10/01/90"
#COMMENT "(c) Copyright INMOS Limited 1988"
--}}}

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

#INCLUDE "realvals.inc"

--{{{  
REAL64 FUNCTION INT32TOREAL64 (VAL INT32 N)

  --{{{  history
  -- V1.1, 21-Feb-89
  -- Changed type of N from INT to INT32; V1.2, 10-Jan-90 SRH
  --}}}

  REAL64 X:
  VALOF
    --{{{  
    [2]INT X RETYPES X :
    VAL INT N RETYPES N :
    --Convert integer to real (exact representation).
    INT Dump, Places, Xexp, Xfrac, Xint, Xsign :
    SEQ
      IF
        --{{{  N = 0
        N = 0
          SEQ
            X [1] := 0
            X [0] := 0
        --}}}
        --{{{  N = Int
        TRUE
          SEQ
            --{{{  Get sign and positive integer
            IF
              N < 0
                SEQ
                  Xsign := SignBit
                  IF
                    --{{{  N = MinInt
                    N = SignBit -- MinInt
                      SEQ       -- (-1) * (1.0) * 2 ^ (BitsPerWord -1)
                        Xint := N -- Positve MinInt (Unsigned)
                    --}}}
                    TRUE
                      Xint := -N
              TRUE
                SEQ
                  Xsign := 0
                  Xint := N
            --}}}
            Places,  Xint,  Dump := NORMALISE ( Xint,  0)
            Xexp := (DRealXcess - 1) + (BitsPerWord - Places)
            Xint := Xint << 1               --drop leading bit
            Xexp,  Xfrac := SHIFTRIGHT ( Xexp,  Xint,  DRealShift + 1)
            Xexp,  X [0] := SHIFTRIGHT ( Xint,  0,  DRealShift + 1)
            X [1] := Xsign \/ Xfrac
        --}}}
    --}}}
    RESULT X
:
--}}}

#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 INT32TOREAL64 "INT32TOREAL64%CHK"
--}}}

#INCLUDE "realvals.inc"

--{{{  
REAL64 FUNCTION INT32TOREAL64 (VAL INT32 N)

  -- Function version (10 Jun 87)

  REAL64 X :
  VALOF
    [4]INT X RETYPES X :
    VAL [2]INT N RETYPES N :
    -- Convert quad length integer to real, round to zero or nearest.
    --  Exact
    --  N         : source integer
    INT Carry, Places, Xexp :
    [4] INT Xfrac :
    PLACE Xfrac IN WORKSPACE :
    [4] INT Xint :
    PLACE Xint IN WORKSPACE :
    INT Xsign :
    SEQ
      IF
        --{{{  N = 0
        (N[1] \/ N[0]) = 0
          X := [0,0,0,0]
        --}}}
        --{{{  N <> 0
        TRUE
          SEQ
            IF
              N[1] < 0
                SEQ
                  Xsign := SignBit
                  Carry,  Xint[0] := LONGDIFF ( 0,  N[0],  0)
                  Carry,  Xint[1] := LONGDIFF ( 0,  N[1],  Carry)
              TRUE
                SEQ
                  Xsign := 0
                  Xint[1] := N[1]
                  Xint[0] := N[0]
            Places,  Xint[1], Xint[0] := NORMALISE (Xint[1], Xint[0])
            Xexp := (DRealXcess - 1) + ((2 * BitsPerWord) - Places)
            --{{{  Pack Xfrac
            Xexp,  Xfrac[3] := SHIFTRIGHT ( Xexp,  Xint[1] << 1,  DRealShift + 1)
            Xexp,  Xfrac[2] := SHIFTRIGHT ( Xint[1],  Xint[0],  DRealShift)
            Xexp,  Xfrac[1] := SHIFTRIGHT ( Xint[0],  0,  DRealShift)
            Xfrac [0] := 0
            --}}}
            --{{{  set sign
            X[3] := Xsign \/ Xfrac[3]
            X[2] := Xfrac[2]
            X[1] := Xfrac[1]
            X[0] := Xfrac[0]
            --}}}
        --}}}
    RESULT X
:
--}}}

#ENDIF
