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

--{{{  #options and #comments
#OPTION "E V"  -- no compiler libs, no vector space
#COMMENT "T8 compiler library, V1.4 10/07/90"
#COMMENT "(c) Copyright INMOS Limited 1988"
--}}}

#PRAGMA TRANSLATE NEXTAFTER "NEXTAFTER%O"

#INCLUDE "realvals.inc"

-- replaced the T8 code with the TA code because there is no good FPU
-- implementation possible ( the previous one was wrong with -0.0 and
-- infinities ); 10-July-90 SRH
--{{{  TA and T8 code the same
REAL32 FUNCTION NEXTAFTER (VAL REAL32 Y, Z)

  -- Post Beta-2 (22-jun-87)
  -- V1.1, 21-Feb-89
  -- Removed the code that changed -0's into +0's for Y
  -- negative; V1.2, 10-Jul-90 SRH ( Due to TA code being used
  -- for T8 as well, the version number has been chosen as the
  -- larger of the two ( V1.2 and V1.4 ); hence the previous TA
  -- version was V1.1, 21-Feb-89, and the previous T8 versions
  -- were V1.1, 21-Feb-89, V1.2, 12-Feb-90, and V1.3, 25-Jun-90.
  -- V1.3 of TA never existed. )

  REAL32 X :
  VALOF
    --{{{  VALS
    VAL Bias      IS       127 :
    --}}}
    INT X RETYPES X:
    VAL INT Y RETYPES Y:
    VAL INT Z RETYPES Z:
    INT frac1, frac2, exp1, exp2:
    SEQ
      exp1, frac1 := SHIFTLEFT (0, (Y << 1), RealShift)
      exp2, frac2 := SHIFTLEFT (0, (Z << 1), RealShift)
      IF
        ((INT exp1) = RealExp) AND (frac1 <> 0)
          --{{{  at least one NaN
          IF
            ((INT exp2) = RealExp) AND (frac2 <> 0)
              --{{{  precedence rule for NaNs
              IF
                frac1 > frac2
                  X := Y
                TRUE -- NaNs are equal, or Z is greater
                  X := Z
              --}}}
            TRUE
              X := Y
          --}}}
        ((INT exp2) = RealExp) AND (frac2 <> 0)
          X := Z
        --{{{  Y zero
        (Y << 1) = 0
          IF
            (Z << 1) = 0
              X := Y                  -- Z zero; need to keep sign of the 0
            (Z /\ (MOSTNEG INT)) = 0
              X := 1                  -- Z positive
            TRUE
              X := (MOSTNEG INT) + 1  -- Z negative
        --}}}
        --{{{  Y positive
        (Y /\ (MOSTNEG INT)) = 0
          IF
            Z > Y     -- Y +ve so can use integer ordering
              X := Y + 1
            Z < Y
              X := Y - 1
            TRUE
              X := Y
        --}}}
        --{{{  Y negative
        TRUE
          IF
            Z = Y
              X := Y
            (Z >< (MOSTNEG INT)) < (Y >< (MOSTNEG INT))  -- Z > Y as Reals as Y is -ve
              X := Y - 1  -- keep -0's if they arise
            TRUE
              X := Y + 1
        --}}}
    RESULT X
:
--}}}


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

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

#PRAGMA TRANSLATE NEXTAFTER "NEXTAFTER%O"

#INCLUDE "realvals.inc"

--{{{  T2
REAL32 FUNCTION NEXTAFTER (VAL REAL32 Y, Z)

  --{{{  history
  -- Function version (24 Jun 87)
  -- V1.1, 21-Feb-89
  -- Removed the code that changed -0's into +0's for Y
  -- negative; V1.2, 10/07/90 SRH
  --}}}

  REAL32 X :
  VALOF
    --{{{  VALS FOR 16 bit machine
    VAL SignBit   IS #80000000(INT32) :
    --}}}
    [2]INT X RETYPES X:
    VAL [2]INT Y RETYPES Y:
    VAL [2]INT Z RETYPES Z:
    INT frac1, frac2, exp1, exp2:
    SEQ
      exp1, frac1 := SHIFTLEFT (0, (Y[1] << 1), RealShift)
      exp2, frac2 := SHIFTLEFT (0, (Z[1] << 1), RealShift)
      IF
        (exp1 = RealExp) AND ((frac1 \/ Y[0]) <> 0)
          --{{{  at least one NaN
          IF
            (exp2 = RealExp) AND ((frac2 \/ Z[0]) <> 0) -- 2 NaNs
              --{{{  precedence rule for NaNs
              IF
                frac1 > frac2
                  X := Y
                frac2 > frac1
                  X := Z
                Y[1] > Z[1]
                  X := Y
                TRUE -- NaNs are equal, or Z is greater
                  X := Z
              --}}}
            TRUE
              X := Y
          --}}}
        (exp2 = RealExp) AND ((frac2 \/ Z[0]) <> 0)
          X := Z
        INT32 X RETYPES X:
        VAL INT32 Y RETYPES Y:
        VAL INT32 Z RETYPES Z:
        IF
          --{{{  Y zero
          (Y << 1) = 0(INT32)
            IF
              (Z << 1) = 0(INT32)
                X := Y                   -- Z zero; need to keep sign of the 0
              (Z /\ SignBit) = 0(INT32)
                X := 1 (INT32)           -- Z positive
              TRUE
                X := SignBit + 1(INT32)  -- Z negative
          --}}}
          --{{{  Y positive
          (Y /\ SignBit) = 0(INT32)
            IF
              Z > Y     -- Y +ve so can use integer ordering
                X := Y + 1(INT32)
              Z < Y
                X := Y - 1(INT32)
              TRUE
                X := Y
          --}}}
          --{{{  Y negative
          TRUE
            IF
              Z = Y
                X := Y
              (Z >< SignBit) < (Y >< SignBit)  -- Z > Y as Reals as Y is -ve
                X := Y - 1(INT32)  -- keep -0's if they arise
              TRUE
                X := Y + 1(INT32)
          --}}}
    RESULT X
:
--}}}

#ENDIF
