1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3 * Support for Intel Camera Imaging ISP subsystem.
4 * Copyright (c) 2015, Intel Corporation.
5 */
6
7 #ifndef __SH_CSS_FRAC_H
8 #define __SH_CSS_FRAC_H
9
10 #include <linux/minmax.h>
11
12 #include "mamoiada_params.h"
13
14 #define sISP_REG_BIT ISP_VEC_ELEMBITS
15 #define uISP_REG_BIT ((unsigned int)(sISP_REG_BIT - 1))
16 #define sSHIFT (16 - sISP_REG_BIT)
17 #define uSHIFT ((unsigned int)(16 - uISP_REG_BIT))
18 #define sFRACTION_BITS_FITTING(a) (a - sSHIFT)
19 #define uFRACTION_BITS_FITTING(a) ((unsigned int)(a - uSHIFT))
20 #define sISP_VAL_MIN (-(1 << uISP_REG_BIT))
21 #define sISP_VAL_MAX ((1 << uISP_REG_BIT) - 1)
22 #define uISP_VAL_MIN (0U)
23 #define uISP_VAL_MAX ((unsigned int)((1 << uISP_REG_BIT) - 1))
24
25 /* a:fraction bits for 16bit precision, b:fraction bits for ISP precision */
sDIGIT_FITTING(int v,int a,int b)26 static inline int sDIGIT_FITTING(int v, int a, int b)
27 {
28 int fit_shift = sFRACTION_BITS_FITTING(a) - b;
29
30 v >>= sSHIFT;
31 if (fit_shift > 0)
32 v >>= fit_shift;
33
34 return clamp_t(int, v, sISP_VAL_MIN, sISP_VAL_MAX);
35 }
36
uDIGIT_FITTING(unsigned int v,int a,int b)37 static inline unsigned int uDIGIT_FITTING(unsigned int v, int a, int b)
38 {
39 int fit_shift = uFRACTION_BITS_FITTING(a) - b;
40
41 v >>= uSHIFT;
42 if (fit_shift > 0)
43 v >>= fit_shift;
44
45 return clamp_t(unsigned int, v, uISP_VAL_MIN, uISP_VAL_MAX);
46 }
47
48 #endif /* __SH_CSS_FRAC_H */
49