VirtualBox

Changeset 22004 in vbox


Ignore:
Timestamp:
Aug 5, 2009 6:26:53 PM (16 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
50739
Message:

iprt/asm.h: Added ASMMemIsZeroPage.

Location:
trunk
Files:
2 edited

Legend:

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

    r21943 r22004  
    50535053
    50545054/**
     5055 * Checks if a memory page is all zeros.
     5056 *
     5057 * @returns true / false.
     5058 *
     5059 * @param   pvPage      Pointer to the page.  This must be naturally aligned if
     5060 *                      not page aligned.
     5061 */
     5062DECLINLINE(bool) ASMMemIsZeroPage(void const *pvPage)
     5063{
     5064# if 0 /*RT_INLINE_ASM_GNU_STYLE - this is actually slower... */
     5065    union { RTCCUINTREG r; bool f; } uAX;
     5066    RTCCUINTREG xCX, xDI;
     5067   Assert(!((uintptr_t)pvPage & (sizeof(uintptr_t) - 1)));
     5068    __asm__ __volatile__("repe; "
     5069#  ifdef RT_ARCH_AMD64
     5070                         "scasq\n\t"
     5071#  else
     5072                         "scasl\n\t"
     5073#  endif
     5074                         "setnc %%al\n\t"
     5075                         : "=&c" (xCX),
     5076                           "=&D" (xDI),
     5077                           "=&a" (uAX.r)
     5078                         : "mr" (pvPage),
     5079#  ifdef RT_ARCH_AMD64
     5080                         "0" (0x1000/8),
     5081#  else
     5082                         "0" (0x1000/4),
     5083#  endif
     5084                         "1" (pvPage),
     5085                         "2" (0));
     5086    return uAX.f;
     5087# else
     5088   uintptr_t const *puPtr = (uintptr_t const *)pvPage;
     5089   int              cLeft = 0x1000 / sizeof(uintptr_t) / 8;
     5090   Assert(!((uintptr_t)pvPage & (sizeof(uintptr_t) - 1)));
     5091   for (;;)
     5092   {
     5093       if (puPtr[0])        return false;
     5094       if (puPtr[4])        return false;
     5095
     5096       if (puPtr[2])        return false;
     5097       if (puPtr[6])        return false;
     5098
     5099       if (puPtr[1])        return false;
     5100       if (puPtr[5])        return false;
     5101
     5102       if (puPtr[3])        return false;
     5103       if (puPtr[7])        return false;
     5104
     5105       if (!--cLeft)
     5106           return true;
     5107       puPtr += 8;
     5108   }
     5109   return true;
     5110# endif
     5111}
     5112
     5113
     5114/**
    50555115 * Checks if a memory block is filled with the specified byte.
    50565116 *
  • trunk/src/VBox/Runtime/testcase/tstInlineAsm.cpp

    r21451 r22004  
    3838#include <iprt/param.h>
    3939#include <iprt/thread.h>
    40 
    41 
    42 /*******************************************************************************
    43 *   Global Variables                                                           *
    44 *******************************************************************************/
    45 /** Global error count. */
    46 static unsigned g_cErrors;
     40#include <iprt/test.h>
     41
    4742
    4843
     
    5550        if ((val) != (expect)) \
    5651        { \
    57             g_cErrors++; \
     52            RTTestIErrorInc(); \
    5853            RTPrintf("%s, %d: " #val ": expected " fmt " got " fmt "\n", __FUNCTION__, __LINE__, (expect), (val)); \
    5954        } \
     
    6661        if (val != (type)(expect)) \
    6762        { \
    68             g_cErrors++; \
     63            RTTestIErrorInc(); \
    6964            RTPrintf("%s, %d: " #op ": expected " fmt " got " fmt "\n", __FUNCTION__, __LINE__, (type)(expect), val); \
    7065        } \
     
    693688        { \
    694689            RTPrintf("%s, %d: FAILURE: %s -> %d expected %d\n", __FUNCTION__, __LINE__, #op, i32Rc, rc); \
    695             g_cErrors++; \
     690            RTTestIErrorInc(); \
    696691        } \
    697692        if (i32 != (val)) \
    698693        { \
    699694            RTPrintf("%s, %d: FAILURE: %s => i32=%d expected %d\n", __FUNCTION__, __LINE__, #op, i32, val); \
    700             g_cErrors++; \
     695            RTTestIErrorInc(); \
    701696        } \
    702697    } while (0)
     
    722717        { \
    723718            RTPrintf("%s, %d: FAILURE: %s -> %d expected %d\n", __FUNCTION__, __LINE__, #op, i32Rc, rc); \
    724             g_cErrors++; \
     719            RTTestIErrorInc(); \
    725720        } \
    726721        if (i32 != (rc)) \
    727722        { \
    728723            RTPrintf("%s, %d: FAILURE: %s => i32=%d expected %d\n", __FUNCTION__, __LINE__, #op, i32, rc); \
    729             g_cErrors++; \
     724            RTTestIErrorInc(); \
    730725        } \
    731726    } while (0)
     
    817812    {
    818813        RTPrintf("tstInlineAsm: ASMMemZeroPage violated one/both magic(s)!\n");
    819         g_cErrors++;
     814        RTTestIErrorInc();
    820815    }
    821816    for (unsigned i = 0; i < sizeof(Buf1.abPage); i++)
     
    823818        {
    824819            RTPrintf("tstInlineAsm: ASMMemZeroPage didn't clear byte at offset %#x!\n", i);
    825             g_cErrors++;
     820            RTTestIErrorInc();
    826821        }
    827822    for (unsigned i = 0; i < sizeof(Buf2.abPage); i++)
     
    829824        {
    830825            RTPrintf("tstInlineAsm: ASMMemZeroPage didn't clear byte at offset %#x!\n", i);
    831             g_cErrors++;
     826            RTTestIErrorInc();
    832827        }
    833828    for (unsigned i = 0; i < sizeof(Buf3.abPage); i++)
     
    835830        {
    836831            RTPrintf("tstInlineAsm: ASMMemZeroPage didn't clear byte at offset %#x!\n", i);
    837             g_cErrors++;
     832            RTTestIErrorInc();
    838833        }
     834}
     835
     836
     837void tstASMMemIsZeroPage(RTTEST hTest)
     838{
     839    RTTestSub(hTest, "ASMMemIsZeroPage");
     840
     841    void *pvPage1 = RTTestGuardedAllocHead(hTest, PAGE_SIZE);
     842    void *pvPage2 = RTTestGuardedAllocTail(hTest, PAGE_SIZE);
     843    RTTESTI_CHECK_RETV(pvPage1 && pvPage2);
     844
     845    memset(pvPage1, 0, PAGE_SIZE);
     846    memset(pvPage2, 0, PAGE_SIZE);
     847    RTTESTI_CHECK(ASMMemIsZeroPage(pvPage1));
     848    RTTESTI_CHECK(ASMMemIsZeroPage(pvPage2));
     849
     850    memset(pvPage1, 0xff, PAGE_SIZE);
     851    memset(pvPage2, 0xff, PAGE_SIZE);
     852    RTTESTI_CHECK(!ASMMemIsZeroPage(pvPage1));
     853    RTTESTI_CHECK(!ASMMemIsZeroPage(pvPage2));
     854
     855    memset(pvPage1, 0, PAGE_SIZE);
     856    memset(pvPage2, 0, PAGE_SIZE);
     857    for (unsigned off = 0; off < PAGE_SIZE; off++)
     858    {
     859        ((uint8_t *)pvPage1)[off] = 1;
     860        RTTESTI_CHECK(!ASMMemIsZeroPage(pvPage1));
     861        ((uint8_t *)pvPage1)[off] = 0;
     862
     863        ((uint8_t *)pvPage2)[off] = 0x80;
     864        RTTESTI_CHECK(!ASMMemIsZeroPage(pvPage2));
     865        ((uint8_t *)pvPage2)[off] = 0;
     866    }
     867
     868    RTTestSubDone(hTest);
    839869}
    840870
     
    869899    {
    870900        RTPrintf("tstInlineAsm: ASMMemZero32 violated one/both magic(s)!\n");
    871         g_cErrors++;
     901        RTTestIErrorInc();
    872902    }
    873903    for (unsigned i = 0; i < RT_ELEMENTS(Buf1.abPage); i++)
     
    875905        {
    876906            RTPrintf("tstInlineAsm: ASMMemZero32 didn't clear byte at offset %#x!\n", i);
    877             g_cErrors++;
     907            RTTestIErrorInc();
    878908        }
    879909    for (unsigned i = 0; i < RT_ELEMENTS(Buf2.abPage); i++)
     
    881911        {
    882912            RTPrintf("tstInlineAsm: ASMMemZero32 didn't clear byte at offset %#x!\n", i);
    883             g_cErrors++;
     913            RTTestIErrorInc();
    884914        }
    885915    for (unsigned i = 0; i < RT_ELEMENTS(Buf3.abPage); i++)
     
    887917        {
    888918            RTPrintf("tstInlineAsm: ASMMemZero32 didn't clear byte at offset %#x!\n", i);
    889             g_cErrors++;
     919            RTTestIErrorInc();
    890920        }
    891921}
     
    933963    {
    934964        RTPrintf("tstInlineAsm: ASMMemFill32 violated one/both magic(s)!\n");
    935         g_cErrors++;
     965        RTTestIErrorInc();
    936966    }
    937967    for (unsigned i = 0; i < RT_ELEMENTS(Buf1.au32Page); i++)
     
    939969        {
    940970            RTPrintf("tstInlineAsm: ASMMemFill32 %#x: %#x exepcted %#x\n", i, Buf1.au32Page[i], 0xdeadbeef);
    941             g_cErrors++;
     971            RTTestIErrorInc();
    942972        }
    943973    for (unsigned i = 0; i < RT_ELEMENTS(Buf2.au32Page); i++)
     
    945975        {
    946976            RTPrintf("tstInlineAsm: ASMMemFill32 %#x: %#x exepcted %#x\n", i, Buf2.au32Page[i], 0xcafeff01);
    947             g_cErrors++;
     977            RTTestIErrorInc();
    948978        }
    949979    for (unsigned i = 0; i < RT_ELEMENTS(Buf3.au32Page); i++)
     
    951981        {
    952982            RTPrintf("tstInlineAsm: ASMMemFill32 %#x: %#x exepcted %#x\n", i, Buf3.au32Page[i], 0xf00dd00f);
    953             g_cErrors++;
     983            RTTestIErrorInc();
    954984        }
    955985}
     
    11681198int main(int argc, char *argv[])
    11691199{
    1170     RTR3Init();
    1171     RTPrintf("tstInlineAsm: TESTING\n");
     1200    RTTEST hTest;
     1201    int rc = RTTestInitAndCreate("tstInlineAsm", &hTest);
     1202    if (rc)
     1203        return rc;
     1204    RTTestBanner(hTest);
    11721205
    11731206    /*
     
    11911224    tstASMAtomicAndOrU32();
    11921225    tstASMMemZeroPage();
     1226    tstASMMemIsZeroPage(hTest);
    11931227    tstASMMemZero32();
    11941228    tstASMMemFill32();
    11951229    tstASMMath();
    11961230    tstASMByteSwap();
    1197 
    11981231    tstASMBench();
    11991232
     
    12011234     * Show the result.
    12021235     */
    1203     if (!g_cErrors)
    1204         RTPrintf("tstInlineAsm: SUCCESS\n", g_cErrors);
    1205     else
    1206         RTPrintf("tstInlineAsm: FAILURE - %d errors\n", g_cErrors);
    1207     return !!g_cErrors;
    1208 }
    1209 
     1236    return RTTestSummaryAndDestroy(hTest);
     1237}
     1238
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