Thread overview
[Issue 20112] __vector casts don't do type conversions
August 09
https://issues.dlang.org/show_bug.cgi?id=20112

Iain Buclaw <ibuclaw@gdcproject.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |ibuclaw@gdcproject.org

--- Comment #1 from Iain Buclaw <ibuclaw@gdcproject.org> ---
That's because `__vector(int[4]) i = cast(__vector(int[4])) f;` is a
reinterpret cast.

Semantically, this can only be done by unrolling the assignment, but probably easier to do this in phobos std.conv instead.

private T to(T, S)(S value)
{
    alias E = typeof(T.init[0]);
    T res = void;
    static foreach (i; 0 .. S.length)
        res[i] = cast(E)value[i];
    return res;
}

void main() {
    import std.stdio;
    __vector(float[4]) f = [3, 2, 1, 0];
    __vector(int[4]) i = to!(__vector(int[4]) f;
    writeln(i[0]);
}

--
August 09
https://issues.dlang.org/show_bug.cgi?id=20112

--- Comment #2 from thomas.bockman@gmail.com ---
That is very surprising. There is already a way to express reinterpretation casts: `*cast(T*) &variable`. Why is it necessary to overload the conversion syntax in such a confusing fashion? Is this documented anywhere in the language standard?

--
August 09
https://issues.dlang.org/show_bug.cgi?id=20112

--- Comment #3 from thomas.bockman@gmail.com ---
> Semantically, this can only be done by unrolling the assignment

I've found that this is very unreliable. Sometimes the optimizer correctly replaces the individual component casts with the SIMD conversion instructions, and sometimes it doesn't. On LLVM, at least, inlining sometimes undoes the optimization.

I haven't been able to get this working reliably without resorting to inline assembly language.

--