Browse Source

Add __constant_flsl(), because it's useful for finding out the next

power-of-two up from a given constant via
  ( 1 << fls ( constant - 1 ) )

fls(), flsl(), ffs() and ffsl() appear in strings.h according to
POSIX.
tags/v0.9.3
Michael Brown 18 years ago
parent
commit
2f0d412210
1 changed files with 54 additions and 0 deletions
  1. 54
    0
      src/include/strings.h

+ 54
- 0
src/include/strings.h View File

@@ -0,0 +1,54 @@
1
+#ifndef _STRINGS_H
2
+#define _STRINGS_H
3
+
4
+#include <limits.h>
5
+
6
+static inline __attribute__ (( always_inline )) int
7
+__constant_flsl ( unsigned long x ) {
8
+	int r = 0;
9
+
10
+#if ULONG_MAX > 0xffffffff
11
+	if ( x & 0xffffffff00000000UL ) {
12
+		x >>= 32;
13
+		r += 32;
14
+	}
15
+#endif
16
+	if ( x & 0xffff0000UL ) {
17
+		x >>= 16;
18
+		r += 16;
19
+	}
20
+	if ( x & 0xff00 ) {
21
+		x >>= 8;
22
+		r += 8;
23
+	}
24
+	if ( x & 0xf0 ) {
25
+		x >>= 4;
26
+		r += 4;
27
+	}
28
+	if ( x & 0xc ) {
29
+		x >>= 2;
30
+		r += 2;
31
+	}
32
+	if ( x & 0x2 ) {
33
+		x >>= 1;
34
+		r += 1;
35
+	}
36
+	if ( x & 0x1 ) {
37
+		r += 1;
38
+	}
39
+	return r;
40
+}
41
+
42
+#define __constant_fls(x) __constant_flsl(x)
43
+
44
+/* We don't actually have these functions yet */
45
+extern int __fls ( int x );
46
+extern int __flsl ( long x );
47
+
48
+#define flsl( x ) \
49
+	( __builtin_constant_p ( x ) ? __constant_flsl ( x ) : __flsl ( x ) )
50
+
51
+#define fls( x ) \
52
+	( __builtin_constant_p ( x ) ? __constant_fls ( x ) : __fls ( x ) )
53
+
54
+#endif /* _STRINGS_H */

Loading…
Cancel
Save