iPXE
strings.h
Go to the documentation of this file.
1 #ifndef _STRINGS_H
2 #define _STRINGS_H
3 
4 /** @file
5  *
6  * String functions
7  *
8  */
9 
10 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
11 FILE_SECBOOT ( PERMITTED );
12 
13 #include <string.h>
14 #include <bits/strings.h>
15 
16 /**
17  * Find first (i.e. least significant) set bit
18  *
19  * @v x Value
20  * @ret lsb Least significant bit set in value (LSB=1), or zero
21  */
22 static inline __attribute__ (( always_inline )) int
23 __constant_ffsll ( unsigned long long x ) {
24  int r = 0;
25 
26  if ( ! ( x & 0x00000000ffffffffULL ) ) {
27  x >>= 32;
28  r += 32;
29  }
30  if ( ! ( x & 0x0000ffffUL ) ) {
31  x >>= 16;
32  r += 16;
33  }
34  if ( ! ( x & 0x00ff ) ) {
35  x >>= 8;
36  r += 8;
37  }
38  if ( ! ( x & 0x0f ) ) {
39  x >>= 4;
40  r += 4;
41  }
42  if ( ! ( x & 0x3 ) ) {
43  x >>= 2;
44  r += 2;
45  }
46  if ( ! ( x & 0x1 ) ) {
47  x >>= 1;
48  r += 1;
49  }
50  return ( x ? ( r + 1 ) : 0 );
51 }
52 
53 /**
54  * Find first (i.e. least significant) set bit
55  *
56  * @v x Value
57  * @ret lsb Least significant bit set in value (LSB=1), or zero
58  */
59 static inline __attribute__ (( always_inline )) int
60 __constant_ffsl ( unsigned long x ) {
61  return __constant_ffsll ( x );
62 }
63 
64 /**
65  * Find last (i.e. most significant) set bit
66  *
67  * @v x Value
68  * @ret msb Most significant bit set in value (LSB=1), or zero
69  */
70 static inline __attribute__ (( always_inline )) int
71 __constant_flsll ( unsigned long long x ) {
72  int r = 0;
73 
74  if ( x & 0xffffffff00000000ULL ) {
75  x >>= 32;
76  r += 32;
77  }
78  if ( x & 0xffff0000UL ) {
79  x >>= 16;
80  r += 16;
81  }
82  if ( x & 0xff00 ) {
83  x >>= 8;
84  r += 8;
85  }
86  if ( x & 0xf0 ) {
87  x >>= 4;
88  r += 4;
89  }
90  if ( x & 0xc ) {
91  x >>= 2;
92  r += 2;
93  }
94  if ( x & 0x2 ) {
95  x >>= 1;
96  r += 1;
97  }
98  return ( x ? ( r + 1 ) : 0 );
99 }
100 
101 /**
102  * Find last (i.e. most significant) set bit
103  *
104  * @v x Value
105  * @ret msb Most significant bit set in value (LSB=1), or zero
106  */
107 static inline __attribute__ (( always_inline )) int
108 __constant_flsl ( unsigned long x ) {
109  return __constant_flsll ( x );
110 }
111 
112 int __ffsll ( long long x );
113 int __ffsl ( long x );
114 int __flsll ( long long x );
115 int __flsl ( long x );
116 
117 /**
118  * Find first (i.e. least significant) set bit
119  *
120  * @v x Value
121  * @ret lsb Least significant bit set in value (LSB=1), or zero
122  */
123 #define ffsll( x ) \
124  ( __builtin_constant_p ( x ) ? __constant_ffsll ( x ) : __ffsll ( x ) )
125 
126 /**
127  * Find first (i.e. least significant) set bit
128  *
129  * @v x Value
130  * @ret lsb Least significant bit set in value (LSB=1), or zero
131  */
132 #define ffsl( x ) \
133  ( __builtin_constant_p ( x ) ? __constant_ffsl ( x ) : __ffsl ( x ) )
134 
135 /**
136  * Find first (i.e. least significant) set bit
137  *
138  * @v x Value
139  * @ret lsb Least significant bit set in value (LSB=1), or zero
140  */
141 #define ffs( x ) ffsl ( x )
142 
143 /**
144  * Find last (i.e. most significant) set bit
145  *
146  * @v x Value
147  * @ret msb Most significant bit set in value (LSB=1), or zero
148  */
149 #define flsll( x ) \
150  ( __builtin_constant_p ( x ) ? __constant_flsll ( x ) : __flsll ( x ) )
151 
152 /**
153  * Find last (i.e. most significant) set bit
154  *
155  * @v x Value
156  * @ret msb Most significant bit set in value (LSB=1), or zero
157  */
158 #define flsl( x ) \
159  ( __builtin_constant_p ( x ) ? __constant_flsl ( x ) : __flsl ( x ) )
160 
161 /**
162  * Find last (i.e. most significant) set bit
163  *
164  * @v x Value
165  * @ret msb Most significant bit set in value (LSB=1), or zero
166  */
167 #define fls( x ) flsl ( x )
168 
169 /**
170  * Copy memory
171  *
172  * @v src Source
173  * @v dest Destination
174  * @v len Length
175  */
176 static inline __attribute__ (( always_inline )) void
177 bcopy ( const void *src, void *dest, size_t len ) {
178  memmove ( dest, src, len );
179 }
180 
181 /**
182  * Zero memory
183  *
184  * @v dest Destination
185  * @v len Length
186  */
187 static inline __attribute__ (( always_inline )) void
188 bzero ( void *dest, size_t len ) {
189  memset ( dest, 0, len );
190 }
191 
192 int __pure strcasecmp ( const char *first, const char *second ) __nonnull;
193 int __pure strncasecmp ( const char *first, const char *second,
194  size_t max ) __nonnull;
195 
196 #endif /* _STRINGS_H */
#define __pure
Declare a function as pure - i.e.
Definition: compiler.h:578
#define max(x, y)
Definition: ath.h:41
uint32_t first
First block in range.
Definition: pccrr.h:15
static unsigned int x
Definition: pixbuf.h:63
int __flsl(long x)
static void size_t len
Definition: strings.h:177
int __flsll(long long x)
#define __nonnull
Declare a function's pointer parameters as non-null - i.e.
Definition: compiler.h:592
int __ffsl(long x)
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
int __pure strncasecmp(const char *first, const char *second, size_t max) __nonnull
Compare case-insensitive strings.
Definition: string.c:222
static const void * src
Definition: string.h:48
FILE_SECBOOT(PERMITTED)
static void * dest
Definition: strings.h:177
void * memmove(void *dest, const void *src, size_t len) __nonnull
int __pure strcasecmp(const char *first, const char *second) __nonnull
Compare case-insensitive strings.
Definition: string.c:209
static __attribute__((always_inline)) int __constant_ffsll(unsigned long long x)
Find first (i.e.
Definition: strings.h:22
int __ffsll(long long x)
String functions.
void * memset(void *dest, int character, size_t len) __nonnull
static const uint8_t r[3][4]
MD4 shift amounts.
Definition: md4.c:54