18b52a31eSHanoh Haim#ifndef BIT_MAN_H
28b52a31eSHanoh Haim#define BIT_MAN_H
38b52a31eSHanoh Haim/*
48b52a31eSHanoh HaimCopyright (c) 2015-2015 Cisco Systems, Inc.
58b52a31eSHanoh Haim
68b52a31eSHanoh HaimLicensed under the Apache License, Version 2.0 (the "License");
78b52a31eSHanoh Haimyou may not use this file except in compliance with the License.
88b52a31eSHanoh HaimYou may obtain a copy of the License at
98b52a31eSHanoh Haim
108b52a31eSHanoh Haim    http://www.apache.org/licenses/LICENSE-2.0
118b52a31eSHanoh Haim
128b52a31eSHanoh HaimUnless required by applicable law or agreed to in writing, software
138b52a31eSHanoh Haimdistributed under the License is distributed on an "AS IS" BASIS,
148b52a31eSHanoh HaimWITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
158b52a31eSHanoh HaimSee the License for the specific language governing permissions and
168b52a31eSHanoh Haimlimitations under the License.
178b52a31eSHanoh Haim*/
188b52a31eSHanoh Haim
198b52a31eSHanoh Haim
208b52a31eSHanoh Haim
218b52a31eSHanoh Haimtemplate <class T>
228b52a31eSHanoh Haiminline T btGetShift(unsigned int stopbit){
238b52a31eSHanoh Haim    return(T)((sizeof(T)*8)-stopbit-1);
248b52a31eSHanoh Haim}
258b52a31eSHanoh Haim
268b52a31eSHanoh Haim//this function return mask with 1 from the start bit
278b52a31eSHanoh Haim// 0 in this bit are the MSB - big edian mode
288b52a31eSHanoh Haim// if T is int (32) bit 31 is the last
298b52a31eSHanoh Haimtemplate <class T>
308b52a31eSHanoh Haiminline T btGetMask(unsigned int startbit,
318b52a31eSHanoh Haim				   unsigned int stopbit){
328b52a31eSHanoh Haim    register T shft=btGetShift<T>(stopbit);
338b52a31eSHanoh Haim    return ((T)( (((1<<(stopbit-startbit+1))-1)<<shft)) );
348b52a31eSHanoh Haim}
358b52a31eSHanoh Haim
368b52a31eSHanoh Haim
378b52a31eSHanoh Haim//this function are used for big endian mode
388b52a31eSHanoh Haim// e.x btGetMaskBitBigE(0x80000000,0,0)==1
398b52a31eSHanoh Haim// e.x btGetMaskBitBigE(0xc0000000,0,1)==3
408b52a31eSHanoh Haimtemplate <class T>
418b52a31eSHanoh Haiminline T btGetMaskBitBigE(T a,
428b52a31eSHanoh Haim                          int startbit,
438b52a31eSHanoh Haim                          int stopbit ) {
448b52a31eSHanoh Haim    if((sizeof(T) * 8) == (stopbit - startbit + 1))// the case where the mask is the whole data
458b52a31eSHanoh Haim    {
468b52a31eSHanoh Haim        return a;
478b52a31eSHanoh Haim    }
488b52a31eSHanoh Haim    else
498b52a31eSHanoh Haim    {
508b52a31eSHanoh Haim    	register T mask=btGetMask<T>(startbit,stopbit);
518b52a31eSHanoh Haim    	register T shift=btGetShift<T>(stopbit);
528b52a31eSHanoh Haim    	T result;
538b52a31eSHanoh Haim    	result=((a & mask) >>shift);
548b52a31eSHanoh Haim    	return(result);
558b52a31eSHanoh Haim    }
568b52a31eSHanoh Haim}
578b52a31eSHanoh Haim
588b52a31eSHanoh Haiminline uint32_t btGetMaskBitBigE32(uint32_t a,
598b52a31eSHanoh Haim                                       int startbit,
608b52a31eSHanoh Haim                                       int stopbit ) {
618b52a31eSHanoh Haim    return(btGetMaskBitBigE<uint32_t>(a,startbit,stopbit));
628b52a31eSHanoh Haim}
638b52a31eSHanoh Haim
648b52a31eSHanoh Haiminline unsigned short btGetMaskBitBigE16(uint16_t a,
658b52a31eSHanoh Haim                                       int startbit,
668b52a31eSHanoh Haim                                       int stopbit ) {
678b52a31eSHanoh Haim     return(btGetMaskBitBigE<uint16_t>(a,startbit,stopbit));
688b52a31eSHanoh Haim}
698b52a31eSHanoh Haim
708b52a31eSHanoh Haiminline uint8_t btGetMaskBitBigE8(uint8_t a,
718b52a31eSHanoh Haim                                       int startbit,
728b52a31eSHanoh Haim                                       int stopbit ) {
738b52a31eSHanoh Haim     return(btGetMaskBitBigE<uint8_t>(a,startbit,stopbit));
748b52a31eSHanoh Haim}
758b52a31eSHanoh Haim
768b52a31eSHanoh Haim
778b52a31eSHanoh Haimtemplate <class T>
788b52a31eSHanoh Haiminline void btSetMaskBitBigE(T & a,
798b52a31eSHanoh Haim                             int startbit,
808b52a31eSHanoh Haim                             int stopbit,
818b52a31eSHanoh Haim                             T  newval) {
828b52a31eSHanoh Haim    if((sizeof(T) * 8) == (stopbit - startbit + 1))// the case where the mask is the whole data
838b52a31eSHanoh Haim    {
848b52a31eSHanoh Haim        a = newval;
858b52a31eSHanoh Haim    }
868b52a31eSHanoh Haim    else
878b52a31eSHanoh Haim    {
888b52a31eSHanoh Haim	    register T mask=btGetMask<T>(startbit,stopbit);
898b52a31eSHanoh Haim	    register T shift=btGetShift<T>(stopbit);
908b52a31eSHanoh Haim	    a=((a & ~mask) | ( (newval <<shift) & mask ) );
918b52a31eSHanoh Haim    }
928b52a31eSHanoh Haim}
938b52a31eSHanoh Haim
948b52a31eSHanoh Haim
958b52a31eSHanoh Haim
968b52a31eSHanoh Haiminline void btSetMaskBitBigE32(uint32_t & a,
978b52a31eSHanoh Haim                               int           startbit,
988b52a31eSHanoh Haim                               int           stopbit,
998b52a31eSHanoh Haim                               uint32_t  newVal
1008b52a31eSHanoh Haim                                ) {
1018b52a31eSHanoh Haim     btSetMaskBitBigE<uint32_t>(a,startbit,stopbit,newVal);
1028b52a31eSHanoh Haim}
1038b52a31eSHanoh Haim
1048b52a31eSHanoh Haiminline void btSetMaskBitBigE16(uint16_t  & a,
1058b52a31eSHanoh Haim                               int            startbit,
1068b52a31eSHanoh Haim                               int            stopbit,
1078b52a31eSHanoh Haim                               uint16_t newVal  ) {
1088b52a31eSHanoh Haim     btSetMaskBitBigE<uint16_t>(a,startbit,stopbit,newVal);
1098b52a31eSHanoh Haim}
1108b52a31eSHanoh Haim
1118b52a31eSHanoh Haiminline void  btSetMaskBitBigE8(uint8_t & a,
1128b52a31eSHanoh Haim                               int           startbit,
1138b52a31eSHanoh Haim                               int           stopbit,
1148b52a31eSHanoh Haim                               uint8_t newVal ) {
1158b52a31eSHanoh Haim     btSetMaskBitBigE<uint8_t>(a,startbit,stopbit,newVal);
1168b52a31eSHanoh Haim}
1178b52a31eSHanoh Haim
1188b52a31eSHanoh Haim
1198b52a31eSHanoh Haim
1208b52a31eSHanoh Haimtemplate <class T>
1218b52a31eSHanoh Haiminline T btGetMaskBit(T a,
1228b52a31eSHanoh Haim                      int startbit,
1238b52a31eSHanoh Haim                      int stopbit ) {
1248b52a31eSHanoh Haim    return(btGetMaskBitBigE<T>(a,(sizeof(T)*8)-1-startbit,(sizeof(T)*8)-1-stopbit));
1258b52a31eSHanoh Haim}
1268b52a31eSHanoh Haim
1278b52a31eSHanoh Haimtemplate <class T>
1288b52a31eSHanoh Haiminline void btSetMaskBit(T & a,
1298b52a31eSHanoh Haim                             int startbit,
1308b52a31eSHanoh Haim                             int stopbit,
1318b52a31eSHanoh Haim                             T  newval) {
1328b52a31eSHanoh Haim    btSetMaskBitBigE<T>(a,(sizeof(T)*8)-1-startbit,((sizeof(T)*8)-1-stopbit),newval);
1338b52a31eSHanoh Haim}
1348b52a31eSHanoh Haim
1358b52a31eSHanoh Haim
1368b52a31eSHanoh Haiminline unsigned int btGetMaskBit32(unsigned int a,
1378b52a31eSHanoh Haim                                       int startbit,
1388b52a31eSHanoh Haim                                       int stopbit ) {
1398b52a31eSHanoh Haim     return(btGetMaskBit<unsigned int>(a,startbit,stopbit));
1408b52a31eSHanoh Haim}
1418b52a31eSHanoh Haim
1428b52a31eSHanoh Haiminline unsigned short btGetMaskBit16(unsigned short a,
1438b52a31eSHanoh Haim                                       int startbit,
1448b52a31eSHanoh Haim                                       int stopbit ) {
1458b52a31eSHanoh Haim     return(btGetMaskBit<unsigned short>(a,startbit,stopbit));
1468b52a31eSHanoh Haim}
1478b52a31eSHanoh Haim
1488b52a31eSHanoh Haiminline uint8_t btGetMaskBit8(uint8_t a,
1498b52a31eSHanoh Haim                                       int startbit,
1508b52a31eSHanoh Haim                                       int stopbit ) {
1518b52a31eSHanoh Haim     return(btGetMaskBit<uint8_t>(a,startbit,stopbit));
1528b52a31eSHanoh Haim}
1538b52a31eSHanoh Haim
1548b52a31eSHanoh Haim
1558b52a31eSHanoh Haiminline void btSetMaskBit32(unsigned int & a,
1568b52a31eSHanoh Haim                               int           startbit,
1578b52a31eSHanoh Haim                               int           stopbit,
1588b52a31eSHanoh Haim                               unsigned int  newVal
1598b52a31eSHanoh Haim                                ) {
1608b52a31eSHanoh Haim     btSetMaskBit<unsigned int>(a,startbit,stopbit,newVal);
1618b52a31eSHanoh Haim}
1628b52a31eSHanoh Haim
163c181e882SIdo Barnea/* Notice:
164c181e882SIdo Barnea   startbit should be bigger (or equal) than stopbit
1658b52a31eSHanoh Haim
1668b52a31eSHanoh Haimcount like big E
1678b52a31eSHanoh Haim
1688b52a31eSHanoh Haim*/
1698b52a31eSHanoh Haiminline void btSetMaskBit16(unsigned short & a,
1708b52a31eSHanoh Haim                               int            startbit,
1718b52a31eSHanoh Haim                               int            stopbit,
1728b52a31eSHanoh Haim                               unsigned short newVal  ) {
1738b52a31eSHanoh Haim     btSetMaskBit<unsigned short>(a,startbit,stopbit,newVal);
1748b52a31eSHanoh Haim}
1758b52a31eSHanoh Haim
1768b52a31eSHanoh Haiminline void  btSetMaskBit8(uint8_t & a,
1778b52a31eSHanoh Haim                               int           startbit,
1788b52a31eSHanoh Haim                               int           stopbit,
1798b52a31eSHanoh Haim                               uint8_t newVal ) {
1808b52a31eSHanoh Haim     btSetMaskBit<uint8_t>(a,startbit,stopbit,newVal);
1818b52a31eSHanoh Haim}
1828b52a31eSHanoh Haim
1838b52a31eSHanoh Haim#endif
1848b52a31eSHanoh Haim
1858b52a31eSHanoh Haim