POK
/home/jaouen/pok_official/pok/trunk/libpok/libm/k_tanf.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 Sat Jan 31 20:12:07 2009 
00015  */
00016 
00017 /* k_tanf.c -- float version of k_tan.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 
00034 #include <libm.h>
00035 #include "math_private.h"
00036 static const float
00037 one   =  1.0000000000e+00, /* 0x3f800000 */
00038 pio4  =  7.8539812565e-01, /* 0x3f490fda */
00039 pio4lo=  3.7748947079e-08, /* 0x33222168 */
00040 T[] =  {
00041   3.3333334327e-01, /* 0x3eaaaaab */
00042   1.3333334029e-01, /* 0x3e088889 */
00043   5.3968254477e-02, /* 0x3d5d0dd1 */
00044   2.1869488060e-02, /* 0x3cb327a4 */
00045   8.8632395491e-03, /* 0x3c11371f */
00046   3.5920790397e-03, /* 0x3b6b6916 */
00047   1.4562094584e-03, /* 0x3abede48 */
00048   5.8804126456e-04, /* 0x3a1a26c8 */
00049   2.4646313977e-04, /* 0x398137b9 */
00050   7.8179444245e-05, /* 0x38a3f445 */
00051   7.1407252108e-05, /* 0x3895c07a */
00052  -1.8558637748e-05, /* 0xb79bae5f */
00053   2.5907305826e-05, /* 0x37d95384 */
00054 };
00055 
00056 float
00057 __kernel_tanf(float x, float y, int iy)
00058 {
00059         float z,r,v,w,s;
00060         int32_t ix,hx;
00061         GET_FLOAT_WORD(hx,x);
00062         ix = hx&0x7fffffff;     /* high word of |x| */
00063         if(ix<0x31800000)                       /* x < 2**-28 */
00064             {if((int)x==0) {                    /* generate inexact */
00065                 if((ix|(iy+1))==0) return one/fabsf(x);
00066                 else return (iy==1)? x: -one/x;
00067             }
00068             }
00069         if(ix>=0x3f2ca140) {                    /* |x|>=0.6744 */
00070             if(hx<0) {x = -x; y = -y;}
00071             z = pio4-x;
00072             w = pio4lo-y;
00073             x = z+w; y = 0.0;
00074         }
00075         z       =  x*x;
00076         w       =  z*z;
00077     /* Break x^5*(T[1]+x^2*T[2]+...) into
00078      *    x^5(T[1]+x^4*T[3]+...+x^20*T[11]) +
00079      *    x^5(x^2*(T[2]+x^4*T[4]+...+x^22*[T12]))
00080      */
00081         r = T[1]+w*(T[3]+w*(T[5]+w*(T[7]+w*(T[9]+w*T[11]))));
00082         v = z*(T[2]+w*(T[4]+w*(T[6]+w*(T[8]+w*(T[10]+w*T[12])))));
00083         s = z*x;
00084         r = y + z*(s*(r+v)+y);
00085         r += T[0]*s;
00086         w = x+r;
00087         if(ix>=0x3f2ca140) {
00088             v = (float)iy;
00089             return (float)(1-((hx>>30)&2))*(v-(float)2.0*(x-(w*w/(w+v)-r)));
00090         }
00091         if(iy==1) return w;
00092         else {          /* if allow error up to 2 ulp,
00093                            simply return -1.0/(x+r) here */
00094      /*  compute -1.0/(x+r) accurately */
00095             float a,t;
00096             int32_t i;
00097             z  = w;
00098             GET_FLOAT_WORD(i,z);
00099             SET_FLOAT_WORD(z,i&0xfffff000);
00100             v  = r-(z - x);     /* z+v = r+x */
00101             t = a  = -(float)1.0/w;     /* a = -1.0/w */
00102             GET_FLOAT_WORD(i,t);
00103             SET_FLOAT_WORD(t,i&0xfffff000);
00104             s  = (float)1.0+t*z;
00105             return t+a*(s+t*v);
00106         }
00107 }
00108 
00109 #endif