summaryrefslogtreecommitdiffstats
path: root/src/include/strings.h
blob: fab26dc28ebf15622801791071f92656675f63bb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#ifndef _STRINGS_H
#define _STRINGS_H

/** @file
 *
 * String functions
 *
 */

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );

#include <string.h>
#include <bits/strings.h>

/**
 * Find first (i.e. least significant) set bit
 *
 * @v x			Value
 * @ret lsb		Least significant bit set in value (LSB=1), or zero
 */
static inline __attribute__ (( always_inline )) int
__constant_ffsll ( unsigned long long x ) {
	int r = 0;

	if ( ! ( x & 0x00000000ffffffffULL ) ) {
		x >>= 32;
		r += 32;
	}
	if ( ! ( x & 0x0000ffffUL ) ) {
		x >>= 16;
		r += 16;
	}
	if ( ! ( x & 0x00ff ) ) {
		x >>= 8;
		r += 8;
	}
	if ( ! ( x & 0x0f ) ) {
		x >>= 4;
		r += 4;
	}
	if ( ! ( x & 0x3 ) ) {
		x >>= 2;
		r += 2;
	}
	if ( ! ( x & 0x1 ) ) {
		x >>= 1;
		r += 1;
	}
	return ( x ? ( r + 1 ) : 0 );
}

/**
 * Find first (i.e. least significant) set bit
 *
 * @v x			Value
 * @ret lsb		Least significant bit set in value (LSB=1), or zero
 */
static inline __attribute__ (( always_inline )) int
__constant_ffsl ( unsigned long x ) {
	return __constant_ffsll ( x );
}

/**
 * Find last (i.e. most significant) set bit
 *
 * @v x			Value
 * @ret msb		Most significant bit set in value (LSB=1), or zero
 */
static inline __attribute__ (( always_inline )) int
__constant_flsll ( unsigned long long x ) {
	int r = 0;

	if ( x & 0xffffffff00000000ULL ) {
		x >>= 32;
		r += 32;
	}
	if ( x & 0xffff0000UL ) {
		x >>= 16;
		r += 16;
	}
	if ( x & 0xff00 ) {
		x >>= 8;
		r += 8;
	}
	if ( x & 0xf0 ) {
		x >>= 4;
		r += 4;
	}
	if ( x & 0xc ) {
		x >>= 2;
		r += 2;
	}
	if ( x & 0x2 ) {
		x >>= 1;
		r += 1;
	}
	return ( x ? ( r + 1 ) : 0 );
}

/**
 * Find last (i.e. most significant) set bit
 *
 * @v x			Value
 * @ret msb		Most significant bit set in value (LSB=1), or zero
 */
static inline __attribute__ (( always_inline )) int
__constant_flsl ( unsigned long x ) {
	return __constant_flsll ( x );
}

int __ffsll ( long long x );
int __ffsl ( long x );
int __flsll ( long long x );
int __flsl ( long x );

/**
 * Find first (i.e. least significant) set bit
 *
 * @v x			Value
 * @ret lsb		Least significant bit set in value (LSB=1), or zero
 */
#define ffsll( x ) \
	( __builtin_constant_p ( x ) ? __constant_ffsll ( x ) : __ffsll ( x ) )

/**
 * Find first (i.e. least significant) set bit
 *
 * @v x			Value
 * @ret lsb		Least significant bit set in value (LSB=1), or zero
 */
#define ffsl( x ) \
	( __builtin_constant_p ( x ) ? __constant_ffsl ( x ) : __ffsl ( x ) )

/**
 * Find first (i.e. least significant) set bit
 *
 * @v x			Value
 * @ret lsb		Least significant bit set in value (LSB=1), or zero
 */
#define ffs( x ) ffsl ( x )

/**
 * Find last (i.e. most significant) set bit
 *
 * @v x			Value
 * @ret msb		Most significant bit set in value (LSB=1), or zero
 */
#define flsll( x ) \
	( __builtin_constant_p ( x ) ? __constant_flsll ( x ) : __flsll ( x ) )

/**
 * Find last (i.e. most significant) set bit
 *
 * @v x			Value
 * @ret msb		Most significant bit set in value (LSB=1), or zero
 */
#define flsl( x ) \
	( __builtin_constant_p ( x ) ? __constant_flsl ( x ) : __flsl ( x ) )

/**
 * Find last (i.e. most significant) set bit
 *
 * @v x			Value
 * @ret msb		Most significant bit set in value (LSB=1), or zero
 */
#define fls( x ) flsl ( x )

/**
 * Copy memory
 *
 * @v src		Source
 * @v dest		Destination
 * @v len		Length
 */
static inline __attribute__ (( always_inline )) void
bcopy ( const void *src, void *dest, size_t len ) {
	memmove ( dest, src, len );
}

/**
 * Zero memory
 *
 * @v dest		Destination
 * @v len		Length
 */
static inline __attribute__ (( always_inline )) void
bzero ( void *dest, size_t len ) {
	memset ( dest, 0, len );
}

int __pure strcasecmp ( const char *first, const char *second ) __nonnull;

#endif /* _STRINGS_H */