include/bitopts: Use the operating system byteswapping functions
The swap macros are already in libc. Co-Author: Cristian Rodríguez <crrodriguez@opensuse.org> Signed-off-by: Karel Zak <kzak@redhat.com>
This commit is contained in:
parent
cd04b26bf8
commit
1a1eb4e188
|
@ -191,6 +191,9 @@ AC_CHECK_HEADERS([ \
|
|||
stdint.h \
|
||||
stdio_ext.h \
|
||||
stdlib.h \
|
||||
endian.h \
|
||||
byteswap.h \
|
||||
sys/endian.h \
|
||||
sys/disk.h \
|
||||
sys/disklabel.h \
|
||||
sys/file.h \
|
||||
|
|
160
include/bitops.h
160
include/bitops.h
|
@ -2,12 +2,107 @@
|
|||
#define BITOPS_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include <sys/param.h>
|
||||
|
||||
#if defined(HAVE_BYTESWAP_H)
|
||||
# include <byteswap.h>
|
||||
#endif
|
||||
|
||||
#if defined(HAVE_ENDIAN_H)
|
||||
# include <endian.h>
|
||||
#elif defined(HAVE_SYS_ENDIAN_H) /* BSDs have them here */
|
||||
# include <sys/endian.h>
|
||||
#endif
|
||||
|
||||
#if defined(__OpenBSD__)
|
||||
# include <sys/types.h>
|
||||
# define be16toh(x) betoh16(x)
|
||||
# define be32toh(x) betoh32(x)
|
||||
# define be64toh(x) betoh64(x)
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Fallbacks
|
||||
*/
|
||||
#ifndef bswap_16
|
||||
# define bswap_16(x) ((((x) & 0x00FF) << 8) | \
|
||||
(((x) & 0xFF00) >> 8))
|
||||
#endif
|
||||
|
||||
#ifndef bswap_32
|
||||
# define bswap_32(x) ((((x) & 0x000000FF) << 24) | \
|
||||
(((x) & 0x0000FF00) << 8) | \
|
||||
(((x) & 0x00FF0000) >> 8) | \
|
||||
(((x) & 0xFF000000) >> 24))
|
||||
#endif
|
||||
|
||||
#ifndef bswap_64
|
||||
# define bswap_64(x) ((((x) & 0x00000000000000FFULL) << 56) | \
|
||||
(((x) & 0x000000000000FF00ULL) << 40) | \
|
||||
(((x) & 0x0000000000FF0000ULL) << 24) | \
|
||||
(((x) & 0x00000000FF000000ULL) << 8) | \
|
||||
(((x) & 0x000000FF00000000ULL) >> 8) | \
|
||||
(((x) & 0x0000FF0000000000ULL) >> 24) | \
|
||||
(((x) & 0x00FF000000000000ULL) >> 40) | \
|
||||
(((x) & 0xFF00000000000000ULL) >> 56))
|
||||
#endif
|
||||
|
||||
#ifndef htobe16
|
||||
# if !defined(WORDS_BIGENDIAN)
|
||||
# define htobe16(x) bswap_16 (x)
|
||||
# define htole16(x) (x)
|
||||
# define be16toh(x) bswap_16 (x)
|
||||
# define le16toh(x) (x)
|
||||
# define htobe32(x) bswap_32 (x)
|
||||
# define htole32(x) (x)
|
||||
# define be32toh(x) bswap_32 (x)
|
||||
# define le32toh(x) (x)
|
||||
# define htobe64(x) bswap_64 (x)
|
||||
# define htole64(x) (x)
|
||||
# define be64toh(x) bswap_64 (x)
|
||||
# define le64toh(x) (x)
|
||||
# else
|
||||
# define htobe16(x) (x)
|
||||
# define htole16(x) bswap_16 (x)
|
||||
# define be16toh(x) (x)
|
||||
# define le16toh(x) bswap_16 (x)
|
||||
# define htobe32(x) (x)
|
||||
# define htole32(x) bswap_32 (x)
|
||||
# define be32toh(x) (x)
|
||||
# define le32toh(x) bswap_32 (x)
|
||||
# define htobe64(x) (x)
|
||||
# define htole64(x) bswap_64 (x)
|
||||
# define be64toh(x) (x)
|
||||
# define le64toh(x) bswap_64 (x)
|
||||
# endif
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Byte swab macros (based on linux/byteorder/swab.h)
|
||||
*/
|
||||
#define swab16(x) bswap_16(x)
|
||||
#define swab32(x) bswap_32(x)
|
||||
#define swab64(x) bswap_64(x)
|
||||
|
||||
#define cpu_to_le16(x) ((uint16_t) htole16(x))
|
||||
#define cpu_to_le32(x) ((uint32_t) htole32(x))
|
||||
#define cpu_to_le64(x) ((uint64_t) htole64(x))
|
||||
|
||||
#define cpu_to_be16(x) ((uint16_t) htobe16(x))
|
||||
#define cpu_to_be32(x) ((uint32_t) htobe32(x))
|
||||
#define cpu_to_be64(x) ((uint64_t) htobe64(x))
|
||||
|
||||
#define le16_to_cpu(x) ((uint16_t) le16toh(x))
|
||||
#define le32_to_cpu(x) ((uint32_t) le32toh(x))
|
||||
#define le64_to_cpu(x) ((uint64_t) le64toh(x))
|
||||
|
||||
#define be16_to_cpu(x) ((uint16_t) be16toh(x))
|
||||
#define be32_to_cpu(x) ((uint32_t) be32toh(x))
|
||||
#define be64_to_cpu(x) ((uint64_t) be64toh(x))
|
||||
|
||||
/*
|
||||
* Bit map related macros. Usually provided by libc.
|
||||
*/
|
||||
#include <sys/param.h>
|
||||
|
||||
#ifndef NBBY
|
||||
# define NBBY CHAR_BIT
|
||||
#endif
|
||||
|
@ -19,66 +114,5 @@
|
|||
# define isclr(a,i) (((a)[(i)/NBBY] & (1<<((i)%NBBY))) == 0)
|
||||
#endif
|
||||
|
||||
/*
|
||||
* 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) ((uint16_t)(x))
|
||||
#define cpu_to_be32(x) ((uint32_t)(x))
|
||||
#define cpu_to_be64(x) ((uint64_t)(x))
|
||||
|
||||
#define le16_to_cpu(x) swab16(x)
|
||||
#define le32_to_cpu(x) swab32(x)
|
||||
#define le64_to_cpu(x) swab64(x)
|
||||
#define be16_to_cpu(x) ((uint16_t)(x))
|
||||
#define be32_to_cpu(x) ((uint32_t)(x))
|
||||
#define be64_to_cpu(x) ((uint64_t)(x))
|
||||
|
||||
#else /* !WORDS_BIGENDIAN */
|
||||
|
||||
#define cpu_to_le16(x) ((uint16_t)(x))
|
||||
#define cpu_to_le32(x) ((uint32_t)(x))
|
||||
#define cpu_to_le64(x) ((uint64_t)(x))
|
||||
#define cpu_to_be16(x) swab16(x)
|
||||
#define cpu_to_be32(x) swab32(x)
|
||||
#define cpu_to_be64(x) swab64(x)
|
||||
|
||||
#define le16_to_cpu(x) ((uint16_t)(x))
|
||||
#define le32_to_cpu(x) ((uint32_t)(x))
|
||||
#define le64_to_cpu(x) ((uint64_t)(x))
|
||||
#define be16_to_cpu(x) swab16(x)
|
||||
#define be32_to_cpu(x) swab32(x)
|
||||
#define be64_to_cpu(x) swab64(x)
|
||||
|
||||
#endif /* WORDS_BIGENDIAN */
|
||||
|
||||
#endif /* BITOPS_H */
|
||||
|
||||
|
|
Loading…
Reference in New Issue