AVR Libc Home Page | AVR Libc Development Pages | ||||
Main Page | User Manual | Library Reference | FAQ | Alphabetical Index | Example Projects |
00001 /* Copyright (c) 2002,2007-2009 Michael Stumpf 00002 00003 Portions of documentation Copyright (c) 1990 - 1994 00004 The Regents of the University of California. 00005 00006 All rights reserved. 00007 00008 Redistribution and use in source and binary forms, with or without 00009 modification, are permitted provided that the following conditions are met: 00010 00011 * Redistributions of source code must retain the above copyright 00012 notice, this list of conditions and the following disclaimer. 00013 00014 * Redistributions in binary form must reproduce the above copyright 00015 notice, this list of conditions and the following disclaimer in 00016 the documentation and/or other materials provided with the 00017 distribution. 00018 00019 * Neither the name of the copyright holders nor the names of 00020 contributors may be used to endorse or promote products derived 00021 from this software without specific prior written permission. 00022 00023 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00024 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00025 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00026 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 00027 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 00028 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 00029 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00030 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 00031 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 00032 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00033 POSSIBILITY OF SUCH DAMAGE. */ 00034 00035 /* $Id: math.h 2064 2009-11-29 06:19:16Z dmix $ */ 00036 00037 /* 00038 math.h - mathematical functions 00039 00040 Author : Michael Stumpf 00041 Michael.Stumpf@t-online.de 00042 00043 __ATTR_CONST__ added by marekm@linux.org.pl for functions 00044 that "do not examine any values except their arguments, and have 00045 no effects except the return value", for better optimization by gcc. 00046 */ 00047 00048 #ifndef __MATH_H 00049 #define __MATH_H 00050 00051 /** \file */ 00052 /** \defgroup avr_math <math.h>: Mathematics 00053 \code #include <math.h> \endcode 00054 00055 This header file declares basic mathematics constants and 00056 functions. 00057 00058 \par Notes: 00059 - In order to access the functions delcared herein, it is usually 00060 also required to additionally link against the library \c libm.a. 00061 See also the related \ref faq_libm "FAQ entry". 00062 - Math functions do not raise exceptions and do not change the 00063 \c errno variable. Therefore the majority of them are declared 00064 with const attribute, for better optimization by GCC. */ 00065 00066 00067 /** \ingroup avr_math */ 00068 /*@{*/ 00069 00070 /** The constant \a e. */ 00071 #define M_E 2.7182818284590452354 00072 00073 /** The logarithm of the \a e to base 2. */ 00074 #define M_LOG2E 1.4426950408889634074 /* log_2 e */ 00075 00076 /** The logarithm of the \a e to base 10. */ 00077 #define M_LOG10E 0.43429448190325182765 /* log_10 e */ 00078 00079 /** The natural logarithm of the 2. */ 00080 #define M_LN2 0.69314718055994530942 /* log_e 2 */ 00081 00082 /** The natural logarithm of the 10. */ 00083 #define M_LN10 2.30258509299404568402 /* log_e 10 */ 00084 00085 /** The constant \a pi. */ 00086 #define M_PI 3.14159265358979323846 /* pi */ 00087 00088 /** The constant \a pi/2. */ 00089 #define M_PI_2 1.57079632679489661923 /* pi/2 */ 00090 00091 /** The constant \a pi/4. */ 00092 #define M_PI_4 0.78539816339744830962 /* pi/4 */ 00093 00094 /** The constant \a 1/pi. */ 00095 #define M_1_PI 0.31830988618379067154 /* 1/pi */ 00096 00097 /** The constant \a 2/pi. */ 00098 #define M_2_PI 0.63661977236758134308 /* 2/pi */ 00099 00100 /** The constant \a 2/sqrt(pi). */ 00101 #define M_2_SQRTPI 1.12837916709551257390 /* 2/sqrt(pi) */ 00102 00103 /** The square root of 2. */ 00104 #define M_SQRT2 1.41421356237309504880 /* sqrt(2) */ 00105 00106 /** The constant \a 1/sqrt(2). */ 00107 #define M_SQRT1_2 0.70710678118654752440 /* 1/sqrt(2) */ 00108 00109 /** NAN constant. */ 00110 #define NAN __builtin_nan("") 00111 00112 /** INFINITY constant. */ 00113 #define INFINITY __builtin_inf() 00114 00115 00116 #ifndef __ATTR_CONST__ 00117 # define __ATTR_CONST__ __attribute__((__const__)) 00118 #endif 00119 00120 #ifdef __cplusplus 00121 extern "C" { 00122 #endif 00123 00124 /** 00125 The cos() function returns the cosine of \a __x, measured in radians. 00126 */ 00127 extern double cos(double __x) __ATTR_CONST__; 00128 #define cosf cos /**< The alias for cos(). */ 00129 00130 /** 00131 The sin() function returns the sine of \a __x, measured in radians. 00132 */ 00133 extern double sin(double __x) __ATTR_CONST__; 00134 #define sinf sin /**< The alias for sin(). */ 00135 00136 /** 00137 The tan() function returns the tangent of \a __x, measured in radians. 00138 */ 00139 extern double tan(double __x) __ATTR_CONST__; 00140 #define tanf tan /**< The alias for tan(). */ 00141 00142 /** 00143 The fabs() function computes the absolute value of a floating-point 00144 number \a __x. 00145 */ 00146 extern double fabs(double __x) __ATTR_CONST__; 00147 #define fabsf fabs /**< The alias for fabs(). */ 00148 00149 /** 00150 The function fmod() returns the floating-point remainder of <em>__x / 00151 __y</em>. 00152 */ 00153 extern double fmod(double __x, double __y) __ATTR_CONST__; 00154 #define fmodf fmod /**< The alias for fmod(). */ 00155 00156 /** 00157 The modf() function breaks the argument \a __x into integral and 00158 fractional parts, each of which has the same sign as the argument. 00159 It stores the integral part as a double in the object pointed to by 00160 \a __iptr. 00161 00162 The modf() function returns the signed fractional part of \a __x. 00163 00164 \note This implementation skips writing by zero pointer. However, 00165 the GCC 4.3 can replace this function with inline code that does not 00166 permit to use NULL address for the avoiding of storing. 00167 */ 00168 extern double modf(double __x, double *__iptr); 00169 00170 /** The alias for modf(). 00171 */ 00172 extern float modff (float __x, float *__iptr); 00173 00174 /** 00175 The sqrt() function returns the non-negative square root of \a __x. 00176 */ 00177 extern double sqrt(double __x) __ATTR_CONST__; 00178 #define sqrtf sqrt /**< The alias for sqrt(). */ 00179 00180 /** 00181 The cbrt() function returns the cube root of \a __x. 00182 */ 00183 extern double cbrt(double __x) __ATTR_CONST__; 00184 #define cbrtf cbrt /**< The alias for cbrt(). */ 00185 00186 /** 00187 The hypot() function returns <em>sqrt(__x*__x + __y*__y)</em>. This 00188 is the length of the hypotenuse of a right triangle with sides of 00189 length \a __x and \a __y, or the distance of the point (\a __x, \a 00190 __y) from the origin. Using this function instead of the direct 00191 formula is wise, since the error is much smaller. No underflow with 00192 small \a __x and \a __y. No overflow if result is in range. 00193 */ 00194 extern double hypot (double __x, double __y) __ATTR_CONST__; 00195 #define hypotf hypot /**< The alias for hypot(). */ 00196 00197 /** 00198 The function square() returns <em>__x * __x</em>. 00199 00200 \note This function does not belong to the C standard definition. 00201 */ 00202 extern double square(double __x) __ATTR_CONST__; 00203 #define squaref square /**< The alias for square(). */ 00204 00205 /** 00206 The floor() function returns the largest integral value less than or 00207 equal to \a __x, expressed as a floating-point number. 00208 */ 00209 extern double floor(double __x) __ATTR_CONST__; 00210 #define floorf floor /**< The alias for floor(). */ 00211 00212 /** 00213 The ceil() function returns the smallest integral value greater than 00214 or equal to \a __x, expressed as a floating-point number. 00215 */ 00216 extern double ceil(double __x) __ATTR_CONST__; 00217 #define ceilf ceil /**< The alias for ceil(). */ 00218 00219 /** 00220 The frexp() function breaks a floating-point number into a normalized 00221 fraction and an integral power of 2. It stores the integer in the \c 00222 int object pointed to by \a __pexp. 00223 00224 If \a __x is a normal float point number, the frexp() function 00225 returns the value \c v, such that \c v has a magnitude in the 00226 interval [1/2, 1) or zero, and \a __x equals \c v times 2 raised to 00227 the power \a __pexp. If \a __x is zero, both parts of the result are 00228 zero. If \a __x is not a finite number, the frexp() returns \a __x as 00229 is and stores 0 by \a __pexp. 00230 00231 \note This implementation permits a zero pointer as a directive to 00232 skip a storing the exponent. 00233 */ 00234 extern double frexp(double __x, int *__pexp); 00235 #define frexpf frexp /**< The alias for frexp(). */ 00236 00237 /** 00238 The ldexp() function multiplies a floating-point number by an integral 00239 power of 2. It returns the value of \a __x times 2 raised to the power 00240 \a __exp. 00241 */ 00242 extern double ldexp(double __x, int __exp) __ATTR_CONST__; 00243 #define ldexpf ldexp /**< The alias for ldexp(). */ 00244 00245 /** 00246 The exp() function returns the exponential value of \a __x. 00247 */ 00248 extern double exp(double __x) __ATTR_CONST__; 00249 #define expf exp /**< The alias for exp(). */ 00250 00251 /** 00252 The cosh() function returns the hyperbolic cosine of \a __x. 00253 */ 00254 extern double cosh(double __x) __ATTR_CONST__; 00255 #define coshf cosh /**< The alias for cosh(). */ 00256 00257 /** 00258 The sinh() function returns the hyperbolic sine of \a __x. 00259 */ 00260 extern double sinh(double __x) __ATTR_CONST__; 00261 #define sinhf sinh /**< The alias for sinh(). */ 00262 00263 /** 00264 The tanh() function returns the hyperbolic tangent of \a __x. 00265 */ 00266 extern double tanh(double __x) __ATTR_CONST__; 00267 #define tanhf tanh /**< The alias for tanh(). */ 00268 00269 /** 00270 The acos() function computes the principal value of the arc cosine of 00271 \a __x. The returned value is in the range [0, pi] radians. A domain 00272 error occurs for arguments not in the range [-1, +1]. 00273 */ 00274 extern double acos(double __x) __ATTR_CONST__; 00275 #define acosf acos /**< The alias for acos(). */ 00276 00277 /** 00278 The asin() function computes the principal value of the arc sine of 00279 \a __x. The returned value is in the range [-pi/2, pi/2] radians. A 00280 domain error occurs for arguments not in the range [-1, +1]. 00281 */ 00282 extern double asin(double __x) __ATTR_CONST__; 00283 #define asinf asin /**< The alias for asin(). */ 00284 00285 /** 00286 The atan() function computes the principal value of the arc tangent 00287 of \a __x. The returned value is in the range [-pi/2, pi/2] radians. 00288 */ 00289 extern double atan(double __x) __ATTR_CONST__; 00290 #define atanf atan /**< The alias for atan(). */ 00291 00292 /** 00293 The atan2() function computes the principal value of the arc tangent 00294 of <em>__y / __x</em>, using the signs of both arguments to determine 00295 the quadrant of the return value. The returned value is in the range 00296 [-pi, +pi] radians. 00297 */ 00298 extern double atan2(double __y, double __x) __ATTR_CONST__; 00299 #define atan2f atan2 /**< The alias for atan2(). */ 00300 00301 /** 00302 The log() function returns the natural logarithm of argument \a __x. 00303 */ 00304 extern double log(double __x) __ATTR_CONST__; 00305 #define logf log /**< The alias for log(). */ 00306 00307 /** 00308 The log10() function returns the logarithm of argument \a __x to base 10. 00309 */ 00310 extern double log10(double __x) __ATTR_CONST__; 00311 #define log10f log10 /**< The alias for log10(). */ 00312 00313 /** 00314 The function pow() returns the value of \a __x to the exponent \a __y. 00315 */ 00316 extern double pow(double __x, double __y) __ATTR_CONST__; 00317 #define powf pow /**< The alias for pow(). */ 00318 00319 /** 00320 The function isnan() returns 1 if the argument \a __x represents a 00321 "not-a-number" (NaN) object, otherwise 0. 00322 */ 00323 extern int isnan(double __x) __ATTR_CONST__; 00324 #define isnanf isnan /**< The alias for isnan(). */ 00325 00326 /** 00327 The function isinf() returns 1 if the argument \a __x is positive 00328 infinity, -1 if \a __x is negative infinity, and 0 otherwise. 00329 00330 \note The GCC 4.3 can replace this function with inline code that 00331 returns the 1 value for both infinities (gcc bug #35509). 00332 */ 00333 extern int isinf(double __x) __ATTR_CONST__; 00334 #define isinff isinf /**< The alias for isinf(). */ 00335 00336 /** 00337 The isfinite() function returns a nonzero value if \a __x is finite: 00338 not plus or minus infinity, and not NaN. 00339 */ 00340 __ATTR_CONST__ static inline int isfinite (double __x) 00341 { 00342 unsigned char __exp; 00343 __asm__ ( 00344 "mov %0, %C1 \n\t" 00345 "lsl %0 \n\t" 00346 "mov %0, %D1 \n\t" 00347 "rol %0 " 00348 : "=r" (__exp) 00349 : "r" (__x) ); 00350 return __exp != 0xff; 00351 } 00352 #define isfinitef isfinite /**< The alias for isfinite(). */ 00353 00354 /** 00355 The copysign() function returns \a __x but with the sign of \a __y. 00356 They work even if \a __x or \a __y are NaN or zero. 00357 */ 00358 __ATTR_CONST__ static inline double copysign (double __x, double __y) 00359 { 00360 __asm__ ( 00361 "bst %D2, 7 \n\t" 00362 "bld %D0, 7 " 00363 : "=r" (__x) 00364 : "0" (__x), "r" (__y) ); 00365 return __x; 00366 } 00367 #define copysignf copysign /**< The alias for copysign(). */ 00368 00369 /** 00370 The signbit() function returns a nonzero value if the value of \a __x 00371 has its sign bit set. This is not the same as `\a __x < 0.0', 00372 because IEEE 754 floating point allows zero to be signed. The 00373 comparison `-0.0 < 0.0' is false, but `signbit (-0.0)' will return a 00374 nonzero value. 00375 */ 00376 extern int signbit (double __x) __ATTR_CONST__; 00377 #define signbitf signbit /**< The alias for signbit(). */ 00378 00379 /** 00380 The fdim() function returns <em>max(__x - __y, 0)</em>. If \a __x or 00381 \a __y or both are NaN, NaN is returned. 00382 */ 00383 extern double fdim (double __x, double __y) __ATTR_CONST__; 00384 #define fdimf fdim /**< The alias for fdim(). */ 00385 00386 /** 00387 The fma() function performs floating-point multiply-add. This is the 00388 operation <em>(__x * __y) + __z</em>, but the intermediate result is 00389 not rounded to the destination type. This can sometimes improve the 00390 precision of a calculation. 00391 */ 00392 extern double fma (double __x, double __y, double __z) __ATTR_CONST__; 00393 #define fmaf fma /**< The alias for fma(). */ 00394 00395 /** 00396 The fmax() function returns the greater of the two values \a __x and 00397 \a __y. If an argument is NaN, the other argument is returned. If 00398 both arguments are NaN, NaN is returned. 00399 */ 00400 extern double fmax (double __x, double __y) __ATTR_CONST__; 00401 #define fmaxf fmax /**< The alias for fmax(). */ 00402 00403 /** 00404 The fmin() function returns the lesser of the two values \a __x and 00405 \a __y. If an argument is NaN, the other argument is returned. If 00406 both arguments are NaN, NaN is returned. 00407 */ 00408 extern double fmin (double __x, double __y) __ATTR_CONST__; 00409 #define fminf fmin /**< The alias for fmin(). */ 00410 00411 /** 00412 The trunc() function rounds \a __x to the nearest integer not larger 00413 in absolute value. 00414 */ 00415 extern double trunc (double __x) __ATTR_CONST__; 00416 #define truncf trunc /**< The alias for trunc(). */ 00417 00418 /** 00419 The round() function rounds \a __x to the nearest integer, but rounds 00420 halfway cases away from zero (instead of to the nearest even integer). 00421 Overflow is impossible. 00422 00423 \return The rounded value. If \a __x is an integral or infinite, \a 00424 __x itself is returned. If \a __x is \c NaN, then \c NaN is returned. 00425 */ 00426 extern double round (double __x) __ATTR_CONST__; 00427 #define roundf round /**< The alias for round(). */ 00428 00429 /** 00430 The lround() function rounds \a __x to the nearest integer, but rounds 00431 halfway cases away from zero (instead of to the nearest even integer). 00432 This function is similar to round() function, but it differs in type of 00433 return value and in that an overflow is possible. 00434 00435 \return The rounded long integer value. If \a __x is not a finite number 00436 or an overflow was, this realization returns the \c LONG_MIN value 00437 (0x80000000). 00438 */ 00439 extern long lround (double __x) __ATTR_CONST__; 00440 #define lroundf lround /**< The alias for lround(). */ 00441 00442 /** 00443 The lrint() function rounds \a __x to the nearest integer, rounding the 00444 halfway cases to the even integer direction. (That is both 1.5 and 2.5 00445 values are rounded to 2). This function is similar to rint() function, 00446 but it differs in type of return value and in that an overflow is 00447 possible. 00448 00449 \return The rounded long integer value. If \a __x is not a finite 00450 number or an overflow was, this realization returns the \c LONG_MIN 00451 value (0x80000000). 00452 */ 00453 extern long lrint (double __x) __ATTR_CONST__; 00454 #define lrintf lrint /**< The alias for lrint(). */ 00455 00456 #ifdef __cplusplus 00457 } 00458 #endif 00459 00460 /*@}*/ 00461 #endif /* !__MATH_H */