You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

byteswap.h 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. #ifndef BYTESWAP_H
  2. #define BYTESWAP_H
  3. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  4. #include <stdint.h>
  5. #include <endian.h>
  6. #include <bits/byteswap.h>
  7. /**
  8. * Byte-swap a 16-bit constant
  9. *
  10. * @v value Constant value
  11. * @ret swapped Byte-swapped value
  12. */
  13. #define __bswap_constant_16( value ) \
  14. ( ( ( (value) & 0x00ff ) << 8 ) | \
  15. ( ( (value) & 0xff00 ) >> 8 ) )
  16. /**
  17. * Byte-swap a 32-bit constant
  18. *
  19. * @v value Constant value
  20. * @ret swapped Byte-swapped value
  21. */
  22. #define __bswap_constant_32( value ) \
  23. ( ( ( (value) & 0x000000ffUL ) << 24 ) | \
  24. ( ( (value) & 0x0000ff00UL ) << 8 ) | \
  25. ( ( (value) & 0x00ff0000UL ) >> 8 ) | \
  26. ( ( (value) & 0xff000000UL ) >> 24 ) )
  27. /**
  28. * Byte-swap a 64-bit constant
  29. *
  30. * @v value Constant value
  31. * @ret swapped Byte-swapped value
  32. */
  33. #define __bswap_constant_64( value ) \
  34. ( ( ( (value) & 0x00000000000000ffULL ) << 56 ) | \
  35. ( ( (value) & 0x000000000000ff00ULL ) << 40 ) | \
  36. ( ( (value) & 0x0000000000ff0000ULL ) << 24 ) | \
  37. ( ( (value) & 0x00000000ff000000ULL ) << 8 ) | \
  38. ( ( (value) & 0x000000ff00000000ULL ) >> 8 ) | \
  39. ( ( (value) & 0x0000ff0000000000ULL ) >> 24 ) | \
  40. ( ( (value) & 0x00ff000000000000ULL ) >> 40 ) | \
  41. ( ( (value) & 0xff00000000000000ULL ) >> 56 ) )
  42. /**
  43. * Byte-swap a 16-bit value
  44. *
  45. * @v value Value
  46. * @ret swapped Byte-swapped value
  47. */
  48. #define __bswap_16( value ) \
  49. ( __builtin_constant_p (value) ? \
  50. ( ( uint16_t ) __bswap_constant_16 ( ( uint16_t ) (value) ) ) \
  51. : __bswap_variable_16 (value) )
  52. #define bswap_16( value ) __bswap_16 (value)
  53. /**
  54. * Byte-swap a 32-bit value
  55. *
  56. * @v value Value
  57. * @ret swapped Byte-swapped value
  58. */
  59. #define __bswap_32( value ) \
  60. ( __builtin_constant_p (value) ? \
  61. ( ( uint32_t ) __bswap_constant_32 ( ( uint32_t ) (value) ) ) \
  62. : __bswap_variable_32 (value) )
  63. #define bswap_32( value ) __bswap_32 (value)
  64. /**
  65. * Byte-swap a 64-bit value
  66. *
  67. * @v value Value
  68. * @ret swapped Byte-swapped value
  69. */
  70. #define __bswap_64( value ) \
  71. ( __builtin_constant_p (value) ? \
  72. ( ( uint64_t ) __bswap_constant_64 ( ( uint64_t ) (value) ) ) \
  73. : __bswap_variable_64 (value) )
  74. #define bswap_64( value ) __bswap_64 (value)
  75. #if __BYTE_ORDER == __LITTLE_ENDIAN
  76. #define __cpu_to_leNN( bits, value ) (value)
  77. #define __cpu_to_beNN( bits, value ) __bswap_ ## bits (value)
  78. #define __leNN_to_cpu( bits, value ) (value)
  79. #define __beNN_to_cpu( bits, value ) __bswap_ ## bits (value)
  80. #define __cpu_to_leNNs( bits, ptr ) do { } while ( 0 )
  81. #define __cpu_to_beNNs( bits, ptr ) __bswap_ ## bits ## s (ptr)
  82. #define __leNN_to_cpus( bits, ptr ) do { } while ( 0 )
  83. #define __beNN_to_cpus( bits, ptr ) __bswap_ ## bits ## s (ptr)
  84. #endif
  85. #if __BYTE_ORDER == __BIG_ENDIAN
  86. #define __cpu_to_leNN( bits, value ) __bswap_ ## bits (value)
  87. #define __cpu_to_beNN( bits, value ) (value)
  88. #define __leNN_to_cpu( bits, value ) __bswap_ ## bits (value)
  89. #define __beNN_to_cpu( bits, value ) (value)
  90. #define __cpu_to_leNNs( bits, ptr ) __bswap_ ## bits ## s (ptr)
  91. #define __cpu_to_beNNs( bits, ptr ) do { } while ( 0 )
  92. #define __leNN_to_cpus( bits, ptr ) __bswap_ ## bits ## s (ptr)
  93. #define __beNN_to_cpus( bits, ptr ) do { } while ( 0 )
  94. #endif
  95. #define cpu_to_le16( value ) __cpu_to_leNN ( 16, value )
  96. #define cpu_to_le32( value ) __cpu_to_leNN ( 32, value )
  97. #define cpu_to_le64( value ) __cpu_to_leNN ( 64, value )
  98. #define cpu_to_be16( value ) __cpu_to_beNN ( 16, value )
  99. #define cpu_to_be32( value ) __cpu_to_beNN ( 32, value )
  100. #define cpu_to_be64( value ) __cpu_to_beNN ( 64, value )
  101. #define le16_to_cpu( value ) __leNN_to_cpu ( 16, value )
  102. #define le32_to_cpu( value ) __leNN_to_cpu ( 32, value )
  103. #define le64_to_cpu( value ) __leNN_to_cpu ( 64, value )
  104. #define be16_to_cpu( value ) __beNN_to_cpu ( 16, value )
  105. #define be32_to_cpu( value ) __beNN_to_cpu ( 32, value )
  106. #define be64_to_cpu( value ) __beNN_to_cpu ( 64, value )
  107. #define cpu_to_le16s( ptr ) __cpu_to_leNNs ( 16, ptr )
  108. #define cpu_to_le32s( ptr ) __cpu_to_leNNs ( 32, ptr )
  109. #define cpu_to_le64s( ptr ) __cpu_to_leNNs ( 64, ptr )
  110. #define cpu_to_be16s( ptr ) __cpu_to_beNNs ( 16, ptr )
  111. #define cpu_to_be32s( ptr ) __cpu_to_beNNs ( 32, ptr )
  112. #define cpu_to_be64s( ptr ) __cpu_to_beNNs ( 64, ptr )
  113. #define le16_to_cpus( ptr ) __leNN_to_cpus ( 16, ptr )
  114. #define le32_to_cpus( ptr ) __leNN_to_cpus ( 32, ptr )
  115. #define le64_to_cpus( ptr ) __leNN_to_cpus ( 64, ptr )
  116. #define be16_to_cpus( ptr ) __beNN_to_cpus ( 16, ptr )
  117. #define be32_to_cpus( ptr ) __beNN_to_cpus ( 32, ptr )
  118. #define be64_to_cpus( ptr ) __beNN_to_cpus ( 64, ptr )
  119. #define htonll( value ) cpu_to_be64 (value)
  120. #define ntohll( value ) be64_to_cpu (value)
  121. #define htonl( value ) cpu_to_be32 (value)
  122. #define ntohl( value ) be32_to_cpu (value)
  123. #define htons( value ) cpu_to_be16 (value)
  124. #define ntohs( value ) be16_to_cpu (value)
  125. #endif /* BYTESWAP_H */