rmath.h

Go to the documentation of this file.
00001 /******************************************************************************
00002 *       SOFA, Simulation Open-Framework Architecture, version 1.0 beta 3      *
00003 *                (c) 2006-2008 MGH, INRIA, USTL, UJF, CNRS                    *
00004 *                                                                             *
00005 * This library is free software; you can redistribute it and/or modify it     *
00006 * under the terms of the GNU Lesser General Public License as published by    *
00007 * the Free Software Foundation; either version 2.1 of the License, or (at     *
00008 * your option) any later version.                                             *
00009 *                                                                             *
00010 * This library is distributed in the hope that it will be useful, but WITHOUT *
00011 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or       *
00012 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License *
00013 * for more details.                                                           *
00014 *                                                                             *
00015 * You should have received a copy of the GNU Lesser General Public License    *
00016 * along with this library; if not, write to the Free Software Foundation,     *
00017 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.          *
00018 *******************************************************************************
00019 *                              SOFA :: Framework                              *
00020 *                                                                             *
00021 * Authors: M. Adam, J. Allard, B. Andre, P-J. Bensoussan, S. Cotin, C. Duriez,*
00022 * H. Delingette, F. Falipou, F. Faure, S. Fonteneau, L. Heigeas, C. Mendoza,  *
00023 * M. Nesme, P. Neumann, J-P. de la Plata Alcade, F. Poyer and F. Roy          *
00024 *                                                                             *
00025 * Contact information: contact@sofa-framework.org                             *
00026 ******************************************************************************/
00027 #ifndef SOFA_HELPER_RMATH_H
00028 #define SOFA_HELPER_RMATH_H
00029 
00030 #include <sofa/helper/system/config.h>
00031 #include <math.h>
00032 
00033 namespace sofa
00034 {
00035 
00036 namespace helper
00037 {
00038 
00039 #ifdef M_PI
00040 #define R_PI M_PI
00041 #else
00042 #define R_PI 3.141592653589793238462
00043 #endif
00044 
00045 /*
00046 // Most portable version, but also the slowest
00047 inline int rfloor(double r)
00048 {
00049   return ((int)(r+1000000))-1000000;
00050 }
00051  
00052 inline int rnear(double r)
00053 {
00054   return ((int)(r+1000000.5))-1000000;
00055 }
00056 */
00057 
00058 /*
00059 // Does not work on gcc 4.x due to more aggressive optimizations
00060 inline int rfloor(double r)
00061 {
00062   static const double FLOATTOINTCONST=(1.5*(1LL<<(52-16)));
00063   r+=FLOATTOINTCONST;
00064   return ((((int*)&r)[0])>>16);
00065 }
00066  
00067 inline int rnear(double r)
00068 {
00069   static const double FLOATTOINTCONST_0_5=(1.5*(1LL<<(52-16)))+0.5;
00070   r+=FLOATTOINTCONST_0_5;
00071   return ((((int*)&r)[0])>>16);
00072 }
00073 */
00074 
00075 // Works on gcc 3.x and 4.x
00076 template<class real>
00077 inline int rfloor(real r)
00078 {
00079     static const double FLOATTOINTCONST=(1.5*(1LL<<(52-16)));
00080     union { double d;
00081         int i;
00082     } temp;
00083     temp.d = r+FLOATTOINTCONST;
00084     return ((temp.i)>>16);
00085 }
00086 
00087 template<class real>
00088 inline int rnear(real r)
00089 {
00090     static const double FLOATTOINTCONST_0_5=(1.5*(1LL<<(52-16)))+0.5;
00091     union { double d;
00092         int i;
00093     } temp;
00094     temp.d = r+FLOATTOINTCONST_0_5;
00095     return ((temp.i)>>16);
00096 }
00097 
00098 inline int rceil(double r)
00099 {
00100     return -rfloor(-r);
00101 }
00102 
00103 template<class real>
00104 inline real rabs(real r)
00105 {
00106     return (r>=0)?r:-r;
00107 }
00108 
00109 template<class real>
00110 inline real rmin(real r, real s)
00111 {
00112     return (r<=s)?r:s;
00113 }
00114 
00115 template<class real>
00116 inline real rmax(real r, real s)
00117 {
00118     return (r>s)?r:s;
00119 }
00120 
00121 template<class T>
00122 inline T rlerp(const T& a, const T& b, float f)
00123 {
00124     return a+(b-a)*f;
00125 }
00126 
00127 template<class T>
00128 inline T rsqrt(const T& a)
00129 {
00130     return (T)sqrtf((float)a);
00131 }
00132 
00133 inline double rsqrt(const double& a)
00134 {
00135 #if defined(__arm__) || defined(__mips__)
00136     return sqrt((double)a);
00137 #else
00138     return (double)sqrtl((long double)a);
00139 #endif
00140 }
00141 
00142 inline long double rsqrt(const long double& a)
00143 {
00144 #if defined(__arm__) || defined(__mips__)
00145     return (long double)sqrt((double)a);
00146 #else
00147     return sqrtl(a);
00148 #endif
00149 }
00150 
00151 template<class T>
00152 inline const T SQR(const T& a)
00153 {
00154     return a*a;
00155 }
00156 
00157 template<class T>
00158 inline const T SIGN(const T &a, const T &b)
00159 {
00160     return b >= 0 ? (a >= 0 ? a : -a) : (a >= 0 ? -a : a);
00161 }
00162 
00163 template<class T>
00164 inline void SWAP(T &a, T &b)
00165 {
00166     T dum=a;
00167     a=b;
00168     b=dum;
00169 }
00170 
00171 inline void shft3(double &a, double &b, double &c, const double d)
00172 {
00173     a=b;
00174     b=c;
00175     c=d;
00176 }
00177 
00178 
00179 //inline void printerror( const char* msg )
00180 //{
00181 //    std::cerr<<msg<<std::endl;
00182 //    assert(0);
00183 //}
00184 
00185 
00186 } // namespace helper
00187 
00188 } // namespace sofa
00189 
00190 #endif
00191 
00192 

Generated on Sun Nov 23 02:29:30 2008 for Sofa by  doxygen 1.5.7.1