--
--      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 "T2 compiler library, V1.2 16/11/89"
#COMMENT "(c) Copyright INMOS Limited 1988"
--}}}

#PRAGMA TRANSLATE INT64TOINT16 "INT64TOINT16%CHK"

--{{{  INT64TOINT16 -- T2
INT16 FUNCTION INT64TOINT16 (VAL INT64 B)

  --{{{  history
  -- Function version (10 Jun 87)
  -- V1.1, 21-Feb-89
  -- Changed the result type from INT to INT16; V1.2, 16 Nov 89 SRH
  --}}}

  INT Res :
  VALOF
    --{{{  16 Bit Constants
    VAL INT MaxInt  IS  #FFFF :
    VAL INT SignBit IS  #8000 :
    VAL INT BitsPerWord IS 16 :
    --}}}
    VAL [4]INT B RETYPES B :
    SEQ
      Res := B[0]
      --{{{  test for overflow
      IF
        (Res >= 0) AND (((B[1]\/B[2])\/B[3]) <> 0)
          CAUSEERROR()
        (Res < 0) AND (((B[1]/\B[2])/\B[3]) <> MaxInt)
          CAUSEERROR()
        TRUE
          SKIP
      --}}}
    RESULT INT16 Res
:
--}}}

#ENDIF
