	.file "sincos.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: sincos.s 1.4 89/10/10 $
 *
 *		SINE & COSINE FUNCTIONS
 *
 * Functional Description:
 *
 *	This routine implements the sine & cosine functions 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/27/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:
 *
 *
 *	The Sine and Cosine functions are calculated using the fsin &
 *	fcos instructions.  If the x > 2^63 then an error is returned.
 */

#include "sys.s"

SINERR1:
	.byte	0x73,0x69,0x6e,0x00

SINERR2:
	.byte	0x73,0x69,0x6e,0x3a,0x20,0x54,0x4c,0x4f,0x53,0x53
	.byte	0x20,0x65,0x72,0x72,0x6f,0x72,0x0a,0x00

COSERR1:
	.byte	0x63,0x6f,0x73,0x00

COSERR2:
	.byte	0x63,0x6f,0x73,0x3a,0x20,0x54,0x4c,0x4f,0x53,0x53
	.byte	0x20,0x65,0x72,0x72,0x6f,0x72,0x0a,0x00
	.data
	.align 2

ENTRY(cos)
	fldl	4(%esp)		/ put arg on the stack
	fabs			/ see if abs(x) > X_TLOSS
	fcompl	libm_tloss
	fstsw	%ax
	sahf			/Set flags for the test
	ja	cos_tloss_err
	fldl	4(%esp)		/ put arg on the stack
	fcos
	/.byte	0xd9, 0xff	/ ASM won't do FCOS
	fldl	4(%esp)		/ put arg back on the stack
	fabs			/ see if abs(x) > X_PLOSS
	fcompl	libm_ploss
	fstsw	%ax
	sahf			/Set flags for the test
	ja	cos_ploss_err
	ret

ENTRY(sin)
	fldl	4(%esp)
	fabs			/ see if abs(x) > X_TLOSS
	fcompl	libm_tloss
	fstsw	%ax
	sahf			/ Set flags for the test
	ja	sin_tloss_err
	fldl	4(%esp)		/ put arg on the stack
	fsin
	/.byte	0xd9, 0xfe	/ ASM won't do FSIN
	fldl	4(%esp)		/ put arg back on the stack
	fabs			/ see if abs(x) > X_PLOSS
	fcompl	libm_ploss
	fstsw	%ax
	sahf			/ Set flags for test
	ja	sin_ploss_err
	ret

cos_tloss_err:
	subl	$0x20,%esp		/ make space for struct exec
	fldl	0x24(%esp)		/ load x
	fstpl	0x8(%esp)		/ exc.arg1 = x
	movl	$0x5,(%esp)		/ exc.type = TLOSS
	movl	$COSERR1,0x4(%esp)	/ exc.name = "cos"
	fldz
	fstpl	0x18(%esp)		/ exc.retval = 0
	leal	(%esp),%eax
	pushl	%eax
	call	matherr
	popl	%ecx
	testl	%eax,%eax
	jne	cos_exit

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

sin_tloss_err:
	subl	$0x20,%esp		/ make space for struct except
	fldl	0x24(%esp)		/ load x
	fstpl	0x8(%esp)		/ exc.arg1 = x
	movl	$0x5,(%esp)		/ exc.type = TLOSS
	movl	$SINERR1,0x4(%esp)	/ exc.name = "sin"
	fldz
	fstpl	0x18(%esp)		/ exc.retval = 0
	leal	(%esp),%eax
	pushl	%eax
	call	matherr
	popl	%ecx
	testl	%eax,%eax
	jne	sin_exit

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

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

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