	.file	"atan.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: atan.s 1.2 89/10/10 $
 *
 *		ARC TANGENT FUNCTION
 *
 * Functional Description:
 *
 *	This routine implements the arctangent function for the C language.
 *	Both atan and atan2 are computed in this routine.
 *	It uses the implicit fpatan instruction of the 287 NPX.
 *
 * Input:	Argument is located at 4(%esp)
 *		For ATAN2, second argument is located at 12(%esp)
 *
 * Output:	Double Precision value is returned in ST(0)
 *
 * Method:
 *		Uses the FPATAN instruction to calculate atan & atan2.
 *		Check for errors where the arguments are zero.
 *
 *		where fpatan = arctan(st(1)/st)
 *		let x = st  & y = st(1)
 *		sign(y)	   sign(x)   |y| < |x|		final result
 *	       ________  __________  ____________      __________________
 *		  +	     +	       yes		 atan(y/x)
 *		  +	     +	       no		 Pi/2-atan(x/y)
 *		  +	     -	       yes		 Pi-atan(y/-x)
 *		  +	     -	       no		 Pi/2+atan(x/y)
 *		  -	     +	       yes		 atan(-y/x)
 *		  -	     +	       no		 -Pi/2+atan(x/-y)
 *		  -	     +	       yes		 -Pi+atan(-y/-x)
 *		  -	     +	       no		 -Pi/2-atan(-x/-y)
 *		
 *
 * Created:
 *	05/28/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"

	.data
	.align 4
ZERO:
	.double	0Dx0000000000000000
	.align	4
STR1:
	.byte	0x61,0x74,0x61,0x6e,0x32,0x00
STR2:
	.byte	0x61,0x74,0x61,0x6e,0x32,0x3a,0x20,0x44,0x4f,0x4d
	.byte	0x41,0x49,0x4e,0x20,0x65,0x72,0x72,0x6f,0x72,0x0a
	.byte	0x00
	.data
	.align 2

ENTRY(atan)
	fldl	4(%esp)
	fld1
	fpatan
	ret

ENTRY(atan2)
	fldl	0x4(%esp)			/make sure x and y != 0
	fcompl	ZERO
	fstsw	%ax
	sahf	
	jne	atan2_continue			/ x != 0 go continue
	fldl	0xc(%esp)
	fcompl	ZERO
	fstsw	%ax
	sahf
	jne	atan2_continue			/ y != 0 continue

	fldl	0x4(%esp)			/load x
	fldl	0xc(%esp)			/load y
	subl	$0x20,%esp			/allocate struct exception exc
	
	fstpl	0x10(%esp)			/ exc.arg2 = x
	fstpl	0x8(%esp)			/ exc.arg1 = y
	movl	$0x1,(%esp)			/ exc.type = DOMAIN
	movl	$STR1,0x4(%esp)			/ exc.name = "atan2"
	fldz					/ exc.retval = 0.0
	fstpl	0x18(%esp)

	leal	(%esp),%eax			/ if (!matherr(&erc))
	pushl	%eax
	call	matherr
	popl	%ecx
	testl	%eax,%eax
	jne	atan2_exit

	pushl	$0x14				/ write out an error msg
	pushl	$STR2
	pushl	$0x2
	call	write
	addl	$0xc,%esp
	movl	$0x21,errno
atan2_exit:
	fldl	0x18(%esp)
	addl	$0x20,%esp
	ret

atan2_continue:
	fldl	4(%esp)
	fldl	12(%esp)
	fpatan
	ret
