Back to index

lightning-sunbird  0.9+nobinonly
k_cos.c
Go to the documentation of this file.
00001 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
00002  *
00003  * ***** BEGIN LICENSE BLOCK *****
00004  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00005  *
00006  * The contents of this file are subject to the Mozilla Public License Version
00007  * 1.1 (the "License"); you may not use this file except in compliance with
00008  * the License. You may obtain a copy of the License at
00009  * http://www.mozilla.org/MPL/
00010  *
00011  * Software distributed under the License is distributed on an "AS IS" basis,
00012  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00013  * for the specific language governing rights and limitations under the
00014  * License.
00015  *
00016  * The Original Code is Mozilla Communicator client code, released
00017  * March 31, 1998.
00018  *
00019  * The Initial Developer of the Original Code is
00020  * Sun Microsystems, Inc.
00021  * Portions created by the Initial Developer are Copyright (C) 1998
00022  * the Initial Developer. All Rights Reserved.
00023  *
00024  * Contributor(s):
00025  *
00026  * Alternatively, the contents of this file may be used under the terms of
00027  * either of the GNU General Public License Version 2 or later (the "GPL"),
00028  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00029  * in which case the provisions of the GPL or the LGPL are applicable instead
00030  * of those above. If you wish to allow use of your version of this file only
00031  * under the terms of either the GPL or the LGPL, and not to allow others to
00032  * use your version of this file under the terms of the MPL, indicate your
00033  * decision by deleting the provisions above and replace them with the notice
00034  * and other provisions required by the GPL or the LGPL. If you do not delete
00035  * the provisions above, a recipient may use your version of this file under
00036  * the terms of any one of the MPL, the GPL or the LGPL.
00037  *
00038  * ***** END LICENSE BLOCK ***** */
00039 
00040 /* @(#)k_cos.c 1.3 95/01/18 */
00041 /*
00042  * ====================================================
00043  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
00044  *
00045  * Developed at SunSoft, a Sun Microsystems, Inc. business.
00046  * Permission to use, copy, modify, and distribute this
00047  * software is freely granted, provided that this notice 
00048  * is preserved.
00049  * ====================================================
00050  */
00051 
00052 /*
00053  * __kernel_cos( x,  y )
00054  * kernel cos function on [-pi/4, pi/4], pi/4 ~ 0.785398164
00055  * Input x is assumed to be bounded by ~pi/4 in magnitude.
00056  * Input y is the tail of x. 
00057  *
00058  * Algorithm
00059  *     1. Since cos(-x) = cos(x), we need only to consider positive x.
00060  *     2. if x < 2^-27 (hx<0x3e400000 0), return 1 with inexact if x!=0.
00061  *     3. cos(x) is approximated by a polynomial of degree 14 on
00062  *        [0,pi/4]
00063  *                                    4            14
00064  *            cos(x) ~ 1 - x*x/2 + C1*x + ... + C6*x
00065  *        where the remez error is
00066  *     
00067  *     |              2     4     6     8     10    12     14 |     -58
00068  *     |cos(x)-(1-.5*x +C1*x +C2*x +C3*x +C4*x +C5*x  +C6*x  )| <= 2
00069  *     |                                                 | 
00070  * 
00071  *                    4     6     8     10    12     14 
00072  *     4. let r = C1*x +C2*x +C3*x +C4*x +C5*x  +C6*x  , then
00073  *            cos(x) = 1 - x*x/2 + r
00074  *        since cos(x+y) ~ cos(x) - sin(x)*y 
00075  *                     ~ cos(x) - x*y,
00076  *        a correction term is necessary in cos(x) and hence
00077  *            cos(x+y) = 1 - (x*x/2 - (r - x*y))
00078  *        For better accuracy when x > 0.3, let qx = |x|/4 with
00079  *        the last 32 bits mask off, and if x > 0.78125, let qx = 0.28125.
00080  *        Then
00081  *            cos(x+y) = (1-qx) - ((x*x/2-qx) - (r-x*y)).
00082  *        Note that 1-qx and (x*x/2-qx) is EXACT here, and the
00083  *        magnitude of the latter is at least a quarter of x*x/2,
00084  *        thus, reducing the rounding error in the subtraction.
00085  */
00086 
00087 #include "fdlibm.h"
00088 
00089 #ifdef __STDC__
00090 static const double 
00091 #else
00092 static double 
00093 #endif
00094 one =  1.00000000000000000000e+00, /* 0x3FF00000, 0x00000000 */
00095 C1  =  4.16666666666666019037e-02, /* 0x3FA55555, 0x5555554C */
00096 C2  = -1.38888888888741095749e-03, /* 0xBF56C16C, 0x16C15177 */
00097 C3  =  2.48015872894767294178e-05, /* 0x3EFA01A0, 0x19CB1590 */
00098 C4  = -2.75573143513906633035e-07, /* 0xBE927E4F, 0x809C52AD */
00099 C5  =  2.08757232129817482790e-09, /* 0x3E21EE9E, 0xBDB4B1C4 */
00100 C6  = -1.13596475577881948265e-11; /* 0xBDA8FAE9, 0xBE8838D4 */
00101 
00102 #ifdef __STDC__
00103        double __kernel_cos(double x, double y)
00104 #else
00105        double __kernel_cos(x, y)
00106        double x,y;
00107 #endif
00108 {
00109         fd_twoints u;
00110         double qx = 0;
00111        double a,hz,z,r;
00112        int ix;
00113         u.d = x;
00114        ix = __HI(u)&0x7fffffff;    /* ix = |x|'s high word*/
00115        if(ix<0x3e400000) {                /* if x < 2**27 */
00116            if(((int)x)==0) return one;           /* generate inexact */
00117        }
00118        z  = x*x;
00119        r  = z*(C1+z*(C2+z*(C3+z*(C4+z*(C5+z*C6)))));
00120        if(ix < 0x3FD33333)                /* if |x| < 0.3 */ 
00121            return one - (0.5*z - (z*r - x*y));
00122        else {
00123            if(ix > 0x3fe90000) {          /* x > 0.78125 */
00124               qx = 0.28125;
00125            } else {
00126                 u.d = qx;
00127                __HI(u) = ix-0x00200000;   /* x/4 */
00128                __LO(u) = 0;
00129                 qx = u.d;
00130            }
00131            hz = 0.5*z-qx;
00132            a  = one-qx;
00133            return a - (hz - (z*r-x*y));
00134        }
00135 }