Fast Array Bitwise OR?

Posted on 2005-04-11
Last Modified: 2012-05-05
I 've got one array with bit pattern, and other array which I would like to Bitwise OR with pattern.
Are there instructions (MMX, SSE, ...) which allow to do this fast, (like REP MOVSD for fast array copy).

This is part of code in C that I want to optimize:

                  for(i=0; i<iSize; i++) {
                        pArray[i] |= pattern[i % iPatternSize];

Question by:milosr
    LVL 5

    Accepted Solution

    you could use the SSE2 instructions, which operate on 128bit registers at a time.

    MOVDQU                will load a double-quadword from memory to a 128bit register
    db 066h POR          will perform bitwise OR between it's operands

    (NOTE: the db 066h is part of the instruction. using just POR will operate on MMX 64 bit registers. we want 128bit).
    LVL 22

    Expert Comment

    it's not going to make much difference-- or'ing is something CPU's do very quickly-- much quicker than the typical RAM.

    There's no point in speeding up code that's already many times faster than the memory bus can handle.

    LVL 5

    Expert Comment

    right, so we want to minimize memory access.
    why read 1 byte at a time when we can read 32 bytes... ?
    LVL 22

    Expert Comment

    Most PC memory buses are only 4 or 8 bytes wide.   So there's not much gain in reading more than that at one gulp.

    It is cool to think of reading 128 bits at a time!    Wow!

    LVL 5

    Expert Comment

    well, they didn't invent SSE2 for nothing... 8-)
    processors that support SSE2 have sufficient FSB width (athlon 64 -> 128bit, some pentium 4 -> 256 bit).
    there's even the newer SSE3.

    on systems with "only" 64 bit wide busses you can use MMX, SSE or 3DNOW!.
    It all depends on which processor he wants to optimize for. obviously, it may not run on older processors.

    Author Comment


    mzvika is probably right, bitwise or with 128 bits at once is faster then, 4 bitwise or with 32 bits.
    For small arrays (less than proc cash) that would be 4 times speedup. (I'm doing this operation lot of times)

    However pArray is more than 10 MBytes long, so processor spends lot of times waiting
    for data to load in cash. So speedup probably won't be much significant.

    I will try to optimize code in another way. I have different small patterns which I apply to array,
    so maybe to precalculate all patterns, then to aply them in parallel, with one iteration through pArray.

    Thanks for spending your time.

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    Join & Write a Comment

    This is an issue that we can get adding / removing permissions in the vCSA 6.0. We can also have issues searching for users / groups in the AD (using your identify sources). This is how one of the ways to handle this issues and fix it.
    "In order to have an organized way for empathy mapping, we rely on a psychological model and trying to model it in a simple way, so we will split the board to three section for each persona and a scenario and try to see what those personas would Do,…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now