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 
00029 /*
00030  * GET_BIT_n(x) macros: extract the (n+1) th bit from byte x starting from
00031  *                      the right and do not right-shift it
00032  */
00033 
00034 #define GET_BIT_0(x)  ((*(x)) & 0x01)
00035 #define GET_BIT_1(x)  ((*(x)) & 0x02)
00036 #define GET_BIT_2(x)  ((*(x)) & 0x04)
00037 #define GET_BIT_3(x)  ((*(x)) & 0x08)
00038 #define GET_BIT_4(x)  ((*(x)) & 0x10)
00039 #define GET_BIT_5(x)  ((*(x)) & 0x20)
00040 #define GET_BIT_6(x)  ((*(x)) & 0x40)
00041 #define GET_BIT_7(x)  ((*(x)) & 0x80)
00042 
00043 
00044 /*
00045  * GET_BIT_0_m(x) macros: extract bits 0 to m included from byte x and do not
00046  *                        right-shift them
00047  */
00048 
00049 #define GET_BIT_0_2(x)  ((*(x)) & 0x07)
00050 #define GET_BIT_0_4(x)  ((*(x)) & 0x1f)
00051 #define GET_BIT_0_3(x)  ((*(x)) & 0x0f)
00052 #define GET_BIT_0_5(x)  ((*(x)) & 0x3f)
00053 #define GET_BIT_0_6(x)  ((*(x)) & 0x7f)
00054 #define GET_BIT_0_7(x)  ((*(x)) & 0xff)
00055 
00056 
00057 /*
00058  * GET_BIT_n_m(x) macros: extract bits n to m included from byte x and
00059  *                        right-shift them
00060  */
00061 
00062 #define GET_BIT_1_7(x)  ( ((*(x)) & 0xfe) >> 1 )
00063 #define GET_BIT_3_4(x)  ( ((*(x)) & 0x18) >> 3 )
00064 #define GET_BIT_3_5(x)  ( ((*(x)) & 0x38) >> 3 )
00065 #define GET_BIT_3_6(x)  ( ((*(x)) & 0x78) >> 3 )
00066 #define GET_BIT_3_7(x)  ( ((*(x)) & 0xf8) >> 3 )
00067 #define GET_BIT_4_7(x)  ( ((*(x)) & 0xf0) >> 4 )
00068 #define GET_BIT_5_7(x)  ( ((*(x)) & 0xe0) >> 5 )
00069 #define GET_BIT_6_7(x)  ( ((*(x)) & 0xc0) >> 6 )
00070 #define GET_BIT_4_6(x)  ( ((*(x)) & 0x70) >> 4 )
00071 
00072 
00073 /**
00074  * @brief Convert GET_BIT_* values to 0 or 1
00075  *
00076  * example: GET_REAL(GET_BIT_5(data_ptr));
00077  */
00078 #define GET_REAL(x)  ((x) ? 1 : 0)
00079 
00080 
00081 /**
00082  * @brief Convert GET_BIT_* values to boolean
00083  *
00084  * example: GET_BOOL(GET_BIT_5(data_ptr));
00085  */
00086 #define GET_BOOL(x)  ((x) ? true : false)
00087 
00088 
00089 /**
00090  * @brief Get the next 16 bits at the given memory location
00091  *        in Network Byte Order
00092  */
00093 #if WORDS_BIGENDIAN == 1
00094         #define GET_NEXT_16_BITS(x) \
00095                 ((((*(x)) << 8) & 0xff00) | ((*((x) + 1)) & 0x00ff))
00096 #else
00097         #define GET_NEXT_16_BITS(x) \
00098                 ((((*((x) + 1)) << 8) & 0xff00) | ((*(x)) & 0x00ff))
00099 #endif
00100 
00101 
00102 #endif
00103