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

#OPTION "V"  -- no vector space
#COMMENT "Floating point maths library, V1.1 27/03/90"
#COMMENT "(c) Copyright INMOS Limited 1988"
#INCLUDE "mathhdr.inc"

--{{{  history
-- V1.0 1-Jul-88
-- Added OPTION "V"; V1.1 27-Mar-90 SRH
--}}}
--{{{  ALOG
--* Single-precision natural logarithm.
--
-- @param X Input value
-- @return Natural logarithm of X
REAL32 FUNCTION ALOG (VAL REAL32 X)

  REAL32 RWork :
  VALOF
    IF
      NOTFINITE (X)
        --{{{  NaN or Inf
        IF
          ISNAN (X)
            RWork := X
          TRUE -- must be inf
            RWork := undefined.NaN
        --}}}
      (X <= Zero) -- X is negative
        RWork := undefined.NaN
      TRUE
        REAL32 XWork :
        INT N :
        SEQ
          N, XWork := FLOATING.UNPACK (X)
          XWork := DIVBY2 (XWork) -- to produce fraction in [0.5, 1.0)
          --{{{  Reduced Log
          VAL REAL32 C0 RETYPES #3F3504F3(INT32) : -- SQRT(0.5) in floating point
          VAL A0 IS -5.527074855E-1(REAL32) :
          VAL B0 IS -6.632718214(REAL32) :
          REAL32 Z :
          SEQ
            IF
              XWork > C0
                SEQ
                  Z := ((XWork - Half) - Half) / ((XWork * Half) + Half)
                  N := N + 1             -- to ensure exponent corresponds
              TRUE
                VAL ZNum IS XWork - Half :
                VAL ZDen IS (ZNum * Half) + Half :
                Z := ZNum / ZDen
            VAL W IS Z * Z :
            RWork := Z + (Z * ((W * A0)/(W + B0)))
          --}}}
          IF
            N = 0
              SKIP
            TRUE
              RWork := REAL32 ROUND((REAL64 RWork) + ((REAL64 ROUND N) * DLn2))
    RESULT RWork
:
--}}}
--{{{  ALOG10
--* Single-precision base-10 logarithm.
--
-- @param X Input value
-- @return Base-10 logarithm of X
REAL32 FUNCTION ALOG10 (VAL REAL32 X)

  REAL32 NLog :
  VALOF
    SEQ
      NLog := ALOG (X)
      IF
        ISNAN (NLog)
          SKIP
        TRUE
          -- LogE = 0.43429449200630187988281250 which is closest IEEE single
          -- precision representation to the exact 0.43429448190325182765...
          VAL REAL32 LogE RETYPES #3EDE5BD9(INT32) :
          NLog := NLog * LogE
    RESULT NLog
:
--}}}
