/*
 * $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.
 *
 */

#ident	"$Header: fmod.c 1.6 90/06/28 $"

/*LINTLIBRARY*/

/* fmod(x,y)
 * Return f =x-n*y, for some integer n, where f has same sign
 * as x and |f| < |y|
 *
 *  for y == 0, f = 0 
 */

#include <math.h>
#include <errno.h>

asm void
afmod(x, y)
{
%mem	x,y;
	fldl	y
	movl	x,%eax
	fldl	(%eax)
.loop:
	fprem
	fstsw	%ax
	testl	$0x400,%eax
	jne	.loop
	movl	x,%eax
	fstpl	(%eax)
	fstp	%st(0)
}

double	fmod(x, y)
double	x, y;
{
	double *dptr = &x;

	if (y == 0.0) {
		errno = EDOM;
		return 0.0;
	}
	afmod(dptr,y);
	return x;
}
