www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Comparison vector ops

Are comparison operators too going vectorial?

void main() {
    int[5] a1, a2, a3;
    bool[5] b1, b2;
    ubyte[5] u1, u2;

    a1 = [1, 2, 3, 4, 5];
    a2 = [5, 2, 1, 4, 4];

    b1 = a1[] > 3;
    assert(b1 == [false, false, false, true, true]);
    u1 = a1[] > 3;
    assert(u1 == [0, 0, 0, 1, 1]);

    b2 = a1[] > a2[];
    assert(b2 == [false, false, true, false, true]);
    u2 = a1[] > a2[];
    assert(u2 == [0, 0, 1, 0, 1]);

    a3 = max(a1[], a2[]);
    assert(a3 == [5, 2, 3, 4, 5]);
    
    assert(pack(a1, a1[] > 3) == [4, 5]);
}

Vectorial comparisons are common in NumPy code:
https://github.com/paulnasca/paulstretch_python/raw/master/paulstretch_stereo.py


If you take a look at the AVX instructions you get ideas for some other
vectorial operations beside just the comparison ones, like vectorial loops:
http://software.intel.com/file/33301

A useless example:

void main() {
    int[5] a0 = [0, 0, 0, 0, 0];
    int[5] a = [1, 2, 3, 4, 5];
    while (a[] > 0)
        a[] = min(a[] - 1, a0[]);
    assert(a0[] == a[]);
}

Bye,
bearophile
Feb 20 2011