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