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 #include "storage_class.h" /* for STORAGE_CLASS_INLINE */
19 #if defined(__KERNEL__)
20 #include <linux/kernel.h> /* Override the definition of max/min from linux kernel*/
24 #include <stdlib.h> /* Override the definition of max/min from stdlib.h*/
27 /* in case we have min/max/MIN/MAX macro's undefine them */
34 #ifdef MIN /* also defined in include/hrt/numeric.h from SDK */
44 #define IS_ODD(a) ((a) & 0x1)
45 #define IS_EVEN(a) (!IS_ODD(a))
47 /* force a value to a lower even value */
48 #define EVEN_FLOOR(x) ((x) & ~1)
51 /* If the number is odd, find the next even number */
52 #define EVEN_CEIL(x) ((IS_ODD(x)) ? ((x) + 1) : (x))
56 #define IMPLIES(a, b) (!(a) || (b))
58 #define ABS(a) ((a) >= 0 ? (a) : -(a))
60 /* for preprocessor and array sizing use MIN and MAX
61 otherwise use min and max */
62 #define MAX(a, b) (((a) > (b)) ? (a) : (b))
63 #define MIN(a, b) (((a) < (b)) ? (a) : (b))
65 #define ROUND_DIV(a, b) (((b) != 0) ? ((a) + ((b) >> 1)) / (b) : 0)
67 #define CEIL_DIV(a, b) (((b) != 0) ? ((a) + (b) - 1) / (b) : 0)
68 #define CEIL_MUL(a, b) (CEIL_DIV(a, b) * (b))
69 #define CEIL_MUL2(a, b) (((a) + (b) - 1) & ~((b) - 1))
70 #define CEIL_SHIFT(a, b) (((a) + (1 << (b)) - 1)>>(b))
71 #define CEIL_SHIFT_MUL(a, b) (CEIL_SHIFT(a, b) << (b))
73 #define ROUND_HALF_DOWN_DIV(a, b) (((b) != 0) ? ((a) + (b / 2) - 1) / (b) : 0)
74 #define ROUND_HALF_DOWN_MUL(a, b) (ROUND_HALF_DOWN_DIV(a, b) * (b))
78 /*To Find next power of 2 number from x */
79 #define bit2(x) ((x) | ((x) >> 1))
80 #define bit4(x) (bit2(x) | (bit2(x) >> 2))
81 #define bit8(x) (bit4(x) | (bit4(x) >> 4))
82 #define bit16(x) (bit8(x) | (bit8(x) >> 8))
83 #define bit32(x) (bit16(x) | (bit16(x) >> 16))
84 #define NEXT_POWER_OF_2(x) (bit32(x-1) + 1)
87 /* min and max should not be macros as they will evaluate their arguments twice.
88 if you really need a macro (e.g. for CPP or for initializing an array)
89 use MIN() and MAX(), otherwise use min() and max().
94 #if !defined(PIPE_GENERATION)
96 #ifndef INLINE_MATH_SUPPORT_UTILS
98 This macro versions are added back as we are mixing types in usage of inline.
99 This causes corner cases of calculations to be incorrect due to conversions
100 between signed and unsigned variables or overflows.
101 Before the addition of the inline functions, max, min and ceil_div were macros
102 and therefore adding them back.
104 Leaving out the other math utility functions as they are newly added
107 #define max(a, b) (MAX(a, b))
108 #define min(a, b) (MIN(a, b))
109 #define ceil_div(a, b) (CEIL_DIV(a, b))
111 #else /* !defined(INLINE_MATH_SUPPORT_UTILS) */
113 STORAGE_CLASS_INLINE int max(int a, int b)
118 STORAGE_CLASS_INLINE int min(int a, int b)
123 STORAGE_CLASS_INLINE unsigned int ceil_div(unsigned int a, unsigned int b)
125 return CEIL_DIV(a, b);
127 #endif /* !defined(INLINE_MATH_SUPPORT_UTILS) */
129 STORAGE_CLASS_INLINE unsigned int umax(unsigned int a, unsigned int b)
134 STORAGE_CLASS_INLINE unsigned int umin(unsigned int a, unsigned int b)
140 STORAGE_CLASS_INLINE unsigned int ceil_mul(unsigned int a, unsigned int b)
142 return CEIL_MUL(a, b);
145 STORAGE_CLASS_INLINE unsigned int ceil_mul2(unsigned int a, unsigned int b)
147 return CEIL_MUL2(a, b);
150 STORAGE_CLASS_INLINE unsigned int ceil_shift(unsigned int a, unsigned int b)
152 return CEIL_SHIFT(a, b);
155 STORAGE_CLASS_INLINE unsigned int ceil_shift_mul(unsigned int a, unsigned int b)
157 return CEIL_SHIFT_MUL(a, b);
161 STORAGE_CLASS_INLINE unsigned int round_half_down_div(unsigned int a, unsigned int b)
163 return ROUND_HALF_DOWN_DIV(a, b);
166 STORAGE_CLASS_INLINE unsigned int round_half_down_mul(unsigned int a, unsigned int b)
168 return ROUND_HALF_DOWN_MUL(a, b);
172 /** @brief Next Power of Two
174 * @param[in] unsigned number
176 * @return next power of two
178 * This function rounds input to the nearest power of 2 (2^x)
181 * Input Range: 0 .. 2^(8*sizeof(int)-1)
183 * IF input is a power of 2
186 * out = 2^(ceil(log2(in))
190 STORAGE_CLASS_INLINE unsigned int ceil_pow2(unsigned int a)
195 /* IF input is already a power of two*/
196 else if ((!((a)&((a)-1)))) {
210 #endif /* !defined(PIPE_GENERATION) */
214 * For SP and ISP, SDK provides the definition of OP_std_modadd.
215 * We need it only for host
217 #define OP_std_modadd(base, offset, size) ((base+offset)%(size))
218 #endif /* !defined(__ISP) */
220 #if !defined(__KERNEL__)
221 #define clamp(a, min_val, max_val) MIN(MAX((a), (min_val)), (max_val))
222 #endif /* !defined(__KERNEL__) */
224 #endif /* __MATH_SUPPORT_H */