	.file "sqrt.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: sqrt.s 1.2 89/10/10 $
 *
 *
 *		SQUARE ROOT FUNCTION
 *
 * Functional Description:
 *
 *	This routine implements the square root function for the C language.
 *	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)
 *		Integer value is returned in %eax
 *
 * Created:
 *	05/20/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"

SQRTERR1:
	.byte	0x73,0x71,0x72,0x74,0x00

SQRTERR2:
	.byte	0x73,0x71,0x72,0x74,0x3a,0x20,0x44,0x4f,0x4d,0x41
	.byte	0x49,0x4e,0x20,0x65,0x72,0x72,0x6f,0x72,0x0a,0x00
	.data
	.align 2

ENTRY(sqrt)
	testb	$ 0x80, 11(%esp)
	jnz	sqrt_negative_arg
	fldl	4(%esp)			/put argument into st(0)
	fsqrt				/compute sqrt(arg)
	ret

sqrt_negative_arg:
	fldl	4(%esp)			/put x into st(0)
	subl	$0x20,%esp		/allocate struct execption exc
	fstpl	0x8(%esp)		/exc.arg1 = x
	movl	$0x1,(%esp)		/exc.type = DOMAIN
	movl	$SQRTERR1,0x4(%esp)	/exc.name = "sqrt"
	fldz				/exc.retval = 0.0
	fstpl	0x18(%esp)
	leal	(%esp),%eax
	pushl	%eax
	call	matherr
	popl	%ecx
	testl	%eax,%eax
	jne	sqrt_exit

	pushl	$0x13
	pushl	$SQRTERR2
	pushl	$0x2
	call	write
	addl	$0xc,%esp
	movl	$0x21,errno
sqrt_exit:
	fldl	0x18(%esp)
	addl	$0x20,%esp
	ret
