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