Optimization problem: bulk Boolean operations on vectors

Walter Bright via Digitalmars-d digitalmars-d at puremagic.com
Fri Dec 23 16:04:48 PST 2016


On 12/23/2016 3:35 PM, Johan Engelen wrote:
> On Friday, 23 December 2016 at 22:11:31 UTC, Walter Bright wrote:
>>
>> enum SIZE = 100000000;
>>
>> void foo(int* a, int* b) {
>>     int* atop = a + 1000; // should be `a + SIZE`, right?
>>     ptrdiff_t offset = b - a;
>>     for (; a < atop; ++a)
>>     *a &= *(a + offset);
>> }
>
> This code is not equivalent with the plain foreach loop. Execution is different
> when a > (size_t.max-SIZE).

The assumption is that 'a' points to the start of an array [0..SIZE], so there's 
no overflow.

> Thus the "ptrdiff" loop cannot be vectorized (or can
> only be vectorized when guarded with a check for potential overflow first).
> LDC:
> https://godbolt.org/g/YcCJdZ
> (note the funny jmp .LBB1_6 to a ret instruction... what's that?!)
>
> GDC does something more complex:
> https://godbolt.org/g/3XeI9p
>
> Just for info. Don't know which is faster, but I'm guessing the vectorized
> foreach loop.

The vectorized one probably is. But it sure is a lot of code. (The loop speed 
could possibly be doubled just by unrolling it a few times.)



More information about the Digitalmars-d mailing list