Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core.simd.int4 equality #18835

Open
dlangBugzillaToGithub opened this issue Jun 6, 2014 · 0 comments
Open

core.simd.int4 equality #18835

dlangBugzillaToGithub opened this issue Jun 6, 2014 · 0 comments

Comments

@dlangBugzillaToGithub
Copy link

bearophile_hugs reported this on 2014-06-06T17:18:27Z

Transferred from https://issues.dlang.org/show_bug.cgi?id=12868

CC List

Description

Value equality is quite important for the usability of a type. This code is currently refused:

void main() {
    import core.simd: int4;
    import std.algorithm: uniq;
    int4[] data = [[1, 2, 3, 4], [1, 2, 3, 4], [10, 20, 30, 40]];
    data.uniq;
}



dmd 2.066alpha gives:

test.d(5): Error: template std.algorithm.uniq does not match any function template declaration. Candidates are:
...\ldc2\bin/../import\std\algorithm.d(3346):        std.algorithm.uniq(alias pred = "a == b", Range)(Range r) if (isInputRange!Range && is(typeof(binaryFun!pred(r.front, r.front)) == bool))
test.d(5): Error: template std.algorithm.uniq(alias pred = "a == b", Range)(Range r) if (isInputRange!Range && is(typeof(binaryFun!pred(r.front, r.front)) == bool)) cannot deduce template function from argument types !()(__vector(int[4])[])



A workaround is to call .array, but this is quite inefficient code:

void main() {
    import core.simd: int4;
    import std.algorithm: uniq;
    int4[] data = [[1, 2, 3, 4], [1, 2, 3, 4], [10, 20, 30, 40]];
    data.uniq!q{ a.array == b.array };
}


There are two different possible equalities among SIMD values: the first returns a bool normally, and the second returns a SIMD value that contains N booleans represented as zero/notzero values. The first can be computed with a xor followed by a sum + shuffle + sum + shuffle ... for log2(N) cycles.

I think the first equality operation should be built-in. 

An alternative is to have a function like this but I think it's not a large improvement because it has to perform the same operations as a built-in equality:

void main() {
    import core.simd: int4;
    import std.algorithm: uniq;
    int4[] data = [[1, 2, 3, 4], [1, 2, 3, 4], [10, 20, 30, 40]];
    data.uniq!q{ simdEquality(a, b) };
}



But such simdEquality function is OK for the second kind of SIMD equality, that returns a SIMD of booleans.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant