summaryrefslogtreecommitdiffstats
path: root/src/include/strings.h
diff options
context:
space:
mode:
authorMichael Brown2006-04-25 01:00:32 +0200
committerMichael Brown2006-04-25 01:00:32 +0200
commit2f0d412210676d27eabd747741b1974ba9cef072 (patch)
tree2e0bf3212f8413bbc3e599048f44137299a76b5d /src/include/strings.h
parentAdded missing headers required for compilation in Etherboot. (diff)
downloadipxe-2f0d412210676d27eabd747741b1974ba9cef072.tar.gz
ipxe-2f0d412210676d27eabd747741b1974ba9cef072.tar.xz
ipxe-2f0d412210676d27eabd747741b1974ba9cef072.zip
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.
Diffstat (limited to 'src/include/strings.h')
-rw-r--r--src/include/strings.h54
1 files changed, 54 insertions, 0 deletions
diff --git a/src/include/strings.h b/src/include/strings.h
new file mode 100644
index 00000000..38db90be
--- /dev/null
+++ b/src/include/strings.h
@@ -0,0 +1,54 @@
+#ifndef _STRINGS_H
+#define _STRINGS_H
+
+#include <limits.h>
+
+static inline __attribute__ (( always_inline )) int
+__constant_flsl ( unsigned long x ) {
+ int r = 0;
+
+#if ULONG_MAX > 0xffffffff
+ if ( x & 0xffffffff00000000UL ) {
+ x >>= 32;
+ r += 32;
+ }
+#endif
+ 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;
+ }
+ if ( x & 0x1 ) {
+ r += 1;
+ }
+ return r;
+}
+
+#define __constant_fls(x) __constant_flsl(x)
+
+/* We don't actually have these functions yet */
+extern int __fls ( int x );
+extern int __flsl ( long x );
+
+#define flsl( x ) \
+ ( __builtin_constant_p ( x ) ? __constant_flsl ( x ) : __flsl ( x ) )
+
+#define fls( x ) \
+ ( __builtin_constant_p ( x ) ? __constant_fls ( x ) : __fls ( x ) )
+
+#endif /* _STRINGS_H */