ROHC compression/decompression library
rohc_bit_ops.h
Go to the documentation of this file.
00001 /*
00002  * This program is free software; you can redistribute it and/or modify
00003  * it under the terms of the GNU General Public License as published by
00004  * the Free Software Foundation; either version 2 of the License, or
00005  * (at your option) any later version.
00006  *
00007  * This program is distributed in the hope that it will be useful,
00008  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00009  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00010  * GNU General Public License for more details.
00011  *
00012  * You should have received a copy of the GNU General Public License
00013  * along with this program; if not, write to the Free Software
00014  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00015  */
00016 
00017 /**
00018  * @file    rohc_bit_ops.h
00019  * @brief   Bitwised operations for ROHC compression/decompression
00020  * @author  Didier Barvaux <didier.barvaux@toulouse.viveris.com>
00021  * @author  The hackers from ROHC for Linux
00022  * @author  Didier Barvaux <didier@barvaux.org>
00023  */
00024 
00025 #ifndef ROHC_BIT_OPS_H
00026 #define ROHC_BIT_OPS_H
00027 
00028 #include <endian.h>
00029 
00030 
00031 /*
00032  * GET_BIT_n(x) macros: extract the (n+1) th bit from byte x starting from
00033  *                      the right and do not right-shift it
00034  */
00035 
00036 #define GET_BIT_0(x)  ((*(x)) & 0x01)
00037 #define GET_BIT_1(x)  ((*(x)) & 0x02)
00038 #define GET_BIT_2(x)  ((*(x)) & 0x04)
00039 #define GET_BIT_3(x)  ((*(x)) & 0x08)
00040 #define GET_BIT_4(x)  ((*(x)) & 0x10)
00041 #define GET_BIT_5(x)  ((*(x)) & 0x20)
00042 #define GET_BIT_6(x)  ((*(x)) & 0x40)
00043 #define GET_BIT_7(x)  ((*(x)) & 0x80)
00044 
00045 
00046 /*
00047  * GET_BIT_0_m(x) macros: extract bits 0 to m included from byte x and do not
00048  *                        right-shift them
00049  */
00050 
00051 #define GET_BIT_0_2(x)  ((*(x)) & 0x07)
00052 #define GET_BIT_0_4(x)  ((*(x)) & 0x1f)
00053 #define GET_BIT_0_3(x)  ((*(x)) & 0x0f)
00054 #define GET_BIT_0_5(x)  ((*(x)) & 0x3f)
00055 #define GET_BIT_0_6(x)  ((*(x)) & 0x7f)
00056 #define GET_BIT_0_7(x)  ((*(x)) & 0xff)
00057 
00058 
00059 /*
00060  * GET_BIT_n_m(x) macros: extract bits n to m included from byte x and
00061  *                        right-shift them
00062  */
00063 
00064 #define GET_BIT_1_7(x)  ( ((*(x)) & 0xfe) >> 1 )
00065 #define GET_BIT_3_4(x)  ( ((*(x)) & 0x18) >> 3 )
00066 #define GET_BIT_3_5(x)  ( ((*(x)) & 0x38) >> 3 )
00067 #define GET_BIT_3_6(x)  ( ((*(x)) & 0x78) >> 3 )
00068 #define GET_BIT_3_7(x)  ( ((*(x)) & 0xf8) >> 3 )
00069 #define GET_BIT_4_7(x)  ( ((*(x)) & 0xf0) >> 4 )
00070 #define GET_BIT_5_7(x)  ( ((*(x)) & 0xe0) >> 5 )
00071 #define GET_BIT_6_7(x)  ( ((*(x)) & 0xc0) >> 6 )
00072 #define GET_BIT_4_6(x)  ( ((*(x)) & 0x70) >> 4 )
00073 
00074 
00075 /**
00076  * @brief Convert GET_BIT_* values to 0 or 1
00077  *
00078  * example: GET_REAL(GET_BIT_5(data_ptr));
00079  */
00080 #define GET_REAL(x)  ((x) ? 1 : 0)
00081 
00082 
00083 /**
00084  * @brief Get the next 16 bits at the given memory location
00085  *        in Network Byte Order
00086  */
00087 #if __BYTE_ORDER == __LITTLE_ENDIAN
00088         #define GET_NEXT_16_BITS(x) \
00089                 ((((*((x) + 1)) << 8) & 0xff00) | ((*(x)) & 0x00ff))
00090 #elif __BYTE_ORDER == __BIG_ENDIAN
00091         #define GET_NEXT_16_BITS(x) \
00092                 ((((*(x)) << 8) & 0xff00) | ((*((x) + 1)) & 0x00ff))
00093 #else
00094         #error "Adjust your <bits/endian.h> defines"
00095 #endif
00096 
00097 
00098 #endif
00099