	.file "tan.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: tan.s 1.6 89/10/10 $
 *
 *		TAN FUNCTION
 *
 * Functional Description:
 *
 *	This routine implements the tangent function for the C language.
 *	It uses the fptan 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)
 *	        Assumed that the argument is normal.
 *
 * Output:	Double Precision value is returned in ST(0)
 *
 * Assumptions:  ST(0) & ST(1) are scratch registers
 *		Input argument is normal.
 *
 * Created:
 *	05/28/86	GS - 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.
 *
 * Method:
 *	This routine uses the fptan instruction to calculate the tangent.
 *	The fptan instruction requires arguments in the range 0 <= x <= pi/4.
 *	The relationship between fptan and the tangent depends on the 
 *	octant in which the angle lies and is as follows:
 *
 *		where K = angle mod pi/4
 *
 *	      Octant		        Relationship
 *				    (Negate if the angle < 0)
 *	
 *		0			    tan(K)	
 *		1			1/tan(pi/4 - K)
 *		2			  -1/tan(K)
 *		3			-tan(pi/4 - K)
 *		4			    tan(K)	
 *		5			1/tan(pi/4 - K)
 *		6			  -1/tan(K)
 *		7			-tan(pi/4 - K)
 *
 */

#include "sys.s"

	.set mask_cond0,0x01
	.set mask_cond1,0x02
	.set mask_Scond13,0x0240
	.set mask_cond13,0x42

TANERR1:
	.byte	0x74,0x61,0x6e,0x00

TANERR2:
	.byte	0x74,0x61,0x6e,0x3a,0x20,0x54,0x4c,0x4f,0x53,0x53
	.byte	0x20,0x65,0x72,0x72,0x6f,0x72,0x0a,0x00
	.data
	.align 2

ENTRY(tan)

	fldl	libm_quarter_pi
	fldl	4(%esp)			/put argument onto stack
       		
        fxam				/ Examine the operand
	fstsw 	%ax			/ Save S.W. to determine sign of output
	movw	%ax,%dx			/ Store the S.W. of fxam in dx
	sahf				/ Set the flags from the fxam
/*
 *    This would be the place to add a funny parameter test
 */
	jnz 	calc_tan		/ If Angle != 0 then calc the tan 
					/ Else
	jmp	tan_return		/    tan(0) = 0 so return 0
/*
 *    Now we have  	ST(0) = Angle  (Angle != 0)
 *    			ST(1) = PI/4
 *                    	Status word from fxam in dx
 */
calc_tan:

	fld	%st			/ copy arg
	fabs				/ see if abs(x) > X_TLOSS/2
	fcompl	libm_half_tloss		/ check for to big an arg
	fstsw	%ax
	sahf
	ja	tan_tloss_err
	
	fprem				/ Angle mod PI/4

	fstsw	%ax			/ Get the fprem status word
        movw 	%ax,%cx			/ Store the fprem S.W. into cx

		/ Assert -PI/4 < ST(0) < PI/4

	fabs				/ Now 0 < ST(0) < PI/4

	testb	$mask_cond1,%ch		/ If fprem(a) C1 is cleared then
	jz	no_tan_reverse		/    Angle OK as is
					/ Else

		/ Angle in octants 1,3,5,7. 

	fsubr				/    Reverse the angle
	jmp	do_tangent		/ Calculate the tangent


no_tan_reverse:
                / Angle in octants 0,2,4,6

	fstp	%st(1)			/ Pop to balance the stack
	ftst				/ Check for angle of zero
	fstsw	%ax			/ Get results of ftst
	sahf				/ Set flags from test results
	jnz	do_tangent		/ If angle!=0 then use fptan 
					/ Else   simulate fptan

tan_zero:				/ Just return 0

	jmp	tan_return

		/ Calculate the tangent
do_tangent:

	fptan				/ tan(st(0)) = st(1)/st(0)

after_tangent:
	/ Decide on the order and sign of the operands for the final division.

	movb	%ch,%dl			/ If C1 or C3 of fxam(a) are set then 
	andw	$mask_Scond13,%edx
	testb	$mask_cond13,%ch
	jpo	reverse_divide		/     need to reverse divide
					/ Else 
	/Angle was in octants 0,3,4,7	

	orb 	%dl,%dh			
	jpe 	positive_divide		/    If sign of result will be neg then
	fchs				/	Force result negative
					/    Else
positive_divide:
					/	Do positive divide

	/fdivr	%st(1),%st(0)			/ Do the final division
	.byte	0xd8,0xf9
	jmp	tan_return
	
		/ Angle was in octants 1,2,5,6

reverse_divide:

	orb	%dl,%dh			/ If Result should be positive then
	jpe	positive_r_divide	/     Do positive Reverse Divide
					/ Else
	fchs				/     Force the result negative

positive_r_divide:
	
	/fdiv	%st(1),%st(0)		/    Do the Reverse Division
	.byte	0xd8,0xf1

tan_return: / Assert that result is in st(0), which is the original st(1) 
	fstp	%st(1)		/stack model conversion requires empty stack
	fldl	0x4(%esp)	/check for ploss
	fabs
	fcompl	libm_half_ploss
	fstsw	%ax
	sahf
	ja	tan_ploss_err
	ret

tan_tloss_err:
	subl	$0x20,%esp		/allocate struct exception exc
	fstpl	0x8(%esp)		/exc.arg1 = x
	fstp	%st
	movl	$0x5,(%esp)		/exc.type = TLOSS
	movl	$TANERR1,0x4(%esp)	/exc.name = "tan"
	fldz
	fstpl	0x18(%esp)		/exc.retval = 0
	leal	(%esp),%eax
	push	%eax
	call	matherr
	popl	%ecx
	testl	%eax,%eax
	jne	tan_exit

	pushl	$0x11
	pushl	$TANERR2
	pushl	$0x2
	call	write
	addl	$0xc,%esp
	movl	$0x22,errno
	jmp	tan_exit
tan_exit:
	fldl	0x18(%esp)
	addl	$0x20,%esp
	ret

tan_ploss_err:
	subl	$0x20,%esp
	fldl	0x24(%esp)		/load x
	fstpl	0x8(%esp)		/exc.arg1 = x
	movl	$0x6,(%esp)		/exc.type = PLOSS
	movl	$TANERR1,0x4(%esp)	/exc.name = "tan"
	fstpl	0x18(%esp)		/exc.retval = tan(x)
	leal	(%esp),%eax
	pushl	%eax
	call	matherr
	popl	%ecx
	testl	%eax,%eax
	jne	tan_exit
	movl	$0x22,errno
	jmp	tan_exit
