--
--	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 separate vector space
#COMMENT "TA compiler library, V1.2 06/04/90"
#COMMENT "(c) Copyright INMOS Limited 1988"
--}}}

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

#INCLUDE "realvals.inc"

--{{{  INT64TOREAL64
REAL64 FUNCTION INT64TOREAL64 (VAL INT Mode, VAL INT64 N)
  -- Beta-2 (27feb87)

  REAL64 X:
  VALOF
    --{{{  
    --{{{  DEFs
    --{{{  IEEE Double length constants for 32 bit implementation
    VAL DRealExpSh  IS                    20 :
    --}}}
    --}}}
    [2]INT X RETYPES X :
    VAL [2]INT N RETYPES N :
    --Convert double length integer to real, round to zero or nearest.
    --  X         : destination real
    --  Mode      : Truncate or Nearest (rounding mode)
    --  N         : source integer
    INT Carry, Places, Xexp :
    [2] INT Xfrac :
    PLACE Xfrac IN WORKSPACE :
    [2] INT Xint :
    PLACE Xint IN WORKSPACE :
    INT Xsign :
    SEQ
      IF
        (N [1] \/ N [0]) = 0
          SEQ
            X [1] := 0
            X [0] := 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)
            Xint [1],  Xint [0] := SHIFTLEFT ( Xint [1],  Xint [0],  1)
    
            Xexp,  Xfrac [1] := SHIFTRIGHT ( Xexp,  Xint [1],  DRealShift + 1)
            Xexp,  Xfrac [0] := SHIFTRIGHT ( Xint [1],  Xint [0],  DRealShift +1)
            IF
              Places > DRealShift  --exact answer
                SKIP
              TRUE                                   --must round
                SEQ
                  Xint [0] := (Xint [0] >> 1) /\ DRealExp   --get bottom bits
                  IF
                    (Mode = Truncate) OR ((Xint [0] /\ DRealRBit) = 0)
                      SKIP
                    ((Xint [0] /\ DRealXcess) \/ (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


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

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

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

#INCLUDE "realvals.inc"

--{{{  INT64TOREAL64
REAL64 FUNCTION INT64TOREAL64 (VAL INT Mode, VAL INT64 N)

  --{{{  history
  -- Function version (10 Jun 87)
  -- V1.1; 21/02/89
  --
  -- V1.2; 05/06/92: CON
  -- Used QUADNORMALISE from occamutl.lib
  --}}}

  REAL64 X :
  VALOF
    --{{{  #USE "occamutl.lib"
    #PRAGMA TRANSLATE QUADNORMALISE "QUADNORMALISE%CHK"
    #USE "occamutl.lib"
    --}}}
    --{{{  IEEE Quad length constants for 16 bit implementation
    VAL DRealExpSh  IS     4 :
    --}}}
    [4]INT X RETYPES X :
    VAL [4]INT N RETYPES N :
    --Convert quad length integer to real, round to zero or nearest.
    --  X         : destination real
    --  Mode      : Truncate or Nearest (rounding mode)
    --  N         : source integer
    INT Carry, Places, Xexp :
    [4] INT Xfrac :
    PLACE Xfrac IN WORKSPACE :
    [4] INT Xint :
    PLACE Xint IN WORKSPACE :
    INT Xsign :
    IF
      --{{{  N = 0
      (((N[3] \/ N[2]) \/ N[1]) \/ N[0]) = 0
        X := [0,0,0,0]
      --}}}
      --{{{  N <> 0
      TRUE
        SEQ
          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
      
          [4]INT temp.Xint :
          SEQ
            temp.Xint := Xint
            QUADNORMALISE (Places, Xint, Carry, temp.Xint, 0)
      
          Xexp := (DRealXcess - 1) + ((4 * BitsPerWord) - Places)
          --{{{  Pack Xfrac
          Xfrac[3],  Xexp := SHIFTLEFT ( Xexp,  Xint[3] << 1,  DRealExpSh)
          Xfrac[2],  Xexp := SHIFTLEFT ( Xint[3],  Xint[2],  DRealExpSh + 1)
          Xfrac[1],  Xexp := SHIFTLEFT ( Xint[2],  Xint[1],  DRealExpSh + 1)
          Xfrac[0],  Xexp := SHIFTLEFT ( Xint[1],  Xint[0],  DRealExpSh + 1)
          --}}}
          --{{{  Round result
          IF
            Places > (BitsPerWord + DRealShift) --exact answer
              SKIP
            (Mode = Truncate) OR ((Xint[0] /\ DRealRBit) = 0)--round to zero
              --round bit clear
              SKIP
            ((Xint[0] /\ DRealXcess) \/ (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)
                Carry,  Xfrac[2] := LONGSUM ( Xfrac[2],  0,  Carry)
                Carry,  Xfrac[3] := LONGSUM ( Xfrac[3],  0,  Carry)
          --}}}
          --{{{  set sign
          X[3] := Xsign \/ Xfrac[3]
          X[2] := Xfrac[2]
          X[1] := Xfrac[1]
          X[0] := Xfrac[0]
          --}}}
      --}}}
    RESULT X
:
--}}}

#ENDIF
