vector constructors

Hi!

I'd like to do this myself but I have to dig into clang only for this wich takes some
time therefore I'd like to ask if someone is already is working on it (or wants to do it :wink: :

for c, the cast operator is extended to vectors (opencl):
(int)(1)
(int4)(1, 2, 3, 4)

for c++ the same could be done:
int(1)
int4(1, 2, 3, 4)

I use this variant since it can be emulated with c++ classes when no vector
extensions are available.

Will it be supported in the near future?

-Jochen

This is already supported with -faltivec, you can enable the LangOptions flag to get this.

-Chris

Hi!

This is already supported with -faltivec, you can enable the LangOptions flag to get this.
   

Unfortunately I didn't get it to work.
I have langOptions.AltiVec = 1; and I tried
typedef __vector float float4;
and
typedef float __attribute__((ext_vector_type(4))) float4;

I get this error:

testc.cpp:51:23:{51:14-51:33}: error: function-style cast to a builtin type can
       only take one argument
          float4 b = float4(1, 2, 3, 4);

- Jochen

This works for me:

$ cat t.c
void foo() {
  vector float x;
  x = (vector float)(1,2,3,4);
}
$ clang -cc1 t.c -fsyntax-only -faltivec

This works for me:

$ cat t.c
void foo() {
  vector float x;
  x = (vector float)(1,2,3,4);
}
$ clang -cc1 t.c -fsyntax-only -faltivec

Yes I know that this works. It's the c-like extension of the
cast operator to vectors, but in contrast I'd like to have
a c++-like extension of the function style cast:

int(1)
int4(1, 2, 3, 4)

-Jochen

Do other compilers support this syntax? (e.g., GCC)?

  - Doug

int(1)
int4(1, 2, 3, 4)
    
Do other compilers support this syntax? (e.g., GCC)?
  
Cg, glsl and hlsl support it.

- Jochen

We do not aim to implement those languages in clang.

-Chris

Cg, glsl and hlsl support it.
    
We do not aim to implement those languages in clang.

I do not expect this, but will a patch for only the function style constructors
be accepted into mainline?
I think it's a natural extension of c++ syntax to vectors.

-Jochen

If you're okay with accepting it only in C++, I think it's a reasonable extension.

John.

In my opinion, there is no reason to do this. If you want C++ syntax to do this, define your_float4 as a struct with a constructor. There is already too much needless diversity in vector initialization, and C++'0x may add a whole new world of pain here.

-Chris

If you're okay with accepting it only in C++, I think it's a reasonable extension.

of course only in c++ mode (like int(1) also only works in c++ mode)

- Jochen

In my opinion, there is no reason to do this. If you want C++ syntax to do this, define your_float4 as a struct with a constructor. There is already too much needless diversity in vector initialization, and C++'0x may add a whole new world of pain here.
  

Then I can keep my current implementation that does not use vectors and use an auto-vectorization
pass. By the way what is the current state of the art on this topic?

But maybe I add the function style constructor to my local copy since not having it raises other
questions like default-initialization:

struct Foo
{
    Foo() : x(), y() {}

    int x;
    int4 y;
};

If I get it working I will ask again :wink:

- Jochen

Hi!

just to capture the current state of c++ style vector support I made a little test:

struct Foo
{
     Foo(int) : x() {} // works
     Foo(float f) : x(f) {} // works
     Foo(float4 x) : x(x) {} // works
     Foo() : x(1, 2, 3, 4) {} // error: excess elements in scalar initializer

     float4 bar1() {return float4();} // works
     float4 bar2() {return float4(f);} // works
     float4 bar3() {return float4(x);} // works
     float4 bar4() {return float4(1, 2, 3, 4);} // error: function-style cast to a builtin type can only take one argument

     float f;
     float4 x;
};

I only checked if the statements compile.

-Jochen

Looks right to me!

-Chris

while being allowed to write float4(1,2,3,4) would be deluxe-support that
I know from graphics-centric compilers I found another way: e.g.
float4 makeFloat4(float a, float b, float c, float d){return (float4)(a, b, c, d);}

this can look the same from the outside if no vector support is available and
float4 is a struct with no constructors.

-Jochen

Hi!

while playing with vectors I get this error for int. it works for float:

typedef int __attribute__((ext_vector_type(4))) int4;

int4 foo = (int4)(1, 2, 3, 4);

testc.cpp:72:13:{72:13-72:19}: error: C-style cast from scalar 'int' to vector
       'int4' of different size
          int4 foo = (int4)(1, 2, 3, 4);

-Jochen