00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00028 #ifndef __CDIO_BYTESEX_H__
00029 #define __CDIO_BYTESEX_H__
00030
00031 #include <cdio/types.h>
00032 #include <cdio/bytesex_asm.h>
00033 #include <cdio/logging.h>
00034
00036 #define UINT16_SWAP_LE_BE_C(val) ((uint16_t) ( \
00037 (((uint16_t) (val) & (uint16_t) 0x00ffU) << 8) | \
00038 (((uint16_t) (val) & (uint16_t) 0xff00U) >> 8)))
00039
00041 #define UINT32_SWAP_LE_BE_C(val) ((uint32_t) ( \
00042 (((uint32_t) (val) & (uint32_t) 0x000000ffU) << 24) | \
00043 (((uint32_t) (val) & (uint32_t) 0x0000ff00U) << 8) | \
00044 (((uint32_t) (val) & (uint32_t) 0x00ff0000U) >> 8) | \
00045 (((uint32_t) (val) & (uint32_t) 0xff000000U) >> 24)))
00046
00048 #define UINT64_SWAP_LE_BE_C(val) ((uint64_t) ( \
00049 (((uint64_t) (val) & (uint64_t) UINT64_C(0x00000000000000ff)) << 56) | \
00050 (((uint64_t) (val) & (uint64_t) UINT64_C(0x000000000000ff00)) << 40) | \
00051 (((uint64_t) (val) & (uint64_t) UINT64_C(0x0000000000ff0000)) << 24) | \
00052 (((uint64_t) (val) & (uint64_t) UINT64_C(0x00000000ff000000)) << 8) | \
00053 (((uint64_t) (val) & (uint64_t) UINT64_C(0x000000ff00000000)) >> 8) | \
00054 (((uint64_t) (val) & (uint64_t) UINT64_C(0x0000ff0000000000)) >> 24) | \
00055 (((uint64_t) (val) & (uint64_t) UINT64_C(0x00ff000000000000)) >> 40) | \
00056 (((uint64_t) (val) & (uint64_t) UINT64_C(0xff00000000000000)) >> 56)))
00057
00058 #ifndef UINT16_SWAP_LE_BE
00059 # define UINT16_SWAP_LE_BE UINT16_SWAP_LE_BE_C
00060 #endif
00061
00062 #ifndef UINT32_SWAP_LE_BE
00063 # define UINT32_SWAP_LE_BE UINT32_SWAP_LE_BE_C
00064 #endif
00065
00066 #ifndef UINT64_SWAP_LE_BE
00067 # define UINT64_SWAP_LE_BE UINT64_SWAP_LE_BE_C
00068 #endif
00069
00070 inline static
00071 uint16_t uint16_swap_le_be (const uint16_t val)
00072 {
00073 return UINT16_SWAP_LE_BE (val);
00074 }
00075
00076 inline static
00077 uint32_t uint32_swap_le_be (const uint32_t val)
00078 {
00079 return UINT32_SWAP_LE_BE (val);
00080 }
00081
00082 inline static
00083 uint64_t uint64_swap_le_be (const uint64_t val)
00084 {
00085 return UINT64_SWAP_LE_BE (val);
00086 }
00087
00088 # define UINT8_TO_BE(val) ((uint8_t) (val))
00089 # define UINT8_TO_LE(val) ((uint8_t) (val))
00090 #ifdef WORDS_BIGENDIAN
00091 # define UINT16_TO_BE(val) ((uint16_t) (val))
00092 # define UINT16_TO_LE(val) ((uint16_t) UINT16_SWAP_LE_BE(val))
00093
00094 # define UINT32_TO_BE(val) ((uint32_t) (val))
00095 # define UINT32_TO_LE(val) ((uint32_t) UINT32_SWAP_LE_BE(val))
00096
00097 # define UINT64_TO_BE(val) ((uint64_t) (val))
00098 # define UINT64_TO_LE(val) ((uint64_t) UINT64_SWAP_LE_BE(val))
00099 #else
00100 # define UINT16_TO_BE(val) ((uint16_t) UINT16_SWAP_LE_BE(val))
00101 # define UINT16_TO_LE(val) ((uint16_t) (val))
00102
00103 # define UINT32_TO_BE(val) ((uint32_t) UINT32_SWAP_LE_BE(val))
00104 # define UINT32_TO_LE(val) ((uint32_t) (val))
00105
00106 # define UINT64_TO_BE(val) ((uint64_t) UINT64_SWAP_LE_BE(val))
00107 # define UINT64_TO_LE(val) ((uint64_t) (val))
00108 #endif
00109
00111 #define UINT8_FROM_BE(val) (UINT8_TO_BE (val))
00112 #define UINT8_FROM_LE(val) (UINT8_TO_LE (val))
00113 #define UINT16_FROM_BE(val) (UINT16_TO_BE (val))
00114 #define UINT16_FROM_LE(val) (UINT16_TO_LE (val))
00115 #define UINT32_FROM_BE(val) (UINT32_TO_BE (val))
00116 #define UINT32_FROM_LE(val) (UINT32_TO_LE (val))
00117 #define UINT64_FROM_BE(val) (UINT64_TO_BE (val))
00118 #define UINT64_FROM_LE(val) (UINT64_TO_LE (val))
00119
00121 #define CVT_TO_FUNC(bits) \
00122 static inline uint ## bits ## _t \
00123 uint ## bits ## _to_be (uint ## bits ## _t val) \
00124 { return UINT ## bits ## _TO_BE (val); } \
00125 static inline uint ## bits ## _t \
00126 uint ## bits ## _to_le (uint ## bits ## _t val) \
00127 { return UINT ## bits ## _TO_LE (val); } \
00128
00129 CVT_TO_FUNC(8)
00130 CVT_TO_FUNC(16)
00131 CVT_TO_FUNC(32)
00132 CVT_TO_FUNC(64)
00133
00134 #undef CVT_TO_FUNC
00135
00136 #define uint8_from_be(val) (uint8_to_be (val))
00137 #define uint8_from_le(val) (uint8_to_le (val))
00138 #define uint16_from_be(val) (uint16_to_be (val))
00139 #define uint16_from_le(val) (uint16_to_le (val))
00140 #define uint32_from_be(val) (uint32_to_be (val))
00141 #define uint32_from_le(val) (uint32_to_le (val))
00142 #define uint64_from_be(val) (uint64_to_be (val))
00143 #define uint64_from_le(val) (uint64_to_le (val))
00144
00148 #define to_711(i) uint8_to_le(i)
00149
00151 #define from_711(i) uint8_from_le(i)
00152
00154 #define to_721(i) uint16_to_le(i)
00155
00157 #define from_721(i) uint16_from_le(i)
00158
00160 #define to_722(i) uint16_to_be(i)
00161
00163 #define from_722(i) uint16_from_be(i)
00164
00166 static inline uint32_t
00167 to_723(uint16_t i)
00168 {
00169 return uint32_swap_le_be(i) | i;
00170 }
00171
00173 static inline uint16_t
00174 from_723 (uint32_t p)
00175 {
00176 if (uint32_swap_le_be (p) != p)
00177 cdio_warn ("from_723: broken byte order");
00178
00179 return (0xFFFF & p);
00180 }
00181
00183 #define to_731(i) uint32_to_le(i)
00184
00186 #define from_731(i) uint32_from_le(i)
00187
00189 #define to_732(i) uint32_to_be(i)
00190
00192 #define from_732(i) uint32_from_be(i)
00193
00195 static inline uint64_t
00196 to_733(uint32_t i)
00197 {
00198 return uint64_swap_le_be(i) | i;
00199 }
00200
00202 static inline uint32_t
00203 from_733 (uint64_t p)
00204 {
00205 if (uint64_swap_le_be (p) != p)
00206 cdio_warn ("from_733: broken byte order");
00207
00208 return (UINT32_C(0xFFFFFFFF) & p);
00209 }
00210
00211 #endif
00212
00213
00214
00215
00216
00217
00218
00219
00220