iPXE
strings.h
Go to the documentation of this file.
00001 #ifndef _STRINGS_H
00002 #define _STRINGS_H
00003 
00004 /** @file
00005  *
00006  * String functions
00007  *
00008  */
00009 
00010 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
00011 
00012 #include <string.h>
00013 #include <bits/strings.h>
00014 
00015 /**
00016  * Find first (i.e. least significant) set bit
00017  *
00018  * @v x                 Value
00019  * @ret lsb             Least significant bit set in value (LSB=1), or zero
00020  */
00021 static inline __attribute__ (( always_inline )) int
00022 __constant_ffsll ( unsigned long long x ) {
00023         int r = 0;
00024 
00025         if ( ! ( x & 0x00000000ffffffffULL ) ) {
00026                 x >>= 32;
00027                 r += 32;
00028         }
00029         if ( ! ( x & 0x0000ffffUL ) ) {
00030                 x >>= 16;
00031                 r += 16;
00032         }
00033         if ( ! ( x & 0x00ff ) ) {
00034                 x >>= 8;
00035                 r += 8;
00036         }
00037         if ( ! ( x & 0x0f ) ) {
00038                 x >>= 4;
00039                 r += 4;
00040         }
00041         if ( ! ( x & 0x3 ) ) {
00042                 x >>= 2;
00043                 r += 2;
00044         }
00045         if ( ! ( x & 0x1 ) ) {
00046                 x >>= 1;
00047                 r += 1;
00048         }
00049         return ( x ? ( r + 1 ) : 0 );
00050 }
00051 
00052 /**
00053  * Find first (i.e. least significant) set bit
00054  *
00055  * @v x                 Value
00056  * @ret lsb             Least significant bit set in value (LSB=1), or zero
00057  */
00058 static inline __attribute__ (( always_inline )) int
00059 __constant_ffsl ( unsigned long x ) {
00060         return __constant_ffsll ( x );
00061 }
00062 
00063 /**
00064  * Find last (i.e. most significant) set bit
00065  *
00066  * @v x                 Value
00067  * @ret msb             Most significant bit set in value (LSB=1), or zero
00068  */
00069 static inline __attribute__ (( always_inline )) int
00070 __constant_flsll ( unsigned long long x ) {
00071         int r = 0;
00072 
00073         if ( x & 0xffffffff00000000ULL ) {
00074                 x >>= 32;
00075                 r += 32;
00076         }
00077         if ( x & 0xffff0000UL ) {
00078                 x >>= 16;
00079                 r += 16;
00080         }
00081         if ( x & 0xff00 ) {
00082                 x >>= 8;
00083                 r += 8;
00084         }
00085         if ( x & 0xf0 ) {
00086                 x >>= 4;
00087                 r += 4;
00088         }
00089         if ( x & 0xc ) {
00090                 x >>= 2;
00091                 r += 2;
00092         }
00093         if ( x & 0x2 ) {
00094                 x >>= 1;
00095                 r += 1;
00096         }
00097         return ( x ? ( r + 1 ) : 0 );
00098 }
00099 
00100 /**
00101  * Find last (i.e. most significant) set bit
00102  *
00103  * @v x                 Value
00104  * @ret msb             Most significant bit set in value (LSB=1), or zero
00105  */
00106 static inline __attribute__ (( always_inline )) int
00107 __constant_flsl ( unsigned long x ) {
00108         return __constant_flsll ( x );
00109 }
00110 
00111 int __ffsll ( long long x );
00112 int __ffsl ( long x );
00113 int __flsll ( long long x );
00114 int __flsl ( long x );
00115 
00116 /**
00117  * Find first (i.e. least significant) set bit
00118  *
00119  * @v x                 Value
00120  * @ret lsb             Least significant bit set in value (LSB=1), or zero
00121  */
00122 #define ffsll( x ) \
00123         ( __builtin_constant_p ( x ) ? __constant_ffsll ( x ) : __ffsll ( x ) )
00124 
00125 /**
00126  * Find first (i.e. least significant) set bit
00127  *
00128  * @v x                 Value
00129  * @ret lsb             Least significant bit set in value (LSB=1), or zero
00130  */
00131 #define ffsl( x ) \
00132         ( __builtin_constant_p ( x ) ? __constant_ffsl ( x ) : __ffsl ( x ) )
00133 
00134 /**
00135  * Find first (i.e. least significant) set bit
00136  *
00137  * @v x                 Value
00138  * @ret lsb             Least significant bit set in value (LSB=1), or zero
00139  */
00140 #define ffs( x ) ffsl ( x )
00141 
00142 /**
00143  * Find last (i.e. most significant) set bit
00144  *
00145  * @v x                 Value
00146  * @ret msb             Most significant bit set in value (LSB=1), or zero
00147  */
00148 #define flsll( x ) \
00149         ( __builtin_constant_p ( x ) ? __constant_flsll ( x ) : __flsll ( x ) )
00150 
00151 /**
00152  * Find last (i.e. most significant) set bit
00153  *
00154  * @v x                 Value
00155  * @ret msb             Most significant bit set in value (LSB=1), or zero
00156  */
00157 #define flsl( x ) \
00158         ( __builtin_constant_p ( x ) ? __constant_flsl ( x ) : __flsl ( x ) )
00159 
00160 /**
00161  * Find last (i.e. most significant) set bit
00162  *
00163  * @v x                 Value
00164  * @ret msb             Most significant bit set in value (LSB=1), or zero
00165  */
00166 #define fls( x ) flsl ( x )
00167 
00168 /**
00169  * Copy memory
00170  *
00171  * @v src               Source
00172  * @v dest              Destination
00173  * @v len               Length
00174  */
00175 static inline __attribute__ (( always_inline )) void
00176 bcopy ( const void *src, void *dest, size_t len ) {
00177         memmove ( dest, src, len );
00178 }
00179 
00180 /**
00181  * Zero memory
00182  *
00183  * @v dest              Destination
00184  * @v len               Length
00185  */
00186 static inline __attribute__ (( always_inline )) void
00187 bzero ( void *dest, size_t len ) {
00188         memset ( dest, 0, len );
00189 }
00190 
00191 int __pure strcasecmp ( const char *first, const char *second ) __nonnull;
00192 
00193 #endif /* _STRINGS_H */