2 * Support for Intel Camera Imaging ISP subsystem.
3 * Copyright (c) 2015, Intel Corporation.
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 #ifndef __MATH_SUPPORT_H
16 #define __MATH_SUPPORT_H
18 #if defined(__KERNEL__)
19 #include <linux/kernel.h> /* Override the definition of max/min from linux kernel*/
23 #include <stdlib.h> /* Override the definition of max/min from stdlib.h*/
26 /* in case we have min/max/MIN/MAX macro's undefine them */
33 #ifdef MIN /* also defined in include/hrt/numeric.h from SDK */
43 #define IS_ODD(a) ((a) & 0x1)
44 #define IS_EVEN(a) (!IS_ODD(a))
46 /* force a value to a lower even value */
47 #define EVEN_FLOOR(x) ((x) & ~1)
50 /* If the number is odd, find the next even number */
51 #define EVEN_CEIL(x) ((IS_ODD(x)) ? ((x) + 1) : (x))
55 #define IMPLIES(a, b) (!(a) || (b))
57 #define ABS(a) ((a) >= 0 ? (a) : -(a))
59 /* for preprocessor and array sizing use MIN and MAX
60 otherwise use min and max */
61 #define MAX(a, b) (((a) > (b)) ? (a) : (b))
62 #define MIN(a, b) (((a) < (b)) ? (a) : (b))
64 #define ROUND_DIV(a, b) (((b) != 0) ? ((a) + ((b) >> 1)) / (b) : 0)
66 #define CEIL_DIV(a, b) (((b) != 0) ? ((a) + (b) - 1) / (b) : 0)
67 #define CEIL_MUL(a, b) (CEIL_DIV(a, b) * (b))
68 #define CEIL_MUL2(a, b) (((a) + (b) - 1) & ~((b) - 1))
69 #define CEIL_SHIFT(a, b) (((a) + (1 << (b)) - 1)>>(b))
70 #define CEIL_SHIFT_MUL(a, b) (CEIL_SHIFT(a, b) << (b))
72 #define ROUND_HALF_DOWN_DIV(a, b) (((b) != 0) ? ((a) + (b / 2) - 1) / (b) : 0)
73 #define ROUND_HALF_DOWN_MUL(a, b) (ROUND_HALF_DOWN_DIV(a, b) * (b))
77 /*To Find next power of 2 number from x */
78 #define bit2(x) ((x) | ((x) >> 1))
79 #define bit4(x) (bit2(x) | (bit2(x) >> 2))
80 #define bit8(x) (bit4(x) | (bit4(x) >> 4))
81 #define bit16(x) (bit8(x) | (bit8(x) >> 8))
82 #define bit32(x) (bit16(x) | (bit16(x) >> 16))
83 #define NEXT_POWER_OF_2(x) (bit32(x-1) + 1)
86 /* min and max should not be macros as they will evaluate their arguments twice.
87 if you really need a macro (e.g. for CPP or for initializing an array)
88 use MIN() and MAX(), otherwise use min() and max().
93 #if !defined(PIPE_GENERATION)
95 #ifndef INLINE_MATH_SUPPORT_UTILS
97 This macro versions are added back as we are mixing types in usage of inline.
98 This causes corner cases of calculations to be incorrect due to conversions
99 between signed and unsigned variables or overflows.
100 Before the addition of the inline functions, max, min and ceil_div were macros
101 and therefore adding them back.
103 Leaving out the other math utility functions as they are newly added
106 #define max(a, b) (MAX(a, b))
107 #define min(a, b) (MIN(a, b))
108 #define ceil_div(a, b) (CEIL_DIV(a, b))
110 #else /* !defined(INLINE_MATH_SUPPORT_UTILS) */
112 static inline int max(int a, int b)
117 static inline int min(int a, int b)
122 static inline unsigned int ceil_div(unsigned int a, unsigned int b)
124 return CEIL_DIV(a, b);
126 #endif /* !defined(INLINE_MATH_SUPPORT_UTILS) */
128 static inline unsigned int umax(unsigned int a, unsigned int b)
133 static inline unsigned int umin(unsigned int a, unsigned int b)
139 static inline unsigned int ceil_mul(unsigned int a, unsigned int b)
141 return CEIL_MUL(a, b);
144 static inline unsigned int ceil_mul2(unsigned int a, unsigned int b)
146 return CEIL_MUL2(a, b);
149 static inline unsigned int ceil_shift(unsigned int a, unsigned int b)
151 return CEIL_SHIFT(a, b);
154 static inline unsigned int ceil_shift_mul(unsigned int a, unsigned int b)
156 return CEIL_SHIFT_MUL(a, b);
160 static inline unsigned int round_half_down_div(unsigned int a, unsigned int b)
162 return ROUND_HALF_DOWN_DIV(a, b);
165 static inline unsigned int round_half_down_mul(unsigned int a, unsigned int b)
167 return ROUND_HALF_DOWN_MUL(a, b);
171 /* @brief Next Power of Two
173 * @param[in] unsigned number
175 * @return next power of two
177 * This function rounds input to the nearest power of 2 (2^x)
180 * Input Range: 0 .. 2^(8*sizeof(int)-1)
182 * IF input is a power of 2
185 * out = 2^(ceil(log2(in))
189 static inline unsigned int ceil_pow2(unsigned int a)
194 /* IF input is already a power of two*/
195 else if ((!((a)&((a)-1)))) {
209 #endif /* !defined(PIPE_GENERATION) */
213 * For SP and ISP, SDK provides the definition of OP_std_modadd.
214 * We need it only for host
216 #define OP_std_modadd(base, offset, size) ((base+offset)%(size))
217 #endif /* !defined(__ISP) */
219 #if !defined(__KERNEL__)
220 #define clamp(a, min_val, max_val) MIN(MAX((a), (min_val)), (max_val))
221 #endif /* !defined(__KERNEL__) */
223 #endif /* __MATH_SUPPORT_H */