[zbin] Fix check for existence of most recent output byte
[ipxe.git] / src / include / strings.h
1 #ifndef _STRINGS_H
2 #define _STRINGS_H
3
4 /** @file
5 *
6 * String functions
7 *
8 */
9
10 FILE_LICENCE ( GPL2_OR_LATER );
11
12 #include <string.h>
13 #include <bits/strings.h>
14
15 /**
16 * Find last (i.e. most significant) set bit
17 *
18 * @v x Value
19 * @ret msb Most significant bit set in value (LSB=1), or zero
20 */
21 static inline __attribute__ (( always_inline )) int
22 __constant_flsll ( unsigned long long x ) {
23 int r = 0;
24
25 if ( x & 0xffffffff00000000ULL ) {
26 x >>= 32;
27 r += 32;
28 }
29 if ( x & 0xffff0000UL ) {
30 x >>= 16;
31 r += 16;
32 }
33 if ( x & 0xff00 ) {
34 x >>= 8;
35 r += 8;
36 }
37 if ( x & 0xf0 ) {
38 x >>= 4;
39 r += 4;
40 }
41 if ( x & 0xc ) {
42 x >>= 2;
43 r += 2;
44 }
45 if ( x & 0x2 ) {
46 x >>= 1;
47 r += 1;
48 }
49 if ( x & 0x1 ) {
50 r += 1;
51 }
52 return r;
53 }
54
55 /**
56 * Find last (i.e. most significant) set bit
57 *
58 * @v x Value
59 * @ret msb Most significant bit set in value (LSB=1), or zero
60 */
61 static inline __attribute__ (( always_inline )) int
62 __constant_flsl ( unsigned long x ) {
63 return __constant_flsll ( x );
64 }
65
66 int __flsll ( long long x );
67 int __flsl ( long x );
68
69 /**
70 * Find last (i.e. most significant) set bit
71 *
72 * @v x Value
73 * @ret msb Most significant bit set in value (LSB=1), or zero
74 */
75 #define flsll( x ) \
76 ( __builtin_constant_p ( x ) ? __constant_flsll ( x ) : __flsll ( x ) )
77
78 /**
79 * Find last (i.e. most significant) set bit
80 *
81 * @v x Value
82 * @ret msb Most significant bit set in value (LSB=1), or zero
83 */
84 #define flsl( x ) \
85 ( __builtin_constant_p ( x ) ? __constant_flsl ( x ) : __flsl ( x ) )
86
87 /**
88 * Find last (i.e. most significant) set bit
89 *
90 * @v x Value
91 * @ret msb Most significant bit set in value (LSB=1), or zero
92 */
93 #define fls( x ) flsl ( x )
94
95 /**
96 * Copy memory
97 *
98 * @v src Source
99 * @v dest Destination
100 * @v len Length
101 */
102 static inline __attribute__ (( always_inline )) void
103 bcopy ( const void *src, void *dest, size_t len ) {
104 memmove ( dest, src, len );
105 }
106
107 /**
108 * Zero memory
109 *
110 * @v dest Destination
111 * @v len Length
112 */
113 static inline __attribute__ (( always_inline )) void
114 bzero ( void *dest, size_t len ) {
115 memset ( dest, 0, len );
116 }
117
118 int __pure strcasecmp ( const char *first, const char *second ) __nonnull;
119
120 #endif /* _STRINGS_H */