rtapi_math.h
1 // Copyright 2006-2010 Various Authors 2 // 3 // This program is free software; you can redistribute it and/or modify 4 // it under the terms of the GNU General Public License as published by 5 // the Free Software Foundation; either version 2 of the License, or 6 // (at your option) any later version. 7 // 8 // This program is distributed in the hope that it will be useful, 9 // but WITHOUT ANY WARRANTY; without even the implied warranty of 10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 // GNU General Public License for more details. 12 // 13 // You should have received a copy of the GNU General Public License 14 // along with this program; if not, write to the Free Software 15 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 16 #ifndef RTAPI_MATH_H 17 #define RTAPI_MATH_H 18 19 #include "rtapi.h" /* Because of all the rtapi refs */ 20 #include <float.h> /* DBL_MAX and other FP goodies */ 21 22 #ifndef M_PIl 23 #define M_PIl 3.1415926535897932384626433832795029L /* pi */ 24 #endif 25 #ifndef M_PI_2l 26 #define M_PI_2l 1.570796326794896619231321691639751442L /* pi/2 */ 27 #endif 28 #ifndef M_PI 29 #define M_PI 3.1415926535897932384626433832795029 /* pi */ 30 #endif 31 32 #if defined(__KERNEL__) 33 extern double sin(double); 34 extern double cos(double); 35 extern double tan(double); 36 extern double sqrt(double); 37 extern double fabs(double); 38 extern double atan(double); 39 extern double atan2(double, double); 40 extern double asin(double); 41 extern double acos(double); 42 extern double exp(double); 43 extern double pow(double, double); 44 extern double fmin(double, double); 45 extern double fmax(double, double); 46 extern double fmod(double, double); 47 48 extern double round(double); 49 extern double ceil(double); 50 extern double floor(double); 51 52 #define frexp(p,q) __builtin_frexp((p),(q)) 53 #define isnan(x) __builtin_isnan((x)) 54 #define signbit(x) __builtin_signbit((x)) 55 #define nan(x) __builtin_nan((x)) 56 57 #define isinf(x) __builtin_isinf((x)) 58 #define isfinite(x) __builtin_isfinite((x)) 59 60 extern __inline double atan (double __y) { 61 return atan2(__y, 1.); 62 } 63 64 extern __inline double asin (double __x) { 65 return atan2(__x, sqrt (1.0 - __x * __x)); 66 } 67 68 extern __inline double acos (double __x) { 69 return atan2(sqrt(1.0 - __x * __x), __x); 70 } 71 72 extern __inline double fmax(double __y, double __x) { 73 return __y > __x || __builtin_isnan(__x) ? __y : __x; 74 } 75 extern __inline double fmin(double __y, double __x) { 76 return __y < __x || __builtin_isnan(__x) ? __y : __x; 77 } 78 79 #ifdef __i386__ 80 #include "rtapi_math_i386.h" 81 #endif 82 83 #else 84 #include <math.h> 85 #endif 86 87 #include "rtapi_byteorder.h" 88 89 // adapted from ieee754.h 90 union ieee754_double 91 { 92 double d; 93 94 /* This is the IEEE 754 double-precision format. */ 95 struct 96 { 97 #if RTAPI_BIG_ENDIAN 98 unsigned int negative:1; 99 unsigned int exponent:11; 100 /* Together these comprise the mantissa. */ 101 unsigned int mantissa0:20; 102 unsigned int mantissa1:32; 103 #endif /* Big endian. */ 104 #if RTAPI_LITTLE_ENDIAN 105 # if RTAPI_FLOAT_BIG_ENDIAN 106 unsigned int mantissa0:20; 107 unsigned int exponent:11; 108 unsigned int negative:1; 109 unsigned int mantissa1:32; 110 # else 111 /* Together these comprise the mantissa. */ 112 unsigned int mantissa1:32; 113 unsigned int mantissa0:20; 114 unsigned int exponent:11; 115 unsigned int negative:1; 116 # endif 117 #endif /* Little endian. */ 118 } ieee; 119 120 /* This format makes it easier to see if a NaN is a signalling NaN. */ 121 struct 122 { 123 #if RTAPI_BIG_ENDIAN 124 unsigned int negative:1; 125 unsigned int exponent:11; 126 unsigned int quiet_nan:1; 127 /* Together these comprise the mantissa. */ 128 unsigned int mantissa0:19; 129 unsigned int mantissa1:32; 130 #else 131 # if RTAPI_FLOAT_BIG_ENDIAN 132 unsigned int mantissa0:19; 133 unsigned int quiet_nan:1; 134 unsigned int exponent:11; 135 unsigned int negative:1; 136 unsigned int mantissa1:32; 137 # else 138 /* Together these comprise the mantissa. */ 139 unsigned int mantissa1:32; 140 unsigned int mantissa0:19; 141 unsigned int quiet_nan:1; 142 unsigned int exponent:11; 143 unsigned int negative:1; 144 # endif 145 #endif 146 } ieee_nan; 147 }; 148 149 #define IEEE754_DOUBLE_BIAS 0x3ff /* Added to exponent. */ 150 151 #endif