/include/linux/byteorder.h

https://github.com/namebasedsockets/kernel · C Header · 372 lines · 311 code · 52 blank · 9 comment · 4 complexity · 94e775e43fdafc26e222736b52274438 MD5 · raw file

  1. #ifndef _LINUX_BYTEORDER_H
  2. #define _LINUX_BYTEORDER_H
  3. #include <linux/types.h>
  4. #include <linux/swab.h>
  5. #if defined(__LITTLE_ENDIAN) && defined(__BIG_ENDIAN)
  6. # error Fix asm/byteorder.h to define one endianness
  7. #endif
  8. #if !defined(__LITTLE_ENDIAN) && !defined(__BIG_ENDIAN)
  9. # error Fix asm/byteorder.h to define arch endianness
  10. #endif
  11. #ifdef __LITTLE_ENDIAN
  12. # undef __LITTLE_ENDIAN
  13. # define __LITTLE_ENDIAN 1234
  14. #endif
  15. #ifdef __BIG_ENDIAN
  16. # undef __BIG_ENDIAN
  17. # define __BIG_ENDIAN 4321
  18. #endif
  19. #if defined(__LITTLE_ENDIAN) && !defined(__LITTLE_ENDIAN_BITFIELD)
  20. # define __LITTLE_ENDIAN_BITFIELD
  21. #endif
  22. #if defined(__BIG_ENDIAN) && !defined(__BIG_ENDIAN_BITFIELD)
  23. # define __BIG_ENDIAN_BITFIELD
  24. #endif
  25. #ifdef __LITTLE_ENDIAN
  26. # define __le16_to_cpu(x) ((__force __u16)(__le16)(x))
  27. # define __le32_to_cpu(x) ((__force __u32)(__le32)(x))
  28. # define __le64_to_cpu(x) ((__force __u64)(__le64)(x))
  29. # define __cpu_to_le16(x) ((__force __le16)(__u16)(x))
  30. # define __cpu_to_le32(x) ((__force __le32)(__u32)(x))
  31. # define __cpu_to_le64(x) ((__force __le64)(__u64)(x))
  32. # define __be16_to_cpu(x) __swab16((__force __u16)(__be16)(x))
  33. # define __be32_to_cpu(x) __swab32((__force __u32)(__be32)(x))
  34. # define __be64_to_cpu(x) __swab64((__force __u64)(__be64)(x))
  35. # define __cpu_to_be16(x) ((__force __be16)__swab16(x))
  36. # define __cpu_to_be32(x) ((__force __be32)__swab32(x))
  37. # define __cpu_to_be64(x) ((__force __be64)__swab64(x))
  38. #endif
  39. #ifdef __BIG_ENDIAN
  40. # define __be16_to_cpu(x) ((__force __u16)(__be16)(x))
  41. # define __be32_to_cpu(x) ((__force __u32)(__be32)(x))
  42. # define __be64_to_cpu(x) ((__force __u64)(__be64)(x))
  43. # define __cpu_to_be16(x) ((__force __be16)(__u16)(x))
  44. # define __cpu_to_be32(x) ((__force __be32)(__u32)(x))
  45. # define __cpu_to_be64(x) ((__force __be64)(__u64)(x))
  46. # define __le16_to_cpu(x) __swab16((__force __u16)(__le16)(x))
  47. # define __le32_to_cpu(x) __swab32((__force __u32)(__le32)(x))
  48. # define __le64_to_cpu(x) __swab64((__force __u64)(__le64)(x))
  49. # define __cpu_to_le16(x) ((__force __le16)__swab16(x))
  50. # define __cpu_to_le32(x) ((__force __le32)__swab32(x))
  51. # define __cpu_to_le64(x) ((__force __le64)__swab64(x))
  52. #endif
  53. /*
  54. * These helpers could be phased out over time as the base version
  55. * handles constant folding.
  56. */
  57. #define __constant_htonl(x) __cpu_to_be32(x)
  58. #define __constant_ntohl(x) __be32_to_cpu(x)
  59. #define __constant_htons(x) __cpu_to_be16(x)
  60. #define __constant_ntohs(x) __be16_to_cpu(x)
  61. #define __constant_le16_to_cpu(x) __le16_to_cpu(x)
  62. #define __constant_le32_to_cpu(x) __le32_to_cpu(x)
  63. #define __constant_le64_to_cpu(x) __le64_to_cpu(x)
  64. #define __constant_be16_to_cpu(x) __be16_to_cpu(x)
  65. #define __constant_be32_to_cpu(x) __be32_to_cpu(x)
  66. #define __constant_be64_to_cpu(x) __be64_to_cpu(x)
  67. #define __constant_cpu_to_le16(x) __cpu_to_le16(x)
  68. #define __constant_cpu_to_le32(x) __cpu_to_le32(x)
  69. #define __constant_cpu_to_le64(x) __cpu_to_le64(x)
  70. #define __constant_cpu_to_be16(x) __cpu_to_be16(x)
  71. #define __constant_cpu_to_be32(x) __cpu_to_be32(x)
  72. #define __constant_cpu_to_be64(x) __cpu_to_be64(x)
  73. static inline void __le16_to_cpus(__u16 *p)
  74. {
  75. #ifdef __BIG_ENDIAN
  76. __swab16s(p);
  77. #endif
  78. }
  79. static inline void __cpu_to_le16s(__u16 *p)
  80. {
  81. #ifdef __BIG_ENDIAN
  82. __swab16s(p);
  83. #endif
  84. }
  85. static inline void __le32_to_cpus(__u32 *p)
  86. {
  87. #ifdef __BIG_ENDIAN
  88. __swab32s(p);
  89. #endif
  90. }
  91. static inline void __cpu_to_le32s(__u32 *p)
  92. {
  93. #ifdef __BIG_ENDIAN
  94. __swab32s(p);
  95. #endif
  96. }
  97. static inline void __le64_to_cpus(__u64 *p)
  98. {
  99. #ifdef __BIG_ENDIAN
  100. __swab64s(p);
  101. #endif
  102. }
  103. static inline void __cpu_to_le64s(__u64 *p)
  104. {
  105. #ifdef __BIG_ENDIAN
  106. __swab64s(p);
  107. #endif
  108. }
  109. static inline void __be16_to_cpus(__u16 *p)
  110. {
  111. #ifdef __LITTLE_ENDIAN
  112. __swab16s(p);
  113. #endif
  114. }
  115. static inline void __cpu_to_be16s(__u16 *p)
  116. {
  117. #ifdef __LITTLE_ENDIAN
  118. __swab16s(p);
  119. #endif
  120. }
  121. static inline void __be32_to_cpus(__u32 *p)
  122. {
  123. #ifdef __LITTLE_ENDIAN
  124. __swab32s(p);
  125. #endif
  126. }
  127. static inline void __cpu_to_be32s(__u32 *p)
  128. {
  129. #ifdef __LITTLE_ENDIAN
  130. __swab32s(p);
  131. #endif
  132. }
  133. static inline void __be64_to_cpus(__u64 *p)
  134. {
  135. #ifdef __LITTLE_ENDIAN
  136. __swab64s(p);
  137. #endif
  138. }
  139. static inline void __cpu_to_be64s(__u64 *p)
  140. {
  141. #ifdef __LITTLE_ENDIAN
  142. __swab64s(p);
  143. #endif
  144. }
  145. static inline __u16 __le16_to_cpup(const __le16 *p)
  146. {
  147. #ifdef __LITTLE_ENDIAN
  148. return (__force __u16)*p;
  149. #else
  150. return __swab16p((__force __u16 *)p);
  151. #endif
  152. }
  153. static inline __u32 __le32_to_cpup(const __le32 *p)
  154. {
  155. #ifdef __LITTLE_ENDIAN
  156. return (__force __u32)*p;
  157. #else
  158. return __swab32p((__force __u32 *)p);
  159. #endif
  160. }
  161. static inline __u64 __le64_to_cpup(const __le64 *p)
  162. {
  163. #ifdef __LITTLE_ENDIAN
  164. return (__force __u64)*p;
  165. #else
  166. return __swab64p((__force __u64 *)p);
  167. #endif
  168. }
  169. static inline __le16 __cpu_to_le16p(const __u16 *p)
  170. {
  171. #ifdef __LITTLE_ENDIAN
  172. return (__force __le16)*p;
  173. #else
  174. return (__force __le16)__swab16p(p);
  175. #endif
  176. }
  177. static inline __le32 __cpu_to_le32p(const __u32 *p)
  178. {
  179. #ifdef __LITTLE_ENDIAN
  180. return (__force __le32)*p;
  181. #else
  182. return (__force __le32)__swab32p(p);
  183. #endif
  184. }
  185. static inline __le64 __cpu_to_le64p(const __u64 *p)
  186. {
  187. #ifdef __LITTLE_ENDIAN
  188. return (__force __le64)*p;
  189. #else
  190. return (__force __le64)__swab64p(p);
  191. #endif
  192. }
  193. static inline __u16 __be16_to_cpup(const __be16 *p)
  194. {
  195. #ifdef __BIG_ENDIAN
  196. return (__force __u16)*p;
  197. #else
  198. return __swab16p((__force __u16 *)p);
  199. #endif
  200. }
  201. static inline __u32 __be32_to_cpup(const __be32 *p)
  202. {
  203. #ifdef __BIG_ENDIAN
  204. return (__force __u32)*p;
  205. #else
  206. return __swab32p((__force __u32 *)p);
  207. #endif
  208. }
  209. static inline __u64 __be64_to_cpup(const __be64 *p)
  210. {
  211. #ifdef __BIG_ENDIAN
  212. return (__force __u64)*p;
  213. #else
  214. return __swab64p((__force __u64 *)p);
  215. #endif
  216. }
  217. static inline __be16 __cpu_to_be16p(const __u16 *p)
  218. {
  219. #ifdef __BIG_ENDIAN
  220. return (__force __be16)*p;
  221. #else
  222. return (__force __be16)__swab16p(p);
  223. #endif
  224. }
  225. static inline __be32 __cpu_to_be32p(const __u32 *p)
  226. {
  227. #ifdef __BIG_ENDIAN
  228. return (__force __be32)*p;
  229. #else
  230. return (__force __be32)__swab32p(p);
  231. #endif
  232. }
  233. static inline __be64 __cpu_to_be64p(const __u64 *p)
  234. {
  235. #ifdef __BIG_ENDIAN
  236. return (__force __be64)*p;
  237. #else
  238. return (__force __be64)__swab64p(p);
  239. #endif
  240. }
  241. #ifdef __KERNEL__
  242. # define le16_to_cpu __le16_to_cpu
  243. # define le32_to_cpu __le32_to_cpu
  244. # define le64_to_cpu __le64_to_cpu
  245. # define be16_to_cpu __be16_to_cpu
  246. # define be32_to_cpu __be32_to_cpu
  247. # define be64_to_cpu __be64_to_cpu
  248. # define cpu_to_le16 __cpu_to_le16
  249. # define cpu_to_le32 __cpu_to_le32
  250. # define cpu_to_le64 __cpu_to_le64
  251. # define cpu_to_be16 __cpu_to_be16
  252. # define cpu_to_be32 __cpu_to_be32
  253. # define cpu_to_be64 __cpu_to_be64
  254. # define le16_to_cpup __le16_to_cpup
  255. # define le32_to_cpup __le32_to_cpup
  256. # define le64_to_cpup __le64_to_cpup
  257. # define be16_to_cpup __be16_to_cpup
  258. # define be32_to_cpup __be32_to_cpup
  259. # define be64_to_cpup __be64_to_cpup
  260. # define cpu_to_le16p __cpu_to_le16p
  261. # define cpu_to_le32p __cpu_to_le32p
  262. # define cpu_to_le64p __cpu_to_le64p
  263. # define cpu_to_be16p __cpu_to_be16p
  264. # define cpu_to_be32p __cpu_to_be32p
  265. # define cpu_to_be64p __cpu_to_be64p
  266. # define le16_to_cpus __le16_to_cpus
  267. # define le32_to_cpus __le32_to_cpus
  268. # define le64_to_cpus __le64_to_cpus
  269. # define be16_to_cpus __be16_to_cpus
  270. # define be32_to_cpus __be32_to_cpus
  271. # define be64_to_cpus __be64_to_cpus
  272. # define cpu_to_le16s __cpu_to_le16s
  273. # define cpu_to_le32s __cpu_to_le32s
  274. # define cpu_to_le64s __cpu_to_le64s
  275. # define cpu_to_be16s __cpu_to_be16s
  276. # define cpu_to_be32s __cpu_to_be32s
  277. # define cpu_to_be64s __cpu_to_be64s
  278. /*
  279. * They have to be macros in order to do the constant folding
  280. * correctly - if the argument passed into a inline function
  281. * it is no longer constant according to gcc..
  282. */
  283. # undef ntohl
  284. # undef ntohs
  285. # undef htonl
  286. # undef htons
  287. # define ___htonl(x) __cpu_to_be32(x)
  288. # define ___htons(x) __cpu_to_be16(x)
  289. # define ___ntohl(x) __be32_to_cpu(x)
  290. # define ___ntohs(x) __be16_to_cpu(x)
  291. # define htonl(x) ___htonl(x)
  292. # define ntohl(x) ___ntohl(x)
  293. # define htons(x) ___htons(x)
  294. # define ntohs(x) ___ntohs(x)
  295. static inline void le16_add_cpu(__le16 *var, u16 val)
  296. {
  297. *var = cpu_to_le16(le16_to_cpup(var) + val);
  298. }
  299. static inline void le32_add_cpu(__le32 *var, u32 val)
  300. {
  301. *var = cpu_to_le32(le32_to_cpup(var) + val);
  302. }
  303. static inline void le64_add_cpu(__le64 *var, u64 val)
  304. {
  305. *var = cpu_to_le64(le64_to_cpup(var) + val);
  306. }
  307. static inline void be16_add_cpu(__be16 *var, u16 val)
  308. {
  309. *var = cpu_to_be16(be16_to_cpup(var) + val);
  310. }
  311. static inline void be32_add_cpu(__be32 *var, u32 val)
  312. {
  313. *var = cpu_to_be32(be32_to_cpup(var) + val);
  314. }
  315. static inline void be64_add_cpu(__be64 *var, u64 val)
  316. {
  317. *var = cpu_to_be64(be64_to_cpup(var) + val);
  318. }
  319. #endif /* __KERNEL__ */
  320. #endif /* _LINUX_BYTEORDER_H */