VirtualBox

Changeset 11207 in vbox for trunk/include


Ignore:
Timestamp:
Aug 7, 2008 3:36:08 PM (16 years ago)
Author:
vboxsync
Message:

iprt/cdefs.h: Created a set of byte swapping macros for constants for use in case statements and swapping at compile time.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/include/iprt/cdefs.h

    r11198 r11207  
    10571057 * Reverses the byte order of an uint64_t value. */
    10581058#if 0
    1059 # define RT_BSWAP_U64(u64)  RT_MAKE_U64(RT_H2BE_U32((u64) >> 32), RT_H2BE_U32((u64) & 0xffffffff))
    1060 #else
     1059# define RT_BSWAP_U64(u64)  RT_BSWAP_U64_C(u64)
     1060#elif defined(__GNUC__)
     1061/** @todo use __builtin_constant_p? */
    10611062# define RT_BSWAP_U64(u64)  ASMByteSwapU64(u64)
     1063#else
     1064# define RT_BSWAP_U64(u64)  ASMByteSwapU64(u64)
    10621065#endif
    10631066
    10641067/** @def RT_BSWAP_U32
    1065  * Converts uint32_t value from host to big endian byte order. */
     1068 * Reverses the byte order of an uint32_t value. */
    10661069#if 0
    1067 # define RT_BSWAP_U32(u32)  (RT_BYTE4(u32) | (RT_BYTE3(u32) << 8) | (RT_BYTE2(u32) << 16) | (RT_BYTE1(u32) << 24))
    1068 #else
     1070# define RT_BSWAP_U32(u32)  RT_BSWAP_U32_C(u32)
     1071#elif defined(__GNUC__)
     1072/** @todo use __builtin_constant_p? */
    10691073# define RT_BSWAP_U32(u32)  ASMByteSwapU32(u32)
     1074#else
     1075# define RT_BSWAP_U32(u32)  ASMByteSwapU32(u32)
    10701076#endif
    10711077
    10721078/** @def RT_BSWAP_U16
    1073  * Converts uint16_t value from host to big endian byte order. */
     1079 * Reverses the byte order of an uint16_t value. */
    10741080#if 0
    1075 # define RT_BSWAP_U16(u16)  (RT_HIBYTE(u16) | (RT_LOBYTE(u16) << 8))
    1076 #else
     1081# define RT_BSWAP_U16(u16)  RT_BSWAP_U16_C(u16)
     1082#elif defined(__GNUC__)
     1083/** @todo use __builtin_constant_p? */
    10771084# define RT_BSWAP_U16(u16)  ASMByteSwapU16(u16)
    1078 #endif
     1085#else
     1086# define RT_BSWAP_U16(u16)  ASMByteSwapU16(u16)
     1087#endif
     1088
     1089
     1090/** @def RT_BSWAP_U64_C
     1091 * Reverses the byte order of an uint64_t constant. */
     1092#define RT_BSWAP_U64_C(u64) RT_MAKE_U64(RT_BSWAP_U32_C((u64) >> 32), RT_BSWAP_U32_C((u64) & 0xffffffff))
     1093
     1094/** @def RT_BSWAP_U32_C
     1095 * Reverses the byte order of an uint32_t constant. */
     1096#define RT_BSWAP_U32_C(u32) (RT_BYTE4(u32) | (RT_BYTE3(u32) << 8) | (RT_BYTE2(u32) << 16) | (RT_BYTE1(u32) << 24))
     1097
     1098/** @def RT_BSWAP_U16_C
     1099 * Reverses the byte order of an uint16_t constant. */
     1100#define RT_BSWAP_U16_C(u16) (RT_HIBYTE(u16) | (RT_LOBYTE(u16) << 8))
    10791101
    10801102
     
    10871109#endif
    10881110
     1111/** @def RT_H2LE_U64_C
     1112 * Converts an uint64_t constant from host to little endian byte order. */
     1113#ifdef RT_BIG_ENDIAN
     1114# define RT_H2LE_U64_C(u64) RT_BSWAP_U64_C(u64)
     1115#else
     1116# define RT_H2LE_U64_C(u64) (u64)
     1117#endif
     1118
    10891119/** @def RT_H2LE_U32
    10901120 * Converts an uint32_t value from host to little endian byte order. */
     
    10951125#endif
    10961126
     1127/** @def RT_H2LE_U32_C
     1128 * Converts an uint32_t constant from host to little endian byte order. */
     1129#ifdef RT_BIG_ENDIAN
     1130# define RT_H2LE_U32_C(u32) RT_BSWAP_U32_C(u32)
     1131#else
     1132# define RT_H2LE_U32_C(u32) (u32)
     1133#endif
     1134
    10971135/** @def RT_H2LE_U16
    10981136 * Converts an uint16_t value from host to little endian byte order. */
     
    11031141#endif
    11041142
     1143/** @def RT_H2LE_U16_C
     1144 * Converts an uint16_t constant from host to little endian byte order. */
     1145#ifdef RT_BIG_ENDIAN
     1146# define RT_H2LE_U16_C(u16) RT_BSWAP_U16_C(u16)
     1147#else
     1148# define RT_H2LE_U16_C(u16) (u16)
     1149#endif
     1150
     1151
    11051152/** @def RT_LE2H_U64
    11061153 * Converts an uint64_t value from little endian to host byte order. */
     
    11111158#endif
    11121159
     1160/** @def RT_LE2H_U64_C
     1161 * Converts an uint64_t constant from little endian to host byte order. */
     1162#ifdef RT_BIG_ENDIAN
     1163# define RT_LE2H_U64_C(u64) RT_BSWAP_U64_C(u64)
     1164#else
     1165# define RT_LE2H_U64_C(u64) (u64)
     1166#endif
     1167
    11131168/** @def RT_LE2H_U32
    11141169 * Converts an uint32_t value from little endian to host byte order. */
     
    11191174#endif
    11201175
     1176/** @def RT_LE2H_U32_C
     1177 * Converts an uint32_t constant from little endian to host byte order. */
     1178#ifdef RT_BIG_ENDIAN
     1179# define RT_LE2H_U32_C(u32) RT_BSWAP_U32_C(u32)
     1180#else
     1181# define RT_LE2H_U32_C(u32) (u32)
     1182#endif
     1183
    11211184/** @def RT_LE2H_U16
    11221185 * Converts an uint16_t value from little endian to host byte order. */
     
    11271190#endif
    11281191
     1192/** @def RT_LE2H_U16_C
     1193 * Converts an uint16_t constant from little endian to host byte order. */
     1194#ifdef RT_BIG_ENDIAN
     1195# define RT_LE2H_U16_C(u16) RT_BSWAP_U16_C(u16)
     1196#else
     1197# define RT_LE2H_U16_C(u16) (u16)
     1198#endif
     1199
    11291200
    11301201/** @def RT_H2BE_U64
     
    11361207#endif
    11371208
     1209/** @def RT_H2BE_U64_C
     1210 * Converts an uint64_t constant from host to big endian byte order. */
     1211#ifdef RT_BIG_ENDIAN
     1212# define RT_H2BE_U64_C(u64) (u64)
     1213#else
     1214# define RT_H2BE_U64_C(u64) RT_BSWAP_U64_C(u64)
     1215#endif
     1216
    11381217/** @def RT_H2BE_U32
    11391218 * Converts an uint32_t value from host to big endian byte order. */
     
    11441223#endif
    11451224
     1225/** @def RT_H2BE_U32_C
     1226 * Converts an uint32_t constant from host to big endian byte order. */
     1227#ifdef RT_BIG_ENDIAN
     1228# define RT_H2BE_U32_C(u32) (u32)
     1229#else
     1230# define RT_H2BE_U32_C(u32) RT_BSWAP_U32_C(u32)
     1231#endif
     1232
    11461233/** @def RT_H2BE_U16
    11471234 * Converts an uint16_t value from host to big endian byte order. */
     
    11521239#endif
    11531240
     1241/** @def RT_H2BE_U16_C
     1242 * Converts an uint16_t constant from host to big endian byte order. */
     1243#ifdef RT_BIG_ENDIAN
     1244# define RT_H2BE_U16_C(u16) (u16)
     1245#else
     1246# define RT_H2BE_U16_C(u16) RT_BSWAP_U16_C(u16)
     1247#endif
     1248
    11541249/** @def RT_BE2H_U64
    11551250 * Converts an uint64_t value from big endian to host byte order. */
     
    11601255#endif
    11611256
     1257/** @def RT_BE2H_U64
     1258 * Converts an uint64_t constant from big endian to host byte order. */
     1259#ifdef RT_BIG_ENDIAN
     1260# define RT_BE2H_U64_C(u64) (u64)
     1261#else
     1262# define RT_BE2H_U64_C(u64) RT_BSWAP_U64_C(u64)
     1263#endif
     1264
    11621265/** @def RT_BE2H_U32
    11631266 * Converts an uint32_t value from big endian to host byte order. */
     
    11681271#endif
    11691272
     1273/** @def RT_BE2H_U32_C
     1274 * Converts an uint32_t value from big endian to host byte order. */
     1275#ifdef RT_BIG_ENDIAN
     1276# define RT_BE2H_U32_C(u32) (u32)
     1277#else
     1278# define RT_BE2H_U32_C(u32) RT_BSWAP_U32_C(u32)
     1279#endif
     1280
    11701281/** @def RT_BE2H_U16
    11711282 * Converts an uint16_t value from big endian to host byte order. */
     
    11741285#else
    11751286# define RT_BE2H_U16(u16)   RT_BSWAP_U16(u16)
     1287#endif
     1288
     1289/** @def RT_BE2H_U16_C
     1290 * Converts an uint16_t constant from big endian to host byte order. */
     1291#ifdef RT_BIG_ENDIAN
     1292# define RT_BE2H_U16_C(u16) (u16)
     1293#else
     1294# define RT_BE2H_U16_C(u16) RT_BSWAP_U16_C(u16)
    11761295#endif
    11771296
     
    11811300#define RT_H2N_U64(u64)     RT_H2BE_U64(u64)
    11821301
     1302/** @def RT_H2N_U64_C
     1303 * Converts an uint64_t constant from host to network byte order. */
     1304#define RT_H2N_U64_C(u64)   RT_H2BE_U64_C(u64)
     1305
    11831306/** @def RT_H2N_U32
    11841307 * Converts an uint32_t value from host to network byte order. */
    11851308#define RT_H2N_U32(u32)     RT_H2BE_U32(u32)
    11861309
     1310/** @def RT_H2N_U32_C
     1311 * Converts an uint32_t constant from host to network byte order. */
     1312#define RT_H2N_U32_C(u32)   RT_H2BE_U32_C(u32)
     1313
    11871314/** @def RT_H2N_U16
    11881315 * Converts an uint16_t value from host to network byte order. */
    11891316#define RT_H2N_U16(u16)     RT_H2BE_U16(u16)
    11901317
     1318/** @def RT_H2N_U16_C
     1319 * Converts an uint16_t constant from host to network byte order. */
     1320#define RT_H2N_U16_C(u16)   RT_H2BE_U16_C(u16)
     1321
    11911322/** @def RT_N2H_U64
    11921323 * Converts an uint64_t value from network to host byte order. */
    11931324#define RT_N2H_U64(u64)     RT_BE2H_U64(u64)
    11941325
     1326/** @def RT_N2H_U64_C
     1327 * Converts an uint64_t constant from network to host byte order. */
     1328#define RT_N2H_U64_C(u64)   RT_BE2H_U64_C(u64)
     1329
    11951330/** @def RT_N2H_U32
    11961331 * Converts an uint32_t value from network to host byte order. */
    11971332#define RT_N2H_U32(u32)     RT_BE2H_U32(u32)
    11981333
     1334/** @def RT_N2H_U32_C
     1335 * Converts an uint32_t constant from network to host byte order. */
     1336#define RT_N2H_U32_C(u32)   RT_BE2H_U32_C(u32)
     1337
    11991338/** @def RT_N2H_U16
    12001339 * Converts an uint16_t value from network to host byte order. */
    12011340#define RT_N2H_U16(u16)     RT_BE2H_U16(u16)
     1341
     1342/** @def RT_N2H_U16_C
     1343 * Converts an uint16_t value from network to host byte order. */
     1344#define RT_N2H_U16_C(u16)   RT_BE2H_U16_C(u16)
    12021345
    12031346
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette