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