bswap.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. * Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
  3. * The President and Fellows of Harvard College.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of the University nor the names of its contributors
  14. * may be used to endorse or promote products derived from this software
  15. * without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
  18. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
  21. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27. * SUCH DAMAGE.
  28. */
  29. #include <types.h>
  30. #include <endian.h>
  31. /*
  32. * Unconditional byte-swap functions.
  33. *
  34. * bswap16, 32, and 64 unconditionally swap byte order of integers of
  35. * the respective bitsize.
  36. *
  37. * The advantage of writing them out like this is that the bit
  38. * patterns are easily validated by inspection. Also, this form is
  39. * more likely to be picked up by the compiler and converted into
  40. * byte-swap machine instructions (if those exist) than something
  41. * loop-based.
  42. */
  43. uint16_t
  44. bswap16(uint16_t val)
  45. {
  46. return ((val & 0x00ff) << 8)
  47. | ((val & 0xff00) >> 8);
  48. }
  49. uint32_t
  50. bswap32(uint32_t val)
  51. {
  52. return ((val & 0x000000ff) << 24)
  53. | ((val & 0x0000ff00) << 8)
  54. | ((val & 0x00ff0000) >> 8)
  55. | ((val & 0xff000000) >> 24);
  56. }
  57. uint64_t
  58. bswap64(uint64_t val)
  59. {
  60. return ((val & 0x00000000000000ff) << 56)
  61. | ((val & 0x000000000000ff00) << 40)
  62. | ((val & 0x0000000000ff0000) << 24)
  63. | ((val & 0x00000000ff000000) << 8)
  64. | ((val & 0x000000ff00000000) << 8)
  65. | ((val & 0x0000ff0000000000) << 24)
  66. | ((val & 0x00ff000000000000) >> 40)
  67. | ((val & 0xff00000000000000) >> 56);
  68. }
  69. /*
  70. * Network byte order byte-swap functions.
  71. *
  72. * For ntoh* and hton*:
  73. * *s are for "short" (16-bit)
  74. * *l are for "long" (32-bit)
  75. * *ll are for "long long" (64-bit)
  76. *
  77. * hton* convert from host byte order to network byte order.
  78. * ntoh* convert from network byte order to host byte order.
  79. *
  80. * Network byte order is big-endian.
  81. *
  82. * Note that right now the only platforms OS/161 runs on are
  83. * big-endian, so these functions are actually all empty.
  84. *
  85. * These should maybe be made inline.
  86. */
  87. #if _BYTE_ORDER == _LITTLE_ENDIAN
  88. #define TO(tag, bits, type) \
  89. type ntoh##tag(type val) { return bswap##bits(val); } \
  90. type hton##tag(type val) { return bswap##bits(val); }
  91. #endif
  92. /*
  93. * Use a separate #if, so if the header file defining the symbols gets
  94. * omitted or messed up the build will fail instead of silently choosing
  95. * the wrong option.
  96. */
  97. #if _BYTE_ORDER == _BIG_ENDIAN
  98. #define TO(tag, bits, type) \
  99. type ntoh##tag(type val) { return val; } \
  100. type hton##tag(type val) { return val; }
  101. #endif
  102. #if _BYTE_ORDER == _PDP_ENDIAN
  103. #error "You lose."
  104. #endif
  105. #ifndef TO
  106. #error "_BYTE_ORDER not set"
  107. #endif
  108. TO(s, 16, uint16_t)
  109. TO(l, 32, uint32_t)
  110. TO(ll, 64, uint64_t)
  111. /*
  112. * Some utility functions for handling 64-bit values.
  113. *
  114. * join32to64 pastes two adjoining 32-bit values together in the right
  115. * way to treat them as a 64-bit value, depending on endianness.
  116. * split64to32 is the inverse operation.
  117. *
  118. * The 32-bit arguments should be passed in the order they appear in
  119. * memory, not as high word and low word; the whole point of these
  120. * functions is to know which is the high word and which is the low
  121. * word.
  122. */
  123. void
  124. join32to64(uint32_t x1, uint32_t x2, uint64_t *y2)
  125. {
  126. #if _BYTE_ORDER == _BIG_ENDIAN
  127. *y2 = ((uint64_t)x1 << 32) | (uint64_t)x2;
  128. #elif _BYTE_ORDER == _LITTLE_ENDIAN
  129. *y2 = (uint64_t)x1 | ((uint64_t)x2 << 32);
  130. #else
  131. #error "Eh?"
  132. #endif
  133. }
  134. void
  135. split64to32(uint64_t x, uint32_t *y1, uint32_t *y2)
  136. {
  137. #if _BYTE_ORDER == _BIG_ENDIAN
  138. *y1 = x >> 32;
  139. *y2 = x & 0xffffffff;
  140. #elif _BYTE_ORDER == _LITTLE_ENDIAN
  141. *y1 = x & 0xffffffff;
  142. *y2 = x >> 32;
  143. #else
  144. #error "Eh?"
  145. #endif
  146. }