POK
/home/jaouen/pok_official/pok/trunk/libpok/libm/e_sqrtf.c
00001 /*
00002  *                               POK header
00003  * 
00004  * The following file is a part of the POK project. Any modification should
00005  * made according to the POK licence. You CANNOT use this file or a part of
00006  * this file is this part of a file for your own project
00007  *
00008  * For more information on the POK licence, please see our LICENCE FILE
00009  *
00010  * Please follow the coding guidelines described in doc/CODING_GUIDELINES
00011  *
00012  *                                      Copyright (c) 2007-2009 POK team 
00013  *
00014  * Created by julien on Fri Jan 30 14:41:34 2009 
00015  */
00016 
00017 /* e_sqrtf.c -- float version of e_sqrt.c.
00018  * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com.
00019  */
00020 
00021 /*
00022  * ====================================================
00023  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
00024  *
00025  * Developed at SunPro, a Sun Microsystems, Inc. business.
00026  * Permission to use, copy, modify, and distribute this
00027  * software is freely granted, provided that this notice
00028  * is preserved.
00029  * ====================================================
00030  */
00031 
00032 #ifdef POK_NEEDS_LIBMATH
00033 #include <types.h>
00034 #include "math_private.h"
00035 
00036 static  const float     one     = 1.0, tiny=1.0e-30;
00037 
00038 float
00039 __ieee754_sqrtf(float x)
00040 {
00041         float z;
00042         int32_t sign = (int)0x80000000;
00043         int32_t ix,s,q,m,t,i;
00044         uint32_t r;
00045 
00046         GET_FLOAT_WORD(ix,x);
00047 
00048     /* take care of Inf and NaN */
00049         if((ix&0x7f800000)==0x7f800000) {
00050             return x*x+x;               /* sqrt(NaN)=NaN, sqrt(+inf)=+inf
00051                                            sqrt(-inf)=sNaN */
00052         }
00053     /* take care of zero */
00054         if(ix<=0) {
00055             if((ix&(~sign))==0) return x;/* sqrt(+-0) = +-0 */
00056             else if(ix<0)
00057                 return (x-x)/(x-x);             /* sqrt(-ve) = sNaN */
00058         }
00059     /* normalize x */
00060         m = (ix>>23);
00061         if(m==0) {                              /* subnormal x */
00062             for(i=0;(ix&0x00800000)==0;i++) ix<<=1;
00063             m -= i-1;
00064         }
00065         m -= 127;       /* unbias exponent */
00066         ix = (ix&0x007fffff)|0x00800000;
00067         if(m&1) /* odd m, double x to make it even */
00068             ix += ix;
00069         m >>= 1;        /* m = [m/2] */
00070 
00071     /* generate sqrt(x) bit by bit */
00072         ix += ix;
00073         q = s = 0;              /* q = sqrt(x) */
00074         r = 0x01000000;         /* r = moving bit from right to left */
00075 
00076         while(r!=0) {
00077             t = s+r;
00078             if(t<=ix) {
00079                 s    = t+r;
00080                 ix  -= t;
00081                 q   += r;
00082             }
00083             ix += ix;
00084             r>>=1;
00085         }
00086 
00087     /* use floating add to find out rounding direction */
00088         if(ix!=0) {
00089             z = one-tiny; /* trigger inexact flag */
00090             if (z>=one) {
00091                 z = one+tiny;
00092                 if (z>one)
00093                     q += 2;
00094                 else
00095                     q += (q&1);
00096             }
00097         }
00098         ix = (q>>1)+0x3f000000;
00099         ix += (m <<23);
00100         SET_FLOAT_WORD(z,ix);
00101         return z;
00102 }
00103 #endif
00104