summaryrefslogtreecommitdiff
path: root/compiler.h
diff options
context:
space:
mode:
authorTobias Klauser <tklauser@distanz.ch>2010-11-10 17:10:07 +0100
committerTobias Klauser <tklauser@distanz.ch>2010-11-10 17:10:07 +0100
commit3cb7c05906b8eb8e884013b441f352256fded011 (patch)
tree4061936126e43746ed5d6157ead0eb778b256beb /compiler.h
parent32f507ce5f66dd9c89a45854688f46bde33c5e3d (diff)
First bunch of rework, stil work in progress
Diffstat (limited to 'compiler.h')
-rw-r--r--compiler.h61
1 files changed, 61 insertions, 0 deletions
diff --git a/compiler.h b/compiler.h
new file mode 100644
index 0000000..1005017
--- /dev/null
+++ b/compiler.h
@@ -0,0 +1,61 @@
+#ifndef _COMPILER_H_
+#define _COMPILER_H_
+
+#include <stdint.h>
+
+#ifdef __GNUC__
+# define __packed __attribute__ ((packed))
+# define __unused __attribute__ ((unused))
+# define __noreturn __attribute__ ((noreturn))
+# define likely(x) __builtin_expect(!!(x), 1)
+# define unlikely(x) __builtin_expect(!!(x), 0)
+
+static inline void prefetch(const void *ptr)
+{
+ /* We could use some arch specific instructions here, but a GCC builtin
+ is fine for now */
+ __builtin_prefetch(ptr, 0, 3);
+}
+
+static inline int32_t bswap_32(int32_t x)
+{
+ return __builtin_bswap32(x);
+}
+
+#else
+# define __packed
+# define __unused
+# define __noreturn
+# define likely(x) (x)
+# define unlikely(x) (x)
+
+static inline prefetch(const void *ptr)
+{
+ /* empty */
+}
+
+static inline int32_t bswap_32(int32_t x)
+{
+ int32_t ret = 0;
+
+ ret |= ((x & 0xFF000000) >> 24);
+ ret |= ((x & 0x00FF0000) >> 8);
+ ret |= ((x & 0x0000FF00) << 8);
+ ret |= ((x & 0x000000FF) << 24);
+ return ret;
+
+}
+
+#endif /* __GNUC__ */
+
+#ifndef offsetof
+# define offsetof(type, member) ((size_t) &((type *)0)->member)
+#endif /* offsetof */
+
+#ifndef container_of
+# define container_of(ptr, type, member) ({ \
+ const typeof( ((type *)0)->member ) *__mptr = (ptr); \
+ (type *)( (char *)__mptr - offsetof(type, member) );})
+#endif /* container_of */
+
+#endif /* _COMPILER_H_ */