ldc icon indicating copy to clipboard operation
ldc copied to clipboard

Implicit conversion on the left side of a vector assignment?

Open bartek-siudeja opened this issue 2 years ago • 2 comments

I just stumbled into the following:

import core.simd;

double test(double x, double2 y)
{
    x += y;
    return x;
}

with -O3 compiles to

double example.test(double, __vector(double[2])):
        ret

At first I would expect a type error, and if not then a change in x. But perhaps this is handled via implicit conversion of x to double2, then opOpAssign on two double2 variables. This would explain no change in x.

This behavior would not be surprising for x + y, or even more so for 1.0 + y (implicit conversion is great). And compilation breaks with x = y. I guess I would expect += to be more like =.

bartek-siudeja avatar Aug 24 '22 01:08 bartek-siudeja

But perhaps this is handled via implicit conversion of x to double2

Yeah, that's what's happening in the frontend - this is the -vcg-ast output (with DMD too), which you can also easily check with the AST button on run.dlang.io:

double test(double x, __vector(double[2]) y)
{
	cast(__vector(double[2]))x += y;
	return x;
}

kinke avatar Aug 24 '22 10:08 kinke

This behavior would not be surprising for x + y, or even more so for 1.0 + y (implicit conversion is great). And compilation breaks with x = y. I guess I would expect += to be more like =.

Indeed x += y should modify x, see https://dlang.org/spec/expression.html#assignment_operator_expressions "a op= b is semantically equivalent to a = cast(typeof(a))(a op b)"

JohanEngelen avatar Aug 24 '22 10:08 JohanEngelen