	.file "log.s"
/*
 * $Copyright:	$
 * Copyright (c) 1984, 1985, 1986, 1987, 1988, 1989, 1990 
 * Sequent Computer Systems, Inc.   All rights reserved.
 *  
 * This software is furnished under a license and may be used
 * only in accordance with the terms of that license and with the
 * inclusion of the above copyright notice.   This software may not
 * be provided or otherwise made available to, or used by, any
 * other person.  No title to or ownership of the software is
 * hereby transferred.
 *
 */

/* $Header: log.s 1.4 90/06/28 $
 *
 *		LOGARITHM FUNCTION
 *
 * Functional Description:
 *
 *	This routine implements the logarithm function for the C language.
 *	Both the natural log and log base 10 are computed in this routine.
 *	It uses the implicit fsqrt instruction of the 287 NPX. No error checking
 *	is performed since it is assumed that the 287 will correctly handle
 *	an illegal argument.
 *
 * Input:	Argument is located at 4(%esp)
 *
 * Output:	Double Precision value is returned in ST(0)
 *
 * Method:	IF (x != 0)
 *		logn(X) = logn(2) * log2(X)
 *		   where for small arguments (< 1-(sqrt(2)/2)
 *			use argument of (x-1) & the fy12xp1 instruction
 *
 * Created:
 *	05/23/86	MF - Quantitative Technology Corporation for
 *				Sequent Computer Systems
 *	09/04/86	Phil Hochstetler - converted to C library conventions
 *
 *	05/22/87	Gary Tracy - converted to stack model C interface
 *			  under this model, the caller is responsible for
 *			  saving and restoring any required 387 state.
 *			  this need not be done if:
 *				1.  the callee does no calls.
 *				2.  the sum of 387 registers used by the caller
 *				    and callee does not exceed 8.
 *
 */

#include "sys.s"

LOGERR1:
	.byte	0x6c,0x6f,0x67,0x00

LOGERR2:
	.byte	0x6c,0x6f,0x67,0x3a,0x20,0x44,0x4f,0x4d,0x41
	.byte	0x49,0x4e,0x20,0x65,0x72,0x72,0x6f,0x72,0x0a
	.byte	0x00

LOGERR3:
	.byte	0x6c,0x6f,0x67,0x3a,0x20,0x53,0x49,0x4e,0x47
	.byte	0x20,0x65,0x72,0x72,0x6f,0x72,0x0a,0x00
	.data

LOG10ERR1:
	.byte	0x6c,0x6f,0x67,0x31,0x30,0x00

LOG10ERR2:
	.byte	0x6c,0x6f,0x67,0x31,0x30,0x3a,0x20,0x44,0x4f,0x4d
	.byte	0x41,0x49,0x4e,0x20,0x65,0x72,0x72,0x6f,0x72,0x0a
	.byte	0x00

LOG10ERR3:
	.byte	0x6c,0x6f,0x67,0x31,0x30,0x3a,0x20,0x53,0x49
	.byte	0x4e,0x47,0x20,0x65,0x72,0x72,0x6f,0x72,0x0a
	.byte	0x00
	.data
	.align 2

ENTRY(log)
	movl	8(%esp), %eax
	testl	$ 0x80000000, %eax
	jnz	log_negative_arg
	orl	4(%esp), %eax
	jz	log_zero_arg
	fldln2
	fldl	4(%esp)
	fyl2x
	ret

ENTRY(log10)
	movl	8(%esp), %eax
	testl	$ 0x80000000, %eax
	jnz	log10_negative_arg
	orl	4(%esp), %eax
	jz	log10_zero_arg
	fldlg2
	fldl	4(%esp)
	fyl2x
	ret

log_negative_arg:
	fldl	4(%esp)				/load x in stack
	subl	$0x20,%esp			/allocate struct exception exp
	fstpl	0x8(%esp)			/exc.arg1 = x
	movl	$0x1,(%esp)			/exc.type = DOMAIN
	movl	$LOGERR1,0x4(%esp)		/exc.name = "log"
	fldl	_maxdouble			/exc.retval = -HUGE_VAL
	fchs
	fstpl	0x18(%esp)
	leal	(%esp),%eax			/ matherr(&erc)
	pushl	%eax
	call	matherr
	popl	%ecx
	testl	%eax,%eax
	jne	log_exit

	pushl	$0x12
	pushl	$LOGERR2
	pushl	$0x2
	call	write
	addl	$0xc,%esp
	movl	$0x21,errno
log_exit:
	fldl	0x18(%esp)
	addl	$0x20,%esp
	ret

log_zero_arg:
	fldl	4(%esp)				/load x in stack
	subl	$0x20,%esp			/allocate struct exception exp
	fstpl	0x8(%esp)			/exc.arg1 = x
	movl	$0x2,(%esp)			/exc.type = SING
	movl	$LOGERR1,0x4(%esp)		/exc.name = "log"
	fldl	_maxdouble			/exc.retval = -HUGE_VAL
	fchs
	fstpl	0x18(%esp)
	leal	(%esp),%eax			/ matherr(&erc)
	pushl	%eax
	call	matherr
	popl	%ecx
	testl	%eax,%eax
	jne	log_exit

	pushl	$0x10
	pushl	$LOGERR3
	pushl	$0x2
	call	write
	addl	$0xc,%esp
	movl	$0x21,errno
	jmp	log_exit

log10_negative_arg:
	fldl	4(%esp)				/load x in stack
	subl	$0x20,%esp			/allocate struct exception exp
	fstpl	0x8(%esp)			/exc.arg1 = x
	movl	$0x1,(%esp)			/exc.type = DOMAIN
	movl	$LOG10ERR1,0x4(%esp)		/exc.name = "log10"
	fldl	_maxdouble			/exc.retval = -HUGE_VAL
	fchs
	fstpl	0x18(%esp)
	leal	(%esp),%eax			/ matherr(&erc)
	pushl	%eax
	call	matherr
	popl	%ecx
	testl	%eax,%eax
	jne	log_exit

	pushl	$0x14
	pushl	$LOG10ERR2
	pushl	$0x2
	call	write
	addl	$0xc,%esp
	movl	$0x21,errno
	jmp	log_exit

log10_zero_arg:
	fldl	4(%esp)				/load x in stack
	subl	$0x20,%esp			/allocate struct exception exp
	fstpl	0x8(%esp)			/exc.arg1 = x
	movl	$0x2,(%esp)			/exc.type = SING
	movl	$LOG10ERR1,0x4(%esp)		/exc.name = "log10"
	fldl	_maxdouble			/exc.retval = -HUGE_VAL
	fchs
	fstpl	0x18(%esp)
	leal	(%esp),%eax			/ matherr(&erc)
	pushl	%eax
	call	matherr
	popl	%ecx
	testl	%eax,%eax
	jne	log_exit

	pushl	$0x12
	pushl	$LOG10ERR3
	pushl	$0x2
	call	write
	addl	$0xc,%esp
	movl	$0x21,errno
	jmp	log_exit



