]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_A5_SAMA5D2x_Xplained_IAR/AtmelFiles/utils/intmath.h
Add SAMA5D2 Xplained IAR demo.
[freertos] / FreeRTOS / Demo / CORTEX_A5_SAMA5D2x_Xplained_IAR / AtmelFiles / utils / intmath.h
1 /* ----------------------------------------------------------------------------\r
2  *         ATMEL Microcontroller Software Support\r
3  * ----------------------------------------------------------------------------\r
4  * Copyright (c) 2011, Atmel Corporation\r
5  *\r
6  * All rights reserved.\r
7  *\r
8  * Redistribution and use in source and binary forms, with or without\r
9  * modification, are permitted provided that the following conditions are met:\r
10  *\r
11  * - Redistributions of source code must retain the above copyright notice,\r
12  * this list of conditions and the disclaimer below.\r
13  *\r
14  * Atmel's name may not be used to endorse or promote products derived from\r
15  * this software without specific prior written permission.\r
16  *\r
17  * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR\r
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\r
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE\r
20  * DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,\r
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\r
22  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,\r
23  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\r
24  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING\r
25  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,\r
26  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
27  * ----------------------------------------------------------------------------\r
28  */\r
29 \r
30 #ifndef _INTMATH_H_\r
31 #define _INTMATH_H_\r
32 \r
33 /**\r
34  * \brief Compute the absolute value of the difference between two integers.\r
35  * \param a  The first integer.\r
36  * \param b  The second integer.\r
37  * \return  The absolute difference.\r
38  */\r
39 #define ABS_DIFF(a,b) ((a) < (b) ? (b) - (a) : (a) - (b))\r
40 \r
41 /*------------------------------------------------------------------------------\r
42  *         Exported functions\r
43  *------------------------------------------------------------------------------*/\r
44 \r
45 #include <stdint.h>\r
46 \r
47 /**\r
48  *  Returns the minimum value between two integers.\r
49  *  \param a First integer to compare\r
50  *  \param b Second integer to compare\r
51  */\r
52 static inline int32_t min_u32(uint32_t a, uint32_t b)\r
53 {\r
54         return a < b ? a : b;\r
55 }\r
56 \r
57 /**\r
58  *  Returns the absolute value of an integer.\r
59  *  \param value Integer value\r
60  */\r
61 static inline uint32_t abs_u32(int32_t value)\r
62 {\r
63         return value > 0 ? value : -value;\r
64 }\r
65 \r
66 /**\r
67  *  Computes and returns x to the power of y.\r
68  *  \param x Value\r
69  *  \param y Power\r
70  */\r
71 static inline uint32_t power_u32(uint32_t x, uint32_t y)\r
72 {\r
73         uint32_t result = 1;\r
74         while (y > 0) {\r
75                 result *= x;\r
76                 y--;\r
77         }\r
78         return result;\r
79 }\r
80 \r
81 /** ISO/IEC 14882:2003(E) - 5.6 Multiplicative operators:\r
82  * The binary / operator yields the quotient, and the binary % operator yields the remainder\r
83  * from the division of the first expression by the second.\r
84  * If the second operand of / or % is zero the behavior is undefined; otherwise (a/b)*b + a%b is equal to a.\r
85  * If both operands are nonnegative then the remainder is nonnegative;\r
86  * if not, the sign of the remainder is implementation-defined 74).\r
87  */\r
88 static inline int fixed_mod(int a, int b)\r
89 {\r
90         int rem = a % b;\r
91         while (rem < 0)\r
92                 rem += b;\r
93 \r
94         return rem;\r
95 }\r
96 \r
97 #endif /* _INTMATH_H_ */\r