POK
/home/jaouen/pok_official/pok/trunk/libpok/libm/cos.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 /* @(#)s_cos.c 5.1 93/09/24 */
00018 /*
00019  * ====================================================
00020  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
00021  *
00022  * Developed at SunPro, a Sun Microsystems, Inc. business.
00023  * Permission to use, copy, modify, and distribute this
00024  * software is freely granted, provided that this notice
00025  * is preserved.
00026  * ====================================================
00027  */
00028 
00029 /* cos(x)
00030  * Return cosine function of x.
00031  *
00032  * kernel function:
00033  *      __kernel_sin            ... sine function on [-pi/4,pi/4]
00034  *      __kernel_cos            ... cosine function on [-pi/4,pi/4]
00035  *      __ieee754_rem_pio2      ... argument reduction routine
00036  *
00037  * Method.
00038  *      Let S,C and T denote the sin, cos and tan respectively on
00039  *      [-PI/4, +PI/4]. Reduce the argument x to y1+y2 = x-k*pi/2
00040  *      in [-pi/4 , +pi/4], and let n = k mod 4.
00041  *      We have
00042  *
00043  *          n        sin(x)      cos(x)        tan(x)
00044  *     ----------------------------------------------------------
00045  *          0          S           C             T
00046  *          1          C          -S            -1/T
00047  *          2         -S          -C             T
00048  *          3         -C           S            -1/T
00049  *     ----------------------------------------------------------
00050  *
00051  * Special cases:
00052  *      Let trig be any of sin, cos, or tan.
00053  *      trig(+-INF)  is NaN, with signals;
00054  *      trig(NaN)    is that NaN;
00055  *
00056  * Accuracy:
00057  *      TRIG(x) returns trig(x) nearly rounded
00058  */
00059 #ifdef POK_NEEDS_LIBMATH
00060 #include <libm.h>
00061 #include "namespace.h"
00062 #include "math_private.h"
00063 
00064 #if 0 /* notyet */
00065 #ifdef __weak_alias
00066 __weak_alias(cos, _cos)
00067 #endif
00068 #endif
00069 
00070 double
00071 cos(double x)
00072 {
00073         double y[2],z=0.0;
00074         int32_t n, ix;
00075 
00076     /* High word of x. */
00077         GET_HIGH_WORD(ix,x);
00078 
00079     /* |x| ~< pi/4 */
00080         ix &= 0x7fffffff;
00081         if(ix <= 0x3fe921fb) return __kernel_cos(x,z);
00082 
00083     /* cos(Inf or NaN) is NaN */
00084         else if (ix>=0x7ff00000) return x-x;
00085 
00086     /* argument reduction needed */
00087         else {
00088             n = __ieee754_rem_pio2(x,y);
00089             switch(n&3) {
00090                 case 0: return  __kernel_cos(y[0],y[1]);
00091                 case 1: return -__kernel_sin(y[0],y[1],1);
00092                 case 2: return -__kernel_cos(y[0],y[1]);
00093                 default:
00094                         return  __kernel_sin(y[0],y[1],1);
00095             }
00096         }
00097 }
00098 
00099 #endif