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