--
--	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 NOT DEFINED(TARGET.HAS.FPU)

#OPTION "E V"  -- no compiler libraries, no vector space
#COMMENT "T2 compiler library, V1.0 28/03/90"
#COMMENT "(c) Copyright INMOS Limited 1990"

#PRAGMA TRANSLATE FRACMUL "FRACMUL%O"

INT FUNCTION FRACMUL( VAL INT X, Y )

  --{{{  specification
  -- Purpose:  To do a signed fixed point multiplication.
  -- In:       X, Y - the two numbers to be multiplied.  They are
  --           considered to represent real fractions in the range
  --           [-1.0, 1.0).
  -- Returned: The product, rounded to nearest, of X and Y, as signed fixed
  --           point numbers, representing a number in the range (-1.0, 1.0).
  -- Notes:    The value of the fractions represented by X, Y and their
  --           product is obtained by multiplying their INT value by
  --           2^( -wordlength + 1 ).  For example, #4000 is 1/2;
  --           #C000 is -1/2; #E000 is -1/4; #A000 is -3/4; #8000 is -1.0.
  --           One must be very careful of the negative fractions due to
  --           their two's complement interpretation as INTs.
  --           A request for the product of -1.0 and -1.0 will result in the
  --           transputer error flag being set.
  --           This occam is effectively intended to simulate the fmul
  --           instruction found on 32-bit transputers.
  --}}}

  INT product :
  VALOF
    --{{{  calculate signed fixed point product
    IF
      ( X = ( MOSTNEG INT ) ) AND ( Y = ( MOSTNEG INT ) )
        CAUSEERROR()  -- product of -1.0 and -1.0 requested
      TRUE
        --{{{  safe calculation
        BOOL Xneg, Yneg, done :
        INT A, B :
        SEQ
          -- X and Y are not both MOSTNEG INT
          Xneg := ( X < 0 )
          Yneg := ( Y < 0 )
          done := FALSE
          --{{{  look at X
          IF
            Xneg
              IF
                X = ( MOSTNEG INT )  -- then X represents -1.0
                  SEQ
                    product := -Y
                    done := TRUE
                TRUE
                  A := -X
            TRUE
              A := X
          --}}}
          --{{{  look at Y
          IF
            Yneg
              IF
                Y = ( MOSTNEG INT )  -- then Y represents -1.0
                  SEQ
                    product := -X
                    done := TRUE
                TRUE
                  B := -Y
            TRUE
              B := Y
          --}}}
          IF
            done
              SKIP -- we already have the correct answer
            TRUE
              --{{{  take product
              INT res.hi, res.lo :
              SEQ
                -- A and B are both positive semi-definite
                res.hi, res.lo := LONGPROD( A, B, 0 )
                res.hi, res.lo := SHIFTLEFT( res.hi, res.lo, 1 )  -- take account of fixed point
                product := res.hi -- but we do not necessarily have the final answer yet
                --{{{  round
                IF
                  res.lo >= 0  -- guard bit is 0, so no definitely no rounding to be done
                    SKIP
                  ( res.lo <> ( MOSTNEG INT ) ) OR ( ( product /\ 1 ) = 1 )
                    product := product + 1  -- round up
                  TRUE  -- border line case, leave last bit unset ( i.e. result even )
                    SKIP
                --}}}
                --{{{  give correct sign to the answer
                IF
                  ( Xneg AND ( NOT Yneg ) ) OR ( ( NOT Xneg ) AND Yneg )  -- exclusive OR
                    product := -product -- this is all right if product is 0 ( it will not give -1.0 )
                  TRUE
                    SKIP
                --}}}
              --}}}
        --}}}
    --}}}
    RESULT product
:

#ENDIF
