xref: /freebsd/sys/compat/linuxkpi/common/include/asm/byteorder.h (revision ebacd8013fe5f7fdf9f6a5b286f6680dd2891036)
1 /*-
2  * Copyright (c) 2010 Isilon Systems, Inc.
3  * Copyright (c) 2010 iX Systems, Inc.
4  * Copyright (c) 2010 Panasas, Inc.
5  * Copyright (c) 2013-2016 Mellanox Technologies, Ltd.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice unmodified, this list of conditions, and the following
13  *    disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  *
29  * $FreeBSD$
30  */
31 #ifndef	_LINUXKPI_ASM_BYTEORDER_H_
32 #define	_LINUXKPI_ASM_BYTEORDER_H_
33 
34 #include <sys/types.h>
35 #include <sys/endian.h>
36 #include <asm/types.h>
37 
38 #if BYTE_ORDER == LITTLE_ENDIAN
39 #define	__LITTLE_ENDIAN
40 #else
41 #define	__BIG_ENDIAN
42 #endif
43 
44 #define	__cpu_to_le64(x)	htole64(x)
45 #define	cpu_to_le64(x)		__cpu_to_le64(x)
46 #define	__le64_to_cpu(x)	le64toh(x)
47 #define	le64_to_cpu(x)		__le64_to_cpu(x)
48 #define	__cpu_to_le32(x)	htole32(x)
49 #define	cpu_to_le32(x)		__cpu_to_le32(x)
50 #define	__le32_to_cpu(x)	le32toh(x)
51 #define	le32_to_cpu(x)		__le32_to_cpu(x)
52 #define	__cpu_to_le16(x)	htole16(x)
53 #define	cpu_to_le16(x)		__cpu_to_le16(x)
54 #define	__le16_to_cpu(x)	le16toh(x)
55 #define	le16_to_cpu(x)		__le16_to_cpu(x)
56 #define	__cpu_to_be64(x)	htobe64(x)
57 #define	cpu_to_be64(x)		__cpu_to_be64(x)
58 #define	__be64_to_cpu(x)	be64toh(x)
59 #define	be64_to_cpu(x)		__be64_to_cpu(x)
60 #define	__cpu_to_be32(x)	htobe32(x)
61 #define	cpu_to_be32(x)		__cpu_to_be32(x)
62 #define	__be32_to_cpu(x)	be32toh(x)
63 #define	be32_to_cpu(x)		__be32_to_cpu(x)
64 #define	__cpu_to_be16(x)	htobe16(x)
65 #define	cpu_to_be16(x)		__cpu_to_be16(x)
66 #define	__be16_to_cpu(x)	be16toh(x)
67 #define	be16_to_cpu(x)		__be16_to_cpu(x)
68 
69 #define	__cpu_to_le64p(x)	htole64(*((const uint64_t *)(x)))
70 #define	cpu_to_le64p(x)		__cpu_to_le64p(x)
71 #define	__le64_to_cpup(x)	le64toh(*((const uint64_t *)(x)))
72 #define	le64_to_cpup(x)		__le64_to_cpup(x)
73 #define	__cpu_to_le32p(x)	htole32(*((const uint32_t *)(x)))
74 #define	cpu_to_le32p(x)		__cpu_to_le32p(x)
75 #define	__le32_to_cpup(x)	le32toh(*((const uint32_t *)(x)))
76 #define	le32_to_cpup(x)		__le32_to_cpup(x)
77 #define	__cpu_to_le16p(x)	htole16(*((const uint16_t *)(x)))
78 #define	cpu_to_le16p(x)		__cpu_to_le16p(x)
79 #define	__le16_to_cpup(x)	le16toh(*((const uint16_t *)(x)))
80 #define	le16_to_cpup(x)		__le16_to_cpup(x)
81 #define	__cpu_to_be64p(x)	htobe64(*((const uint64_t *)(x)))
82 #define	cpu_to_be64p(x)		__cpu_to_be64p(x)
83 #define	__be64_to_cpup(x)	be64toh(*((const uint64_t *)(x)))
84 #define	be64_to_cpup(x)		__be64_to_cpup(x)
85 #define	__cpu_to_be32p(x)	htobe32(*((const uint32_t *)(x)))
86 #define	cpu_to_be32p(x)		__cpu_to_be32p(x)
87 #define	__be32_to_cpup(x)	be32toh(*((const uint32_t *)(x)))
88 #define	be32_to_cpup(x)		__be32_to_cpup(x)
89 #define	__cpu_to_be16p(x)	htobe16(*((const uint16_t *)(x)))
90 #define	cpu_to_be16p(x)		__cpu_to_be16p(x)
91 #define	__be16_to_cpup(x)	be16toh(*((const uint16_t *)(x)))
92 #define	be16_to_cpup(x)		__be16_to_cpup(x)
93 
94 
95 #define	__cpu_to_le64s(x)	do { *((uint64_t *)(x)) = cpu_to_le64p((x)); } while (0)
96 #define	cpu_to_le64s(x)		__cpu_to_le64s(x)
97 #define	__le64_to_cpus(x)	do { *((uint64_t *)(x)) = le64_to_cpup((x)); } while (0)
98 #define	le64_to_cpus(x)		__le64_to_cpus(x)
99 #define	__cpu_to_le32s(x)	do { *((uint32_t *)(x)) = cpu_to_le32p((x)); } while (0)
100 #define	cpu_to_le32s(x)		__cpu_to_le32s(x)
101 #define	__le32_to_cpus(x)	do { *((uint32_t *)(x)) = le32_to_cpup((x)); } while (0)
102 #define	le32_to_cpus(x)		__le32_to_cpus(x)
103 #define	__cpu_to_le16s(x)	do { *((uint16_t *)(x)) = cpu_to_le16p((x)); } while (0)
104 #define	cpu_to_le16s(x)		__cpu_to_le16s(x)
105 #define	__le16_to_cpus(x)	do { *((uint16_t *)(x)) = le16_to_cpup((x)); } while (0)
106 #define	le16_to_cpus(x)		__le16_to_cpus(x)
107 #define	__cpu_to_be64s(x)	do { *((uint64_t *)(x)) = cpu_to_be64p((x)); } while (0)
108 #define	cpu_to_be64s(x)		__cpu_to_be64s(x)
109 #define	__be64_to_cpus(x)	do { *((uint64_t *)(x)) = be64_to_cpup((x)); } while (0)
110 #define	be64_to_cpus(x)		__be64_to_cpus(x)
111 #define	__cpu_to_be32s(x)	do { *((uint32_t *)(x)) = cpu_to_be32p((x)); } while (0)
112 #define	cpu_to_be32s(x)		__cpu_to_be32s(x)
113 #define	__be32_to_cpus(x)	do { *((uint32_t *)(x)) = be32_to_cpup((x)); } while (0)
114 #define	be32_to_cpus(x)		__be32_to_cpus(x)
115 #define	__cpu_to_be16s(x)	do { *((uint16_t *)(x)) = cpu_to_be16p((x)); } while (0)
116 #define	cpu_to_be16s(x)		__cpu_to_be16s(x)
117 #define	__be16_to_cpus(x)	do { *((uint16_t *)(x)) = be16_to_cpup((x)); } while (0)
118 #define	be16_to_cpus(x)		__be16_to_cpus(x)
119 
120 #define	swab16(x)	bswap16(x)
121 #define	swab32(x)	bswap32(x)
122 #define	swab64(x)	bswap64(x)
123 
124 static inline void
125 be64_add_cpu(uint64_t *var, uint64_t val)
126 {
127 	*var = cpu_to_be64(be64_to_cpu(*var) + val);
128 }
129 
130 static inline void
131 be32_add_cpu(uint32_t *var, uint32_t val)
132 {
133 	*var = cpu_to_be32(be32_to_cpu(*var) + val);
134 }
135 
136 static inline void
137 be16_add_cpu(uint16_t *var, uint16_t val)
138 {
139 	*var = cpu_to_be16(be16_to_cpu(*var) + val);
140 }
141 
142 static __inline void
143 le64_add_cpu(uint64_t *var, uint64_t val)
144 {
145 	*var = cpu_to_le64(le64_to_cpu(*var) + val);
146 }
147 
148 static __inline void
149 le32_add_cpu(uint32_t *var, uint32_t val)
150 {
151 	*var = cpu_to_le32(le32_to_cpu(*var) + val);
152 }
153 
154 static inline void
155 le16_add_cpu(uint16_t *var, uint16_t val)
156 {
157 	*var = cpu_to_le16(le16_to_cpu(*var) + val);
158 }
159 
160 #endif	/* _LINUXKPI_ASM_BYTEORDER_H_ */
161