VirtualBox

Changeset 10995 in vbox


Ignore:
Timestamp:
Jul 30, 2008 3:49:16 PM (16 years ago)
Author:
vboxsync
Message:

iprt: Added ASMByteSwapU16/64 and unit test for them + U32.

Location:
trunk
Files:
2 edited

Legend:

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

    r10943 r10995  
    55405540}
    55415541
     5542/**
     5543 * Reverse the byte order of the given 16-bit integer.
     5544 *
     5545 * @returns Revert
     5546 * @param   u16     16-bit integer value.
     5547 */
     5548DECLINLINE(uint16_t) ASMByteSwapU16(uint16_t u16)
     5549{
     5550#if RT_INLINE_ASM_USES_INTRIN
     5551    u16 = _byteswap_ushort(u16);
     5552#elif RT_INLINE_ASM_GNU_STYLE
     5553    __asm__ ("rorw $8, %0" : "=r" (u16) : "0" (u16));
     5554#else
     5555    _asm
     5556    {
     5557        mov     ax, [u16]
     5558        ror     ax, 8
     5559        mov     [u16], ax
     5560    }
     5561#endif
     5562    return u16;
     5563}
    55425564
    55435565/**
    55445566 * Reverse the byte order of the given 32-bit integer.
    5545  * @param  u32      Integer
     5567 *
     5568 * @returns Revert
     5569 * @param   u32     32-bit integer value.
    55465570 */
    55475571DECLINLINE(uint32_t) ASMByteSwapU32(uint32_t u32)
     
    55625586}
    55635587
     5588
     5589/**
     5590 * Reverse the byte order of the given 64-bit integer.
     5591 *
     5592 * @returns Revert
     5593 * @param   u64     64-bit integer value.
     5594 */
     5595DECLINLINE(uint64_t) ASMByteSwapU64(uint64_t u64)
     5596{
     5597#if defined(RT_ARCH_AMD64) && RT_INLINE_ASM_USES_INTRIN
     5598    u64 = _byteswap_uint64(u64);
     5599#else /* !RT_ARCH_AMD64 (assume x86) */
     5600    u64 = (uint64_t)ASMByteSwapU32((uint32_t)u64) << 32
     5601        | (uint64_t)ASMByteSwapU32(u64 >> 32);
     5602#endif
     5603    return u64;
     5604}
     5605
     5606
    55645607/** @} */
    55655608
  • trunk/src/VBox/Runtime/testcase/tstInlineAsm.cpp

    r9909 r10995  
    10711071}
    10721072
    1073 /*
    1074  * Make this static. We don't want to have this located on the stack.
    1075  */
     1073
     1074void tstASMByteSwap(void)
     1075{
     1076    RTPrintf("tstInlineASM: TESTING - ASMByteSwap*\n");
     1077
     1078    uint64_t u64In  = UINT64_C(0x0011223344556677);
     1079    uint64_t u64Out = ASMByteSwapU64(u64In);
     1080    CHECKVAL(u64In, UINT64_C(0x0011223344556677), "%#018RX64");
     1081    CHECKVAL(u64Out, UINT64_C(0x7766554433221100), "%#018RX64");
     1082    u64Out = ASMByteSwapU64(u64Out);
     1083    CHECKVAL(u64Out, u64In, "%#018RX64");
     1084    u64In  = UINT64_C(0x0123456789abcdef);
     1085    u64Out = ASMByteSwapU64(u64In);
     1086    CHECKVAL(u64In, UINT64_C(0x0123456789abcdef), "%#018RX64");
     1087    CHECKVAL(u64Out, UINT64_C(0xefcdab8967452301), "%#018RX64");
     1088    u64Out = ASMByteSwapU64(u64Out);
     1089    CHECKVAL(u64Out, u64In, "%#018RX64");
     1090    u64In  = 0;
     1091    u64Out = ASMByteSwapU64(u64In);
     1092    CHECKVAL(u64Out, u64In, "%#018RX64");
     1093    u64In  = ~(uint64_t)0;
     1094    u64Out = ASMByteSwapU64(u64In);
     1095    CHECKVAL(u64Out, u64In, "%#018RX64");
     1096
     1097    uint32_t u32In  = UINT32_C(0x00112233);
     1098    uint32_t u32Out = ASMByteSwapU32(u32In);
     1099    CHECKVAL(u32In, UINT32_C(0x00112233), "%#010RX32");
     1100    CHECKVAL(u32Out, UINT32_C(0x33221100), "%#010RX32");
     1101    u32Out = ASMByteSwapU32(u32Out);
     1102    CHECKVAL(u32Out, u32In, "%#010RX32");
     1103    u32In  = UINT32_C(0x12345678);
     1104    u32Out = ASMByteSwapU32(u32In);
     1105    CHECKVAL(u32In, UINT32_C(0x12345678), "%#010RX32");
     1106    CHECKVAL(u32Out, UINT32_C(0x78563412), "%#010RX32");
     1107    u32Out = ASMByteSwapU32(u32Out);
     1108    CHECKVAL(u32Out, u32In, "%#010RX32");
     1109    u32In  = 0;
     1110    u32Out = ASMByteSwapU32(u32In);
     1111    CHECKVAL(u32Out, u32In, "%#010RX32");
     1112    u32In  = ~(uint32_t)0;
     1113    u32Out = ASMByteSwapU32(u32In);
     1114    CHECKVAL(u32Out, u32In, "%#010RX32");
     1115
     1116    uint16_t u16In  = UINT16_C(0x0011);
     1117    uint16_t u16Out = ASMByteSwapU16(u16In);
     1118    CHECKVAL(u16In, UINT16_C(0x0011), "%#06RX16");
     1119    CHECKVAL(u16Out, UINT16_C(0x1100), "%#06RX16");
     1120    u16Out = ASMByteSwapU16(u16Out);
     1121    CHECKVAL(u16Out, u16In, "%#06RX16");
     1122    u16In  = UINT16_C(0x1234);
     1123    u16Out = ASMByteSwapU16(u16In);
     1124    CHECKVAL(u16In, UINT16_C(0x1234), "%#06RX16");
     1125    CHECKVAL(u16Out, UINT16_C(0x3412), "%#06RX16");
     1126    u16Out = ASMByteSwapU16(u16Out);
     1127    CHECKVAL(u16Out, u16In, "%#06RX16");
     1128    u16In  = 0;
     1129    u16Out = ASMByteSwapU16(u16In);
     1130    CHECKVAL(u16Out, u16In, "%#06RX16");
     1131    u16In  = ~(uint16_t)0;
     1132    u16Out = ASMByteSwapU16(u16In);
     1133    CHECKVAL(u16Out, u16In, "%#06RX16");
     1134}
     1135
     1136
    10761137void tstASMBench(void)
    10771138{
     1139    /*
     1140     * Make this static. We don't want to have this located on the stack.
     1141     */
    10781142    static uint8_t  volatile s_u8;
    10791143    static int8_t   volatile s_i8;
     
    11751239    tstASMMemFill32();
    11761240    tstASMMath();
     1241    tstASMByteSwap();
    11771242
    11781243    tstASMBench();
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