From: Karel Zak Date: Mon, 17 Dec 2007 12:18:12 +0000 (+0100) Subject: include: add bitops.h with swab{16,32,64} macros X-Git-Url: https://err.no/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=1ed3367f6167c3e5371ce4a24e61a4194c5f99fc;p=util-linux include: add bitops.h with swab{16,32,64} macros Signed-off-by: Karel Zak --- diff --git a/include/Makefile.am b/include/Makefile.am index e2b99d6c..4b86e529 100644 --- a/include/Makefile.am +++ b/include/Makefile.am @@ -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 index 00000000..4ae0ff8e --- /dev/null +++ b/include/bitops.h @@ -0,0 +1,51 @@ +#ifndef BITOPS_H +#define BITOPS_H + +#include +#include + +/* + * 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 */ +