1 /* Copyright 2013-2014 IBM Corp.
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * 	http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12  * implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef __BITUTILS_H
18 #define __BITUTILS_H
19 
20 /* PPC bit number conversion */
21 #ifdef __ASSEMBLY__
22 #define PPC_BIT(bit)		(0x8000000000000000 >> (bit))
23 #define PPC_BIT32(bit)		(0x80000000 >> (bit))
24 #define PPC_BIT16(bit)		(0x8000 >> (bit))
25 #define PPC_BIT8(bit)		(0x80 >> (bit))
26 #else
27 #define PPC_BIT(bit)		(0x8000000000000000UL >> (bit))
28 #define PPC_BIT32(bit)		(0x80000000UL >> (bit))
29 #define PPC_BIT16(bit)		(0x8000UL >> (bit))
30 #define PPC_BIT8(bit)		(0x80UL >> (bit))
31 #endif
32 #define PPC_BITMASK(bs,be)	((PPC_BIT(bs) - PPC_BIT(be)) | PPC_BIT(bs))
33 #define PPC_BITMASK32(bs,be)	((PPC_BIT32(bs) - PPC_BIT32(be))|PPC_BIT32(bs))
34 #define PPC_BITMASK16(bs,be)	((PPC_BIT16(bs) - PPC_BIT16(be))|PPC_BIT16(bs))
35 #define PPC_BITMASK8(bs,be)	((PPC_BIT8(bs) - PPC_BIT8(be))|PPC_BIT8(bs))
36 #define PPC_BITLSHIFT(be)	(63 - (be))
37 #define PPC_BITLSHIFT32(be)	(31 - (be))
38 
39 /*
40  * PPC bitmask field manipulation
41  */
42 
43 /* Find left shift from first set bit in mask */
44 #define MASK_TO_LSH(m)		(__builtin_ffsl(m) - 1)
45 
46 /* Extract field fname from val */
47 #define GETFIELD(m, v)		(((v) & (m)) >> MASK_TO_LSH(m))
48 
49 /* Set field fname of oval to fval
50  * NOTE: oval isn't modified, the combined result is returned
51  */
52 #define SETFIELD(m, v, val)				\
53 	(((v) & ~(m)) |	((((typeof(v))(val)) << MASK_TO_LSH(m)) & (m)))
54 
55 #endif /* __BITUTILS_H */
56