diff options
author | Coly Li <colyli@suse.de> | 2019-12-12 20:46:24 +0800 |
---|---|---|
committer | Coly Li <colyli@suse.de> | 2019-12-12 20:46:24 +0800 |
commit | da01240e8d4e2ea1726601e1537354ccb29e9e66 (patch) | |
tree | 05042c9f757843c9e4ba389c7ef09742e2273152 | |
parent | 26765dca7225372ab6575a116d3851b5ad4801a5 (diff) | |
download | bcache-tools-da01240e8d4e2ea1726601e1537354ccb29e9e66.tar.gz |
bitwise.h: swap bitwise for different CPU endians
To support bcache-tools for s390x (big endian machine), the following
routines are required for writting super block out,
cpu_to_le64()
cpu_to_le32()
cpu_to_le16()
The code is cherry-picked from include/uapi/linux/swab.h of Linux
kernel v5.3 source code.
Signed-off-by: Coly Li <colyli@suse.de>
-rw-r--r-- | bitwise.h | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/bitwise.h b/bitwise.h new file mode 100644 index 00000000..968002f2 --- /dev/null +++ b/bitwise.h @@ -0,0 +1,55 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/** + * Author: Coly Li + * + * Cherry-picked from include/uapi/linux/swab.h of + * Linux kernel v5.3 source code. + */ + +#include <linux/types.h> +#include <asm/swab.h> + +#ifndef __BCH_BITWISE_H +#define __BCH_BITWISE_H + +#if __BYTE_ORDER == __LITTLE_ENDIAN +#define BCH_LITTLE_ENDIAN_LOCAL 1 +#else +#undef BCH_LITTLE_ENDIAN_LOCAL +#endif + + +#define __swab16(x) ((__u16)( \ + (((__u16)(x) & (__u16)0x00ffU) << 8) | \ + (((__u16)(x) & (__u16)0xff00U) >> 8))) + +#define __swab32(x) ((__u32)( \ + (((__u32)(x) & (__u32)0x000000ffUL) << 24) | \ + (((__u32)(x) & (__u32)0x0000ff00UL) << 8) | \ + (((__u32)(x) & (__u32)0x00ff0000UL) >> 8) | \ + (((__u32)(x) & (__u32)0xff000000UL) >> 24))) + +#define __swab64(x) ((__u64)( \ + (((__u64)(x) & (__u64)0x00000000000000ffULL) << 56) | \ + (((__u64)(x) & (__u64)0x000000000000ff00ULL) << 40) | \ + (((__u64)(x) & (__u64)0x0000000000ff0000ULL) << 24) | \ + (((__u64)(x) & (__u64)0x00000000ff000000ULL) << 8) | \ + (((__u64)(x) & (__u64)0x000000ff00000000ULL) >> 8) | \ + (((__u64)(x) & (__u64)0x0000ff0000000000ULL) >> 24) | \ + (((__u64)(x) & (__u64)0x00ff000000000000ULL) >> 40) | \ + (((__u64)(x) & (__u64)0xff00000000000000ULL) >> 56))) + + +#ifdef BCH_LITTLE_ENDIAN_LOCAL +/* For little endian*/ +#define cpu_to_le16(val) ((__le16)(val)) +#define cpu_to_le32(val) ((__le32)(val)) +#define cpu_to_le64(val) ((__le64)(val)) +#else +/* For big endian */ +#define cpu_to_le16(val) ((__be16)__swab16((__u16)(val))) +#define cpu_to_le32(val) ((__be32)__swab32((__u32)(val))) +#define cpu_to_le64(val) ((__be64)__swab64((__u64)(val))) +#endif + +#endif |