Merge tag 'powerpc-4.15-7' of git://git.kernel.org/pub/scm/linux/kernel/git/powerpc...
[sfrench/cifs-2.6.git] / drivers / staging / media / atomisp / pci / atomisp2 / css2400 / hive_isp_css_include / math_support.h
1 /*
2  * Support for Intel Camera Imaging ISP subsystem.
3  * Copyright (c) 2015, Intel Corporation.
4  *
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.
8  *
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
12  * more details.
13  */
14
15 #ifndef __MATH_SUPPORT_H
16 #define __MATH_SUPPORT_H
17
18 #if defined(__KERNEL__)
19 #include <linux/kernel.h> /* Override the definition of max/min from linux kernel*/
20 #endif /*__KERNEL__*/
21
22 #if defined(_MSC_VER)
23 #include <stdlib.h> /* Override the definition of max/min from stdlib.h*/
24 #endif /* _MSC_VER */
25
26 /* in case we have min/max/MIN/MAX macro's undefine them */
27 #ifdef min
28 #undef min
29 #endif
30 #ifdef max
31 #undef max
32 #endif
33 #ifdef MIN /* also defined in include/hrt/numeric.h from SDK */
34 #undef MIN
35 #endif
36 #ifdef MAX
37 #undef MAX
38 #endif
39 #ifdef ABS
40 #undef ABS
41 #endif
42
43 #define IS_ODD(a)            ((a) & 0x1)
44 #define IS_EVEN(a)           (!IS_ODD(a))
45
46 /* force a value to a lower even value */
47 #define EVEN_FLOOR(x)        ((x) & ~1)
48
49 #ifdef ISP2401
50 /* If the number is odd, find the next even number */
51 #define EVEN_CEIL(x)         ((IS_ODD(x)) ? ((x) + 1) : (x))
52
53 #endif
54 /* A => B */
55 #define IMPLIES(a, b)        (!(a) || (b))
56
57 #define ABS(a)               ((a) >= 0 ? (a) : -(a))
58
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))
63 #ifdef ISP2401
64 #define ROUND_DIV(a, b)      (((b) != 0) ? ((a) + ((b) >> 1)) / (b) : 0)
65 #endif
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))
71 #ifdef ISP2401
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))
74 #endif
75
76
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)
84
85
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().
89
90
91 */
92
93 #if !defined(PIPE_GENERATION)
94
95 #ifndef INLINE_MATH_SUPPORT_UTILS
96 /*
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.
102
103 Leaving out the other math utility functions as they are newly added
104 */
105
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))
109
110 #else /* !defined(INLINE_MATH_SUPPORT_UTILS) */
111
112 static inline int max(int a, int b)
113 {
114         return MAX(a, b);
115 }
116
117 static inline int min(int a, int b)
118 {
119         return MIN(a, b);
120 }
121
122 static inline unsigned int ceil_div(unsigned int a, unsigned int b)
123 {
124         return CEIL_DIV(a, b);
125 }
126 #endif /* !defined(INLINE_MATH_SUPPORT_UTILS) */
127
128 static inline unsigned int umax(unsigned int a, unsigned int b)
129 {
130         return MAX(a, b);
131 }
132
133 static inline unsigned int umin(unsigned int a, unsigned int b)
134 {
135         return MIN(a, b);
136 }
137
138
139 static inline unsigned int ceil_mul(unsigned int a, unsigned int b)
140 {
141         return CEIL_MUL(a, b);
142 }
143
144 static inline unsigned int ceil_mul2(unsigned int a, unsigned int b)
145 {
146         return CEIL_MUL2(a, b);
147 }
148
149 static inline unsigned int ceil_shift(unsigned int a, unsigned int b)
150 {
151         return CEIL_SHIFT(a, b);
152 }
153
154 static inline unsigned int ceil_shift_mul(unsigned int a, unsigned int b)
155 {
156         return CEIL_SHIFT_MUL(a, b);
157 }
158
159 #ifdef ISP2401
160 static inline unsigned int round_half_down_div(unsigned int a, unsigned int b)
161 {
162         return ROUND_HALF_DOWN_DIV(a, b);
163 }
164
165 static inline unsigned int round_half_down_mul(unsigned int a, unsigned int b)
166 {
167         return ROUND_HALF_DOWN_MUL(a, b);
168 }
169 #endif
170
171 /* @brief Next Power of Two
172  *
173  *  @param[in] unsigned number
174  *
175  *  @return next power of two
176  *
177  * This function rounds input to the nearest power of 2 (2^x)
178  * towards infinity
179  *
180  * Input Range: 0 .. 2^(8*sizeof(int)-1)
181  *
182  * IF input is a power of 2
183  *     out = in
184  * OTHERWISE
185  *     out = 2^(ceil(log2(in))
186  *
187  */
188
189 static inline unsigned int ceil_pow2(unsigned int a)
190 {
191         if (a == 0) {
192                 return 1;
193         }
194         /* IF input is already a power of two*/
195         else if ((!((a)&((a)-1)))) {
196                 return a;
197         }
198         else {
199                 unsigned int v = a;
200                 v |= v>>1;
201                 v |= v>>2;
202                 v |= v>>4;
203                 v |= v>>8;
204                 v |= v>>16;
205                 return (v+1);
206         }
207 }
208
209 #endif /* !defined(PIPE_GENERATION) */
210
211 #if !defined(__ISP)
212 /*
213  * For SP and ISP, SDK provides the definition of OP_std_modadd.
214  * We need it only for host
215  */
216 #define OP_std_modadd(base, offset, size) ((base+offset)%(size))
217 #endif /* !defined(__ISP) */
218
219 #if !defined(__KERNEL__)
220 #define clamp(a, min_val, max_val) MIN(MAX((a), (min_val)), (max_val))
221 #endif /* !defined(__KERNEL__) */
222
223 #endif /* __MATH_SUPPORT_H */