/*------------------------------------------------------------------------
 * filename - log.cas
 *
 * function(s)
 *        log - natural logarithm function
 *-----------------------------------------------------------------------*/

/*
 *      C/C++ Run Time Library - Version 5.0
 *
 *      Copyright (c) 1987, 1992 by Borland International
 *      All Rights Reserved.
 *
 */


#pragma inline
#include <asmrules.h>

#include <_math.h>
#include <math.h>
#include <errno.h>
#include <stddef.h>

static  unsigned short   NANLOG [4] = {0,0,0x0480, 0xFFF8};

/*--------------------------------------------------------------------------*

Name            log - natural logarithm function

Usage           double log(double x);

Prototype in    math.h

Description     log calculates  the natural logarithm  of x, which  must be
                greater than zero.

Return value    log  returns the  natural logarithm   of x,  which must  be
                greater than zero.

*---------------------------------------------------------------------------*/
#pragma warn -rvl
#pragma warn -use

double _FARFUNC log( double  x )
{
double  temp;

asm     FLD     DOUBLE (x)

asm     mov     ax, W0 (x [6])          /* get the exponent field */
asm     shl     ax, 1
asm     jz      log_zero
asm     jc      log_imaginary
asm     cmp     ax, 0FFE0h
asm     je      log_infinite

#ifdef _Windows
        _f87_Log();
#else
asm     _FAST_  (_FLOG_)
#endif

log_end:
        return;

log_zero:
asm     mov     si, SING
        temp = -HUGE_VAL;
asm     jmp     short   log_complain

log_infinite:
asm     mov     si, OVERFLOW
        temp = HUGE_VAL;
asm     jmp     short   log_complain

log_imaginary:
asm     mov     si, DOMAIN
        temp = *((double *) NANLOG);

log_complain:
asm     FSTP    ST(0)                   /* pop x from stack */
#pragma warn -ret
        return  _matherr (_SI, "log", &x, NULL, temp);
#pragma warn .ret
}
#pragma warn .rvl
#pragma warn .use
