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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-3.0/] [packages/] [language/] [c/] [libm/] [current/] [src/] [double/] [internal/] [k_cos.c] - Blame information for rev 786

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

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