sim-endian.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /* This file is part of the program psim.
  2. Copyright (C) 1994-1995, Andrew Cagney <cagney@highland.com.au>
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 3 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, see <http://www.gnu.org/licenses/>.
  13. */
  14. #ifndef _SIM_ENDIAN_H_
  15. #define _SIM_ENDIAN_H_
  16. /* C byte conversion functions */
  17. INLINE_PSIM_ENDIAN(unsigned_1) endian_h2t_1(unsigned_1 x);
  18. INLINE_PSIM_ENDIAN(unsigned_2) endian_h2t_2(unsigned_2 x);
  19. INLINE_PSIM_ENDIAN(unsigned_4) endian_h2t_4(unsigned_4 x);
  20. INLINE_PSIM_ENDIAN(unsigned_8) endian_h2t_8(unsigned_8 x);
  21. INLINE_PSIM_ENDIAN(unsigned_1) endian_t2h_1(unsigned_1 x);
  22. INLINE_PSIM_ENDIAN(unsigned_2) endian_t2h_2(unsigned_2 x);
  23. INLINE_PSIM_ENDIAN(unsigned_4) endian_t2h_4(unsigned_4 x);
  24. INLINE_PSIM_ENDIAN(unsigned_8) endian_t2h_8(unsigned_8 x);
  25. INLINE_PSIM_ENDIAN(unsigned_1) swap_1(unsigned_1 x);
  26. INLINE_PSIM_ENDIAN(unsigned_2) swap_2(unsigned_2 x);
  27. INLINE_PSIM_ENDIAN(unsigned_4) swap_4(unsigned_4 x);
  28. INLINE_PSIM_ENDIAN(unsigned_8) swap_8(unsigned_8 x);
  29. INLINE_PSIM_ENDIAN(unsigned_1) endian_h2be_1(unsigned_1 x);
  30. INLINE_PSIM_ENDIAN(unsigned_2) endian_h2be_2(unsigned_2 x);
  31. INLINE_PSIM_ENDIAN(unsigned_4) endian_h2be_4(unsigned_4 x);
  32. INLINE_PSIM_ENDIAN(unsigned_8) endian_h2be_8(unsigned_8 x);
  33. INLINE_PSIM_ENDIAN(unsigned_1) endian_be2h_1(unsigned_1 x);
  34. INLINE_PSIM_ENDIAN(unsigned_2) endian_be2h_2(unsigned_2 x);
  35. INLINE_PSIM_ENDIAN(unsigned_4) endian_be2h_4(unsigned_4 x);
  36. INLINE_PSIM_ENDIAN(unsigned_8) endian_be2h_8(unsigned_8 x);
  37. INLINE_PSIM_ENDIAN(unsigned_1) endian_h2le_1(unsigned_1 x);
  38. INLINE_PSIM_ENDIAN(unsigned_2) endian_h2le_2(unsigned_2 x);
  39. INLINE_PSIM_ENDIAN(unsigned_4) endian_h2le_4(unsigned_4 x);
  40. INLINE_PSIM_ENDIAN(unsigned_8) endian_h2le_8(unsigned_8 x);
  41. INLINE_PSIM_ENDIAN(unsigned_1) endian_le2h_1(unsigned_1 x);
  42. INLINE_PSIM_ENDIAN(unsigned_2) endian_le2h_2(unsigned_2 x);
  43. INLINE_PSIM_ENDIAN(unsigned_4) endian_le2h_4(unsigned_4 x);
  44. INLINE_PSIM_ENDIAN(unsigned_8) endian_le2h_8(unsigned_8 x);
  45. /* SWAP */
  46. #define SWAP_1 swap_1
  47. #define SWAP_2 swap_2
  48. #define SWAP_4 swap_4
  49. #define SWAP_8 swap_8
  50. /* HOST to BE */
  51. #define H2BE_1 endian_h2be_1
  52. #define H2BE_2 endian_h2be_2
  53. #define H2BE_4 endian_h2be_4
  54. #define H2BE_8 endian_h2be_8
  55. #define BE2H_1 endian_be2h_1
  56. #define BE2H_2 endian_be2h_2
  57. #define BE2H_4 endian_be2h_4
  58. #define BE2H_8 endian_be2h_8
  59. /* HOST to LE */
  60. #define H2LE_1 endian_h2le_1
  61. #define H2LE_2 endian_h2le_2
  62. #define H2LE_4 endian_h2le_4
  63. #define H2LE_8 endian_h2le_8
  64. #define LE2H_1 endian_le2h_1
  65. #define LE2H_2 endian_le2h_2
  66. #define LE2H_4 endian_le2h_4
  67. #define LE2H_8 endian_le2h_8
  68. /* HOST to TARGET */
  69. #define H2T_1 endian_h2t_1
  70. #define H2T_2 endian_h2t_2
  71. #define H2T_4 endian_h2t_4
  72. #define H2T_8 endian_h2t_8
  73. #define T2H_1 endian_t2h_1
  74. #define T2H_2 endian_t2h_2
  75. #define T2H_4 endian_t2h_4
  76. #define T2H_8 endian_t2h_8
  77. /* CONVERT IN PLACE
  78. These macros, given an argument of unknown size, swap its value in
  79. place if a host/target conversion is required. */
  80. #define H2T(VARIABLE) \
  81. do { \
  82. switch (sizeof(VARIABLE)) { \
  83. case 1: VARIABLE = H2T_1(VARIABLE); break; \
  84. case 2: VARIABLE = H2T_2(VARIABLE); break; \
  85. case 4: VARIABLE = H2T_4(VARIABLE); break; \
  86. case 8: VARIABLE = H2T_8(VARIABLE); break; \
  87. } \
  88. } while (0)
  89. #define T2H(VARIABLE) \
  90. do { \
  91. switch (sizeof(VARIABLE)) { \
  92. case 1: VARIABLE = T2H_1(VARIABLE); break; \
  93. case 2: VARIABLE = T2H_2(VARIABLE); break; \
  94. case 4: VARIABLE = T2H_4(VARIABLE); break; \
  95. case 8: VARIABLE = T2H_8(VARIABLE); break; \
  96. } \
  97. } while (0)
  98. #define SWAP(VARIABLE) \
  99. do { \
  100. switch (sizeof(VARIABLE)) { \
  101. case 1: VARIABLE = SWAP_1(VARIABLE); break; \
  102. case 2: VARIABLE = SWAP_2(VARIABLE); break; \
  103. case 4: VARIABLE = SWAP_4(VARIABLE); break; \
  104. case 8: VARIABLE = SWAP_8(VARIABLE); break; \
  105. } \
  106. } while (0)
  107. #define H2BE(VARIABLE) \
  108. do { \
  109. switch (sizeof(VARIABLE)) { \
  110. case 1: VARIABLE = H2BE_1(VARIABLE); break; \
  111. case 2: VARIABLE = H2BE_2(VARIABLE); break; \
  112. case 4: VARIABLE = H2BE_4(VARIABLE); break; \
  113. case 8: VARIABLE = H2BE_8(VARIABLE); break; \
  114. } \
  115. } while (0)
  116. #define BE2H(VARIABLE) \
  117. do { \
  118. switch (sizeof(VARIABLE)) { \
  119. case 1: VARIABLE = BE2H_1(VARIABLE); break; \
  120. case 2: VARIABLE = BE2H_2(VARIABLE); break; \
  121. case 4: VARIABLE = BE2H_4(VARIABLE); break; \
  122. case 8: VARIABLE = BE2H_8(VARIABLE); break; \
  123. } \
  124. } while (0)
  125. #define H2LE(VARIABLE) \
  126. do { \
  127. switch (sizeof(VARIABLE)) { \
  128. case 1: VARIABLE = H2LE_1(VARIABLE); break; \
  129. case 2: VARIABLE = H2LE_2(VARIABLE); break; \
  130. case 4: VARIABLE = H2LE_4(VARIABLE); break; \
  131. case 8: VARIABLE = H2LE_8(VARIABLE); break; \
  132. } \
  133. } while (0)
  134. #define LE2H(VARIABLE) \
  135. do { \
  136. switch (sizeof(VARIABLE)) { \
  137. case 1: VARIABLE = LE2H_1(VARIABLE); break; \
  138. case 2: VARIABLE = LE2H_2(VARIABLE); break; \
  139. case 4: VARIABLE = LE2H_4(VARIABLE); break; \
  140. case 8: VARIABLE = LE2H_8(VARIABLE); break; \
  141. } \
  142. } while (0)
  143. /* TARGET WORD:
  144. Byte swap a quantity the size of the targets word */
  145. #if (WITH_TARGET_WORD_BITSIZE == 64)
  146. #define H2T_word H2T_8
  147. #define T2H_word T2H_8
  148. #define H2BE_word H2BE_8
  149. #define BE2H_word BE2H_8
  150. #define H2LE_word H2LE_8
  151. #define LE2H_word LE2H_8
  152. #define SWAP_word SWAP_8
  153. #endif
  154. #if (WITH_TARGET_WORD_BITSIZE == 32)
  155. #define H2T_word H2T_4
  156. #define T2H_word T2H_4
  157. #define H2BE_word H2BE_4
  158. #define BE2H_word BE2H_4
  159. #define H2LE_word H2LE_4
  160. #define LE2H_word LE2H_4
  161. #define SWAP_word SWAP_4
  162. #endif
  163. /* TARGET CELL:
  164. Byte swap a quantity the size of the targets IEEE 1275 memory cell */
  165. #define H2T_cell H2T_4
  166. #define T2H_cell T2H_4
  167. #define H2BE_cell H2BE_4
  168. #define BE2H_cell BE2H_4
  169. #define H2LE_cell H2LE_4
  170. #define LE2H_cell LE2H_4
  171. #define SWAP_cell SWAP_4
  172. #if (SIM_ENDIAN_INLINE & INCLUDE_MODULE)
  173. # include "sim-endian.c"
  174. #endif
  175. #endif /* _SIM_ENDIAN_H_ */