	.file "asin.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: asin.s 1.2 89/10/10 $
 *
 *		ARCSINE FUNCTION
 *
 * Functional Description:
 *
 *	This routine implements the arcsine function for the C language.
 *
 * Input:	Argument is located at 4(%esp)
 *
 * Output:	Double Precision value is returned in ST(0)
 *
 * Method:
 *		for |x| <= 1
 *		
 *		asin(x) = atan2(x,sqrt(1-x*x))
 *
 *		    where for better accuracy when x >= 0.5
 *			  1-x*x = 2*(1-|x|)-(1-|x|)*(1-|x|)
 *
 * 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"

STR1:
	.byte	0x61,0x73,0x69,0x6e,0x00
STR2:
	.byte	0x61,0x73,0x69,0x6e,0x3a,0x20,0x44,0x4f,0x4d,0x41
	.byte	0x49,0x4e,0x20,0x65,0x72,0x72,0x6f,0x72,0x0a,0x00
	.data
	.align 2
	.text
half:	.double	0Dx3FE0000000000000	/0.5
	.set	maskC0C3,0x4100		/mask for C0 and C3
	.set	maskC0,0x0100		/mask for C0

ENTRY(asin)
	fldl	4(%esp)			/put argument onto stack [%st(2)]
	fld1				/put 1.0 onto stack
	fld	%st(1)			/put argument onto stack
	fabs				/|x| --> st(0)

	/Assert Registers have the following values
	/St(0) = |x|
	/St(1) = 1
	/St(2) = x

	fcom				/compare 1.0 and |x|
	fstsw	%ax			/store status of compare
	testw	$maskC0C3,%ax		/|x| <= 1.0?
	jne	asin_continue		/if so, continue

	subl	$0x20,%esp		/make room for struct execption exc

	fstp	%st(0)			/clear stack
	fstp	%st(0)
	fstp	%st(0)
	fldl	0x24(%esp)		/exc.arg1 = x
	fstpl	0x8(%esp)
	movl	$0x1,(%esp)		/exc.type = DOMAIN
	movl	$STR1,0x4(%esp)		/exc.name = "asin"
	fldz				/exc.retval = 0.0
	fstpl	0x18(%esp)
	leal	(%esp),%eax		/setup to do the call
	pushl	%eax
	call	matherr
	popl	%ecx
	testl	%eax,%eax
	jne	asin_exit

	pushl	$0x13			/length of error string
	pushl	$STR2			/ write "asin: DOMAIN error\n"
	pushl	$0x2			/ write it on stderr.
	call	write
	addl	$0xc,%esp
	movl	$0x21,errno
asin_exit:
	fldl	0x18(%esp)		/return exc.retval
	addl	$0x20,%esp
	ret

asin_continue:
	subl	$0x10,%esp		/make room for temp storage
	fldl	half			/put 0.5 onto stack
	fcomp				/compare |x| and 0.5
	fstsw	%ax			/store status of compare
	testw	$maskC0,%ax		/|x| > 0.5?
	jne	asin_accuracy		/if so, do more accurate computation
	fmul	%st,%st(0)		/form sqr(x)
	fsubr				/form 1 - sqr(x)
	jmp	asin_do_sqrt		/go take sqrt

/*			|x| > 0.5
 *    Compute 1 - x*x by:
 *		2*(1-|x|) - (1-|x|) * (1-|x|)
 */

asin_accuracy:
	fsubr				/form 1 - |x|
	fld	%st(0)			/make copy of 1 - |x|
	fmul	%st,%st(0)		/form (1-|x|) * (1-|x|)
	fxch	%st(1)			/put 1 - |x| back on top of stack
	fadd	%st,%st(0)		/form 2 * (1-|x|)
	fsub				/form (2*(1-|x|) - (1-|x|)*(1-|x|)

asin_do_sqrt:
	fsqrt				/form sqrt(1-sqr(x))
	fstpl	8(%esp)			/get second arg for atan2
	fstl	(%esp)			/get first arg for atan2
	call	atan2			/form atan2(x, sqrt(1-sqr(x)))
asin_return:
	fstp	%st(1)			/stack model requires empty stack
	addl	$0x10,%esp		/reset stack -- local storage
	ret
