$treeview $search $mathjax $extrastylesheet
avr-libc
2.0.0
$projectbrief
|
$projectbrief
|
$searchbox |
AVR Libc Home Page |
AVR Libc Development Pages |
||||
Main Page |
User Manual |
Library Reference |
FAQ |
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$ */ 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 declared 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 /** An alias for modf(). */ 00171 extern float modff (float __x, float *__iptr); 00172 00173 /** 00174 The sqrt() function returns the non-negative square root of \a __x. 00175 */ 00176 extern double sqrt(double __x) __ATTR_CONST__; 00177 00178 /** An alias for sqrt(). */ 00179 extern float sqrtf (float) __ATTR_CONST__; 00180 00181 /** 00182 The cbrt() function returns the cube root of \a __x. 00183 */ 00184 extern double cbrt(double __x) __ATTR_CONST__; 00185 #define cbrtf cbrt /**< The alias for cbrt(). */ 00186 00187 /** 00188 The hypot() function returns <em>sqrt(__x*__x + __y*__y)</em>. This 00189 is the length of the hypotenuse of a right triangle with sides of 00190 length \a __x and \a __y, or the distance of the point (\a __x, \a 00191 __y) from the origin. Using this function instead of the direct 00192 formula is wise, since the error is much smaller. No underflow with 00193 small \a __x and \a __y. No overflow if result is in range. 00194 */ 00195 extern double hypot (double __x, double __y) __ATTR_CONST__; 00196 #define hypotf hypot /**< The alias for hypot(). */ 00197 00198 /** 00199 The function square() returns <em>__x * __x</em>. 00200 00201 \note This function does not belong to the C standard definition. 00202 */ 00203 extern double square(double __x) __ATTR_CONST__; 00204 #define squaref square /**< The alias for square(). */ 00205 00206 /** 00207 The floor() function returns the largest integral value less than or 00208 equal to \a __x, expressed as a floating-point number. 00209 */ 00210 extern double floor(double __x) __ATTR_CONST__; 00211 #define floorf floor /**< The alias for floor(). */ 00212 00213 /** 00214 The ceil() function returns the smallest integral value greater than 00215 or equal to \a __x, expressed as a floating-point number. 00216 */ 00217 extern double ceil(double __x) __ATTR_CONST__; 00218 #define ceilf ceil /**< The alias for ceil(). */ 00219 00220 /** 00221 The frexp() function breaks a floating-point number into a normalized 00222 fraction and an integral power of 2. It stores the integer in the \c 00223 int object pointed to by \a __pexp. 00224 00225 If \a __x is a normal float point number, the frexp() function 00226 returns the value \c v, such that \c v has a magnitude in the 00227 interval [1/2, 1) or zero, and \a __x equals \c v times 2 raised to 00228 the power \a __pexp. If \a __x is zero, both parts of the result are 00229 zero. If \a __x is not a finite number, the frexp() returns \a __x as 00230 is and stores 0 by \a __pexp. 00231 00232 \note This implementation permits a zero pointer as a directive to 00233 skip a storing the exponent. 00234 */ 00235 extern double frexp(double __x, int *__pexp); 00236 #define frexpf frexp /**< The alias for frexp(). */ 00237 00238 /** 00239 The ldexp() function multiplies a floating-point number by an integral 00240 power of 2. It returns the value of \a __x times 2 raised to the power 00241 \a __exp. 00242 */ 00243 extern double ldexp(double __x, int __exp) __ATTR_CONST__; 00244 #define ldexpf ldexp /**< The alias for ldexp(). */ 00245 00246 /** 00247 The exp() function returns the exponential value of \a __x. 00248 */ 00249 extern double exp(double __x) __ATTR_CONST__; 00250 #define expf exp /**< The alias for exp(). */ 00251 00252 /** 00253 The cosh() function returns the hyperbolic cosine of \a __x. 00254 */ 00255 extern double cosh(double __x) __ATTR_CONST__; 00256 #define coshf cosh /**< The alias for cosh(). */ 00257 00258 /** 00259 The sinh() function returns the hyperbolic sine of \a __x. 00260 */ 00261 extern double sinh(double __x) __ATTR_CONST__; 00262 #define sinhf sinh /**< The alias for sinh(). */ 00263 00264 /** 00265 The tanh() function returns the hyperbolic tangent of \a __x. 00266 */ 00267 extern double tanh(double __x) __ATTR_CONST__; 00268 #define tanhf tanh /**< The alias for tanh(). */ 00269 00270 /** 00271 The acos() function computes the principal value of the arc cosine of 00272 \a __x. The returned value is in the range [0, pi] radians. A domain 00273 error occurs for arguments not in the range [-1, +1]. 00274 */ 00275 extern double acos(double __x) __ATTR_CONST__; 00276 #define acosf acos /**< The alias for acos(). */ 00277 00278 /** 00279 The asin() function computes the principal value of the arc sine of 00280 \a __x. The returned value is in the range [-pi/2, pi/2] radians. A 00281 domain error occurs for arguments not in the range [-1, +1]. 00282 */ 00283 extern double asin(double __x) __ATTR_CONST__; 00284 #define asinf asin /**< The alias for asin(). */ 00285 00286 /** 00287 The atan() function computes the principal value of the arc tangent 00288 of \a __x. The returned value is in the range [-pi/2, pi/2] radians. 00289 */ 00290 extern double atan(double __x) __ATTR_CONST__; 00291 #define atanf atan /**< The alias for atan(). */ 00292 00293 /** 00294 The atan2() function computes the principal value of the arc tangent 00295 of <em>__y / __x</em>, using the signs of both arguments to determine 00296 the quadrant of the return value. The returned value is in the range 00297 [-pi, +pi] radians. 00298 */ 00299 extern double atan2(double __y, double __x) __ATTR_CONST__; 00300 #define atan2f atan2 /**< The alias for atan2(). */ 00301 00302 /** 00303 The log() function returns the natural logarithm of argument \a __x. 00304 */ 00305 extern double log(double __x) __ATTR_CONST__; 00306 #define logf log /**< The alias for log(). */ 00307 00308 /** 00309 The log10() function returns the logarithm of argument \a __x to base 10. 00310 */ 00311 extern double log10(double __x) __ATTR_CONST__; 00312 #define log10f log10 /**< The alias for log10(). */ 00313 00314 /** 00315 The function pow() returns the value of \a __x to the exponent \a __y. 00316 */ 00317 extern double pow(double __x, double __y) __ATTR_CONST__; 00318 #define powf pow /**< The alias for pow(). */ 00319 00320 /** 00321 The function isnan() returns 1 if the argument \a __x represents a 00322 "not-a-number" (NaN) object, otherwise 0. 00323 */ 00324 extern int isnan(double __x) __ATTR_CONST__; 00325 #define isnanf isnan /**< The alias for isnan(). */ 00326 00327 /** 00328 The function isinf() returns 1 if the argument \a __x is positive 00329 infinity, -1 if \a __x is negative infinity, and 0 otherwise. 00330 00331 \note The GCC 4.3 can replace this function with inline code that 00332 returns the 1 value for both infinities (gcc bug #35509). 00333 */ 00334 extern int isinf(double __x) __ATTR_CONST__; 00335 #define isinff isinf /**< The alias for isinf(). */ 00336 00337 /** 00338 The isfinite() function returns a nonzero value if \a __x is finite: 00339 not plus or minus infinity, and not NaN. 00340 */ 00341 __ATTR_CONST__ static inline int isfinite (double __x) 00342 { 00343 unsigned char __exp; 00344 __asm__ ( 00345 "mov %0, %C1 \n\t" 00346 "lsl %0 \n\t" 00347 "mov %0, %D1 \n\t" 00348 "rol %0 " 00349 : "=r" (__exp) 00350 : "r" (__x) ); 00351 return __exp != 0xff; 00352 } 00353 #define isfinitef isfinite /**< The alias for isfinite(). */ 00354 00355 /** 00356 The copysign() function returns \a __x but with the sign of \a __y. 00357 They work even if \a __x or \a __y are NaN or zero. 00358 */ 00359 __ATTR_CONST__ static inline double copysign (double __x, double __y) 00360 { 00361 __asm__ ( 00362 "bst %D2, 7 \n\t" 00363 "bld %D0, 7 " 00364 : "=r" (__x) 00365 : "0" (__x), "r" (__y) ); 00366 return __x; 00367 } 00368 #define copysignf copysign /**< The alias for copysign(). */ 00369 00370 /** 00371 The signbit() function returns a nonzero value if the value of \a __x 00372 has its sign bit set. This is not the same as `\a __x < 0.0', 00373 because IEEE 754 floating point allows zero to be signed. The 00374 comparison `-0.0 < 0.0' is false, but `signbit (-0.0)' will return a 00375 nonzero value. 00376 */ 00377 extern int signbit (double __x) __ATTR_CONST__; 00378 #define signbitf signbit /**< The alias for signbit(). */ 00379 00380 /** 00381 The fdim() function returns <em>max(__x - __y, 0)</em>. If \a __x or 00382 \a __y or both are NaN, NaN is returned. 00383 */ 00384 extern double fdim (double __x, double __y) __ATTR_CONST__; 00385 #define fdimf fdim /**< The alias for fdim(). */ 00386 00387 /** 00388 The fma() function performs floating-point multiply-add. This is the 00389 operation <em>(__x * __y) + __z</em>, but the intermediate result is 00390 not rounded to the destination type. This can sometimes improve the 00391 precision of a calculation. 00392 */ 00393 extern double fma (double __x, double __y, double __z) __ATTR_CONST__; 00394 #define fmaf fma /**< The alias for fma(). */ 00395 00396 /** 00397 The fmax() function returns the greater of the two values \a __x and 00398 \a __y. If an argument is NaN, the other argument is returned. If 00399 both arguments are NaN, NaN is returned. 00400 */ 00401 extern double fmax (double __x, double __y) __ATTR_CONST__; 00402 #define fmaxf fmax /**< The alias for fmax(). */ 00403 00404 /** 00405 The fmin() function returns the lesser of the two values \a __x and 00406 \a __y. If an argument is NaN, the other argument is returned. If 00407 both arguments are NaN, NaN is returned. 00408 */ 00409 extern double fmin (double __x, double __y) __ATTR_CONST__; 00410 #define fminf fmin /**< The alias for fmin(). */ 00411 00412 /** 00413 The trunc() function rounds \a __x to the nearest integer not larger 00414 in absolute value. 00415 */ 00416 extern double trunc (double __x) __ATTR_CONST__; 00417 #define truncf trunc /**< The alias for trunc(). */ 00418 00419 /** 00420 The round() function rounds \a __x to the nearest integer, but rounds 00421 halfway cases away from zero (instead of to the nearest even integer). 00422 Overflow is impossible. 00423 00424 \return The rounded value. If \a __x is an integral or infinite, \a 00425 __x itself is returned. If \a __x is \c NaN, then \c NaN is returned. 00426 */ 00427 extern double round (double __x) __ATTR_CONST__; 00428 #define roundf round /**< The alias for round(). */ 00429 00430 /** 00431 The lround() function rounds \a __x to the nearest integer, but rounds 00432 halfway cases away from zero (instead of to the nearest even integer). 00433 This function is similar to round() function, but it differs in type of 00434 return value and in that an overflow is possible. 00435 00436 \return The rounded long integer value. If \a __x is not a finite number 00437 or an overflow was, this realization returns the \c LONG_MIN value 00438 (0x80000000). 00439 */ 00440 extern long lround (double __x) __ATTR_CONST__; 00441 #define lroundf lround /**< The alias for lround(). */ 00442 00443 /** 00444 The lrint() function rounds \a __x to the nearest integer, rounding the 00445 halfway cases to the even integer direction. (That is both 1.5 and 2.5 00446 values are rounded to 2). This function is similar to rint() function, 00447 but it differs in type of return value and in that an overflow is 00448 possible. 00449 00450 \return The rounded long integer value. If \a __x is not a finite 00451 number or an overflow was, this realization returns the \c LONG_MIN 00452 value (0x80000000). 00453 */ 00454 extern long lrint (double __x) __ATTR_CONST__; 00455 #define lrintf lrint /**< The alias for lrint(). */ 00456 00457 #ifdef __cplusplus 00458 } 00459 #endif 00460 00461 /*@}*/ 00462 #endif /* !__MATH_H */