Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-12-16 10:33:16

0001 /*
0002  * sincos_common.h
0003  * The basic idea is to exploit Pade polynomials.
0004  * A lot of ideas were inspired by the cephes math library (by Stephen L. Moshier
0005  * moshier@na-net.ornl.gov) as well as actual code. 
0006  * The Cephes library can be found here:  http://www.netlib.org/cephes/
0007  * 
0008  *  Created on: Jun 23, 2012
0009  *      Author: Danilo Piparo, Thomas Hauth, Vincenzo Innocente
0010  */
0011 
0012 /* 
0013  * VDT is free software: you can redistribute it and/or modify
0014  * it under the terms of the GNU Lesser Public License as published by
0015  * the Free Software Foundation, either version 3 of the License, or
0016  * (at your option) any later version.
0017  * 
0018  * This program is distributed in the hope that it will be useful,
0019  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0020  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0021  * GNU Lesser Public License for more details.
0022  * 
0023  * You should have received a copy of the GNU Lesser Public License
0024  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
0025  */
0026 
0027 #include "vdtcore_common.h"
0028 #include <cmath>
0029 #include <limits>
0030 
0031 #ifndef SINCOS_COMMON_H_
0032 #define SINCOS_COMMON_H_
0033 
0034 namespace vdt{
0035 
0036 namespace details{
0037 
0038 // double precision constants
0039 
0040 const double DP1sc = 7.85398125648498535156E-1;
0041 const double DP2sc = 3.77489470793079817668E-8;
0042 const double DP3sc = 2.69515142907905952645E-15;
0043 
0044 const double C1sin = 1.58962301576546568060E-10;
0045 const double C2sin =-2.50507477628578072866E-8;
0046 const double C3sin = 2.75573136213857245213E-6;
0047 const double C4sin =-1.98412698295895385996E-4;
0048 const double C5sin = 8.33333333332211858878E-3;
0049 const double C6sin =-1.66666666666666307295E-1;
0050 
0051 const double C1cos =-1.13585365213876817300E-11;
0052 const double C2cos = 2.08757008419747316778E-9;
0053 const double C3cos =-2.75573141792967388112E-7;
0054 const double C4cos = 2.48015872888517045348E-5;
0055 const double C5cos =-1.38888888888730564116E-3;
0056 const double C6cos = 4.16666666666665929218E-2;
0057 
0058 const double DP1 = 7.853981554508209228515625E-1;
0059 const double DP2 = 7.94662735614792836714E-9;
0060 const double DP3 = 3.06161699786838294307E-17;
0061 
0062 // single precision constants 
0063 
0064 const float DP1F = 0.78515625;
0065 const float DP2F = 2.4187564849853515625e-4;
0066 const float DP3F = 3.77489497744594108e-8;
0067 
0068 const float T24M1 = 16777215.;
0069 
0070 //------------------------------------------------------------------------------
0071 
0072 inline double get_sin_px(const double x){
0073     double px=C1sin;
0074     px *= x;
0075     px += C2sin;
0076     px *= x;
0077     px += C3sin;
0078     px *= x;
0079     px += C4sin;
0080     px *= x;
0081     px += C5sin;
0082     px *= x;
0083     px += C6sin;
0084     return px;
0085 }
0086 
0087 //------------------------------------------------------------------------------
0088 
0089 inline double get_cos_px(const double x){
0090     double px=C1cos;
0091     px *= x;
0092     px += C2cos;
0093     px *= x;
0094     px += C3cos;
0095     px *= x;
0096     px += C4cos;
0097     px *= x;
0098     px += C5cos;
0099     px *= x;
0100     px += C6cos;
0101     return px;
0102 }
0103 
0104 
0105 //------------------------------------------------------------------------------
0106 /// Reduce to 0 to 45
0107 inline double reduce2quadrant(double x, int32_t& quad) {
0108 
0109     x = fabs(x);
0110     quad = int (ONEOPIO4 * x); // always positive, so (int) == std::floor
0111     quad = (quad+1) & (~1);    
0112     const double y = double (quad);
0113     // Extended precision modular arithmetic
0114     return ((x - y * DP1) - y * DP2) - y * DP3;
0115   }
0116 
0117 //------------------------------------------------------------------------------
0118 /// Sincos only for -45deg < x < 45deg
0119 inline void fast_sincos_m45_45( const double z, double & s, double &c ) {
0120 
0121     double zz = z * z;    
0122     s = z  +  z * zz * get_sin_px(zz);                
0123     c = 1.0 - zz * .5 + zz * zz * get_cos_px(zz);
0124   }
0125 
0126 
0127 //------------------------------------------------------------------------------
0128 
0129 } // End namespace details
0130 
0131 /// Double precision sincos
0132 inline void fast_sincos( const double xx, double & s, double &c ) {
0133     // I have to use doubles to make it vectorise...
0134 
0135     int j;
0136     double x = details::reduce2quadrant(xx,j);
0137     const double signS = (j&4); 
0138 
0139     j-=2;
0140 
0141     const double signC = (j&4);
0142     const double poly = j&2;
0143 
0144     details::fast_sincos_m45_45(x,s,c);
0145     
0146     //swap
0147     if( poly==0 ) {
0148       const double tmp = c;
0149       c=s; 
0150       s=tmp;
0151     }
0152 
0153     if(signC == 0.) 
0154       c = -c;
0155     if(signS != 0.) 
0156       s = -s;
0157     if (xx < 0.)  
0158       s = -s;
0159 
0160   }
0161 
0162 
0163 // Single precision functions
0164 
0165 namespace details {
0166 //------------------------------------------------------------------------------
0167 /// Reduce to 0 to 45
0168 inline float reduce2quadrant(float x, int & quad) {
0169     /* make argument positive */
0170     x = fabs(x);
0171 
0172     quad = int (ONEOPIO4F * x); /* integer part of x/PIO4 */
0173 
0174     quad = (quad+1) & (~1);
0175     const float y = float(quad);
0176     // quad &=4;
0177     // Extended precision modular arithmetic
0178     return ((x - y * DP1F) - y * DP2F) - y * DP3F;
0179   }
0180   
0181   
0182 //------------------------------------------------------------------------------
0183 
0184 
0185 
0186 /// Sincos only for -45deg < x < 45deg
0187 inline void fast_sincosf_m45_45( const float x, float & s, float &c ) {
0188 
0189     float z = x * x;
0190 
0191     s = (((-1.9515295891E-4f * z
0192        + 8.3321608736E-3f) * z
0193       - 1.6666654611E-1f) * z * x)
0194       + x;
0195 
0196     c = ((  2.443315711809948E-005f * z
0197         - 1.388731625493765E-003f) * z
0198      + 4.166664568298827E-002f) * z * z
0199       - 0.5f * z + 1.0f;
0200   }
0201 
0202 //------------------------------------------------------------------------------
0203 
0204 } // end details namespace
0205 
0206 /// Single precision sincos
0207 inline void fast_sincosf( const float xx, float & s, float &c ) {
0208     
0209 
0210     int j;
0211     const float x = details::reduce2quadrant(xx,j);
0212     int signS = (j&4); 
0213 
0214     j-=2;
0215 
0216     const int signC = (j&4);
0217     const int poly = j&2;
0218 
0219     float ls,lc;
0220     details::fast_sincosf_m45_45(x,ls,lc);
0221 
0222     //swap
0223     if( poly==0 ) {
0224       const float tmp = lc;
0225       lc=ls; ls=tmp;
0226     }
0227 
0228     if(signC == 0) lc = -lc;
0229     if(signS != 0) ls = -ls;
0230     if (xx<0)  ls = -ls;
0231     c=lc;
0232     s=ls;
0233   }
0234 
0235 
0236 } // end namespace vdt
0237 
0238 #endif