iPXE
nx_bitops.h
Go to the documentation of this file.
00001 #ifndef _NX_BITOPS_H
00002 #define _NX_BITOPS_H
00003 
00004 /*
00005  * Copyright (C) 2008 Michael Brown <mbrown@fensystems.co.uk>.
00006  *
00007  * This program is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License as
00009  * published by the Free Software Foundation; either version 2 of the
00010  * License, or any later version.
00011  *
00012  * This program is distributed in the hope that it will be useful, but
00013  * WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00020  * 02110-1301, USA.
00021  *
00022  * You can also choose to distribute this program under the terms of
00023  * the Unmodified Binary Distribution Licence (as given in the file
00024  * COPYING.UBDL), provided that you have satisfied its requirements.
00025  */
00026 
00027 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
00028 
00029 /**
00030  * @file
00031  *
00032  * NetXen bit operations
00033  *
00034  */
00035 
00036 /** Datatype used to represent a bit in the pseudo-structures */
00037 typedef unsigned char pseudo_bit_t;
00038 
00039 /**
00040  * Wrapper structure for pseudo_bit_t structures
00041  *
00042  * This structure provides a wrapper around pseudo_bit_t structures.
00043  * It has the correct size, and also encapsulates type information
00044  * about the underlying pseudo_bit_t-based structure, which allows the
00045  * NX_FILL etc. macros to work without requiring explicit type
00046  * information.
00047  */
00048 #define NX_PSEUDO_BIT_STRUCT( _structure )                                   \
00049         union {                                                              \
00050                 uint8_t bytes[ sizeof ( _structure ) / 8 ];                  \
00051                 uint64_t qwords[ sizeof ( _structure ) / 64 ];               \
00052                 _structure *dummy[0];                                        \
00053         } u;
00054 
00055 /** Get pseudo_bit_t structure type from wrapper structure pointer */
00056 #define NX_PSEUDO_STRUCT( _ptr )                                             \
00057         typeof ( *((_ptr)->u.dummy[0]) )
00058 
00059 /** Bit offset of a field within a pseudo_bit_t structure */
00060 #define NX_BIT_OFFSET( _ptr, _field )                                        \
00061         offsetof ( NX_PSEUDO_STRUCT ( _ptr ), _field )
00062 
00063 /** Bit width of a field within a pseudo_bit_t structure */
00064 #define NX_BIT_WIDTH( _ptr, _field )                                         \
00065         sizeof ( ( ( NX_PSEUDO_STRUCT ( _ptr ) * ) NULL )->_field )
00066 
00067 /** Qword offset of a field within a pseudo_bit_t structure */
00068 #define NX_QWORD_OFFSET( _ptr, _field )                                      \
00069         ( NX_BIT_OFFSET ( _ptr, _field ) / 64 )
00070 
00071 /** Qword bit offset of a field within a pseudo_bit_t structure
00072  *
00073  * Yes, using mod-64 would work, but would lose the check for the
00074  * error of specifying a mismatched field name and qword index.
00075  */
00076 #define NX_QWORD_BIT_OFFSET( _ptr, _index, _field )                          \
00077         ( NX_BIT_OFFSET ( _ptr, _field ) - ( 64 * (_index) ) )
00078 
00079 /** Bit mask for a field within a pseudo_bit_t structure */
00080 #define NX_BIT_MASK( _ptr, _field )                                          \
00081         ( ( ~( ( uint64_t ) 0 ) ) >>                                         \
00082           ( 64 - NX_BIT_WIDTH ( _ptr, _field ) ) )
00083 
00084 /*
00085  * Assemble native-endian qword from named fields and values
00086  *
00087  */
00088 
00089 #define NX_ASSEMBLE_1( _ptr, _index, _field, _value )                        \
00090         ( ( ( uint64_t) (_value) ) <<                                        \
00091           NX_QWORD_BIT_OFFSET ( _ptr, _index, _field ) )
00092 
00093 #define NX_ASSEMBLE_2( _ptr, _index, _field, _value, ... )                   \
00094         ( NX_ASSEMBLE_1 ( _ptr, _index, _field, _value ) |                   \
00095           NX_ASSEMBLE_1 ( _ptr, _index, __VA_ARGS__ ) )
00096 
00097 #define NX_ASSEMBLE_3( _ptr, _index, _field, _value, ... )                   \
00098         ( NX_ASSEMBLE_1 ( _ptr, _index, _field, _value ) |                   \
00099           NX_ASSEMBLE_2 ( _ptr, _index, __VA_ARGS__ ) )
00100 
00101 #define NX_ASSEMBLE_4( _ptr, _index, _field, _value, ... )                   \
00102         ( NX_ASSEMBLE_1 ( _ptr, _index, _field, _value ) |                   \
00103           NX_ASSEMBLE_3 ( _ptr, _index, __VA_ARGS__ ) )
00104 
00105 #define NX_ASSEMBLE_5( _ptr, _index, _field, _value, ... )                   \
00106         ( NX_ASSEMBLE_1 ( _ptr, _index, _field, _value ) |                   \
00107           NX_ASSEMBLE_4 ( _ptr, _index, __VA_ARGS__ ) )
00108 
00109 #define NX_ASSEMBLE_6( _ptr, _index, _field, _value, ... )                   \
00110         ( NX_ASSEMBLE_1 ( _ptr, _index, _field, _value ) |                   \
00111           NX_ASSEMBLE_5 ( _ptr, _index, __VA_ARGS__ ) )
00112 
00113 #define NX_ASSEMBLE_7( _ptr, _index, _field, _value, ... )                   \
00114         ( NX_ASSEMBLE_1 ( _ptr, _index, _field, _value ) |                   \
00115           NX_ASSEMBLE_6 ( _ptr, _index, __VA_ARGS__ ) )
00116 
00117 /*
00118  * Build native-endian (positive) qword bitmasks from named fields
00119  *
00120  */
00121 
00122 #define NX_MASK_1( _ptr, _index, _field )                            \
00123         ( NX_BIT_MASK ( _ptr, _field ) <<                            \
00124           NX_QWORD_BIT_OFFSET ( _ptr, _index, _field ) )
00125 
00126 #define NX_MASK_2( _ptr, _index, _field, ... )                       \
00127         ( NX_MASK_1 ( _ptr, _index, _field ) |                       \
00128           NX_MASK_1 ( _ptr, _index, __VA_ARGS__ ) )
00129 
00130 #define NX_MASK_3( _ptr, _index, _field, ... )                       \
00131         ( NX_MASK_1 ( _ptr, _index, _field ) |                       \
00132           NX_MASK_2 ( _ptr, _index, __VA_ARGS__ ) )
00133 
00134 #define NX_MASK_4( _ptr, _index, _field, ... )                       \
00135         ( NX_MASK_1 ( _ptr, _index, _field ) |                       \
00136           NX_MASK_3 ( _ptr, _index, __VA_ARGS__ ) )
00137 
00138 #define NX_MASK_5( _ptr, _index, _field, ... )                       \
00139         ( NX_MASK_1 ( _ptr, _index, _field ) |                       \
00140           NX_MASK_4 ( _ptr, _index, __VA_ARGS__ ) )
00141 
00142 #define NX_MASK_6( _ptr, _index, _field, ... )                       \
00143         ( NX_MASK_1 ( _ptr, _index, _field ) |                       \
00144           NX_MASK_5 ( _ptr, _index, __VA_ARGS__ ) )
00145 
00146 #define NX_MASK_7( _ptr, _index, _field, ... )                       \
00147         ( NX_MASK_1 ( _ptr, _index, _field ) |                       \
00148           NX_MASK_6 ( _ptr, _index, __VA_ARGS__ ) )
00149 
00150 /*
00151  * Populate big-endian qwords from named fields and values
00152  *
00153  */
00154 
00155 #define NX_FILL( _ptr, _index, _assembled )                                  \
00156         do {                                                                 \
00157                 uint64_t *__ptr = &(_ptr)->u.qwords[(_index)];               \
00158                 uint64_t __assembled = (_assembled);                         \
00159                 *__ptr = cpu_to_le64 ( __assembled );                        \
00160         } while ( 0 )
00161 
00162 #define NX_FILL_1( _ptr, _index, ... )                                       \
00163         NX_FILL ( _ptr, _index, NX_ASSEMBLE_1 ( _ptr, _index, __VA_ARGS__ ) )
00164 
00165 #define NX_FILL_2( _ptr, _index, ... )                                       \
00166         NX_FILL ( _ptr, _index, NX_ASSEMBLE_2 ( _ptr, _index, __VA_ARGS__ ) )
00167 
00168 #define NX_FILL_3( _ptr, _index, ... )                                       \
00169         NX_FILL ( _ptr, _index, NX_ASSEMBLE_3 ( _ptr, _index, __VA_ARGS__ ) )
00170 
00171 #define NX_FILL_4( _ptr, _index, ... )                                       \
00172         NX_FILL ( _ptr, _index, NX_ASSEMBLE_4 ( _ptr, _index, __VA_ARGS__ ) )
00173 
00174 #define NX_FILL_5( _ptr, _index, ... )                                       \
00175         NX_FILL ( _ptr, _index, NX_ASSEMBLE_5 ( _ptr, _index, __VA_ARGS__ ) )
00176 
00177 #define NX_FILL_6( _ptr, _index, ... )                                       \
00178         NX_FILL ( _ptr, _index, NX_ASSEMBLE_6 ( _ptr, _index, __VA_ARGS__ ) )
00179 
00180 #define NX_FILL_7( _ptr, _index, ... )                                       \
00181         NX_FILL ( _ptr, _index, NX_ASSEMBLE_7 ( _ptr, _index, __VA_ARGS__ ) )
00182 
00183 /** Extract value of named field */
00184 #define NX_GET64( _ptr, _field )                                             \
00185         ( {                                                                  \
00186                 unsigned int __index = NX_QWORD_OFFSET ( _ptr, _field );     \
00187                 uint64_t *__ptr = &(_ptr)->u.qwords[__index];                \
00188                 uint64_t __value = le64_to_cpu ( *__ptr );                   \
00189                 __value >>=                                                  \
00190                     NX_QWORD_BIT_OFFSET ( _ptr, __index, _field );           \
00191                 __value &= NX_BIT_MASK ( _ptr, _field );                     \
00192                 __value;                                                     \
00193         } )
00194 
00195 /** Extract value of named field (for fields up to the size of a long) */
00196 #define NX_GET( _ptr, _field )                                               \
00197         ( ( unsigned long ) NX_GET64 ( _ptr, _field ) )
00198 
00199 #endif /* _NX_BITOPS_H */