Commit af8ffdfd2b3b0894a54cb6333217f9f3ae6eaa81

Authored by bellard
1 parent b932caba

byte swap functions


git-svn-id: svn://svn.savannah.nongnu.org/qemu/trunk@1031 c046a42c-6fe2-441c-8c8c-71466251a162
Showing 1 changed file with 99 additions and 0 deletions
... ... @@ -73,4 +73,103 @@ static inline void bswap64s(uint64_t *s)
73 73 *s = bswap64(*s);
74 74 }
75 75  
  76 +#if defined(WORDS_BIGENDIAN)
  77 +#define be_bswap(v, size) (v)
  78 +#define le_bswap(v, size) bswap ## size(v)
  79 +#define be_bswaps(v, size)
  80 +#define le_bswaps(p, size) *p = bswap ## size(*p);
  81 +#else
  82 +#define le_bswap(v, size) (v)
  83 +#define be_bswap(v, size) bswap ## size(v)
  84 +#define le_bswaps(v, size)
  85 +#define be_bswaps(p, size) *p = bswap ## size(*p);
  86 +#endif
  87 +
  88 +#define CPU_CONVERT(endian, size, type)\
  89 +static inline type endian ## size ## _to_cpu(type v)\
  90 +{\
  91 + return endian ## _bswap(v, size);\
  92 +}\
  93 +\
  94 +static inline type cpu_to_ ## endian ## size(type v)\
  95 +{\
  96 + return endian ## _bswap(v, size);\
  97 +}\
  98 +\
  99 +static inline void endian ## size ## _to_cpus(type *p)\
  100 +{\
  101 + endian ## _bswaps(p, size)\
  102 +}\
  103 +\
  104 +static inline void cpu_to_ ## endian ## size ## s(type *p)\
  105 +{\
  106 + endian ## _bswaps(p, size)\
  107 +}\
  108 +\
  109 +static inline type endian ## size ## _to_cpup(const type *p)\
  110 +{\
  111 + return endian ## size ## _to_cpu(*p);\
  112 +}\
  113 +\
  114 +static inline void cpu_to_ ## endian ## size ## w(type *p, type v)\
  115 +{\
  116 + *p = cpu_to_ ## endian ## size(v);\
  117 +}
  118 +
  119 +CPU_CONVERT(be, 16, uint16_t)
  120 +CPU_CONVERT(be, 32, uint32_t)
  121 +CPU_CONVERT(be, 64, uint64_t)
  122 +
  123 +CPU_CONVERT(le, 16, uint16_t)
  124 +CPU_CONVERT(le, 32, uint32_t)
  125 +CPU_CONVERT(le, 64, uint64_t)
  126 +
  127 +/* unaligned versions (optimized for frequent unaligned accesses)*/
  128 +
  129 +#if defined(__i386__) || defined(__powerpc__)
  130 +
  131 +#define cpu_to_le16wu(p, v) cpu_to_le16w(p, v)
  132 +#define cpu_to_le32wu(p, v) cpu_to_le32w(p, v)
  133 +#define le16_to_cpupu(p) le16_to_cpup(p)
  134 +#define le32_to_cpupu(p) le32_to_cpup(p)
  135 +
  136 +#else
  137 +
  138 +static inline void cpu_to_le16wu(uint16_t *p, uint16_t v)
  139 +{
  140 + uint8_t *p1 = (uint8_t *)p;
  141 +
  142 + p1[0] = v;
  143 + p1[1] = v >> 8;
  144 +}
  145 +
  146 +static inline void cpu_to_le32wu(uint32_t *p, uint32_t v)
  147 +{
  148 + uint8_t *p1 = (uint8_t *)p;
  149 +
  150 + p1[0] = v;
  151 + p1[1] = v >> 8;
  152 + p1[2] = v >> 16;
  153 + p1[3] = v >> 24;
  154 +}
  155 +
  156 +static inline uint16_t le16_to_cpupu(const uint16_t *p)
  157 +{
  158 + const uint8_t *p1 = (const uint8_t *)p;
  159 + return p1[0] | (p1[1] << 8);
  160 +}
  161 +
  162 +static inline uint32_t le32_to_cpupu(const uint32_t *p)
  163 +{
  164 + const uint8_t *p1 = (const uint8_t *)p;
  165 + return p1[0] | (p1[1] << 8) | (p1[2] << 16) | (p1[3] << 24);
  166 +}
  167 +
  168 +#endif
  169 +
  170 +#undef le_bswap
  171 +#undef be_bswap
  172 +#undef le_bswaps
  173 +#undef be_bswaps
  174 +
76 175 #endif /* BSWAP_H */
... ...