Optimization problem: bulk Boolean operations on vectors

Johan Engelen via Digitalmars-d digitalmars-d at puremagic.com
Fri Dec 23 15:35:50 PST 2016


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). 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.



More information about the Digitalmars-d mailing list