Thread overview
[Issue 20112] __vector casts don't do type conversions
August 09

Iain Buclaw <> changed:

           What    |Removed                     |Added
                 CC|                            |

--- Comment #1 from Iain Buclaw <> ---
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;

August 09

--- Comment #2 from ---
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

--- Comment #3 from ---
> 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.