Blame sysdeps/ieee754/flt-32/e_sqrtf.c

Packit 6c4009
/* e_sqrtf.c -- float version of e_sqrt.c.
Packit 6c4009
 * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com.
Packit 6c4009
 */
Packit 6c4009
Packit 6c4009
/*
Packit 6c4009
 * ====================================================
Packit 6c4009
 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
Packit 6c4009
 *
Packit 6c4009
 * Developed at SunPro, a Sun Microsystems, Inc. business.
Packit 6c4009
 * Permission to use, copy, modify, and distribute this
Packit 6c4009
 * software is freely granted, provided that this notice
Packit 6c4009
 * is preserved.
Packit 6c4009
 * ====================================================
Packit 6c4009
 */
Packit 6c4009
Packit 6c4009
#include <math.h>
Packit 6c4009
#include <math_private.h>
Packit 6c4009
Packit 6c4009
static	const float	one	= 1.0, tiny=1.0e-30;
Packit 6c4009
Packit 6c4009
float
Packit 6c4009
__ieee754_sqrtf(float x)
Packit 6c4009
{
Packit 6c4009
	float z;
Packit 6c4009
	int32_t sign = (int)0x80000000;
Packit 6c4009
	int32_t ix,s,q,m,t,i;
Packit 6c4009
	uint32_t r;
Packit 6c4009
Packit 6c4009
	GET_FLOAT_WORD(ix,x);
Packit 6c4009
Packit 6c4009
    /* take care of Inf and NaN */
Packit 6c4009
	if((ix&0x7f800000)==0x7f800000) {
Packit 6c4009
	    return x*x+x;		/* sqrt(NaN)=NaN, sqrt(+inf)=+inf
Packit 6c4009
					   sqrt(-inf)=sNaN */
Packit 6c4009
	}
Packit 6c4009
    /* take care of zero */
Packit 6c4009
	if(ix<=0) {
Packit 6c4009
	    if((ix&(~sign))==0) return x;/* sqrt(+-0) = +-0 */
Packit 6c4009
	    else if(ix<0)
Packit 6c4009
		return (x-x)/(x-x);		/* sqrt(-ve) = sNaN */
Packit 6c4009
	}
Packit 6c4009
    /* normalize x */
Packit 6c4009
	m = (ix>>23);
Packit 6c4009
	if(m==0) {				/* subnormal x */
Packit 6c4009
	    for(i=0;(ix&0x00800000)==0;i++) ix<<=1;
Packit 6c4009
	    m -= i-1;
Packit 6c4009
	}
Packit 6c4009
	m -= 127;	/* unbias exponent */
Packit 6c4009
	ix = (ix&0x007fffff)|0x00800000;
Packit 6c4009
	if(m&1)	/* odd m, double x to make it even */
Packit 6c4009
	    ix += ix;
Packit 6c4009
	m >>= 1;	/* m = [m/2] */
Packit 6c4009
Packit 6c4009
    /* generate sqrt(x) bit by bit */
Packit 6c4009
	ix += ix;
Packit 6c4009
	q = s = 0;		/* q = sqrt(x) */
Packit 6c4009
	r = 0x01000000;		/* r = moving bit from right to left */
Packit 6c4009
Packit 6c4009
	while(r!=0) {
Packit 6c4009
	    t = s+r;
Packit 6c4009
	    if(t<=ix) {
Packit 6c4009
		s    = t+r;
Packit 6c4009
		ix  -= t;
Packit 6c4009
		q   += r;
Packit 6c4009
	    }
Packit 6c4009
	    ix += ix;
Packit 6c4009
	    r>>=1;
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
    /* use floating add to find out rounding direction */
Packit 6c4009
	if(ix!=0) {
Packit 6c4009
	    z = one-tiny; /* trigger inexact flag */
Packit 6c4009
	    if (z>=one) {
Packit 6c4009
		z = one+tiny;
Packit 6c4009
		if (z>one)
Packit 6c4009
		    q += 2;
Packit 6c4009
		else
Packit 6c4009
		    q += (q&1;;
Packit 6c4009
	    }
Packit 6c4009
	}
Packit 6c4009
	ix = (q>>1)+0x3f000000;
Packit 6c4009
	ix += (m <<23);
Packit 6c4009
	SET_FLOAT_WORD(z,ix);
Packit 6c4009
	return z;
Packit 6c4009
}
Packit 6c4009
strong_alias (__ieee754_sqrtf, __sqrtf_finite)