/*	Copyright (c) 1984, 1986, 1987, 1988 AT&T	*/
/*	  All Rights Reserved  	*/

/*	THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF AT&T	*/
/*	The copyright notice above does not evidence any   	*/
/*	actual or intended publication of such source code.	*/

#ident	"@(#)libc-i386:libc-i386/gen/frexp.c	1.3.1.1"
/*LINTLIBRARY*/
/*
 * frexp(value, eptr)
 * returns a double x such that x = 0 or 0.5 <= |x| < 1.0
 * and stores an integer n such that value = x * 2 ** n
 * indirectly through eptr.
 *
 */
/*
#include "shlib.h"
*/
#include <nan.h>

asm	double
xfrexp(val,contwo,ptr)
{
%mem	val,contwo,ptr;
	fldl	val
	/ test for unnormal or denormal
	fxam
	fstsw	%ax
	/ if ((flags&(C0|C2|C3)) ||
	/    ((flags&C0)==0 && (flags&(CW|C3))==(C2|C3))) un/denormal
	andw	$0x4500,%ax
	je	.notnorm
	cmpw	$0x4400,%ax
	jne	.norm
.notnorm:
	fxtract / mantissa->ST0, exp->ST1
	fld1	/ get a 1.0
	fadd	%st,%st(1) / add it to de/unnormal
	fsubrp	%st,%st(1) / subtract 1 from 1.xxx
	/ now get additional exponent and sum with saved part
	fxtract
	fxch
	faddp	%st,%st(2)
	/ we now have x * 2^n, with x in st0 and n in st1
	fscale
	fxch
	fstp	%st(0)
.norm:
	movl	ptr,%eax
	fxtract
	fxch	%st(1)
	fistpl	(%eax)
	fidiv	contwo
	incl	(%eax)
}

double
frexp(value, eptr)
double value; /* don't declare register, because of KILLNan! */
int *eptr;
{
	static int contwo = 2;

	KILLNaN(value); /* raise exception on Not-a-Number (3b only) */
	*eptr = 0;
	if (value == 0.0) /* nothing to do for zero */
		return (value);
	return(xfrexp(value,contwo,eptr));
}
