xref: /OK3568_Linux_fs/external/mpp/osal/mpp_common.cpp (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /*
2  * Copyright 2015 Rockchip Electronics Co. LTD
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "mpp_common.h"
18 
19 static const RK_U8 log2_tab[256] = {
20     0, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
21     5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
22     6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
23     6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
24     7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
25     7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
26     7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
27     7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7
28 };
29 
mpp_log2(RK_U32 v)30 RK_S32 mpp_log2(RK_U32 v)
31 {
32     RK_S32 n = 0;
33     if (v & 0xffff0000) {
34         v >>= 16;
35         n += 16;
36     }
37     if (v & 0xff00) {
38         v >>= 8;
39         n += 8;
40     }
41     n += log2_tab[v];
42 
43     return n;
44 }
45 
mpp_log2_16bit(RK_U32 v)46 RK_S32 mpp_log2_16bit(RK_U32 v)
47 {
48     RK_S32 n = 0;
49     if (v & 0xff00) {
50         v >>= 8;
51         n += 8;
52     }
53     n += log2_tab[v];
54 
55     return n;
56 }
57 
axb_div_c(RK_S32 a,RK_S32 b,RK_S32 c)58 RK_S32 axb_div_c(RK_S32 a, RK_S32 b, RK_S32 c)
59 {
60     RK_U32 left = 32;
61     RK_U32 right = 0;
62     RK_U32 shift;
63     RK_S32 sign = 1;
64     RK_S32 tmp;
65 
66     if (a == 0 || b == 0)
67         return 0;
68     else if ((a * b / b) == a && c != 0)
69         return (a * b / c);
70 
71     if (a < 0) {
72         sign = -1;
73         a = -a;
74     }
75     if (b < 0) {
76         sign *= -1;
77         b = -b;
78     }
79     if (c < 0) {
80         sign *= -1;
81         c = -c;
82     }
83 
84     if (c == 0)
85         return 0x7FFFFFFF * sign;
86 
87     if (b > a) {
88         tmp = b;
89         b = a;
90         a = tmp;
91     }
92 
93     for (--left; (((RK_U32)a << left) >> left) != (RK_U32)a; --left)
94         ;
95 
96     left--;
97 
98     while (((RK_U32)b >> right) > (RK_U32)c)
99         right++;
100 
101     if (right > left) {
102         return 0x7FFFFFFF * sign;
103     } else {
104         shift = left - right;
105         return (RK_S32)((((RK_U32)a << shift) /
106                          (RK_U32)c * (RK_U32)b) >> shift) * sign;
107     }
108 }
109 
mpp_align_16(RK_U32 val)110 RK_U32 mpp_align_16(RK_U32 val)
111 {
112     return MPP_ALIGN(val, 16);
113 }
114 
mpp_align_64(RK_U32 val)115 RK_U32 mpp_align_64(RK_U32 val)
116 {
117     return MPP_ALIGN(val, 64);
118 }
119