1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3 * Support for Intel Camera Imaging ISP subsystem.
4 * Copyright (c) 2015, Intel Corporation.
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 */
15
16 #ifndef __SH_CSS_FRAC_H
17 #define __SH_CSS_FRAC_H
18
19 #include <math_support.h>
20
21 #define sISP_REG_BIT ISP_VEC_ELEMBITS
22 #define uISP_REG_BIT ((unsigned int)(sISP_REG_BIT - 1))
23 #define sSHIFT (16 - sISP_REG_BIT)
24 #define uSHIFT ((unsigned int)(16 - uISP_REG_BIT))
25 #define sFRACTION_BITS_FITTING(a) (a - sSHIFT)
26 #define uFRACTION_BITS_FITTING(a) ((unsigned int)(a - uSHIFT))
27 #define sISP_VAL_MIN (-(1 << uISP_REG_BIT))
28 #define sISP_VAL_MAX ((1 << uISP_REG_BIT) - 1)
29 #define uISP_VAL_MIN (0U)
30 #define uISP_VAL_MAX ((unsigned int)((1 << uISP_REG_BIT) - 1))
31
32 /* a:fraction bits for 16bit precision, b:fraction bits for ISP precision */
sDIGIT_FITTING(int v,int a,int b)33 static inline int sDIGIT_FITTING(int v, int a, int b)
34 {
35 int fit_shift = sFRACTION_BITS_FITTING(a) - b;
36
37 v >>= sSHIFT;
38 v >>= fit_shift > 0 ? fit_shift : 0;
39
40 return clamp_t(int, v, sISP_VAL_MIN, sISP_VAL_MAX);
41 }
42
uDIGIT_FITTING(unsigned int v,int a,int b)43 static inline unsigned int uDIGIT_FITTING(unsigned int v, int a, int b)
44 {
45 int fit_shift = uFRACTION_BITS_FITTING(a) - b;
46
47 v >>= uSHIFT;
48 v >>= fit_shift > 0 ? fit_shift : 0;
49
50 return clamp_t(unsigned int, v, uISP_VAL_MIN, uISP_VAL_MAX);
51 }
52
53 #endif /* __SH_CSS_FRAC_H */
54