1 /*
2  * Copyright (C) 2004-2010 NXP Software
3  * Copyright (C) 2010 The Android Open Source Project
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #ifndef _LVM_MACROS_H_
19 #define _LVM_MACROS_H_
20 
21 /**********************************************************************************
22    MUL32x32INTO32(A,B,C,ShiftR)
23         C = (A * B) >> ShiftR
24 
25         A, B and C are all 32 bit SIGNED numbers and ShiftR can vary from 0 to 64
26 
27         The user has to take care that C does not overflow.  The result in case
28         of overflow is undefined.
29 
30 ***********************************************************************************/
31 #define MUL32x32INTO32(A,B,C,ShiftR)   \
32         {LVM_INT32 MUL32x32INTO32_temp,MUL32x32INTO32_temp2,MUL32x32INTO32_mask,MUL32x32INTO32_HH,MUL32x32INTO32_HL,MUL32x32INTO32_LH,MUL32x32INTO32_LL;\
33          LVM_INT32  shiftValue;\
34         shiftValue = (ShiftR);\
35         MUL32x32INTO32_mask=0x0000FFFF;\
36         MUL32x32INTO32_HH= ((LVM_INT32)((LVM_INT16)((A)>>16))*((LVM_INT16)((B)>>16)) );\
37         MUL32x32INTO32_HL= ((LVM_INT32)((B)&MUL32x32INTO32_mask)*((LVM_INT16)((A)>>16))) ;\
38         MUL32x32INTO32_LH= ((LVM_INT32)((A)&MUL32x32INTO32_mask)*((LVM_INT16)((B)>>16)));\
39         MUL32x32INTO32_LL= (LVM_INT32)((A)&MUL32x32INTO32_mask)*(LVM_INT32)((B)&MUL32x32INTO32_mask);\
40         MUL32x32INTO32_temp= (LVM_INT32)(MUL32x32INTO32_HL&MUL32x32INTO32_mask)+(LVM_INT32)(MUL32x32INTO32_LH&MUL32x32INTO32_mask)+(LVM_INT32)((MUL32x32INTO32_LL>>16)&MUL32x32INTO32_mask);\
41         MUL32x32INTO32_HH= MUL32x32INTO32_HH+(LVM_INT32)(MUL32x32INTO32_HL>>16)+(LVM_INT32)(MUL32x32INTO32_LH>>16)+(LVM_INT32)(MUL32x32INTO32_temp>>16);\
42         MUL32x32INTO32_LL=MUL32x32INTO32_LL+(LVM_INT32)(MUL32x32INTO32_HL<<16)+(LVM_INT32)(MUL32x32INTO32_LH<<16);\
43         if(shiftValue<32)\
44         {\
45         MUL32x32INTO32_HH=MUL32x32INTO32_HH<<(32-shiftValue);\
46         MUL32x32INTO32_mask=((LVM_INT32)1<<(32-shiftValue))-1;\
47         MUL32x32INTO32_LL=(MUL32x32INTO32_LL>>shiftValue)&MUL32x32INTO32_mask;\
48         MUL32x32INTO32_temp2=MUL32x32INTO32_HH|MUL32x32INTO32_LL;\
49         }\
50         else\
51        {\
52         MUL32x32INTO32_temp2=(LVM_INT32)MUL32x32INTO32_HH>>(shiftValue-32);\
53        }\
54        (C) = MUL32x32INTO32_temp2;\
55        }
56 
57 /**********************************************************************************
58    MUL32x16INTO32(A,B,C,ShiftR)
59         C = (A * B) >> ShiftR
60 
61         A and C are 32 bit SIGNED numbers.  B is a 16 bit SIGNED number.
62         ShiftR can vary from 0 to 48
63 
64         The user has to take care that C does not overflow.  The result in case
65         of overflow is undefined.
66 
67 ***********************************************************************************/
68 #define MUL32x16INTO32(A,B,C,ShiftR)   \
69         {LVM_INT32 MUL32x16INTO32_mask,MUL32x16INTO32_HH,MUL32x16INTO32_LL;\
70          LVM_INT32  shiftValue;\
71         shiftValue = (ShiftR);\
72         MUL32x16INTO32_mask=0x0000FFFF;\
73         MUL32x16INTO32_HH= ((LVM_INT32)(B)*((LVM_INT16)((A)>>16)));\
74         MUL32x16INTO32_LL= ((LVM_INT32)((A)&MUL32x16INTO32_mask)*(B));\
75         if(shiftValue<16)\
76         {\
77         MUL32x16INTO32_HH=(LVM_INT32)((LVM_UINT32)MUL32x16INTO32_HH<<(16-shiftValue));\
78         (C)=MUL32x16INTO32_HH+(LVM_INT32)(MUL32x16INTO32_LL>>shiftValue);\
79         }\
80         else if(shiftValue<32) {\
81         MUL32x16INTO32_HH=(LVM_INT32)(MUL32x16INTO32_HH>>(shiftValue-16));\
82         (C)=MUL32x16INTO32_HH+(LVM_INT32)(MUL32x16INTO32_LL>>shiftValue);\
83         }\
84         else {\
85         (C)=MUL32x16INTO32_HH>>(shiftValue-16);}\
86         }
87 
88 /**********************************************************************************
89    ADD2_SAT_32x32(A,B,C)
90         C = SAT(A + B)
91 
92         A,B and C are 32 bit SIGNED numbers.
93 ***********************************************************************************/
94 #define ADD2_SAT_32x32(A,B,C)   \
95         {(C)=(A)+(B);\
96          if ((((C) ^ (A)) & ((C) ^ (B))) >> 31)\
97             {\
98                 if((A)<0)\
99                     (C)=0x80000000l;\
100                 else\
101                     (C)=0x7FFFFFFFl;\
102             }\
103         }
104 
105 #endif /* _LVM_MACROS_H_ */
106 
107 /*** End of file ******************************************************************/
108