1#ifndef BIT_MAN_H
2#define BIT_MAN_H
3/*
4Copyright (c) 2015-2015 Cisco Systems, Inc.
5
6Licensed under the Apache License, Version 2.0 (the "License");
7you may not use this file except in compliance with the License.
8You may obtain a copy of the License at
9
10    http://www.apache.org/licenses/LICENSE-2.0
11
12Unless required by applicable law or agreed to in writing, software
13distributed under the License is distributed on an "AS IS" BASIS,
14WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15See the License for the specific language governing permissions and
16limitations under the License.
17*/
18
19
20
21template <class T>
22inline T btGetShift(unsigned int stopbit){
23    return(T)((sizeof(T)*8)-stopbit-1);
24}
25
26//this function return mask with 1 from the start bit
27// 0 in this bit are the MSB - big edian mode
28// if T is int (32) bit 31 is the last
29template <class T>
30inline T btGetMask(unsigned int startbit,
31				   unsigned int stopbit){
32    register T shft=btGetShift<T>(stopbit);
33    return ((T)( (((1<<(stopbit-startbit+1))-1)<<shft)) );
34}
35
36
37//this function are used for big endian mode
38// e.x btGetMaskBitBigE(0x80000000,0,0)==1
39// e.x btGetMaskBitBigE(0xc0000000,0,1)==3
40template <class T>
41inline T btGetMaskBitBigE(T a,
42                          int startbit,
43                          int stopbit ) {
44    if((sizeof(T) * 8) == (stopbit - startbit + 1))// the case where the mask is the whole data
45    {
46        return a;
47    }
48    else
49    {
50    	register T mask=btGetMask<T>(startbit,stopbit);
51    	register T shift=btGetShift<T>(stopbit);
52    	T result;
53    	result=((a & mask) >>shift);
54    	return(result);
55    }
56}
57
58inline uint32_t btGetMaskBitBigE32(uint32_t a,
59                                       int startbit,
60                                       int stopbit ) {
61    return(btGetMaskBitBigE<uint32_t>(a,startbit,stopbit));
62}
63
64inline unsigned short btGetMaskBitBigE16(uint16_t a,
65                                       int startbit,
66                                       int stopbit ) {
67     return(btGetMaskBitBigE<uint16_t>(a,startbit,stopbit));
68}
69
70inline uint8_t btGetMaskBitBigE8(uint8_t a,
71                                       int startbit,
72                                       int stopbit ) {
73     return(btGetMaskBitBigE<uint8_t>(a,startbit,stopbit));
74}
75
76
77template <class T>
78inline void btSetMaskBitBigE(T & a,
79                             int startbit,
80                             int stopbit,
81                             T  newval) {
82    if((sizeof(T) * 8) == (stopbit - startbit + 1))// the case where the mask is the whole data
83    {
84        a = newval;
85    }
86    else
87    {
88	    register T mask=btGetMask<T>(startbit,stopbit);
89	    register T shift=btGetShift<T>(stopbit);
90	    a=((a & ~mask) | ( (newval <<shift) & mask ) );
91    }
92}
93
94
95
96inline void btSetMaskBitBigE32(uint32_t & a,
97                               int           startbit,
98                               int           stopbit,
99                               uint32_t  newVal
100                                ) {
101     btSetMaskBitBigE<uint32_t>(a,startbit,stopbit,newVal);
102}
103
104inline void btSetMaskBitBigE16(uint16_t  & a,
105                               int            startbit,
106                               int            stopbit,
107                               uint16_t newVal  ) {
108     btSetMaskBitBigE<uint16_t>(a,startbit,stopbit,newVal);
109}
110
111inline void  btSetMaskBitBigE8(uint8_t & a,
112                               int           startbit,
113                               int           stopbit,
114                               uint8_t newVal ) {
115     btSetMaskBitBigE<uint8_t>(a,startbit,stopbit,newVal);
116}
117
118
119
120template <class T>
121inline T btGetMaskBit(T a,
122                      int startbit,
123                      int stopbit ) {
124    return(btGetMaskBitBigE<T>(a,(sizeof(T)*8)-1-startbit,(sizeof(T)*8)-1-stopbit));
125}
126
127template <class T>
128inline void btSetMaskBit(T & a,
129                             int startbit,
130                             int stopbit,
131                             T  newval) {
132    btSetMaskBitBigE<T>(a,(sizeof(T)*8)-1-startbit,((sizeof(T)*8)-1-stopbit),newval);
133}
134
135
136inline unsigned int btGetMaskBit32(unsigned int a,
137                                       int startbit,
138                                       int stopbit ) {
139     return(btGetMaskBit<unsigned int>(a,startbit,stopbit));
140}
141
142inline unsigned short btGetMaskBit16(unsigned short a,
143                                       int startbit,
144                                       int stopbit ) {
145     return(btGetMaskBit<unsigned short>(a,startbit,stopbit));
146}
147
148inline uint8_t btGetMaskBit8(uint8_t a,
149                                       int startbit,
150                                       int stopbit ) {
151     return(btGetMaskBit<uint8_t>(a,startbit,stopbit));
152}
153
154
155inline void btSetMaskBit32(unsigned int & a,
156                               int           startbit,
157                               int           stopbit,
158                               unsigned int  newVal
159                                ) {
160     btSetMaskBit<unsigned int>(a,startbit,stopbit,newVal);
161}
162
163/* Notice:
164   startbit should be bigger (or equal) than stopbit
165
166count like big E
167
168*/
169inline void btSetMaskBit16(unsigned short & a,
170                               int            startbit,
171                               int            stopbit,
172                               unsigned short newVal  ) {
173     btSetMaskBit<unsigned short>(a,startbit,stopbit,newVal);
174}
175
176inline void  btSetMaskBit8(uint8_t & a,
177                               int           startbit,
178                               int           stopbit,
179                               uint8_t newVal ) {
180     btSetMaskBit<uint8_t>(a,startbit,stopbit,newVal);
181}
182
183#endif
184
185
186