Whamcloud - gitweb
LU-2675 libcfs: add libcfs/byteorder.h
[fs/lustre-release.git] / libcfs / include / libcfs / byteorder.h
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2014, Intel Corporation.
24  * Author: John L. Hammond <john.hammond@intel.com>
25  */
26 #ifndef _LIBCFS_BYTEORDER_H
27 #define _LIBCFS_BYTERODER_H
28
29 #ifdef __KERNEL__
30 # include <asm/byteorder.h>
31 #else /* __KERNEL__ */
32
33 # include <endian.h>
34 # include <byteswap.h>
35
36 # define __swab16(x) bswap_16(x)
37 # define __swab32(x) bswap_32(x)
38 # define __swab64(x) bswap_64(x)
39 # define __swab16s(x)                           \
40         do {                                    \
41                 *(x) = bswap_16(*(x));          \
42         } while (0)
43 # define __swab32s(x)                           \
44         do {                                    \
45                 *(x) = bswap_32(*(x));          \
46         } while (0)
47 # define __swab64s(x)                           \
48         do {                                    \
49                 *(x) = bswap_64(*(x));          \
50         } while (0)
51 # if __BYTE_ORDER == __LITTLE_ENDIAN
52 #  define le16_to_cpu(x) (x)
53 #  define cpu_to_le16(x) (x)
54 #  define le32_to_cpu(x) (x)
55 #  define cpu_to_le32(x) (x)
56 #  define le64_to_cpu(x) (x)
57 #  define cpu_to_le64(x) (x)
58
59 #  define be16_to_cpu(x) bswap_16(x)
60 #  define cpu_to_be16(x) bswap_16(x)
61 #  define be32_to_cpu(x) bswap_32(x)
62 #  define cpu_to_be32(x) bswap_32(x)
63 #  define be64_to_cpu(x) ((__u64)bswap_64(x))
64 #  define cpu_to_be64(x) ((__u64)bswap_64(x))
65 # elif __BYTE_ORDER == __BIG_ENDIAN
66 #  define le16_to_cpu(x) bswap_16(x)
67 #  define cpu_to_le16(x) bswap_16(x)
68 #  define le32_to_cpu(x) bswap_32(x)
69 #  define cpu_to_le32(x) bswap_32(x)
70 #  define le64_to_cpu(x) ((__u64)bswap_64(x))
71 #  define cpu_to_le64(x) ((__u64)bswap_64(x))
72
73 #  define be16_to_cpu(x) (x)
74 #  define cpu_to_be16(x) (x)
75 #  define be32_to_cpu(x) (x)
76 #  define cpu_to_be32(x) (x)
77 #  define be64_to_cpu(x) (x)
78 #  define cpu_to_be64(x) (x)
79 # else /*  __BYTE_ORDER == __BIG_ENDIAN */
80 #  error "Unknown byte order"
81 # endif /* __BYTE_ORDER != __BIG_ENDIAN */
82
83 #endif /* !__KERNEL__ */
84
85 #endif /* _LIBCFS_BYTEORDER_H */