]> err.no Git - util-linux/commitdiff
include: add bitops.h with swab{16,32,64} macros
authorKarel Zak <kzak@redhat.com>
Mon, 17 Dec 2007 12:18:12 +0000 (13:18 +0100)
committerKarel Zak <kzak@redhat.com>
Mon, 17 Dec 2007 12:18:12 +0000 (13:18 +0100)
Signed-off-by: Karel Zak <kzak@redhat.com>
include/Makefile.am
include/bitops.h [new file with mode: 0644]

index e2b99d6c0c3462114e23bebdf72b80a0a8d71350..4b86e529c1369823e00a203fb816d89fb88247fd 100644 (file)
@@ -2,4 +2,4 @@ include $(top_srcdir)/config/include-Makefile.am
 
 dist_noinst_HEADERS = carefulputc.h env.h linux_reboot.h md5.h \
        nls.h pathnames.h setproctitle.h widechar.h xstrncpy.h \
-       linux_version.h blkdev.h
+       linux_version.h blkdev.h bitops.h
diff --git a/include/bitops.h b/include/bitops.h
new file mode 100644 (file)
index 0000000..4ae0ff8
--- /dev/null
@@ -0,0 +1,51 @@
+#ifndef BITOPS_H
+#define BITOPS_H
+
+#include <linux/posix_types.h>
+#include <stdint.h>
+
+/*
+ * Byte swab macros (based on linux/byteorder/swab.h)
+ */
+#define swab16(x) \
+       ((uint16_t)( \
+               (((uint16_t)(x) & (uint16_t)0x00ffU) << 8) | \
+               (((uint16_t)(x) & (uint16_t)0xff00U) >> 8) ))
+
+#define swab32(x) \
+       ((uint32_t)( \
+               (((uint32_t)(x) & (uint32_t)0x000000ffUL) << 24) | \
+               (((uint32_t)(x) & (uint32_t)0x0000ff00UL) <<  8) | \
+               (((uint32_t)(x) & (uint32_t)0x00ff0000UL) >>  8) | \
+               (((uint32_t)(x) & (uint32_t)0xff000000UL) >> 24) ))
+
+#define swab64(x) \
+       ((uint64_t)( \
+               (uint64_t)(((uint64_t)(x) & (uint64_t)0x00000000000000ffULL) << 56) | \
+               (uint64_t)(((uint64_t)(x) & (uint64_t)0x000000000000ff00ULL) << 40) | \
+               (uint64_t)(((uint64_t)(x) & (uint64_t)0x0000000000ff0000ULL) << 24) | \
+               (uint64_t)(((uint64_t)(x) & (uint64_t)0x00000000ff000000ULL) <<  8) | \
+               (uint64_t)(((uint64_t)(x) & (uint64_t)0x000000ff00000000ULL) >>  8) | \
+               (uint64_t)(((uint64_t)(x) & (uint64_t)0x0000ff0000000000ULL) >> 24) | \
+               (uint64_t)(((uint64_t)(x) & (uint64_t)0x00ff000000000000ULL) >> 40) | \
+               (uint64_t)(((uint64_t)(x) & (uint64_t)0xff00000000000000ULL) >> 56) ))
+
+
+#ifdef WORDS_BIGENDIAN
+#define cpu_to_le16(x) swab16(x)
+#define cpu_to_le32(x) swab32(x)
+#define cpu_to_le64(x) swab64(x)
+#define cpu_to_be16(x) (x)
+#define cpu_to_be32(x) (x)
+#define cpu_to_be64(x) (x)
+#else
+#define cpu_to_le16(x) (x)
+#define cpu_to_le32(x) (x)
+#define cpu_to_le64(x) (x)
+#define cpu_to_be16(x) swab16(x)
+#define cpu_to_be32(x) swab32(x)
+#define cpu_to_be64(x) swab64(x)
+#endif
+
+#endif /* BITOPS_H */
+