| 1 | #ifndef __MDFN_ENDIAN_H |
| 2 | #define __MDFN_ENDIAN_H |
| 3 | |
| 4 | #include <stdio.h> |
| 5 | #include <stdint.h> |
| 6 | |
| 7 | #ifdef MSB_FIRST |
| 8 | #ifndef le32toh |
| 9 | #define le32toh(l) ((((l)>>24) & 0xff) | (((l)>>8) & 0xff00) \ |
| 10 | | (((l)<<8) & 0xff0000) | (((l)<<24) & 0xff000000)) |
| 11 | #endif |
| 12 | #ifndef le16toh |
| 13 | #define le16toh(l) ((((l)>>8) & 0xff) | (((l)<<8) & 0xff00)) |
| 14 | #endif |
| 15 | #else |
| 16 | #ifndef le32toh |
| 17 | #define le32toh(l) (l) |
| 18 | #endif |
| 19 | #ifndef le16toh |
| 20 | #define le16toh(l) (l) |
| 21 | #endif |
| 22 | #endif |
| 23 | |
| 24 | #ifndef htole32 |
| 25 | #define htole32 le32toh |
| 26 | #endif |
| 27 | |
| 28 | #ifndef htole16 |
| 29 | #define htole16 le16toh |
| 30 | #endif |
| 31 | |
| 32 | #ifdef __cplusplus |
| 33 | extern "C" { |
| 34 | #endif |
| 35 | |
| 36 | int write16le(uint16_t b, FILE *fp); |
| 37 | int write32le(uint32_t b, FILE *fp); |
| 38 | int read32le(uint32_t *Bufo, FILE *fp); |
| 39 | |
| 40 | void Endian_A16_Swap(void *src, uint32_t nelements); |
| 41 | void Endian_A32_Swap(void *src, uint32_t nelements); |
| 42 | void Endian_A64_Swap(void *src, uint32_t nelements); |
| 43 | |
| 44 | void Endian_A16_LE_to_NE(void *src, uint32_t nelements); |
| 45 | void Endian_A16_BE_to_NE(void *src, uint32_t nelements); |
| 46 | void Endian_A32_LE_to_NE(void *src, uint32_t nelements); |
| 47 | void Endian_A64_LE_to_NE(void *src, uint32_t nelements); |
| 48 | |
| 49 | void FlipByteOrder(uint8_t *src, uint32_t count); |
| 50 | |
| 51 | // The following functions can encode/decode to unaligned addresses. |
| 52 | |
| 53 | static inline void MDFN_en16lsb(uint8_t *buf, uint16_t morp) |
| 54 | { |
| 55 | buf[0]=morp; |
| 56 | buf[1]=morp>>8; |
| 57 | } |
| 58 | |
| 59 | static inline void MDFN_en24lsb(uint8_t *buf, uint32_t morp) |
| 60 | { |
| 61 | buf[0]=morp; |
| 62 | buf[1]=morp>>8; |
| 63 | buf[2]=morp>>16; |
| 64 | } |
| 65 | |
| 66 | |
| 67 | static inline void MDFN_en32lsb(uint8_t *buf, uint32_t morp) |
| 68 | { |
| 69 | buf[0]=morp; |
| 70 | buf[1]=morp>>8; |
| 71 | buf[2]=morp>>16; |
| 72 | buf[3]=morp>>24; |
| 73 | } |
| 74 | |
| 75 | static inline void MDFN_en64lsb(uint8_t *buf, uint64_t morp) |
| 76 | { |
| 77 | buf[0]=morp >> 0; |
| 78 | buf[1]=morp >> 8; |
| 79 | buf[2]=morp >> 16; |
| 80 | buf[3]=morp >> 24; |
| 81 | buf[4]=morp >> 32; |
| 82 | buf[5]=morp >> 40; |
| 83 | buf[6]=morp >> 48; |
| 84 | buf[7]=morp >> 56; |
| 85 | } |
| 86 | |
| 87 | |
| 88 | static inline void MDFN_en16msb(uint8_t *buf, uint16_t morp) |
| 89 | { |
| 90 | buf[0] = morp >> 8; |
| 91 | buf[1] = morp; |
| 92 | } |
| 93 | |
| 94 | static inline void MDFN_en24msb(uint8_t *buf, uint32_t morp) |
| 95 | { |
| 96 | buf[0] = morp >> 16; |
| 97 | buf[1] = morp >> 8; |
| 98 | buf[2] = morp; |
| 99 | } |
| 100 | |
| 101 | static inline void MDFN_en32msb(uint8_t *buf, uint32_t morp) |
| 102 | { |
| 103 | buf[0] = morp >> 24; |
| 104 | buf[1] = morp >> 16; |
| 105 | buf[2] = morp >> 8; |
| 106 | buf[3] = morp; |
| 107 | } |
| 108 | |
| 109 | static inline void MDFN_en64msb(uint8_t *buf, uint64_t morp) |
| 110 | { |
| 111 | buf[0] = morp >> 56; |
| 112 | buf[1] = morp >> 48; |
| 113 | buf[2] = morp >> 40; |
| 114 | buf[3] = morp >> 32; |
| 115 | buf[4] = morp >> 24; |
| 116 | buf[5] = morp >> 16; |
| 117 | buf[6] = morp >> 8; |
| 118 | buf[7] = morp >> 0; |
| 119 | } |
| 120 | |
| 121 | static inline uint16_t MDFN_de16lsb(const uint8_t *morp) |
| 122 | { |
| 123 | return(morp[0] | (morp[1] << 8)); |
| 124 | } |
| 125 | |
| 126 | static inline uint32_t MDFN_de24lsb(const uint8_t *morp) |
| 127 | { |
| 128 | return(morp[0]|(morp[1]<<8)|(morp[2]<<16)); |
| 129 | } |
| 130 | |
| 131 | static inline uint32_t MDFN_de32lsb(const uint8_t *morp) |
| 132 | { |
| 133 | return(morp[0]|(morp[1]<<8)|(morp[2]<<16)|(morp[3]<<24)); |
| 134 | } |
| 135 | |
| 136 | static inline uint64_t MDFN_de64lsb(const uint8_t *morp) |
| 137 | { |
| 138 | uint64_t ret = 0; |
| 139 | |
| 140 | ret |= (uint64_t)morp[0]; |
| 141 | ret |= (uint64_t)morp[1] << 8; |
| 142 | ret |= (uint64_t)morp[2] << 16; |
| 143 | ret |= (uint64_t)morp[3] << 24; |
| 144 | ret |= (uint64_t)morp[4] << 32; |
| 145 | ret |= (uint64_t)morp[5] << 40; |
| 146 | ret |= (uint64_t)morp[6] << 48; |
| 147 | ret |= (uint64_t)morp[7] << 56; |
| 148 | |
| 149 | return(ret); |
| 150 | } |
| 151 | |
| 152 | static inline uint16_t MDFN_de16msb(const uint8_t *morp) |
| 153 | { |
| 154 | return(morp[1] | (morp[0] << 8)); |
| 155 | } |
| 156 | |
| 157 | static inline uint32_t MDFN_de24msb(const uint8_t *morp) |
| 158 | { |
| 159 | return((morp[2]<<0)|(morp[1]<<8)|(morp[0]<<16)); |
| 160 | } |
| 161 | |
| 162 | |
| 163 | static inline uint32_t MDFN_de32msb(const uint8_t *morp) |
| 164 | { |
| 165 | return(morp[3]|(morp[2]<<8)|(morp[1]<<16)|(morp[0]<<24)); |
| 166 | } |
| 167 | |
| 168 | #ifdef __cplusplus |
| 169 | } |
| 170 | #endif |
| 171 | |
| 172 | #endif |