1 /* Copyright (c) 2002,2007-2009 Michael Stumpf
2 
3    Portions of documentation Copyright (c) 1990 - 1994
4    The Regents of the University of California.
5 
6    All rights reserved.
7 
8    Redistribution and use in source and binary forms, with or without
9    modification, are permitted provided that the following conditions are met:
10 
11    * Redistributions of source code must retain the above copyright
12      notice, this list of conditions and the following disclaimer.
13 
14    * Redistributions in binary form must reproduce the above copyright
15      notice, this list of conditions and the following disclaimer in
16      the documentation and/or other materials provided with the
17      distribution.
18 
19    * Neither the name of the copyright holders nor the names of
20      contributors may be used to endorse or promote products derived
21      from this software without specific prior written permission.
22 
23   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24   AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26   ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
27   LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28   CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29   SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31   CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33   POSSIBILITY OF SUCH DAMAGE. */
34 
35 /* $Id: math.h 2503 2016-02-07 22:59:47Z joerg_wunsch $ */
36 
37 /*
38    math.h - mathematical functions
39 
40    Author : Michael Stumpf
41             Michael.Stumpf@t-online.de
42 
43    __ATTR_CONST__ added by marekm@linux.org.pl for functions
44    that "do not examine any values except their arguments, and have
45    no effects except the return value", for better optimization by gcc.
46  */
47 
48 #ifndef __MATH_H
49 #define __MATH_H
50 
51 /** \file */
52 /** \defgroup avr_math <math.h>: Mathematics
53     \code #include <math.h> \endcode
54 
55     This header file declares basic mathematics constants and
56     functions.
57 
58     \par Notes:
59     - In order to access the functions declared herein, it is usually
60       also required to additionally link against the library \c libm.a.
61       See also the related \ref faq_libm "FAQ entry".
62     - Math functions do not raise exceptions and do not change the
63       \c errno variable. Therefore the majority of them are declared
64       with const attribute, for better optimization by GCC.	*/
65 
66 
67 /** \ingroup avr_math	*/
68 /*@{*/
69 
70 /** The constant \a e.	*/
71 #define M_E		2.7182818284590452354
72 
73 /** The logarithm of the \a e to base 2. */
74 #define M_LOG2E		1.4426950408889634074	/* log_2 e */
75 
76 /** The logarithm of the \a e to base 10. */
77 #define M_LOG10E	0.43429448190325182765	/* log_10 e */
78 
79 /** The natural logarithm of the 2.	*/
80 #define M_LN2		0.69314718055994530942	/* log_e 2 */
81 
82 /** The natural logarithm of the 10.	*/
83 #define M_LN10		2.30258509299404568402	/* log_e 10 */
84 
85 /** The constant \a pi.	*/
86 #define M_PI		3.14159265358979323846	/* pi */
87 
88 /** The constant \a pi/2.	*/
89 #define M_PI_2		1.57079632679489661923	/* pi/2 */
90 
91 /** The constant \a pi/4.	*/
92 #define M_PI_4		0.78539816339744830962	/* pi/4 */
93 
94 /** The constant \a 1/pi.	*/
95 #define M_1_PI		0.31830988618379067154	/* 1/pi */
96 
97 /** The constant \a 2/pi.	*/
98 #define M_2_PI		0.63661977236758134308	/* 2/pi */
99 
100 /** The constant \a 2/sqrt(pi).	*/
101 #define M_2_SQRTPI	1.12837916709551257390	/* 2/sqrt(pi) */
102 
103 /** The square root of 2.	*/
104 #define M_SQRT2		1.41421356237309504880	/* sqrt(2) */
105 
106 /** The constant \a 1/sqrt(2).	*/
107 #define M_SQRT1_2	0.70710678118654752440	/* 1/sqrt(2) */
108 
109 /** NAN constant.	*/
110 #define NAN	__builtin_nan("")
111 
112 /** INFINITY constant.	*/
113 #define INFINITY	__builtin_inf()
114 
115 
116 #ifndef __ATTR_CONST__
117 # define __ATTR_CONST__ __attribute__((__const__))
118 #endif
119 
120 #ifdef __cplusplus
121 extern "C" {
122 #endif
123 
124 /**
125     The cos() function returns the cosine of \a __x, measured in radians.
126  */
127 extern double cos(double __x) __ATTR_CONST__;
128 #define cosf	cos		/**< The alias for cos().	*/
129 
130 /**
131     The sin() function returns the sine of \a __x, measured in radians.
132  */
133 extern double sin(double __x) __ATTR_CONST__;
134 #define sinf	sin		/**< The alias for sin().	*/
135 
136 /**
137     The tan() function returns the tangent of \a __x, measured in radians.
138  */
139 extern double tan(double __x) __ATTR_CONST__;
140 #define tanf	tan		/**< The alias for tan().	*/
141 
142 /**
143     The fabs() function computes the absolute value of a floating-point
144     number \a __x.
145  */
146 extern double fabs(double __x) __ATTR_CONST__;
147 #define fabsf	fabs		/**< The alias for fabs().	*/
148 
149 /**
150     The function fmod() returns the floating-point remainder of <em>__x /
151     __y</em>.
152  */
153 extern double fmod(double __x, double __y) __ATTR_CONST__;
154 #define fmodf	fmod		/**< The alias for fmod().	*/
155 
156 /**
157     The modf() function breaks the argument \a __x into integral and
158     fractional parts, each of which has the same sign as the argument.
159     It stores the integral part as a double in the object pointed to by
160     \a __iptr.
161 
162     The modf() function returns the signed fractional part of \a __x.
163 
164     \note This implementation skips writing by zero pointer.  However,
165     the GCC 4.3 can replace this function with inline code that does not
166     permit to use NULL address for the avoiding of storing.
167  */
168 extern double modf(double __x, double *__iptr);
169 
170 /** An alias for modf(). */
171 extern float modff (float __x, float *__iptr);
172 
173 /**
174     The sqrt() function returns the non-negative square root of \a __x.
175  */
176 extern double sqrt(double __x) __ATTR_CONST__;
177 
178 /** An alias for sqrt(). */
179 extern float sqrtf (float) __ATTR_CONST__;
180 
181 /**
182     The cbrt() function returns the cube root of \a __x.
183  */
184 extern double cbrt(double __x) __ATTR_CONST__;
185 #define cbrtf	cbrt		/**< The alias for cbrt().	*/
186 
187 /**
188     The hypot() function returns <em>sqrt(__x*__x + __y*__y)</em>. This
189     is the length of the hypotenuse of a right triangle with sides of
190     length \a __x and \a __y, or the  distance of the point (\a __x, \a
191     __y) from the origin. Using this function  instead of the direct
192     formula is wise, since the error is much smaller. No underflow with
193     small \a __x and \a __y. No overflow if result is in range.
194  */
195 extern double hypot (double __x, double __y) __ATTR_CONST__;
196 #define hypotf	hypot		/**< The alias for hypot().	*/
197 
198 /**
199     The function square() returns <em>__x * __x</em>.
200 
201     \note This function does not belong to the C standard definition.
202  */
203 extern double square(double __x) __ATTR_CONST__;
204 #define squaref	square		/**< The alias for square().	*/
205 
206 /**
207     The floor() function returns the largest integral value less than or
208     equal to \a __x, expressed as a floating-point number.
209  */
210 extern double floor(double __x) __ATTR_CONST__;
211 #define floorf	floor		/**< The alias for floor().	*/
212 
213 /**
214     The ceil() function returns the smallest integral value greater than
215     or equal to \a __x, expressed as a floating-point number.
216  */
217 extern double ceil(double __x) __ATTR_CONST__;
218 #define ceilf	ceil		/**< The alias for ceil().	*/
219 
220 /**
221     The frexp() function breaks a floating-point number into a normalized
222     fraction and an integral power of 2.  It stores the integer in the \c
223     int object pointed to by \a __pexp.
224 
225     If \a __x is a normal float point number, the frexp() function
226     returns the value \c v, such that \c v has a magnitude in the
227     interval [1/2, 1) or zero, and \a __x equals \c v times 2 raised to
228     the power \a __pexp. If \a __x is zero, both parts of the result are
229     zero. If \a __x is not a finite number, the frexp() returns \a __x as
230     is and stores 0 by \a __pexp.
231 
232     \note  This implementation permits a zero pointer as a directive to
233     skip a storing the exponent.
234  */
235 extern double frexp(double __x, int *__pexp);
236 #define frexpf	frexp		/**< The alias for frexp().	*/
237 
238 /**
239     The ldexp() function multiplies a floating-point number by an integral
240     power of 2. It returns the value of \a __x times 2 raised to the power
241     \a __exp.
242  */
243 extern double ldexp(double __x, int __exp) __ATTR_CONST__;
244 #define ldexpf	ldexp		/**< The alias for ldexp().	*/
245 
246 /**
247     The exp() function returns the exponential value of \a __x.
248  */
249 extern double exp(double __x) __ATTR_CONST__;
250 #define expf	exp		/**< The alias for exp().	*/
251 
252 /**
253     The cosh() function returns the hyperbolic cosine of \a __x.
254  */
255 extern double cosh(double __x) __ATTR_CONST__;
256 #define coshf	cosh		/**< The alias for cosh().	*/
257 
258 /**
259     The sinh() function returns the hyperbolic sine of \a __x.
260  */
261 extern double sinh(double __x) __ATTR_CONST__;
262 #define sinhf	sinh		/**< The alias for sinh().	*/
263 
264 /**
265     The tanh() function returns the hyperbolic tangent of \a __x.
266  */
267 extern double tanh(double __x) __ATTR_CONST__;
268 #define tanhf	tanh		/**< The alias for tanh().	*/
269 
270 /**
271     The acos() function computes the principal value of the arc cosine of
272     \a __x.  The returned value is in the range [0, pi] radians. A domain
273     error occurs for arguments not in the range [-1, +1].
274  */
275 extern double acos(double __x) __ATTR_CONST__;
276 #define acosf	acos		/**< The alias for acos().	*/
277 
278 /**
279     The asin() function computes the principal value of the arc sine of
280     \a __x.  The returned value is in the range [-pi/2, pi/2] radians. A
281     domain error occurs for arguments not in the range [-1, +1].
282  */
283 extern double asin(double __x) __ATTR_CONST__;
284 #define asinf	asin		/**< The alias for asin().	*/
285 
286 /**
287     The atan() function computes the principal value of the arc tangent
288     of \a __x.  The returned value is in the range [-pi/2, pi/2] radians.
289  */
290 extern double atan(double __x) __ATTR_CONST__;
291 #define atanf	atan		/**< The alias for atan().	*/
292 
293 /**
294     The atan2() function computes the principal value of the arc tangent
295     of <em>__y / __x</em>, using the signs of both arguments to determine
296     the quadrant of the return value.  The returned value is in the range
297     [-pi, +pi] radians.
298  */
299 extern double atan2(double __y, double __x) __ATTR_CONST__;
300 #define atan2f	atan2		/**< The alias for atan2().	*/
301 
302 /**
303     The log() function returns the natural logarithm of argument \a __x.
304  */
305 extern double log(double __x) __ATTR_CONST__;
306 #define logf	log		/**< The alias for log().	*/
307 
308 /**
309     The log10() function returns the logarithm of argument \a __x to base 10.
310  */
311 extern double log10(double __x) __ATTR_CONST__;
312 #define log10f	log10		/**< The alias for log10().	*/
313 
314 /**
315     The function pow() returns the value of \a __x to the exponent \a __y.
316  */
317 extern double pow(double __x, double __y) __ATTR_CONST__;
318 #define powf	pow		/**< The alias for pow().	*/
319 
320 /**
321     The function isnan() returns 1 if the argument \a __x represents a
322     "not-a-number" (NaN) object, otherwise 0.
323  */
324 extern int isnan(double __x) __ATTR_CONST__;
325 #define	isnanf	isnan		/**< The alias for isnan().	*/
326 
327 /**
328     The function isinf() returns 1 if the argument \a __x is positive
329     infinity, -1 if \a __x is negative infinity, and 0 otherwise.
330 
331     \note The GCC 4.3 can replace this function with inline code that
332     returns the 1 value for both infinities (gcc bug #35509).
333  */
334 extern int isinf(double __x) __ATTR_CONST__;
335 #define isinff	isinf		/**< The alias for isinf().	*/
336 
337 /**
338     The isfinite() function returns a nonzero value if \a __x is finite:
339     not plus or minus infinity, and not NaN.
340  */
isfinite(double __x)341 __ATTR_CONST__ static inline int isfinite (double __x)
342 {
343     unsigned char __exp;
344     __asm__ (
345 	"mov	%0, %C1		\n\t"
346 	"lsl	%0		\n\t"
347 	"mov	%0, %D1		\n\t"
348 	"rol	%0		"
349 	: "=r" (__exp)
350 	: "r" (__x)	);
351     return __exp != 0xff;
352 }
353 #define isfinitef isfinite	/**< The alias for isfinite().	*/
354 
355 /**
356     The copysign() function returns \a __x but with the sign of \a __y.
357     They work even if \a __x or \a __y are NaN or zero.
358 */
copysign(double __x,double __y)359 __ATTR_CONST__ static inline double copysign (double __x, double __y)
360 {
361     __asm__ (
362 	"bst	%D2, 7	\n\t"
363 	"bld	%D0, 7	"
364 	: "=r" (__x)
365 	: "0" (__x), "r" (__y) );
366     return __x;
367 }
368 #define copysignf copysign	/**< The alias for copysign().	*/
369 
370 /**
371     The signbit() function returns a nonzero value if the value of \a __x
372     has its sign bit set.  This is not the same as `\a __x < 0.0',
373     because IEEE 754 floating point allows zero to be signed. The
374     comparison `-0.0 < 0.0' is false, but `signbit (-0.0)' will return a
375     nonzero value.
376  */
377 extern int signbit (double __x) __ATTR_CONST__;
378 #define signbitf signbit	/**< The alias for signbit().	*/
379 
380 /**
381     The fdim() function returns <em>max(__x - __y, 0)</em>. If \a __x or
382     \a __y or both are NaN, NaN is returned.
383  */
384 extern double fdim (double __x, double __y) __ATTR_CONST__;
385 #define fdimf	fdim		/**< The alias for fdim().	*/
386 
387 /**
388     The fma() function performs floating-point multiply-add. This is the
389     operation <em>(__x * __y) + __z</em>, but the intermediate result is
390     not rounded to the destination type.  This can sometimes improve the
391     precision of a calculation.
392  */
393 extern double fma (double __x, double __y, double __z) __ATTR_CONST__;
394 #define fmaf	fma		/**< The alias for fma().	*/
395 
396 /**
397     The fmax() function returns the greater of the two values \a __x and
398     \a __y. If an argument is NaN, the other argument is returned. If
399     both arguments are NaN, NaN is returned.
400  */
401 extern double fmax (double __x, double __y) __ATTR_CONST__;
402 #define fmaxf	fmax		/**< The alias for fmax().	*/
403 
404 /**
405     The fmin() function returns the lesser of the two values \a __x and
406     \a __y. If an argument is NaN, the other argument is returned. If
407     both arguments are NaN, NaN is returned.
408  */
409 extern double fmin (double __x, double __y) __ATTR_CONST__;
410 #define fminf	fmin		/**< The alias for fmin().	*/
411 
412 /**
413     The trunc() function rounds \a __x to the nearest integer not larger
414     in absolute value.
415  */
416 extern double trunc (double __x) __ATTR_CONST__;
417 #define truncf	trunc		/**< The alias for trunc().	*/
418 
419 /**
420     The round() function rounds \a __x to the nearest integer, but rounds
421     halfway cases away from zero (instead of to the nearest even integer).
422     Overflow is impossible.
423 
424     \return The rounded value. If \a __x is an integral or infinite, \a
425     __x itself is returned. If \a __x is \c NaN, then \c NaN is returned.
426  */
427 extern double round (double __x) __ATTR_CONST__;
428 #define roundf	round		/**< The alias for round().	*/
429 
430 /**
431     The lround() function rounds \a __x to the nearest integer, but rounds
432     halfway cases away from zero (instead of to the nearest even integer).
433     This function is similar to round() function, but it differs in type of
434     return value and in that an overflow is possible.
435 
436     \return The rounded long integer value. If \a __x is not a finite number
437     or an overflow was, this realization returns the \c LONG_MIN value
438     (0x80000000).
439  */
440 extern long lround (double __x) __ATTR_CONST__;
441 #define lroundf	lround		/**< The alias for lround().	*/
442 
443 /**
444     The lrint() function rounds \a __x to the nearest integer, rounding the
445     halfway cases to the even integer direction. (That is both 1.5 and 2.5
446     values are rounded to 2). This function is similar to rint() function,
447     but it differs in type of return value and in that an overflow is
448     possible.
449 
450     \return The rounded long integer value. If \a __x is not a finite
451     number or an overflow was, this realization returns the \c LONG_MIN
452     value (0x80000000).
453  */
454 extern long lrint (double __x) __ATTR_CONST__;
455 #define lrintf	lrint		/**< The alias for lrint().	*/
456 
457 #ifdef __cplusplus
458 }
459 #endif
460 
461 /*@}*/
462 #endif /* !__MATH_H */
463