generic.h 6.1 KB

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