More on vectorized comparisons

bearophile bearophileHUGS at lycos.com
Wed Aug 22 17:19:37 PDT 2012


Some time ago I have suggested to add support to vector 
comparisons in D, because this is sometimes useful and in the 
modern SIMD units there is hardware support for such operations:


double[] a = [1.0, 1.0, -1.0, 1.0, 0.0, -1.0];
bool[] t = a[] > 0;
assert(t == [true, true, false, true, false, false]);

Usable instructions like (here shows the intrinsic):
http://msdn.microsoft.com/en-us/library/11dy102s%28v=vs.80%29.aspx


Now on Reddit I have found a small discussion about a slides pack 
by Intel:
http://www.reddit.com/r/programming/comments/ym8m6/parallel_programming_for_c_and_c_done_right/

The slides:
https://speakerdeck.com/u/multicoreworld/p/james-reinders-intel-united-states

Link to the PDF:
https://speakerd.s3.amazonaws.com/presentations/5006069136af010002005325/Reinders_KEYNOTE_C_done_right.pdf

At page 69 of those slides there is some code that looks 
interesting, I think this is a reduced version of part of it, 
that shows another way to use vectorized comparisons:


void main() {
     double[] a = [1.0, 1.0, -1.0, 1.0, 0.0, -1.0];
     double[] b = [10,   20,   30,  40,  50,   60];
     double[] c = [1,     2,    3,   4,   5,    6];
     if (a[] > 0)
         b[] += c[];
}


I think that code is semantically equivalent to:

void main() {
     double[] a = [1.0, 1.0, -1.0, 1.0, 0.0, -1.0];
     double[] b = [10,   20,   30,  40,  50,   60];
     double[] c = [1,     2,    3,   4,   5,    6];
     foreach (i; 0 .. a.length)
         if (a[i] > 0)
             b[i] += c[i];
}


After that code b is:
[11, 22, 30, 44, 50, 60]


This means the contents of the 'then' branch of the vectorized 
comparison is done only on items of b and c where the comparison 
has given true.

This looks useful. Is it possible to implement this in D, and do 
you like it?

Bye,
bearophile


More information about the Digitalmars-d mailing list