Main Page | Modules | Alphabetical List | Data Structures | File List | Data Fields | Globals | Related Pages

bits_n_bytes.h

Go to the documentation of this file.
00001 
00002 /*
00003 ** Copyright (C) 2002 Russell Leighton
00004 **
00005 ** This program is free software; you can redistribute it and/or modify
00006 ** it under the terms of the GNU General Public License as published by
00007 ** the Free Software Foundation; either version 2 of the License, or
00008 ** (at your option) any later version.
00009 **
00010 ** This program is distributed in the hope that it will be useful,
00011 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 ** GNU General Public License for more details.
00014 **
00015 ** You should have received a copy of the GNU General Public License
00016 ** along with this program; if not, write to the Free Software
00017 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00018 */
00019 
00020 
00031 #ifndef  __AW_BYTES__
00032 #define  __AW_BYTES__
00033 
00034 /* ############################ bytes ############################ */
00035 #define SWAPBYTES16(word) (\
00036 (((word)&0x00ff) << 8) | \
00037 (((word)&0xff00) >> 8) \
00038 )
00039 
00040 #define SWAPBYTES32(word) (\
00041 (((word)&0x000000ff) << 24)| \
00042 (((word)&0x0000ff00) << 8) | \
00043 (((word)&0x00ff0000) >> 8) | \
00044 (((word)&0xff000000) >> 24) \
00045 )
00046 
00047 /* non-zero if byteasword is in word, where byte is held to far right 8bits */
00048 #define BYTEINWORD32(word, byteasword)(\
00049 (((word)&0x000000ff)         == (byteasword)) || \
00050 ((((word)&0x0000ff00) >> 8)  == (byteasword)) || \
00051 ((((word)&0x00ff0000) >> 16) == (byteasword)) || \
00052 ((((word)&0xff000000) >> 24) == (byteasword)) \
00053 )
00054 
00055 /* ############################ bits ############################ */
00056 
00057 
00058 /***********************************************************************\
00059 |macro:         COUNTBITS16                                             |
00060 |purpose:       count the bits that are 1 in a 16 bit lvalue            |
00061 |usage:         COUNTBITS16( bits );                                    |
00062 |   or:         count = COUNTBITS16( bits );                            |
00063 |note:          the lvalue argument always ends up with the result      |
00064 \***********************************************************************/
00065 #define COUNTBITS16(b) (\
00066         (b)=(((b)>> 1)&0x5555)+((b)&0x5555),\
00067         (b)=(((b)>> 2)&0x3333)+((b)&0x3333),\
00068         (b)=(((b)>> 4)&0x0707)+((b)&0x0707),\
00069         (b)=(((b)>> 8)&0x000f)+((b)&0x000f),\
00070         (b))
00071 
00072 /***********************************************************************\
00073 |macro:         COUNTBITS32                                             |
00074 |purpose:       count the bits that are 1 in a 32 bit lvalue            |
00075 |usage:         COUNTBITS32( bits );                                    |
00076 |   or:         count = COUNTBITS32( bits );                            |
00077 |note:          the lvalue argument always ends up with the result      |
00078 \***********************************************************************/
00079 #define COUNTBITS32(b) (\
00080         (b)=(((b)>> 1)&0x55555555)+((b)&0x55555555),\
00081         (b)=(((b)>> 2)&0x33333333)+((b)&0x33333333),\
00082         (b)=(((b)>> 4)&0x07070707)+((b)&0x07070707),\
00083         (b)=(((b)>> 8)&0x000f000f)+((b)&0x000f000f),\
00084         (b)=(((b)>>16)&0x0000001f)+((b)&0x0000001f),\
00085         (b))
00086 
00087 /***********************************************************************\
00088 |macro:         COUNTBITS64                                             |
00089 |purpose:       count the bits that are 1 in a 64 bit lvalue            |
00090 |usage:         COUNTBITS64( bits );                                    |
00091 |   or:         count = COUNTBITS64( bits );                            |
00092 |note:          the lvalue argument always ends up with the result      |
00093 \***********************************************************************/
00094 #define COUNTBITS64(b) (\
00095         (b)=(((b)>> 1)&0x5555555555555555)+((b)&0x5555555555555555),\
00096         (b)=(((b)>> 2)&0x3333333333333333)+((b)&0x3333333333333333),\
00097         (b)=(((b)>> 4)&0x0707070707070707)+((b)&0x0707070707070707),\
00098         (b)=(((b)>> 8)&0x000f000f000f000f)+((b)&0x000f000f000f000f),\
00099         (b)=(((b)>>16)&0x0000001f0000001f)+((b)&0x0000001f0000001f),\
00100         (b)=(((b)>>32)&0x000000000000003f)+((b)&0x000000000000003f),\
00101         (b))
00102 
00103 /***********************************************************************\
00104 |macro:         REVERSEBITS16                                           |
00105 |purpose:       reverse the bit order in a 16 bit lvalue                |
00106 |usage:         REVERSEBITS16( bits );                                  |
00107 |   or:         value = REVERSEBITS16( bits );                          |
00108 |note:          the lvalue argument always ends up with the result      |
00109 \***********************************************************************/
00110 #define REVERSEBITS16(b) (\
00111         (b)=(((b)>> 1)&0x5555)|(((b)&0x5555)<< 1),\
00112         (b)=(((b)>> 2)&0x3333)|(((b)&0x3333)<< 2),\
00113         (b)=(((b)>> 4)&0x0f0f)|(((b)&0x0f0f)<< 4),\
00114         (b)=(((b)>> 8)&0x00ff)|(((b)&0x00ff)<< 8),\
00115         (b))
00116 
00117 /***********************************************************************\
00118 |macro:         REVERSEBITS32                                           |
00119 |purpose:       reverse the bit order in a 32 bit lvalue                |
00120 |usage:         REVERSEBITS32( bits );                                  |
00121 |   or:         value = REVERSEBITS32( bits );                          |
00122 |note:          the lvalue argument always ends up with the result      |
00123 \***********************************************************************/
00124 #define REVERSEBITS32(b) (\
00125         (b)=(((b)>> 1)&0x55555555)|(((b)&0x55555555)<< 1),\
00126         (b)=(((b)>> 2)&0x33333333)|(((b)&0x33333333)<< 2),\
00127         (b)=(((b)>> 4)&0x0f0f0f0f)|(((b)&0x0f0f0f0f)<< 4),\
00128         (b)=(((b)>> 8)&0x00ff00ff)|(((b)&0x00ff00ff)<< 8),\
00129         (b)=(((b)>>16)&0x0000ffff)|(((b)&0x0000ffff)<<16),\
00130         (b))
00131 
00132 /***********************************************************************\
00133 |macro:         REVERSEBITS64                                           |
00134 |purpose:       reverse the bit order in a 64 bit lvalue                |
00135 |usage:         REVERSEBITS64( bits );                                  |
00136 |   or:         value = REVERSEBITS64( bits );                          |
00137 |note:          the lvalue argument always ends up with the result      |
00138 \***********************************************************************/
00139 #define REVERSEBITS64(b) (\
00140         (b)=(((b)>> 1)&0x5555555555555555)|(((b)&0x5555555555555555)<< 1),\
00141         (b)=(((b)>> 2)&0x3333333333333333)|(((b)&0x3333333333333333)<< 2),\
00142         (b)=(((b)>> 4)&0x0f0f0f0f0f0f0f0f)|(((b)&0x0f0f0f0f0f0f0f0f)<< 4),\
00143         (b)=(((b)>> 8)&0x00ff00ff00ff00ff)|(((b)&0x00ff00ff00ff00ff)<< 8),\
00144         (b)=(((b)>>16)&0x0000ffff0000ffff)|(((b)&0x0000ffff0000ffff)<<16),\
00145         (b)=(((b)>>32)&0x00000000ffffffff)|(((b)&0x00000000ffffffff)<<32),\
00146         (b))
00147 
00148 #endif
Aware 0.11.1 Copyright (C) 1998-2005 Russell Leighton (russ@elegant-software.com)