Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 #ifndef _LINUX_BYTEORDER_GENERIC_H
0003 #define _LINUX_BYTEORDER_GENERIC_H
0004 
0005 /*
0006  * linux/byteorder/generic.h
0007  * Generic Byte-reordering support
0008  *
0009  * The "... p" macros, like le64_to_cpup, can be used with pointers
0010  * to unaligned data, but there will be a performance penalty on 
0011  * some architectures.  Use get_unaligned for unaligned data.
0012  *
0013  * Francois-Rene Rideau <fare@tunes.org> 19970707
0014  *    gathered all the good ideas from all asm-foo/byteorder.h into one file,
0015  *    cleaned them up.
0016  *    I hope it is compliant with non-GCC compilers.
0017  *    I decided to put __BYTEORDER_HAS_U64__ in byteorder.h,
0018  *    because I wasn't sure it would be ok to put it in types.h
0019  *    Upgraded it to 2.1.43
0020  * Francois-Rene Rideau <fare@tunes.org> 19971012
0021  *    Upgraded it to 2.1.57
0022  *    to please Linus T., replaced huge #ifdef's between little/big endian
0023  *    by nestedly #include'd files.
0024  * Francois-Rene Rideau <fare@tunes.org> 19971205
0025  *    Made it to 2.1.71; now a facelift:
0026  *    Put files under include/linux/byteorder/
0027  *    Split swab from generic support.
0028  *
0029  * TODO:
0030  *   = Regular kernel maintainers could also replace all these manual
0031  *    byteswap macros that remain, disseminated among drivers,
0032  *    after some grep or the sources...
0033  *   = Linus might want to rename all these macros and files to fit his taste,
0034  *    to fit his personal naming scheme.
0035  *   = it seems that a few drivers would also appreciate
0036  *    nybble swapping support...
0037  *   = every architecture could add their byteswap macro in asm/byteorder.h
0038  *    see how some architectures already do (i386, alpha, ppc, etc)
0039  *   = cpu_to_beXX and beXX_to_cpu might some day need to be well
0040  *    distinguished throughout the kernel. This is not the case currently,
0041  *    since little endian, big endian, and pdp endian machines needn't it.
0042  *    But this might be the case for, say, a port of Linux to 20/21 bit
0043  *    architectures (and F21 Linux addict around?).
0044  */
0045 
0046 /*
0047  * The following macros are to be defined by <asm/byteorder.h>:
0048  *
0049  * Conversion of long and short int between network and host format
0050  *  ntohl(__u32 x)
0051  *  ntohs(__u16 x)
0052  *  htonl(__u32 x)
0053  *  htons(__u16 x)
0054  * It seems that some programs (which? where? or perhaps a standard? POSIX?)
0055  * might like the above to be functions, not macros (why?).
0056  * if that's true, then detect them, and take measures.
0057  * Anyway, the measure is: define only ___ntohl as a macro instead,
0058  * and in a separate file, have
0059  * unsigned long inline ntohl(x){return ___ntohl(x);}
0060  *
0061  * The same for constant arguments
0062  *  __constant_ntohl(__u32 x)
0063  *  __constant_ntohs(__u16 x)
0064  *  __constant_htonl(__u32 x)
0065  *  __constant_htons(__u16 x)
0066  *
0067  * Conversion of XX-bit integers (16- 32- or 64-)
0068  * between native CPU format and little/big endian format
0069  * 64-bit stuff only defined for proper architectures
0070  *  cpu_to_[bl]eXX(__uXX x)
0071  *  [bl]eXX_to_cpu(__uXX x)
0072  *
0073  * The same, but takes a pointer to the value to convert
0074  *  cpu_to_[bl]eXXp(__uXX x)
0075  *  [bl]eXX_to_cpup(__uXX x)
0076  *
0077  * The same, but change in situ
0078  *  cpu_to_[bl]eXXs(__uXX x)
0079  *  [bl]eXX_to_cpus(__uXX x)
0080  *
0081  * See asm-foo/byteorder.h for examples of how to provide
0082  * architecture-optimized versions
0083  *
0084  */
0085 
0086 #define cpu_to_le64 __cpu_to_le64
0087 #define le64_to_cpu __le64_to_cpu
0088 #define cpu_to_le32 __cpu_to_le32
0089 #define le32_to_cpu __le32_to_cpu
0090 #define cpu_to_le16 __cpu_to_le16
0091 #define le16_to_cpu __le16_to_cpu
0092 #define cpu_to_be64 __cpu_to_be64
0093 #define be64_to_cpu __be64_to_cpu
0094 #define cpu_to_be32 __cpu_to_be32
0095 #define be32_to_cpu __be32_to_cpu
0096 #define cpu_to_be16 __cpu_to_be16
0097 #define be16_to_cpu __be16_to_cpu
0098 #define cpu_to_le64p __cpu_to_le64p
0099 #define le64_to_cpup __le64_to_cpup
0100 #define cpu_to_le32p __cpu_to_le32p
0101 #define le32_to_cpup __le32_to_cpup
0102 #define cpu_to_le16p __cpu_to_le16p
0103 #define le16_to_cpup __le16_to_cpup
0104 #define cpu_to_be64p __cpu_to_be64p
0105 #define be64_to_cpup __be64_to_cpup
0106 #define cpu_to_be32p __cpu_to_be32p
0107 #define be32_to_cpup __be32_to_cpup
0108 #define cpu_to_be16p __cpu_to_be16p
0109 #define be16_to_cpup __be16_to_cpup
0110 #define cpu_to_le64s __cpu_to_le64s
0111 #define le64_to_cpus __le64_to_cpus
0112 #define cpu_to_le32s __cpu_to_le32s
0113 #define le32_to_cpus __le32_to_cpus
0114 #define cpu_to_le16s __cpu_to_le16s
0115 #define le16_to_cpus __le16_to_cpus
0116 #define cpu_to_be64s __cpu_to_be64s
0117 #define be64_to_cpus __be64_to_cpus
0118 #define cpu_to_be32s __cpu_to_be32s
0119 #define be32_to_cpus __be32_to_cpus
0120 #define cpu_to_be16s __cpu_to_be16s
0121 #define be16_to_cpus __be16_to_cpus
0122 
0123 /*
0124  * They have to be macros in order to do the constant folding
0125  * correctly - if the argument passed into a inline function
0126  * it is no longer constant according to gcc..
0127  */
0128 
0129 #undef ntohl
0130 #undef ntohs
0131 #undef htonl
0132 #undef htons
0133 
0134 #define ___htonl(x) __cpu_to_be32(x)
0135 #define ___htons(x) __cpu_to_be16(x)
0136 #define ___ntohl(x) __be32_to_cpu(x)
0137 #define ___ntohs(x) __be16_to_cpu(x)
0138 
0139 #define htonl(x) ___htonl(x)
0140 #define ntohl(x) ___ntohl(x)
0141 #define htons(x) ___htons(x)
0142 #define ntohs(x) ___ntohs(x)
0143 
0144 static inline void le16_add_cpu(__le16 *var, u16 val)
0145 {
0146     *var = cpu_to_le16(le16_to_cpu(*var) + val);
0147 }
0148 
0149 static inline void le32_add_cpu(__le32 *var, u32 val)
0150 {
0151     *var = cpu_to_le32(le32_to_cpu(*var) + val);
0152 }
0153 
0154 static inline void le64_add_cpu(__le64 *var, u64 val)
0155 {
0156     *var = cpu_to_le64(le64_to_cpu(*var) + val);
0157 }
0158 
0159 /* XXX: this stuff can be optimized */
0160 static inline void le32_to_cpu_array(u32 *buf, unsigned int words)
0161 {
0162     while (words--) {
0163         __le32_to_cpus(buf);
0164         buf++;
0165     }
0166 }
0167 
0168 static inline void cpu_to_le32_array(u32 *buf, unsigned int words)
0169 {
0170     while (words--) {
0171         __cpu_to_le32s(buf);
0172         buf++;
0173     }
0174 }
0175 
0176 static inline void be16_add_cpu(__be16 *var, u16 val)
0177 {
0178     *var = cpu_to_be16(be16_to_cpu(*var) + val);
0179 }
0180 
0181 static inline void be32_add_cpu(__be32 *var, u32 val)
0182 {
0183     *var = cpu_to_be32(be32_to_cpu(*var) + val);
0184 }
0185 
0186 static inline void be64_add_cpu(__be64 *var, u64 val)
0187 {
0188     *var = cpu_to_be64(be64_to_cpu(*var) + val);
0189 }
0190 
0191 static inline void cpu_to_be32_array(__be32 *dst, const u32 *src, size_t len)
0192 {
0193     size_t i;
0194 
0195     for (i = 0; i < len; i++)
0196         dst[i] = cpu_to_be32(src[i]);
0197 }
0198 
0199 static inline void be32_to_cpu_array(u32 *dst, const __be32 *src, size_t len)
0200 {
0201     size_t i;
0202 
0203     for (i = 0; i < len; i++)
0204         dst[i] = be32_to_cpu(src[i]);
0205 }
0206 
0207 #endif /* _LINUX_BYTEORDER_GENERIC_H */