xref: /DragonStub/inc/dragonstub/linux/byteorder.h (revision f412fd2a1a248b546b7085648dece8d908077fab)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_BYTEORDER_GENERIC_H
3 #define _LINUX_BYTEORDER_GENERIC_H
4 
5 #include "../types.h"
6 #include "byteorder_little_endian.h"
7 
8 /*
9  * linux/byteorder/generic.h
10  * Generic Byte-reordering support
11  *
12  * The "... p" macros, like le64_to_cpup, can be used with pointers
13  * to unaligned data, but there will be a performance penalty on
14  * some architectures.  Use get_unaligned for unaligned data.
15  *
16  * Francois-Rene Rideau <fare@tunes.org> 19970707
17  *    gathered all the good ideas from all asm-foo/byteorder.h into one file,
18  *    cleaned them up.
19  *    I hope it is compliant with non-GCC compilers.
20  *    I decided to put __BYTEORDER_HAS_U64__ in byteorder.h,
21  *    because I wasn't sure it would be ok to put it in types.h
22  *    Upgraded it to 2.1.43
23  * Francois-Rene Rideau <fare@tunes.org> 19971012
24  *    Upgraded it to 2.1.57
25  *    to please Linus T., replaced huge #ifdef's between little/big endian
26  *    by nestedly #include'd files.
27  * Francois-Rene Rideau <fare@tunes.org> 19971205
28  *    Made it to 2.1.71; now a facelift:
29  *    Put files under include/linux/byteorder/
30  *    Split swab from generic support.
31  *
32  * TODO:
33  *   = Regular kernel maintainers could also replace all these manual
34  *    byteswap macros that remain, disseminated among drivers,
35  *    after some grep or the sources...
36  *   = Linus might want to rename all these macros and files to fit his taste,
37  *    to fit his personal naming scheme.
38  *   = it seems that a few drivers would also appreciate
39  *    nybble swapping support...
40  *   = every architecture could add their byteswap macro in asm/byteorder.h
41  *    see how some architectures already do (i386, alpha, ppc, etc)
42  *   = cpu_to_beXX and beXX_to_cpu might some day need to be well
43  *    distinguished throughout the kernel. This is not the case currently,
44  *    since little endian, big endian, and pdp endian machines needn't it.
45  *    But this might be the case for, say, a port of Linux to 20/21 bit
46  *    architectures (and F21 Linux addict around?).
47  */
48 
49 /*
50  * The following macros are to be defined by <asm/byteorder.h>:
51  *
52  * Conversion of long and short int between network and host format
53  *	ntohl(__u32 x)
54  *	ntohs(__u16 x)
55  *	htonl(__u32 x)
56  *	htons(__u16 x)
57  * It seems that some programs (which? where? or perhaps a standard? POSIX?)
58  * might like the above to be functions, not macros (why?).
59  * if that's true, then detect them, and take measures.
60  * Anyway, the measure is: define only ___ntohl as a macro instead,
61  * and in a separate file, have
62  * unsigned long inline ntohl(x){return ___ntohl(x);}
63  *
64  * The same for constant arguments
65  *	__constant_ntohl(__u32 x)
66  *	__constant_ntohs(__u16 x)
67  *	__constant_htonl(__u32 x)
68  *	__constant_htons(__u16 x)
69  *
70  * Conversion of XX-bit integers (16- 32- or 64-)
71  * between native CPU format and little/big endian format
72  * 64-bit stuff only defined for proper architectures
73  *	cpu_to_[bl]eXX(__uXX x)
74  *	[bl]eXX_to_cpu(__uXX x)
75  *
76  * The same, but takes a pointer to the value to convert
77  *	cpu_to_[bl]eXXp(__uXX x)
78  *	[bl]eXX_to_cpup(__uXX x)
79  *
80  * The same, but change in situ
81  *	cpu_to_[bl]eXXs(__uXX x)
82  *	[bl]eXX_to_cpus(__uXX x)
83  *
84  * See asm-foo/byteorder.h for examples of how to provide
85  * architecture-optimized versions
86  *
87  */
88 
89 #define cpu_to_le64 __cpu_to_le64
90 #define le64_to_cpu __le64_to_cpu
91 #define cpu_to_le32 __cpu_to_le32
92 #define le32_to_cpu __le32_to_cpu
93 #define cpu_to_le16 __cpu_to_le16
94 #define le16_to_cpu __le16_to_cpu
95 #define cpu_to_be64 __cpu_to_be64
96 #define be64_to_cpu __be64_to_cpu
97 #define cpu_to_be32 __cpu_to_be32
98 #define be32_to_cpu __be32_to_cpu
99 #define cpu_to_be16 __cpu_to_be16
100 #define be16_to_cpu __be16_to_cpu
101 #define cpu_to_le64p __cpu_to_le64p
102 #define le64_to_cpup __le64_to_cpup
103 #define cpu_to_le32p __cpu_to_le32p
104 #define le32_to_cpup __le32_to_cpup
105 #define cpu_to_le16p __cpu_to_le16p
106 #define le16_to_cpup __le16_to_cpup
107 #define cpu_to_be64p __cpu_to_be64p
108 #define be64_to_cpup __be64_to_cpup
109 #define cpu_to_be32p __cpu_to_be32p
110 #define be32_to_cpup __be32_to_cpup
111 #define cpu_to_be16p __cpu_to_be16p
112 #define be16_to_cpup __be16_to_cpup
113 #define cpu_to_le64s __cpu_to_le64s
114 #define le64_to_cpus __le64_to_cpus
115 #define cpu_to_le32s __cpu_to_le32s
116 #define le32_to_cpus __le32_to_cpus
117 #define cpu_to_le16s __cpu_to_le16s
118 #define le16_to_cpus __le16_to_cpus
119 #define cpu_to_be64s __cpu_to_be64s
120 #define be64_to_cpus __be64_to_cpus
121 #define cpu_to_be32s __cpu_to_be32s
122 #define be32_to_cpus __be32_to_cpus
123 #define cpu_to_be16s __cpu_to_be16s
124 #define be16_to_cpus __be16_to_cpus
125 
126 /*
127  * They have to be macros in order to do the constant folding
128  * correctly - if the argument passed into a inline function
129  * it is no longer constant according to gcc..
130  */
131 
132 #undef ntohl
133 #undef ntohs
134 #undef htonl
135 #undef htons
136 
137 #define ___htonl(x) __cpu_to_be32(x)
138 #define ___htons(x) __cpu_to_be16(x)
139 #define ___ntohl(x) __be32_to_cpu(x)
140 #define ___ntohs(x) __be16_to_cpu(x)
141 
142 #define htonl(x) ___htonl(x)
143 #define ntohl(x) ___ntohl(x)
144 #define htons(x) ___htons(x)
145 #define ntohs(x) ___ntohs(x)
146 
le16_add_cpu(__le16 * var,u16 val)147 static inline void le16_add_cpu(__le16 *var, u16 val)
148 {
149 	*var = cpu_to_le16(le16_to_cpu(*var) + val);
150 }
151 
le32_add_cpu(__le32 * var,u32 val)152 static inline void le32_add_cpu(__le32 *var, u32 val)
153 {
154 	*var = cpu_to_le32(le32_to_cpu(*var) + val);
155 }
156 
le64_add_cpu(__le64 * var,u64 val)157 static inline void le64_add_cpu(__le64 *var, u64 val)
158 {
159 	*var = cpu_to_le64(le64_to_cpu(*var) + val);
160 }
161 
162 /* XXX: this stuff can be optimized */
le32_to_cpu_array(u32 * buf,unsigned int words)163 static inline void le32_to_cpu_array(u32 *buf, unsigned int words)
164 {
165 	while (words--) {
166 		__le32_to_cpus(buf);
167 		buf++;
168 	}
169 }
170 
cpu_to_le32_array(u32 * buf,unsigned int words)171 static inline void cpu_to_le32_array(u32 *buf, unsigned int words)
172 {
173 	while (words--) {
174 		__cpu_to_le32s(buf);
175 		buf++;
176 	}
177 }
178 
be16_add_cpu(__be16 * var,u16 val)179 static inline void be16_add_cpu(__be16 *var, u16 val)
180 {
181 	*var = cpu_to_be16(be16_to_cpu(*var) + val);
182 }
183 
be32_add_cpu(__be32 * var,u32 val)184 static inline void be32_add_cpu(__be32 *var, u32 val)
185 {
186 	*var = cpu_to_be32(be32_to_cpu(*var) + val);
187 }
188 
be64_add_cpu(__be64 * var,u64 val)189 static inline void be64_add_cpu(__be64 *var, u64 val)
190 {
191 	*var = cpu_to_be64(be64_to_cpu(*var) + val);
192 }
193 
cpu_to_be32_array(__be32 * dst,const u32 * src,size_t len)194 static inline void cpu_to_be32_array(__be32 *dst, const u32 *src, size_t len)
195 {
196 	size_t i;
197 
198 	for (i = 0; i < len; i++)
199 		dst[i] = cpu_to_be32(src[i]);
200 }
201 
be32_to_cpu_array(u32 * dst,const __be32 * src,size_t len)202 static inline void be32_to_cpu_array(u32 *dst, const __be32 *src, size_t len)
203 {
204 	size_t i;
205 
206 	for (i = 0; i < len; i++)
207 		dst[i] = be32_to_cpu(src[i]);
208 }
209 
210 #endif /* _LINUX_BYTEORDER_GENERIC_H */
211