VirtualBox

Changeset 11173

Show
Ignore:
Timestamp:
08/06/08 06:20:58 (4 months ago)
Author:
vboxsync
Message:

IPRT: Added ASMModS64ByS32RetS32 and ASMModU64ByU32RetU32.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/include/iprt/asm.h

    r10997 r11173  
    42634263 
    42644264/** 
    4265  * Devides a 64-bit unsigned by a 32-bit unsigned returning an unsigned 32-bit result. 
     4265 * Divides a 64-bit unsigned by a 32-bit unsigned returning an unsigned 32-bit result. 
    42664266 * 
    42674267 * @returns u64 / u32. 
     
    42974297 
    42984298/** 
    4299  * Devides a 64-bit signed by a 32-bit signed returning a signed 32-bit result. 
     4299 * Divides a 64-bit signed by a 32-bit signed returning a signed 32-bit result. 
    43004300 * 
    43014301 * @returns u64 / u32. 
     
    43224322        idiv    ecx 
    43234323        mov     [i32], eax 
     4324    } 
     4325#  endif 
     4326    return i32; 
     4327# endif /* !RT_ARCH_AMD64 */ 
     4328} 
     4329#endif 
     4330 
     4331 
     4332/** 
     4333 * Performs 64-bit unsigned by a 32-bit unsigned division with a 32-bit unsigned result, 
     4334 * returning the rest. 
     4335 * 
     4336 * @returns u64 % u32. 
     4337 * 
     4338 * @remarks It is important that the result is <= UINT32_MAX or we'll overflow and crash. 
     4339 */ 
     4340#if RT_INLINE_ASM_EXTERNAL && !defined(RT_ARCH_AMD64) 
     4341DECLASM(uint32_t) ASMModU64ByU32RetU32(uint64_t u64, uint32_t u32); 
     4342#else 
     4343DECLINLINE(uint32_t) ASMModU64ByU32RetU32(uint64_t u64, uint32_t u32) 
     4344{ 
     4345# ifdef RT_ARCH_AMD64 
     4346    return (uint32_t)(u64 % u32); 
     4347# else /* !RT_ARCH_AMD64 */ 
     4348#  if RT_INLINE_ASM_GNU_STYLE 
     4349    RTCCUINTREG uDummy; 
     4350    __asm__ __volatile__("divl %3" 
     4351                         : "=a" (uDummy), "=d"(u32) 
     4352                         : "A" (u64), "r" (u32)); 
     4353#  else 
     4354    __asm 
     4355    { 
     4356        mov     eax, dword ptr [u64] 
     4357        mov     edx, dword ptr [u64 + 4] 
     4358        mov     ecx, [u32] 
     4359        div     ecx 
     4360        mov     [u32], edx 
     4361    } 
     4362#  endif 
     4363    return u32; 
     4364# endif /* !RT_ARCH_AMD64 */ 
     4365} 
     4366#endif 
     4367 
     4368 
     4369/** 
     4370 * Performs 64-bit signed by a 32-bit signed division with a 32-bit signed result, 
     4371 * returning the rest. 
     4372 * 
     4373 * @returns u64 % u32. 
     4374 * 
     4375 * @remarks It is important that the result is <= UINT32_MAX or we'll overflow and crash. 
     4376 */ 
     4377#if RT_INLINE_ASM_EXTERNAL && !defined(RT_ARCH_AMD64) 
     4378DECLASM(int32_t) ASMModS64ByS32RetS32(int64_t i64, int32_t i32); 
     4379#else 
     4380DECLINLINE(int32_t) ASMModS64ByS32RetS32(int64_t i64, int32_t i32) 
     4381{ 
     4382# ifdef RT_ARCH_AMD64 
     4383    return (int32_t)(i64 % i32); 
     4384# else /* !RT_ARCH_AMD64 */ 
     4385#  if RT_INLINE_ASM_GNU_STYLE 
     4386    RTCCUINTREG iDummy; 
     4387    __asm__ __volatile__("idivl %3" 
     4388                         : "=a" (iDummy), "=d"(i32) 
     4389                         : "A" (i64), "r" (i32)); 
     4390#  else 
     4391    __asm 
     4392    { 
     4393        mov     eax, dword ptr [i64] 
     4394        mov     edx, dword ptr [i64 + 4] 
     4395        mov     ecx, [i32] 
     4396        idiv    ecx 
     4397        mov     [i32], edx 
    43244398    } 
    43254399#  endif 
  • trunk/src/VBox/Runtime/testcase/tstInlineAsm.cpp

    r10995 r11173  
    10691069    CHECKVAL(u64, UINT64_C(0x02b8f9a2aa74e3dc), "%#018RX64"); 
    10701070#endif 
     1071 
     1072    u32 = ASMModU64ByU32RetU32(UINT64_C(0x0ffffff8c65d6731), UINT32_C(0x77d7daf8)); 
     1073    CHECKVAL(u32, UINT32_C(0x3B642451), "%#010RX32"); 
     1074 
     1075    int32_t i32; 
     1076    i32 = ASMModS64ByS32RetS32(INT64_C(-11), INT32_C(-2)); 
     1077    CHECKVAL(i32, INT32_C(-1), "%010RI32"); 
     1078    i32 = ASMModS64ByS32RetS32(INT64_C(-11), INT32_C(2)); 
     1079    CHECKVAL(i32, INT32_C(-1), "%010RI32"); 
     1080    i32 = ASMModS64ByS32RetS32(INT64_C(11), INT32_C(-2)); 
     1081    CHECKVAL(i32, INT32_C(1), "%010RI32"); 
     1082 
     1083    i32 = ASMModS64ByS32RetS32(INT64_C(92233720368547758), INT32_C(2147483647)); 
     1084    CHECKVAL(i32, INT32_C(2104533974), "%010RI32"); 
     1085    i32 = ASMModS64ByS32RetS32(INT64_C(-92233720368547758), INT32_C(2147483647)); 
     1086    CHECKVAL(i32, INT32_C(-2104533974), "%010RI32"); 
    10711087} 
    10721088 

© 2008 Sun Microsystems, Inc.
ContactPrivacy policy