OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-2.0/] [packages/] [language/] [c/] [libm/] [v2_0/] [src/] [double/] [internal/] [k_cos.c] - Blame information for rev 174

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 27 unneback
//===========================================================================
2
//
3
//      k_cos.c
4
//
5
//      Part of the standard mathematical function library
6
//
7
//===========================================================================
8
//####ECOSGPLCOPYRIGHTBEGIN####
9
// -------------------------------------------
10
// This file is part of eCos, the Embedded Configurable Operating System.
11
// Copyright (C) 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.
12
//
13
// eCos is free software; you can redistribute it and/or modify it under
14
// the terms of the GNU General Public License as published by the Free
15
// Software Foundation; either version 2 or (at your option) any later version.
16
//
17
// eCos is distributed in the hope that it will be useful, but WITHOUT ANY
18
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
19
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
20
// for more details.
21
//
22
// You should have received a copy of the GNU General Public License along
23
// with eCos; if not, write to the Free Software Foundation, Inc.,
24
// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
25
//
26
// As a special exception, if other files instantiate templates or use macros
27
// or inline functions from this file, or you compile this file and link it
28
// with other works to produce a work based on this file, this file does not
29
// by itself cause the resulting work to be covered by the GNU General Public
30
// License. However the source code for this file must still be made available
31
// in accordance with section (3) of the GNU General Public License.
32
//
33
// This exception does not invalidate any other reasons why a work based on
34
// this file might be covered by the GNU General Public License.
35
//
36
// Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.
37
// at http://sources.redhat.com/ecos/ecos-license/
38
// -------------------------------------------
39
//####ECOSGPLCOPYRIGHTEND####
40
//===========================================================================
41
//#####DESCRIPTIONBEGIN####
42
//
43
// Author(s):   jlarmour
44
// Contributors:  jlarmour
45
// Date:        1998-02-13
46
// Purpose:     
47
// Description: 
48
// Usage:       
49
//
50
//####DESCRIPTIONEND####
51
//
52
//===========================================================================
53
 
54
// CONFIGURATION
55
 
56
#include <pkgconf/libm.h>   // Configuration header
57
 
58
// Include the Math library?
59
#ifdef CYGPKG_LIBM     
60
 
61
// Derived from code with the following copyright
62
 
63
 
64
/* @(#)k_cos.c 1.3 95/01/18 */
65
/*
66
 * ====================================================
67
 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
68
 *
69
 * Developed at SunSoft, a Sun Microsystems, Inc. business.
70
 * Permission to use, copy, modify, and distribute this
71
 * software is freely granted, provided that this notice
72
 * is preserved.
73
 * ====================================================
74
 */
75
 
76
/*
77
 * __kernel_cos( x,  y )
78
 * kernel cos function on [-pi/4, pi/4], pi/4 ~ 0.785398164
79
 * Input x is assumed to be bounded by ~pi/4 in magnitude.
80
 * Input y is the tail of x.
81
 *
82
 * Algorithm
83
 *      1. Since cos(-x) = cos(x), we need only to consider positive x.
84
 *      2. if x < 2^-27 (hx<0x3e400000 0), return 1 with inexact if x!=0.
85
 *      3. cos(x) is approximated by a polynomial of degree 14 on
86
 *         [0,pi/4]
87
 *                                       4            14
88
 *              cos(x) ~ 1 - x*x/2 + C1*x + ... + C6*x
89
 *         where the remez error is
90
 *
91
 *      |              2     4     6     8     10    12     14 |     -58
92
 *      |cos(x)-(1-.5*x +C1*x +C2*x +C3*x +C4*x +C5*x  +C6*x  )| <= 2
93
 *      |                                                      |
94
 *
95
 *                     4     6     8     10    12     14
96
 *      4. let r = C1*x +C2*x +C3*x +C4*x +C5*x  +C6*x  , then
97
 *             cos(x) = 1 - x*x/2 + r
98
 *         since cos(x+y) ~ cos(x) - sin(x)*y
99
 *                        ~ cos(x) - x*y,
100
 *         a correction term is necessary in cos(x) and hence
101
 *              cos(x+y) = 1 - (x*x/2 - (r - x*y))
102
 *         For better accuracy when x > 0.3, let qx = |x|/4 with
103
 *         the last 32 bits mask off, and if x > 0.78125, let qx = 0.28125.
104
 *         Then
105
 *              cos(x+y) = (1-qx) - ((x*x/2-qx) - (r-x*y)).
106
 *         Note that 1-qx and (x*x/2-qx) is EXACT here, and the
107
 *         magnitude of the latter is at least a quarter of x*x/2,
108
 *         thus, reducing the rounding error in the subtraction.
109
 */
110
 
111
#include "mathincl/fdlibm.h"
112
 
113
static const double
114
one =  1.00000000000000000000e+00, /* 0x3FF00000, 0x00000000 */
115
C1  =  4.16666666666666019037e-02, /* 0x3FA55555, 0x5555554C */
116
C2  = -1.38888888888741095749e-03, /* 0xBF56C16C, 0x16C15177 */
117
C3  =  2.48015872894767294178e-05, /* 0x3EFA01A0, 0x19CB1590 */
118
C4  = -2.75573143513906633035e-07, /* 0xBE927E4F, 0x809C52AD */
119
C5  =  2.08757232129817482790e-09, /* 0x3E21EE9E, 0xBDB4B1C4 */
120
C6  = -1.13596475577881948265e-11; /* 0xBDA8FAE9, 0xBE8838D4 */
121
 
122
        double __kernel_cos(double x, double y)
123
{
124
        double a,hz,z,r,qx;
125
        int ix;
126
        ix = CYG_LIBM_HI(x)&0x7fffffff; /* ix = |x|'s high word*/
127
        if(ix<0x3e400000) {                     /* if x < 2**27 */
128
            if(((int)x)==0) return one;         /* generate inexact */
129
        }
130
        z  = x*x;
131
        r  = z*(C1+z*(C2+z*(C3+z*(C4+z*(C5+z*C6)))));
132
        if(ix < 0x3FD33333)                     /* if |x| < 0.3 */
133
            return one - (0.5*z - (z*r - x*y));
134
        else {
135
            if(ix > 0x3fe90000) {               /* x > 0.78125 */
136
                qx = 0.28125;
137
            } else {
138
                CYG_LIBM_HI(qx) = ix-0x00200000;        /* x/4 */
139
                CYG_LIBM_LO(qx) = 0;
140
            }
141
            hz = 0.5*z-qx;
142
            a  = one-qx;
143
            return a - (hz - (z*r-x*y));
144
        }
145
}
146
 
147
#endif // ifdef CYGPKG_LIBM     
148
 
149
// EOF k_cos.c

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.